Missing Number

You are given an array A. You can decrement any element of the array by 1. This operation can be repeated any number of times. A number is said to be missing if it is the smallest positive number which is a multiple of 2 that is not present in array A. You have to find the maximum missing number after all possible decrements of the elements.

Example:

Input:  n = 6, a = [1 3 3 3 6 7]
Output: 8

Approach

C++

#include <bits/stdc++.h>
using namespace std;

long long missingNumbers(long long n, long long a[])
{
    sort(a, a + n);
    long long res = 2;
    for (long long i = 0; i < n; i++)
    {
        if (a[i] >= res)
            res += 2;
    }
    return res;
}
int main()
{
    long long n = 6;

    long long a[n] = {1, 3, 3, 3, 6, 7};

    cout << missingNumbers(n, a) << "\n";

    return 0;
}


No comments:

Post a Comment