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.

31 comments on “CoCubes Programming Question – 2”


  • Adithya

    int n=6;
    int max=0,diff=0;
    int a[]={7,9,5,6,3,2};
    for(int i=0; i<n; i++)
    {
    for(int j=i+1; j a[i])
    {
    diff=a[j]-a[i];
    }
    if(diff > max)
    {
    max=diff;
    }
    }
    }
    printf(“%d”,max);


  • Tanya

    import java.util.*;
    public class WiproTurbosmallandlargeint {

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner sc = new Scanner(System.in);
    int x = sc.nextInt();
    int[] arr = new int[x];
    for(int i = 0; i < x; i++)
    {
    arr[i] = sc.nextInt();
    }
    int small = arr[0];
    int large = arr[1];
    for(int i = 0; i <x; i++)
    {
    if(arr[i]= large)
    {
    large = arr[i];
    if(i <x-1 && arr[i+1] < large)
    break;
    }
    }
    System.out.println(large-small);
    }

    }


  • Aiiyan

    TC = O(N)
    int max_difference(vector &nums)
    {
    int n = nums.size();
    stack s;
    vector g(n);

    for (int i = n – 1; i >= 0; i–)
    {
    while (!s.empty() && s.top() s.top())
    s.push(nums[i]);
    }

    else if (s.empty())
    {
    g[i] = 0;
    s.push(nums[i]);
    }
    }

    int diff = 0;
    for (int i = 0; i < n; i++)
    diff = max(diff, g[i] – nums[i]);

    return diff;
    }


  • Fozi

    //………….Utkarsh-Codes……………..
    HERE’S AND O(N) SOLUTION 😀
    #include
    using namespace std;
    // in array find max difference btween two numbers but larger appears after
    int main()
    {
    //sliding window O(n) approach

    vector arr2{ 2, 3, 10, 6, 4, 8, 1};
    vector arr1{7, 9, 5, 6, 3, 2 };

    int largest_diff = 0;
    int smallest = arr1[0];

    for (int i = 1; i < arr1.size(); i++) {
    largest_diff = max(largest_diff, arr1[i] – smallest);
    smallest = min(smallest, arr1[i]);

    }
    cout << largest_diff << endl;
    return 0;
    }


  • d6mr07

    list = [2, 3, 10, 6, 4, 8, 1]
    maxElement = max(list)
    maxElementIndex = list.index(max(list))
    minElement = min(list[0:maxElementIndex])
    print(maxElement – minElement)


  • Mahesh

    int main()
    {
    int n;
    cin >> n;
    int a[n];
    for(int i=0;i> a[i];
    int diff=0,min=a[0];
    for(int i=1;ia[i-1])
    curr_diff=a[i]-min;
    sort(a,a+i+1);
    min=a[0];
    if(diff<curr_diff)
    diff=curr_diff;
    }
    cout << diff;
    }


  • Suyash

    arr = [ 7, 9, 5, 6, 3, 2 ]
    maxdiff = arr[1] – arr[0]
    for i in range(len(arr)):
    for j in range(i,len(arr)):
    diff = arr[j] – arr[i]
    if arr[i] < arr[j] and maxdiff < diff:
    maxdiff = diff
    print(maxdiff)


  • surya

    l=[1,2,3,4,5]
    n=len(l)
    b=0
    for i in range(n):
    for j in range(i+1,n):
    if(l[j]>l[i]):
    a=l[j]-l[i]
    if(a>b):
    b=a
    print(b)


  • IPE2K17

    PYTHON 3.8

    n = list(map(int, input().split()))
    n.sort()
    diff= n[len(n)-1]-n[0]
    print(diff)


  • Sourabh

    #include”bits/stdc++.h”
    #define ll long long
    using namespace std;
    void cal()
    {
    int res[] = {2, 3, 10, 6, 4, 8, 1}; // 7, 9, 5, 6, 3, 2
    int len = sizeof(res) / sizeof(res[0]);

    ll max=0;
    for(int i=0;i<len;i++)
    {

    for(int j=i+1;jmax && res[j]>res[i])
    {
    max = ans;
    }
    }

    }
    cout<<max;
    }

    int main()
    {
    cin.tie(0);
    ios::sync_with_stdio(false);

    cal();

    return 0;

    }


  • Sakshi

    l=[int(x) for x in input().split()]
    m=0
    for i in range(0,len(l)):
    ele=l[i]
    for j in range(i+1, len(l)):
    diff=l[j]-l[i]
    if(diff>m):
    m=diff
    print(m)