Problem Statement

Pattern: Pattern 1-n range array


Solution

class Solution {
	public void swap(int[] x, int a, int b) {
		int t = x[a];
		x[a] = x[b];
		x[b] = t;
	}
 
	public int missingNumber(int[] nums) {
		int n = nums.length, ans = n;
 
		// cycle sort
		for (int i = 0; i < n; i++)
			while (nums[i] != i) {
				// if final no.
				if (nums[i] == n) break;
				swap(nums, i, nums[i]);
			}
 
		// scan for el not matching index
		for (int i = 0; i < n; i++) {
			if (nums[i] != i)
				ans = i;
		}
		return ans;
	}
}

Notes

  • Cna also bee done with self-freq array