CoCubes Programming Question – 2

Maximum difference between two elements such that larger element appears after the smaller number

Given an array arr[] of integers, find out the difference between any two elements such that larger element appears after the smaller number in arr[].

Examples: If array is [2, 3, 10, 6, 4, 8, 1] then returned value should be 8 (Diff between 10 and 2). If array is [ 7, 9, 5, 6, 3, 2 ] then returned value should be 2 (Diff between 7 and 9)

Time Complexity: O(n^2)
Auxiliary Space: O(1)

Use two loops. In the outer loop, pick elements one by one and in the inner loop calculate the difference of the picked element with every other element in the array and compare the difference with the maximum difference calculated so far.

#include<stdio.h>
int maxDiff (int arr[], int arr_size)
{
  int max_diff = arr[1] - arr[0];
  int i, j;
  for (i = 0; i < arr_size; i++)
    {
      for (j = i + 1; j < arr_size; j++) { if (arr[j] - arr[i] > max_diff)
	        max_diff = arr[j] - arr[i];
	    }
    }
  return max_diff;
}

int main ()
{
  int len, i;
  printf( "Enter the length of the array\n"); 
  scanf("%d",&len);
  int arr[len];
  printf( "Enter the elements of the array\n");
  for( i=0; i<len; i++) 
  { 
     scanf("%d",&arr[i]);
  }
  printf ("Maximum difference is %d" , maxDiff (arr, len));
  getchar ();
  return 0;
}

import java.util.*;

class MaximumDifference {
    int maxDiff(int arr[], int arr_size) {
        int max_diff = arr[1] - arr[0];
        int i, j;
        for (i = 0; i < arr_size; i++) {
            for (j = i + 1; j < arr_size; j++) { if (arr[j] - arr[i] > max_diff)
                    max_diff = arr[j] - arr[i];
            }
        }
        return max_diff;
    }

    public static void main(String[] args) {
        MaximumDifference maxdif = new MaximumDifference();
        int arr[] = { 1, 2, 90, 10, 110 };
        System.out.println("Maximum differnce is " + maxdif.maxDiff(arr, 5));
    }
}

13 comments on “CoCubes Programming Question – 2”


  • Amarnath

    import java.util.*;
    public class Main
    {
    public static void main(String[] args) {
    int[] arr=new int[]{7, 9, 5, 6, 3, 2};
    Arrays.sort(arr);
    System.out.println(arr[arr.length-1]-arr[arr.length-2]);
    }
    }


    • Anjana

      this wouldn’t work as by sorting we are disturbing the order and in the question we have constraint as “such that larger element appears after the smaller number “.


  • Ravi Chandra

    n=int(input(“Enter the size of the array”))
    array=list(map(int,input().split()))
    ma=array[0]
    for i in range(n):
    for j in range(i+1,n):
    if(array[i]ma):
    ma=array[j]-array[i]
    print(ma)


  • SaiManoj

    #include
    int main()
    {
    int n,a[100],i,dif,max=0;
    scanf(“%d”,&n);
    for(i=1;i<=n;i++)
    scanf("%d",&a[i]);
    dif=a[1]-a[0];
    for(i=1;i<=n;i++)
    {
    for(int j=i+1;j=a[i])
    dif=a[j]-a[i];
    if(max<dif)
    max=dif;
    }
    }
    printf("%d",max);
    return 0;
    }


  • shivam mishra

    l=list(map(int,input().strip().split(” “)))
    l1=[]
    for i in range(len(l)):
    for j in range(i,len(l)):
    if i!=j:
    if l[i]<l[j]:
    l1.append(l[j]-l[i])
    print(max(l1))


  • Mohd Saif

    #include
    using namespace std;
    void diff_arr(int arr[],int n){
    vector temp_arr;
    temp_arr.push_back(arr[0]);
    int k=0;
    for(int i=1;itemp_arr.back()){
    temp_arr.push_back(arr[i]);
    k+=1;
    }
    }
    int x=temp_arr[k];
    int pos2;
    for(int i=0;i<n;i++){
    if(x==arr[i]){
    pos2=i;
    }
    }
    for(int i=1;iarr[i]){
    int pos3=i;
    if(pos3<pos2){
    temp_arr[0]=arr[i];
    }
    }
    }
    x=(temp_arr[k]-temp_arr[0]);
    cout<>n;
    int arr[n];
    for(int i=0;i>arr[i];
    }
    diff_arr(arr,n);
    return 0;
    }


  • prince

    arr=list(map(int,input().split()))
    arr1=arr.copy()
    arr1.sort()
    a=arr1[len(arr1)-1]
    b=arr.index(a)
    mylist=list(arr[0:b])
    mylist.sort()
    c=mylist[0]
    print (a-c)


  • Suraj Potti

    Solution for this question in Python.
    maxi = 0
    diff = 0
    arr = [ 9, 7, 6, 5, 3, 2 ]
    for i in range(len(arr)-1):
    sub_max = max(arr[i:len(arr)]) #slicing the array after the current index.
    if(i maxi):
    maxi = diff
    else:
    pass
    else:
    pass
    print(maxi)
    This code will return zero if no such numbers satisfying the conditions exists.