Concept

From: https://leetcode.com/discuss/study-guide/1902662/cyclic-sort-very-important-and-less-known-pattern

I saw a common pattern which isn't mainstream for some reason. So I decided to make a post on this topic.

Pattern name: Cyclic Sort Identification: given array of 0 to N, do some missing, repeated kind of operation PigeonHole principle: If you have N boxes and >N items, atleast one box has more than 1 item.

  1. https://leetcode.com/problems/missing-number/arrow-up-right Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array.

observation 1: among N+1 numbers we have only N boxes. observation 2: for every number "i", correct index is "i"

so if array was sorted, we will just travel the array and the first number which doesn't match it's index would be answer. Sorting part is where cycle sort comes in.

    int missingNumber(vector<int>& nums) {
        int i=0;
        int n = nums.size();
        while(i<n)
        {
			int correctIdx = nums[i]; //where this element should be in sorted array
            if(correctIdx<n && correctIdx != i) //if not already at correct position and correct position < n
            {
                swap(nums[i],nums[correctIdx]); //put current element at correct position
            } 
			else
				i++; // move to next index
        }
        for(int i=0;i<n; i++)
            if(nums[i]!=i)
                return i;
        return n;
    }
  1. https://leetcode.com/problems/find-all-duplicates-in-an-array/arrow-up-right Given an integer array nums of length n where all the integers of nums are in the range [1, n] and each integer appears once or twice, return an array of all the integers that appears twice. You must write an algorithm that runs in O(n) time and uses only constant extra space.

observation 1: [1,n] integers and some appear twice, we can't put two elements in same box. observation 2: for every element, correctIdx = nums[i] - 1 because instead of [0,n] we have [1,n] numbers

  1. https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/arrow-up-right Given an array nums of n integers where nums[i] is in the range [1, n], return an array of all the integers in the range [1, n] that do not appear in nums.

observation 1: [1,n] integers and some appear twice, we can't put two elements in same box. observation 2: for every element, correctIdx = nums[i] - 1 because instead of [0,n] we have [1,n] numbers

  1. https://leetcode.com/problems/first-missing-positive/arrow-up-right (LC hard) Given an unsorted integer array nums, return the smallest missing positive integer. You must implement an algorithm that runs in O(n) time and uses constant extra space. N = nums.size() observation 1: in N size array, we can maximum have first N positive integers in box because we can't put two elements in same box. observation 2: if we sort the array, then at whichever index nums[i] != i+1, that will be first missing positive observation 1: we don't need to sort every element, we just need to sort elements from [1,N] so they are in right place, we don''t need to check further.

That's all for this pattern, Hope it helps!

related questions: https://leetcode.com/problems/set-mismatch/arrow-up-right https://leetcode.com/problems/couples-holding-hands/arrow-up-right will add more later.

Last updated