I attempted the problem. I am not getting the right solution. Kindly help.
Problem : 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.
evenOdd([1, 0, 1, 0, 0, 1, 1]) → [0, 0, 0, 1, 1, 1, 1] evenOdd([3, 3, 2]) → [2, 3, 3] evenOdd([2, 2, 2]) → [2, 2, 2]
public int[] evenOdd(int[] nums) {
int l = nums.length;
if(l<2)
return nums;
int j=l-1;
for(int i=0;i<l;i++)
{
if(nums[i]%2==1)
{
while(j>i && nums[j]%2!=0) {
j--;
}
int t=nums[i];
nums[i]=nums[j];
nums[j]=t;
j--;
}
}
return nums;
}