Skip to content

Latest commit

 

History

History
54 lines (42 loc) · 1.3 KB

find-missing-and-repeating2512.md

File metadata and controls

54 lines (42 loc) · 1.3 KB

Find Missing And Repeating

Problem Link

Given an unsorted array Arr of size N of positive integers. One number 'A' from set {1, 2, …N} is missing and one number 'B' occurs twice in array. Find these two numbers.

Sample Input

2
2 2

Sample Output

2 1

Solution

Time: O(N), Space: O(N)

class Solution{
public:
    int *findTwoElement(int *arr, int n) {
        vector<bool> nums(n+1, 0);

        int repeated;
        
        int ans[2];

        for(int i = 0; i < n; ++i) {
            int num = arr[i];
            if(nums[num]) {
                ans[0] = num;
            }
            nums[num] = nums[num] ^ 1;
        }

        for(int i = 1; i <= n; ++i) {
            if(nums[i] == 0 && i != ans[0]) {
                ans[1] = i;
                break;
            }
        }

        int *ptr;
        ptr = ans;

        return ptr;

    }
};

Accepted

image