Problem: An array of integers will be given, find the maximum distance of indexes of [j – i] subjected to the constraint of A[i] <= A[j].
A : [4 6 5 3]; Output : 2; For the pair (4, 5).
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
#include <stdio.h> int arr[1005]; int t,n,mx; int main() { //code int i,j; scanf("%d",&t); while(t--) { scanf("%d",&n); for( i=0;i<n;i++) { scanf("%d",&arr[i]); } mx=0; for(i=0;i<n-1;i++) { for(j=n-1;j>=i;j--) { if(arr[i]<arr[j]) { if(j-i>mx) mx=j-i; break; } } } printf("%d\n",mx); } return 0; } |