Print intermediate values in an array

Given an integer array, print the intermediate values between current index element and successive next index elements of array.

Examples: 

Input : arr[] = { 4, 2, 7, 5};
Output :
Intermediate elements between 4 and 2
2 3 4
Intermediate elements between 2 and 7
2 3 4 5 6 7
Intermediate elements between 7 and 5
5 6 7

Implementation:

C++




// C++ program to print the 
// intermediate  value
#include <iostream>
using namespace std;
  
void inter(int arr[], int n)
    {
        for (int l = 0; l < n - 1; l++) 
        {
            // points to first index element
            int i = arr[l];
              
            // points to preceding index element
            int j = arr[l + 1];
              
            // Find big element
            // between the above elements
            int big = i > j ? i : j;
              
            // Find small element 
            // between the above elements
            int sml = i < j ? i : j;
              
            cout<<"Intermediate elements between "<<
                                 i <<" and "<<j<<endl;
              
            for (int k = sml; k <= big; k++)         
                cout<<k<<" ";
  
            cout<<endl;
        }
    }
  
// Driver code
int main() {
      
    int arr[] = { 4, 2, 7, 5 };
      
    int n=sizeof(arr)/sizeof(arr[0]);
      
    inter(arr,n);
      
    return 0;
}
  
// this code is contributed by 'vt_m'


Java




// Java program to print the 
// intermediate  values
   
public class GFG 
{
    static void inter(int[] arr)
    {
        for (int l = 0; l < arr.length - 1; l++) 
        {
            // points to first index element
            int i = arr[l];
              
            //  points to preceding index element
            int j = arr[l + 1];
              
            // Find big element
            // between the above elements
            int big = i > j ? i : j;
              
            // Find  small element 
            // between the above elements
            int sml = i < j ? i : j;
              
            System.out.println("Intermediate elements between "
                                + i + " and " + j);
            for (int k = sml; k <= big; k++)             
                System.out.print(k + " ");
  
            System.out.println();
        }
    }
      
    // Driver code
    public static void main(String[] args)
    {
        int[] arr = { 4, 2, 7, 5 };
        inter(arr);
    }
}


Python3




# Python 3 program to print the 
# intermediate  value
  
def inter(arr, n) :
    for l in range( 0, n - 1) :
  
        # points to first index element
        i = arr[l]
               
        # points to preceding index element
        j = arr[l + 1]
               
        # Find big element
        # between the above elements
        if(i>j) :
            big =
        else :
            big = j
               
        # Find small element 
        # between the above elements
        if(i<j) :
            sml = 
        else
            sml = j
               
        print("Intermediate elements between "
             ,i ," and ",j)
               
        for k in range( sml, big+1) :
            print(k,end = " ")
        print()
              
              
# Driver code
arr = [ 4, 2, 7, 5 ]
       
n= len(arr)
  
inter(arr,n)
  
# This code is contributed
# by Nikita Tiwari.


C#




// C# program to print the 
// intermediate values
using System;
  
public class GFG 
{
    static void inter(int[] arr)
    {
        for (int l = 0; l < arr.Length - 1; l++) 
        {
            // points to first index element
            int i = arr[l];
              
            // points to preceding index element
            int j = arr[l + 1];
              
            // Find big element
            // between the above elements
            int big = i > j ? i : j;
              
            // Find small element 
            // between the above elements
            int sml = i < j ? i : j;
              
            Console.WriteLine("Intermediate elements between "
                                + i + " and " + j);
            for (int k = sml; k <= big; k++)         
                Console.Write(k + " ");
  
            Console.WriteLine();
        }
    }
      
    // Driver code
    public static void Main()
    {
        int[] arr = { 4, 2, 7, 5 };
        inter(arr);
    }
}
  
// This code is contributed by vt_m.


PHP




<?php
// PHP program to print the 
// intermediate  value
  
function inter ($arr, $n)
    {
        for ($l = 0; $l < $n - 1; $l++) 
        {
              
            // points to first index element
            $i = $arr[$l];
              
            // points to preceding index element
            $j = $arr[$l + 1];
              
            // Find big element
            // between the above elements
            $big = $i > $j ? $i : $j;
              
            // Find small element 
            // between the above elements
            $sml = $i < $j ? $i : $j;
              
            echo "interermediate elements between ",
                                $i ," and ",$j,"\n";
              
            for ($k = $sml; $k <= $big; $k++)         
                echo $k," ";
  
            echo "\n";
        }
    }
  
    // Driver Code
    $arr = array(4, 2, 7, 5);
    $n=count($arr);
    inter($arr,$n);
  
// This code is contributed by anuj_67.
?>


Javascript




<script>
  
// Javascript program to print the
// intermediate value
  
function inter( arr, n)
    {
        for (let l = 0; l < n - 1; l++)
        {
            // points to first index element
            let i = arr[l];
              
            // points to preceding index element
            let j = arr[l + 1];
              
            // Find big element
            // between the above elements
            let big = i > j ? i : j;
              
            // Find small element
            // between the above elements
            let sml = i < j ? i : j;
              
    document.write("Intermediate elements between " +
                             i + " and "+ j + "</br>");
              
            for (let k = sml; k <= big; k++)        
                document.write(k + " ");
  
            document.write("</br>");
        }
    }
  
    // Driver Code
      
    let arr = [ 4, 2, 7, 5 ];
      
    let n= arr.length;
      
    inter(arr,n);
      
      
</script>


Output

Intermediate elements between 4 and 2
2 3 4 
Intermediate elements between 2 and 7
2 3 4 5 6 7 
Intermediate elements between 7 and 5
5 6 7 

Time Complexity: O(n*max_diff), Here n is the number of elements in the array and max_diff is the maximum difference between any two adjacent elements.
Auxiliary Space: O(1), As constant extra space is used.

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button