Ques:Return an array that contains the exact same numbers as the given array,
but rearranged so that all the zeros are grouped at the start of the
array. The order of the non-zero numbers does not matter. So {1, 0, 0,
1} becomes {0 ,0, 1, 1}. You may modify and return the given array or
make a new array
Ans:
public static int[] notAlone(int[] nums, int val) {
int countof0=0;
int swap=0;
for(int i=0;i<nums.length;i++){
if(nums[i]==0){
swap=nums[countof0];
nums[countof0]=0;
nums[i]=swap;
countof0++;
}
}
return nums;
}
Ans:
public static int[] notAlone(int[] nums, int val) {
int countof0=0;
int swap=0;
for(int i=0;i<nums.length;i++){
if(nums[i]==0){
swap=nums[countof0];
nums[countof0]=0;
nums[i]=swap;
countof0++;
}
}
return nums;
}
No comments:
Post a Comment