Amazon Question - 2 Greater on right side

You are given an array of size N. Replace every element with the next greatest element(greatest element on its right side) in the array. Also, since there is no element next to the last element, replace it with -1.
Input:
The first line of input contains an integer T denoting the number of test cases. T testcases follow. Each testcase contains two lines of input. The first line is N, the size of tha array. The second line contains space separated integers.
Output:
For each testcase, print the modified array.
Constraints:
1 <= T <= 50
1 <= N <= 100
1 <= Ai <= 1000
Example:
Input:
2
6
16 17 4 3 5 2
4
2 3 1 9
Output:
17 5 5 5 2 -1
9 9 9 -1

Code in C-Language :

#include<stdio.h>
int main() {
int t;
scanf("%d\n",&t);
while(t>0)
{
    int n;
    scanf("%d",&n);
    int arr[n];
    for(int i=0;i<n;i++)
    {
        scanf("%d",&arr[i]);
    }
    
    for(int i=0;i<n-1;i++)
    {
        int max=arr[i+1];
        for(int j=i+1;j<n;j++)
        {
            if(arr[j]>max)
              max=arr[j];
        }
        arr[i]=max;
    }
    arr[n-1]=-1;
    for(int k=0;k<n;k++){
        printf("%d ",arr[k]);
    }
    t--;
    printf("\n");
}
return 0;
}

Comments

Popular posts from this blog

C Program to Print a trapezium pattern

C Program to Print Pattern 1*2*3*4 9*10*11*12 13*14*15*16 5*6*7*8

Glimpse Of SPYDER | Mahesh Babu | A R Murugadoss | Rakul Preet Singh | Harris Jayaraj | #SPYDER Trailer , Teaser