• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to secondary sidebar

GoHired

Interview Questions asked in Google, Microsoft, Amazon

Join WeekEnd Online Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

  • Home
  • Best Java Books
  • Algorithm
  • Internship
  • Certificates
  • About Us
  • Contact Us
  • Privacy Policy
  • Array
  • Stack
  • Queue
  • LinkedList
  • DP
  • Strings
  • Tree
  • Mathametical
  • Puzzles
  • Graph

Printing intermediate Integers between one element & next element of array

February 16, 2018 by Dhaval Dave

Problem : Printing intermediate Integers

User entered numbers are set into array.Intermediate integer of each pair of successive elements to be printed.

Input :
Input of 1st line is number of element in array,inputs of 2nd line are array elements.
Output :
Output of each line is intermediate integers between each two succesive array element.
Example :

Input Output
3
2 6 8 3
3 4 5
7
4 5 6 7
3
8 8 9
"no  intermediate element in between these 8 & 9 elements"
"no  intermediate element in between these 9 & 9 elements"
 Solution : 

Logic for Printing intermediate Integers 

1. Entered numbers kept stored in array                             
2. For loop executes for each pair of successive array elements.
i) If two successive array elements are same or difference between two successive array elements is one ,then no intermediate integer presence message is printed . 
ii) Lesser number is identified in between two numbers of each pair.
iii) printing starts from the number one more than lesser number ,this number incremented by one,again printed,this continues upto the number one less than grater number between pair.

Code : 

#include"iostream";
using namespace std;
main()
{
 int n;
 cout<<"Enter number of Integer in array "<<endl;
 cin>>n;
 int num[n];
 cout<<"Enter elements of array "<<endl;
 for(int p=0;p<n;p++)
 cin>>num[p];
 for(int k=0;k<n-1;k++)
 {
   if((num[k]-num[k+1]==1) || (num[k+1]-num[k]==1) || (num[k]==num[k+1]))
   {
   cout<<"No intermediate element in between these "<<num[k]<<" & "<<num[k+1]<<" elements "<<endl;
   }
   else if(num[k]<num[k+1])
   {
     cout<<"Element between "<<num[k]<<" and "<<num[k+1]<<" is/are ";
     for(int a=num[k];a<num[k+1]-1;)
     {
        cout<<++a<<" ";   //printing intermediate element 
     }
     cout<<endl;
   }
   else if(num[k]>num[k+1])
   {
     cout<<"Element between "<<num[k]<<" and "<<num[k+1]<<" is/are ";
     for(int a=num[k+1];a<num[k]-1;)
       {
         cout<<++a<<" ";   //printing intermediate element
       }
       cout<<endl;
    }
  }
}

Click here to see running code on Ideone with input 4 2 6 8 3

Click here to see running code on Ideone with input 3 8 9 9

Explanation : 

Entered 1st input set’s numbers are stored in array ‘num’ like below –


First (2,6) ,then (6,8),then (8,3) pair get considered.Intermediate values are printed.
Entered 2nd input set’s numbers are stored in array ‘num’ like below –


First (8,9) ,then (9,9) pair get considered.No intermediate element present message is printed.

Another Logic and Code : 

#include"iostream";
using namespace std;
main()
{
int n,a,b,large,small;
cout<<"Enter number of Integer in array "<<endl;
cin>>n;
int num[n];
cout<<"Enter element of array "<<endl;
for(int p=0;p<n;p++)
cin>>num[p];
for(int k=0;k<n-1;k++)
{
 a=num[k];
 b=num[k+1];
 if((num[k]-num[k+1]==1) || (num[k+1]-num[k]==1) || (num[k]==num[k+1]))
 {
 cout<<"No intermediate element present in between these "<<num[k]<<" & "<<num[k+1]<<" elements";
 }
else
{
large=a>b?a:b;   //identifying larger number of pair 
small=a<b?a:b;   //identifying smaller number of pair
cout<<"Element between "<<num[k]<<" and "<<num[k+1]<<" is/are ";
for(int p=small;p<large-1;)
{
 cout<<++p<<" ";   //printing intermediate element
}
}
cout<<endl;
}
}

Click here to see running code on Ideone with input 4 2 6 8 3
Click here to see running code on Ideone with input 3 8 9 9

Explanation : 

If two successive array elements are same or difference between two successive array elements is one ,then no intermediate integer presence message is printed .Larger & smaller number in pair are identified by ternary operator ,then printing starts from the number one more than small ,continues upto the number one less than larger .

Similar Articles

Filed Under: Uncategorized

Reader Interactions

Primary Sidebar

Join WeekEnd Online/Offline Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

Join WeekEnd Online/Offline Batch from 4-April-2020

WhatsApp us

Secondary Sidebar

Custom Search

  • How I cracked AMAZON
  • LeetCode
  • Adobe
  • Amazon
  • Facebook
  • Microsoft
  • Hacker Earth
  • CSE Interview

Top Rated Questions

Flipkart Set 1 On Campus with Answers

Reliance Jio Software Developer Interview Experience

N Petrol bunks or City arranged in circle. You have Fuel and distance between petrol bunks. Is it possible to find starting point so that we can travel all Petrol Bunks

Sequence Finder Dynamic Programming

Memory Efficient LinkedList

Stock Buy Sell to Maximize Profit

Top 10 Interviews Techniqes for Campus Interview in IIT NIT BITS for MTech

Trapping Rain Water

Doubly linked list

Find position of the only set bit

System Design: Designing a LLD for Hotel Booking

Puzzle : 100 doors in a row Visit and Toggle the door. What state the door will be after nth pass ?

Given a float number convert it into the string WITHOUT using any inbuilt Function

Leetcode: Edit Distance

CodeChef Code SGARDEN

C++ OOPs Part2

Search element in a matrix with all rows and columns in sorted order

Find the number ABCD such that when multipled by 4 gives DCBA.

K’th Largest Element in BST when modification to BST is not allowed

Trie Dictionary

Implement LRU Cache

Edit Distance ( Dynamic Programming )

Print Power Set of a Set

Leetcode: Merge Intervals

Adobe Interview Questions 8 month Exp

Get Minimum element in O(1) from input numbers or Stack

ADOBE Aptitude C Language Test

Circular Linked List

Find if two rectangles overlap

Common Ancestor in a Binary Tree or Binary Search Tree

Copyright © 2025 · Genesis Framework · WordPress · Log in