classSolution{publicintmissingNumber(int[]nums){//circular sorting works by putting numbers at their respective index//then finally finding out which number is not at its index that number will //be missinginti=0;while(i<nums.length){intj=nums[i]-1;//nums[i] should be put at index nums[i]-1;if(j<0){i++;continue;}elseif(j!=nums[j]-1){//if the number is not at its correct index then put it on its correct index//swap numbers at index i and jinttemp=nums[j];nums[j]=nums[i];//note j = nums[i]-1; //correct index for number nums[i]nums[i]=temp;}elsei++;}for(i=0;i<nums.length;i++){if(i!=nums[i]-1)returni+1;//missing number}return0;//if the 0 is missing}}
Top comments (0)
Subscribe
We're a place where coders share, stay up-to-date and grow their careers.
Top comments (0)