My issue
public static void main (String[] args) throws java.lang.Exception
{
Scanner s= new Scanner(System.in);
int t=s.nextInt();
for(int j=0;j<t;j++){
int n=s.nextInt();
int[] a=new int[n];
for(int i=0;i<n;i++){
a[i]=s.nextInt();
}
int score = 0;
int operations = a.length / 2;
int i=0;
while(operations>0){
int ad=a[i]-a[i+1];
if(ad<0)
score=score-ad;
else
score+=ad;
i=i+2;
operations--;
}
System.out.println(score);
}
}
#can anyone tell whats wrong in this code?
My code
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner s= new Scanner(System.in);
int t=s.nextInt();
for(int j=0;j<t;j++){
int n=s.nextInt();
int[] a=new int[n];
for(int i=0;i<n;i++){
a[i]=s.nextInt();
}
int score = 0;
int operations = a.length / 2;
int i=0;
while(operations>0){
int ad=a[i]-a[i+1];
if(ad<0)
score=score-ad;
else
score+=ad;
i=i+2;
operations--;
}
System.out.println(score);
}
}
}
Problem Link: Maximum Score Practice Coding Problem - CodeChef

