Arrange Even And Odd Numbers Using Java

 package demo;


public class ArrangeEvenOdd {


public static void main(String[] args) {


int arr[] = { 2, 1, 3, 4, 6, 7, 8, 10 };

int left = 0;

int right = arr.length - 1;

/*

* keep incrementing left until we see an odd number

*/

while (left < right) {

/* Increment left index while we see 0 at left */

while (arr[left] % 2 == 0 && left < right)

left++;


/* Decrement right index while we see 1 at right */

while (arr[right] % 2 == 1 && left < right)

right--;


if (left < right) {

int temp = arr[left];

arr[left] = arr[right];

arr[right] = temp;

left++;

right--;

}


}


for (int i = 0; i < arr.length; i++) {

System.out.print(arr[i]+" ");

}

}

}


Comments

Popular posts from this blog

3 Lines of Code And You Can Download Your Favourite Youtube Video.

Leetcode Problem(Easy) Roman to Integer

Leetcode : (Find Minimum in Rotated Sorted Array)