-
Notifications
You must be signed in to change notification settings - Fork 4
/
q3.c
44 lines (44 loc) · 744 Bytes
/
q3.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<stdio.h> //max(j-i) where a[j]>a[i]
#include<stdlib.h>
struct res
{
int value;
int index;
};
int cmp(const void* a,const void* b)
{
return (*(int*)a-*(int*)b);
}
int main()
{
int n,i;
printf("Enter the no. of elements in the array : \n");
scanf("%d",&n);
int a[n];
printf("Enter the elements for the array : \n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
struct res arr[n];
for(i=0;i<n;i++)
{
arr[i].value=a[i];
arr[i].index=i;
}
qsort(arr,n,sizeof(arr[0]),cmp);
int min=arr[0].index,max=0;
for(i=0;i<n;i++)
{
if(arr[i].index<min)
{
min=arr[i].index;
}
if(arr[i].index-min>max)
{
max=arr[i].index-min;
}
}
printf("The max(j-i) where arr[j]>arr[i] is %d",max);
return 0;
}