Maximum Index

QUESTION

Given an array A of integers, find the maximum of j – i subjected to the constraint of A[i] <= A[j].\n\nExample :\n\nA : [3 5 4 2]\n\nOutput : 2 \nfor the pair (3, 4)\n \n\nInput:\n\nThe first line contains an integer T, depicting total number of test cases. \nThen following T lines contains an integer N depicting the size of array and next line followed by the value of array.\n\n\nOutput:\n\nPrint the maximum difference of the indexes i and j in a separate line.\n\n\nConstraints:\n\n1<=T<=30\n1<=N<=1000\n0<=A[i]<=100.

“TESTCASE_1”: “1\n2\n1 10\n###—###SEPERATOR—###—\n1”, “TESTCASE_2”: “1\n4\n2 5 7 9\n###—###SEPERATOR—###—\n3”, “TESTCASE_3”: “0\n###—###SEPERATOR—###—\n0”, “TESTCASE_4”: “0\n###—###SEPERATOR—###—\n0”, “TESTCASE_5”: “0\n###—###SEPERATOR—###—\n0

ANSWER

#include <stdio.h>
#include<stdlib.h>
int main()
{
  int numTests,t;
  scanf("%d",&numTests);
  int *val=malloc(sizeof(int) *1000);
  for(t=0;t<numTests;t++)
  {
    int count,i,j;
    scanf("%d",&count);
    for(i=0;i<count;i++)
    {
      scanf("%d",&val[i]);
    }
    int m=0;
    for(i=0;i<count-1;i++)
    {
      if(i==0 || val[i]<val[i-1])
      {
        for(j=count-1;j>i+m;j--)
        {
          if(val[i]<=val[j])
          {
            m=j-i;
            break;
          }
        }
      }
    }
    printf("%d\n",m);
  }
  free(val);
  

	return 0;
}
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.