Asked in:
Ola cabs, Morgan-Stanley, Microsoft, MAQ software, Hike,Amazon,Adobe,YatraAccolite,SAP-Labs,Paytm
Question:
Ola cabs, Morgan-Stanley, Microsoft, MAQ software, Hike,Amazon,Adobe,YatraAccolite,SAP-Labs,Paytm
Question:
Write a program to sort an array of 0's,1's and 2's in ascending order.(Also known as Dutch National Flag problem,the given solution is basic method for beginners)
Input:
The first line contains an integer 'T' denoting the total number of test cases. In each test cases, First line is number of elements in array 'N' and second its values.
Output:
Print the sorted array of 0's, 1's and 2's.
Constraints:
The first line contains an integer 'T' denoting the total number of test cases. In each test cases, First line is number of elements in array 'N' and second its values.
Output:
Print the sorted array of 0's, 1's and 2's.
Constraints:
1 <= T <= 100
1 <= N <= 100
0 <= arr[i] <= 2
1 <= N <= 100
0 <= arr[i] <= 2
Example:
Input :
2
5
0 2 1 2 0
3
0 1 0
5
0 2 1 2 0
3
0 1 0
Output:
0 0 1 2 2
0 0 1
0 0 1
Code:
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.Arrays;
class Solution {
public static void main (String[] args) {
Scanner sc = new Scanner (System.in);
int m = sc.nextInt();
for (int i =0;i<m;i++){
int n = sc.nextInt();
int [] arr = new int [n];
for (int j=0;j<n;j++){
arr[j]=sc.nextInt();
}
Arrays.sort(arr);
for (int k=0; k<arr.length; k++){
System.out.print(arr[k]+ " ");
}
System.out.println();
}
}
}
Comments
Post a Comment