Shift 1's to right and 0's to left for the given int array

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter length of array");
int l = sc.nextInt();
System.out.println("Enter " + l + " elements one by one into array");
int arr[] = new int[l];

for (int i = 0; i < l; i++) {
arr[i] = sc.nextInt();
}

for (int i = 0; i < arr.length; i++) {
if (arr[i] == 1) {
System.out.print(arr[i]);
}
}

for (int i = 0; i < arr.length; i++) {
if (arr[i] == 0) {
System.out.print(arr[i]);
}
}
sc.close();
}
}


Input - 
Enter length of array
7
Enter 7 elements one by one into array
1
0
0
1
1
0
1

Output - 
1111000

Comments

Popular posts from this blog

How to check if two Strings are anagrams of each other?

Multimedia search engine using python

How to sort String array by length of each string present in an array