I am trying to sort an array of integers using Arrays.sort() method. After calling this method, the array elements are becoming 0.
Below is my code
import java.util.Arrays;
import java.util.Scanner;
public class WillMuggerWin {
public static void main(String ar[]){
int t,n=10,m=5,sum=0,flag=0;
int notes[]=new int[20];
Scanner s=new Scanner(System.in);
t=s.nextInt();
for(int i=0;i<t;i++){
n=s.nextInt();
m=s.nextInt();
int temp=m;
// System.out.println("m:"+m+"n:"+n);
for(int j=0;j<n;j++){
notes[j]=s.nextInt();
}
System.out.println("note:"+notes[0]+" "+notes[1]+" "+notes[2]);
Arrays.sort(notes);
System.out.println("note:"+notes[0]+" "+notes[1]+" "+notes[2]);
//System.out.println("note1"+notes[0]);
for(int k=n-1;k>=0;k--){
//System.out.println("notes "+notes[k]);
if(notes[k]<=temp){
sum=sum+notes[k];
System.out.println("sum: "+sum);
temp=temp-sum;
if(temp==0){
flag=1;
break;
}
}
}
if(flag==1)
System.out.println("Yes");
else
System.out.println("No");
flag=0;
sum=0;
}
}
}
Input:
5 3 3 1 1 1
Output:
note:1 1 1
note:0 0 0
sum: 0
sum: 0
sum: 0
No
What is wrong with my code?