1054B - Appending Mex - CodeForces Solution


implementation *1000

Please click on ads to support us..

Python Code:

if __name__ == "__main__":
    n = int(input())
    max_element = -1
    flag = 0

    x = list(map(int, input().split()))
    for i in range(n):
        if x[i] > max_element + 1:
            print(i + 1)
            flag = 1
            break
        max_element = max(max_element, x[i])

    if flag == 0:
        print(-1)
  	    			 	 	 	 	  			 				 	

C++ Code:

// Problem : Appending Mex
// Date : 25-06-2023
// Link : https://codeforces.com/problemset/problem/1054/B
// Author : Ansari Mohd Abuzaid

#include <iostream>
#include <cmath>
#include <climits>
#include <bits/stdc++.h>
#include <cstring>
using namespace std;
#define ll long long int
#define inparr(a, n)            \
    for (int i = 0; i < n; i++) \
    cin >> a[i]
#define outarr(a, n)            \
    for (int i = 0; i < n; i++) \
    {                           \
        cout << a[i] << " ";    \
    }                           \
    cout << endl;
#define inpvec(a,n)             \
    for (int i = 0; i < n; i++) \
    {                           \
        int mid;                \
        cin>>mid;               \
        a.push_back(mid);       \
    }                           
#define fl(i, a, b) for (int i = a; i < b; i++)
#define flr(i, a, b) for (int i = a; i > b; i--)
#define pf(a) cout << a << endl;
#define pf2(a, b) cout << a << " " << b << endl;
#define inp(a) cin >> a;
#define inp2(a, b) cin >> a >> b;
#define ret return;
#define NO cout << "NO" << endl;
#define YES cout << "YES" << endl;
#define str string
const int mod = 1000000007;

int largestPower(int n, int p)
{
    // Initialize result
    int x = 0;

    // Calculate x = n/p + n/(p^2) + n/(p^3) + ....
    while (n)
    {
        n /= p;
        x += n;
    }
    return x;
}

// Utility function to do modular exponentiation.
// It returns (x^y) % p
int power(int x, int y, int p)
{
    int res = 1; // Initialize result
    x = x % p;   // Update x if it is more than or
    // equal to p
    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res * x) % p;

        // y must be even now
        y = y >> 1; // y = y/2
        x = (x * x) % p;
    }
    return res;
}

// Returns n! % p
int modFact(int n, int p)
{
    if (n >= p)
        return 0;

    int res = 1;

    // Use Sieve of Eratosthenes to find all primes
    // smaller than n
    bool isPrime[n + 1];
    memset(isPrime, 1, sizeof(isPrime));
    for (int i = 2; i * i <= n; i++)
    {
        if (isPrime[i])
        {
            for (int j = 2 * i; j <= n; j += i)
                isPrime[j] = 0;
        }
    }

    // Consider all primes found by Sieve
    for (int i = 2; i <= n; i++)
    {
        if (isPrime[i])
        {
            // Find the largest power of prime 'i' that divides n
            int k = largestPower(n, i);

            // Multiply result with (i^k) % p
            res = (res * power(i, k, p)) % p;
        }
    }
    return res;
}

int dist(ll arr[], ll n)
{
    unordered_set<int> s;
    int res = 0;
    for (int i = 0; i < n; i++)
    {
        if (s.find(arr[i]) == s.end())
        {
            s.insert(arr[i]);
            res++;
        }
    }
    return res;
}

int freq(string a, ll n, char x)
{
    int count = 0;
    for (int i = 0; i < n; i++)
        if (a[i] == x)
            count++;
    return count;
}

int SumOfDigitsNum(int x)
{
    int n = 0;
    while (x != 0)
    {
        n = n + x % 10;
        x = x / 10;
    }
    return n;
}

int SumOfDigitsStr(string x)
{
    int n = 0;
    fl(i, 0, x.length())
    {
        n = n + x[i] - 48;
    }
    return n;
}

int pos(str s, ll n, char x)
{
    int p = 0;
    for (int i = 0; i < n; i++)
    {
        if (s[i] == x)
        {
            return i + 1;
        }
    }
    return -1;
}

int gcd(int a, int b)
{
    int result = min(a, b); // Find Minimum of a and b
    while (result > 0)
    {
        if (a % result == 0 && b % result == 0)
        {
            break;
        }
        result--;
    }
    return result; // return gcd of a and b
}

bool prime(ll num)
{
    if (num <= 3)
    {
        return true;
    }
    fl(i, 2, sqrt(num) + 1)
    {
        if (num % i == 0)
        {
            return false;
        }
    }
    return true;
}

long add(long a, long b)
{
    return (((long)(a + mod) % mod + (b + mod) % mod) % mod);
}
long sub(long a, long b)
{
    return (((long)(a + mod) % mod + ((-1 * b) + mod) % mod) % mod);
}

long mul(long a, long b)
{
    return (((long)a % mod * b % mod) % mod);
}
long inv(long x)
{
    return pow(x, mod - 2);
}

// long div(long x, long y)
// {
//     return mul(x, inv(y));
// }

long poww(long a, long b)
{
    a %= mod;
    long res = 1;
    while (b > 0)
    {
        if ((b & 1) != 0)
            res = mul(res, a);
        a = mul(a, a);
        b /= 2;
    }
    return res;
}

unsigned int absu(int value)
{
    return (value < 0) ? -((unsigned int)value) : (unsigned int)value;
}

void testcase()
{
    ll m, l,h, n, k, N, K, gin, tin, t1 = 1, t2 = 0, t3 = 0, t4 = 0, t6 = -1, t8 = 0, t5 = 0, t7 = 1, flag = 0, maxx = 0, minn = INT64_MAX, maxy = 0;
    inp(n);
    ll a[n];
    inparr(a,n);
    maxx=-1;
    fl(i,0,n){
        if (a[i]<=(maxx+1))
        {
            maxx=max(a[i],maxx);
            continue;
        }
        else
        {
            pf(i+1);
            ret
        }
        
    }
    pf(-1);
    
    
}

int main()
{
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    // int n;
    // cin >> n;
    // while (n--)
    // {
        testcase();
        // pf("\n");
    // }
    return 0;
}


Comments

Submit
0 Comments
More Questions

677C - Vanya and Label
1583B - Omkar and Heavenly Tree
1703C - Cypher
1511C - Yet Another Card Deck
1698A - XOR Mixup
1702E - Split Into Two Sets
1703B - ICPC Balloons
1702F - Equate Multisets
1700A - Optimal Path
665C - Simple Strings
1708A - Difference Operations
1703E - Mirror Grid
1042A - Benches
1676B - Equal Candies
1705B - Mark the Dust Sweeper
1711A - Perfect Permutation
1701B - Permutation
1692A - Marathon
1066A - Vova and Train
169B - Replacing Digits
171D - Broken checker
380C - Sereja and Brackets
1281B - Azamon Web Services
1702A - Round Down the Price
1681C - Double Sort
12A - Super Agent
1709A - Three Doors
1680C - Binary String
1684B - Z mod X = C
1003A - Polycarp's Pockets