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.
- C
- Java
C
Java
C
#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; }
Java
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)); } }
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);
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);
}
}
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;
}
//………….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;
}
list = [2, 3, 10, 6, 4, 8, 1]
maxElement = max(list)
maxElementIndex = list.index(max(list))
minElement = min(list[0:maxElementIndex])
print(maxElement – minElement)
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;
}
arr=[ 7, 9, 5, 6, 3, 2]
m=arr.index(max(arr))
print(max(arr)-min(arr[:m]))
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)
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)
PYTHON 3.8
n = list(map(int, input().split()))
n.sort()
diff= n[len(n)-1]-n[0]
print(diff)
#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;
}
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)