CONCEPTS USED:
Arrays
DIFFICULTY LEVEL:
Hard
PROBLEM STATEMENT(SIMPLIFIED):
Given an array
AofNpositive integers. The task is to find themaximumof
j - isuch thatA[j] > A[i], wherej > i.
For Example :
Input : N = 10, A[] = [5 16 7 3 2 9 10 4 3 4]
Output : 6
Explanation :
16 > 5, diff = 1 - 0 = 1
7 > 5, diff = 2 - 0 = 2
9 > 5, diff = 5 - 0 = 5
9 > 7, diff = 5 - 2 = 3
10 > 5, diff = 6 - 0 = 6
4 > 2, diff = 7 - 4 = 1
3 > 2, diff = 8 - 4 = 4
4 > 2, diff = 9 - 4 = 5
Out of all these differences, 6 was the maximum difference between indexes of 5 and 10 i.e. 6 - 0 = 6
SOLVING APPROACH:
BRUTE FORCE METHOD:
This method is
SimplebutInefficient.Run two nested loops.
In the outer loop, pick elements one by one from left to right.
In the inner loop, pick the rest of the elements and keep comparing the
maximumvalue of(j-i).
Time complexityof this approach is O(N2).
EFFICIENT METHOD:
The problem can be solved by simply finding out two
Optimum Indexesof the array,left indexiandright indexj.For an element
A[i], we need not consider it for the left index if there exists a smaller element thanA[i]on its left side.Similarly for an element
A[i], we need not consider it for the right index if there exits a greater element on its right side.Now construct two arrays :
leftMin[]andrightMax[],
such thatleftMin[i]contains the smallest element on its left side including itself andrightMax[i]contains the largest element on its right side including itself.Start traversing both the arrays from left to right.
While traversing :-
if
leftMin[i]>rightMax[i], then we simply move ahead inleftMin[]by1, as all elements to the left ofleftMin[i]are either greater than or equal toleftMin[i]if
leftMin[i]<rightMax[i], then we move ahead inrightMax[i]to look for a better value ofj-i.
SOLUTIONS:
#include<stdio.h>
int maximumGap(int arr[],int n) {
int l[n],r[n];
l[0]=arr[0];
for(int i=1;i<n;i++)
{
if(arr[i]<l[i-1])
l[i] = arr[i];
else
l[i] = l[i-1];
}
r[n-1]=arr[n-1];
for(int i=n-2;i>=0;i--)
{
if(arr[i] > r[i+1])
r[i] = arr[i];
else
r[i] = r[i+1];
}
int i=0,j=0;
int max_dist = -1;
while(i<n && j<n)
{
if(l[i]<r[j])
{
if(j-i > max_dist)
max_dist = j-i;
j++;
}
else
{
i++;
}
}
if(max_dist==-1)
return -1;
return max_dist;
}
int main()
{
int n;
scanf("%d",&n);
int arr[n];
for(int i=0;i<n;i++)
scanf("%d",&arr[i]);
printf("%d\n",maximumGap(arr,n));
return 0;
}
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int maximumGap(int arr[],int n) {
int l[n],r[n];
l[0]=arr[0];
for(int i=1;i<n;i++)
{
l[i]=min(arr[i],l[i-1]);
}
r[n-1]=arr[n-1];
for(int i=n-2;i>=0;i--)
{
r[i]=max(arr[i],r[i+1]);
}
int i=0,j=0;
int max_dist = -1;
while(i<n && j<n)
{
if(l[i]<r[j])
{
max_dist = max(max_dist,j-i);
j++;
}
else
{
i++;
}
}
if(max_dist==-1)
return -1;
return max_dist;
}
int main()
{
int n;cin>>n;
int arr[n];
for(int i=0;i<n;i++)
cin>>arr[i];
cout<<maximumGap(arr,n)<<"\n";
return 0;
}
import java.util.*;
import java.io.*;
import java.lang.Math;
public class Main {
static int maximumGap(int arr[],int n) {
int l[] = new int[n];
int r[] = new int[n];
l[0]=arr[0];
for(int i=1;i<n;i++)
{
l[i]=Math.min(arr[i],l[i-1]);
}
r[n-1]=arr[n-1];
for(int i=n-2;i>=0;i--)
{
r[i]=Math.max(arr[i],r[i+1]);
}
int i=0,j=0;
int max_dist = -1;
while(i<n && j<n)
{
if(l[i]<r[j])
{
max_dist = Math.max(max_dist,j-i);
j++;
}
else
{
i++;
}
}
if(max_dist==-1)
return -1;
return max_dist;
}
public static void main(String args[]) throws IOException {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int arr[] = new int[n];
for(int i=0;i<n;i++)
arr[i] = sc.nextInt();
System.out.println(maximumGap(arr,n));
}
}
def maximumGap(arr,n): l=[0 for i in range(n)] r=[0 for i in range(n)] l[0]=arr[0] for i in range(1,n): l[i]=min(arr[i],l[i-1]) r[n-1]=arr[n-1] for i in range(n-2,-1,-1): r[i]=max(arr[i],r[i+1]) i=0 j=0 max_dist=-1 while(i<n and j<n): if l[i]<r[j]: max_dist = max(max_dist,j-i) j+=1 else: i+=1 if max_dist==-1: return -1 return max_dist n=int(input()) arr=list(map(int,input().split())) print(maximumGap(arr,n))
Space Complexity: O(N)
[forminator_quiz id="628"]
This article tried to discuss Arrays. Hope this blog helps you understand and solve the problem. To practice more problems on Arrays you can check out .
