Leetcode in Java && Oracle
419 subscribers
8 photos
397 files
400 links
Second channel: @codeforces_java

Let's Develop Together!
Download Telegram
image_2021-11-04_17-50-48.png
40.4 KB
#N922. Sort Array By Parity II
problem link

#solution
class Solution {
public int[] sortArrayByParityII(int[] nums) {
int[] ans=new int[nums.length];
int evenIndex=0, oddIndex=1;

for(int i=0; i<nums.length; i++){
if(nums[i]%2==0){
ans[evenIndex]=nums[i];
evenIndex+=2;
}
else{
ans[oddIndex]=nums[i];
oddIndex+=2;
}
}

return ans;
}
}