Back to course home
0% completed
Vote For New Content
I need help understanding the first if statement in this problem if(nums[i] != n...
Casey Park
Apr 22, 2022
I need help understanding the first if statement in this problem if(nums[i] != nums[nums[i]-1]). Why is it not sufficient to do a check such as if(nums[i] != i+1) ?
2
0
Comments
Comments
Design Gurus4 years ago
Take the example array: [1, 3, 3, 2]
When i = 1, then based on your check:
nums[i] != i+ 1 ==> 3 != 2
so we call swap(nums, 1, 2). This will result in the same array, as both index 1 and index 2 have the same element (i.e., 3). And, thus, we will be stuck in an ...
D
Daniel 3 years ago
But if all the numbers are distinct, then we can use if(nums[i] != i+1) ?
On this page