May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.
Become a Premium Member and unlock a new, free course in leading technologies each month.
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;
}
Add your voice to the tech community where 5M+ people just like you are talking about what matters.
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;
If you are experiencing a similar issue, please ask a related question
Join the community of 500,000 technology professionals and ask your questions.