This is Discussion thread about Finquest
Discussion About Finquest
Please tell which step am I missing in my code as I am getting the right output for a number of test cases.
we might need your code to tell u so…
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner scn=new Scanner(System.in);
int n=scn.nextInt();
for(int i=0;i<n;i++)
{
int x=scn.nextInt();
int arr[]=new int[x];
for(int j=0;j<x;j++)
{
arr[j]=scn.nextInt();
}
int max[]=new int[x];
max[x-1]=-1;
for(int j=x-2;j>-1;j--)
{
if(max[j+1]!=-1 && arr[j]<arr[max[j+1]])
{
max[j]=max[j+1];
}
else if(arr[j]<arr[j+1])
{
max[j]=j+1;
}
else
max[j]=-1;
}
int t=0;
for(int j=0;j<max.length;j++)
{
if(max[j]==-1)
t+=0;
else
{
t+=(arr[max[j]]-arr[j]);
}
}
System.out.println(t);
}
}
}