|
| 1 | +Question: Given an array of integers. Move all non-zero elements to the left of all zero elements. |
| 2 | + |
| 3 | +Source: http://www.careercup.com/question?id=5668212962230272 |
| 4 | + |
| 5 | +Solution: |
| 6 | +I have given, two solutions: |
| 7 | +1. Using Single Pointer in the array |
| 8 | +2. Using Two Pointers in the array |
| 9 | + |
| 10 | +Both have Time Complexity = O(n) and Space Complexity = O(1) |
| 11 | + |
| 12 | +************************************ Using Two Pointers **************************************************** |
| 13 | +package MoveNonZeroToTheLeftOfZeroElementsInAnArray; |
| 14 | + |
| 15 | +import java.util.Scanner; |
| 16 | + |
| 17 | +public class UsingTwoPointersInTheArray { |
| 18 | +public static void main(String[] args) { |
| 19 | + Scanner in = new Scanner(System.in); |
| 20 | + try{ |
| 21 | + System.out.println("Enter the number of elements of the array"); |
| 22 | + int n = in.nextInt(); |
| 23 | + System.out.println("Enter the elements - zero and non-zero"); |
| 24 | + int[] a = new int[n]; |
| 25 | + for(int i=0;i<n;i++) |
| 26 | + a[i]=in.nextInt(); |
| 27 | + a = moveElements(a); |
| 28 | + printArray(a); |
| 29 | + } |
| 30 | + finally{ |
| 31 | + in.close(); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +private static void printArray(int[] a) { |
| 36 | + for(int i=0;i<a.length;i++) |
| 37 | + System.out.print(a[i]+" "); |
| 38 | + System.out.println(); |
| 39 | +} |
| 40 | + |
| 41 | +private static int[] moveElements(int[] a) { |
| 42 | + int low = 0; |
| 43 | + int high = a.length-1; |
| 44 | + while(low<high){ |
| 45 | + if(a[low]==0 && a[high]!=0){ |
| 46 | + a=swap(a,low,high); |
| 47 | + high--; |
| 48 | + low++; |
| 49 | + continue; |
| 50 | + } |
| 51 | + if(a[low]==0 && a[high]==0){ |
| 52 | + high--; |
| 53 | + } |
| 54 | + if(a[low]!=0 && a[high]==0){ |
| 55 | + low++; |
| 56 | + high--; |
| 57 | + } |
| 58 | + if(a[low]!=0 && a[high]!=0){ |
| 59 | + low++; |
| 60 | + } |
| 61 | + } |
| 62 | + return a; |
| 63 | + } |
| 64 | + |
| 65 | +private static int[] swap(int[] a, int low, int high) { |
| 66 | + a[low] = a[low]^a[high]; |
| 67 | + a[high] = a[low]^a[high]; |
| 68 | + a[low] = a[low]^a[high]; |
| 69 | + return a; |
| 70 | + } |
| 71 | +} |
| 72 | +/* |
| 73 | +Analysis: |
| 74 | + Time Complexity = O(n) |
| 75 | + Space Complexity = O(1) |
| 76 | +*/ |
| 77 | + |
| 78 | +************************************ Using Single Pointer **************************************************** |
| 79 | +package MoveNonZeroToTheLeftOfZeroElementsInAnArray; |
| 80 | + |
| 81 | +import java.util.Scanner; |
| 82 | + |
| 83 | +public class UsingSinglePointer { |
| 84 | + public static void main(String[] args) { |
| 85 | + Scanner in = new Scanner(System.in); |
| 86 | + try{ |
| 87 | + System.out.println("Enter the number of elements in the array"); |
| 88 | + int n = in.nextInt(); |
| 89 | + System.out.println("Enter the elements of the array - zero and non zero"); |
| 90 | + int[] a = new int[n]; |
| 91 | + for(int i=0;i<n;i++) |
| 92 | + a[i] = in.nextInt(); |
| 93 | + a = moveElements(a); |
| 94 | + printArray(a); |
| 95 | + } |
| 96 | + finally{ |
| 97 | + in.close(); |
| 98 | + } |
| 99 | + } |
| 100 | + private static int[] moveElements(int[] a) { |
| 101 | + int zeroPoisitionPointer = 0; // initialize a pointer which points to the element which is 0 in the array. |
| 102 | + // assume initially this pointer is at position 0 |
| 103 | + for(int i=0;i<a.length;i++){ // This loop is only for iterating, it doesn't act like a second pointer. |
| 104 | + // Hence, there is only one pointer in this program(zeroPositionPointer) |
| 105 | + if(a[i]!=0){ // if the element is not 0, then shift to 0 position |
| 106 | + a[zeroPoisitionPointer++] = a[i]; |
| 107 | + } |
| 108 | + } |
| 109 | + for(;zeroPoisitionPointer<a.length;zeroPoisitionPointer++) // fill the remaining array elements with 0 |
| 110 | + a[zeroPoisitionPointer]=0; |
| 111 | + return a; |
| 112 | + } |
| 113 | + private static void printArray(int[] a) { |
| 114 | + for(int i=0;i<a.length;i++) |
| 115 | + System.out.print(a[i]+" "); |
| 116 | + System.out.println(); |
| 117 | + } |
| 118 | + |
| 119 | +} |
| 120 | +/* |
| 121 | +Analysis: |
| 122 | + Time Complexity = O(n) |
| 123 | + Space Complexity = O(1) |
| 124 | +*/ |
0 commit comments