The Best & Easy JAVA solution. Sk


#1

Done By : Shashank Kumar.
public class Solution {
void Swaped(ArrayList A,int a,int b){
int temp = A.get(a);
A.set(a,A.get(b));
A.set(b,temp);
}
public ArrayList wave(ArrayList A) {
Collections.sort(A);
for(int i=0;i<A.size()-1;i+=2){
if(A.get(i)<A.get(i+1)){
Swaped(A,i,i+1);
}
}
return A;
}
}


#2

You can use Collections.swap(list, index1, index2) as well.


#3

ya,
you can do it .
i didn’t recalled that time.

Thanks;
For your feedback.