My issue
why this code doesn’t work?
My code
import java.util.*;
import java.lang.*;
import java.io.*;
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
int[] a = new int[n/2 + n%2];
int[] b = new int[n/2];
int aa = 0, bb = 0;
for(int i=0;i<n;){
a[aa] = sc.nextInt();
if(a[aa]<0) a[aa]*=-1;
aa++;
i++;
if(i<n){
b[bb] = sc.nextInt();
if(b[bb]<0) b[bb]*=-1;
bb++;
}
i++;
}
int sa =0,sb=0;
for(int i=0;i<aa;i++) sa+=a[i];
for(int j=0;j<bb;j++) sb+=b[j];
Arrays.sort(a);
Arrays.sort(b);
int temp = a[0];
a[0] = b[bb-1];
b[bb-1] = temp;
int suma = 0;
int sumb = 0;
for(int i=0;i<aa;i++) suma+=a[i];
for(int i=0;i<bb;i++) sumb +=b[i];
System.out.println(Math.max(sa-sb, suma - sumb));
}
}
}
Problem Link: Plusle and Minun on Array Practice Coding Problem - CodeChef