Find Maximum Distance [ Google-Amazon ]

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).

Link

#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;
}

 

 

Comments are closed.