forked from mirandaio/codingbat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevenOdd.java
More file actions
22 lines (19 loc) · 687 Bytes
/
evenOdd.java
File metadata and controls
22 lines (19 loc) · 687 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/* Return an array that contains the exact same numbers as the given array,
* but rearranged so that all the even numbers come before all the odd
* numbers. Other than that, the numbers can be in any order. You may modify
* and return the given array, or make a new array.
*/
public int[] evenOdd(int[] nums) {
int i = 0;
while(i < nums.length && nums[i] % 2 == 0)
i++;
for(int j = i + 1; j < nums.length; j++) {
if(nums[j] % 2 == 0) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
i++;
}
}
return nums;
}