With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.
public int[] evenOdd(int[] nums) {
for(int i=0;i<nums.length;i++){
if(nums[i]%2==0){
}
else if(nums[i]%2==1){
}
}
return nums;
}
public int[] evenOdd(int[] nums) {
for(int i=0;i<nums.length-1;i++){
for(int j=i+1;j<nums.length;j++){
if(nums[i]%2==0){
break;
} else {
if(nums[j]%2==0){
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
break;
}
}
}
}
return nums;
}
Go through array once, odd number found -> exchange with first position in array. Increase first replace position when exchange is done.
public int[] evenOdd(int[] nums) {
int len=nums.length;
int[] arr=new int[len];
int front=0;
int back=len-1;
for(int i=0;i<nums.length;i++){
if(nums[i]%2==0){
arr[front]=nums[i];
front++;
}
else if(nums[i]%2==1){
arr[back]=nums[i];
back--;
}
}
return arr;
}
Expected Run
evenOdd([1, 0, 1, 0, 0, 1, 1]) → [0, 0, 0, 1, 1, 1, 1] [0, 0, 0, 1, 1, 1, 1] OK
evenOdd([3, 3, 2]) → [2, 3, 3] [2, 3, 3] OK
evenOdd([2, 2, 2]) → [2, 2, 2] [2, 2, 2] OK
evenOdd([3, 2, 2]) → [2, 2, 3] [2, 2, 3] OK
evenOdd([1, 1, 0, 1, 0]) → [0, 0, 1, 1, 1] [0, 0, 1, 1, 1] OK
evenOdd([1]) → [1] [1] OK
evenOdd([1, 2]) → [2, 1] [2, 1] OK
evenOdd([2, 1]) → [2, 1] [2, 1] OK
evenOdd([]) → [] [] OK
other tests
OK
import java.util.Arrays;
public class EvenOdd {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] ar={1, 0, 1, 0, 0, 1, 1};
System.out.println("value-->"+Arrays.toString(evenOdd(ar)));
}
public static int[] evenOdd(int[] nums) {
int len=nums.length;
int[] arr=new int[len];
int front=0;
int back=len-1;
for(int i=0;i<nums.length;i++){
if(nums[i]%2==0){
arr[front]=nums[i];
front++;
}
else if(nums[i]%2==1){
arr[back]=nums[i];
back--;
}
}
return arr;
}
/*Another possible solution, would be to create a new array of the same size,
loop through the original array, if the number is even fill the result array from the
front, but if it is odd, fill the result array from the back. It does require a bit
extra memory to store an extra array plus you need 3 index variables... One for the main
loop index, one to track where you are filling the result array from the front and one to
track where you are filling from the back. But the advantage is that you only need to
loop through the array once, whereas the other solutions loop through many times.
*/
}
public int[] evenOdd(int[] nums) {
for(int i=0;i<nums.length-1;i++){
for(int j=i+1;j<nums.length;j++){
if(nums[i]%2==0){
break;
} else {
if(nums[j]%2==0){
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
break;
}
}
}
}
return nums;
}
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;