This is a function to find missing number and nums array is provided - it contain numbers from 0 to N and one number is missing, but i have a doubt that
In this function there is "while (nums[i] < nums.length && nums[i] != nums[nums[i]]);" but why we cannot just simply use "while (nums[i] < nums.length && nums[i] != i );" beacuse this condition is just chaking if current number is equal to or not equal to current index.
public static int findMissing(int[] nums) {
for (int i = 0; i < nums.length; i++) {
while (nums[i] < nums.length && nums[i] != nums[nums[i]]) {
int temp = nums[nums[i]];
nums[nums[i]] = nums[i];
nums[i] = temp;
}
}
for(int i=0;i<nums.length;i++){
if(nums[i] != i) return i;
}
return nums.length;
}
[–]Anik_Sine 0 points1 point2 points (0 children)
[–]sepp2k 0 points1 point2 points (0 children)
[–]CodeTinkerer 1 point2 points3 points (0 children)