Header Ad

HackerEarth Missing Number problem solution

In this HackerEarth Missing Number problem solution, 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 the array A. You have to find the maximum missing number after all possible decrements of the elements.


HackerEarth Missing Number problem solution


HackerEarth Missing Number problem solution.

#include<bits/stdc++.h>
#include <stdint.h>

using namespace std;

#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define inf 1e18
#define PI 3.14159265
#define mset(A,num,n); memset(A,num,n);
#define all(a) a.begin(),a.end()
#define allr(a) a.rbegin(),a.rend()
#define nl cout << endl
#define vi vector<int>
#define vll vector<ll>
#define pi pair<int,int>
//
#define trace1(x) cout <<#x<<": "<<x<< endl;
#define trace2(x, y) cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<< endl;
#define trace3(x, y, z) cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl;
#define trace4(a, b, c, d) cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl;
#define trace5(a, b, c, d, e) cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<<": "<<e<<endl;

const int MOD = 1e9+7;
const int N = 1e6+5;

typedef long long int ll;
typedef int64_t lln;

ll POWER[65];
ll aonb(ll a, ll b) {ll ret=1;while(b) {if(b&1) ret*=a;a*=a;if(ret>=MOD) ret%=MOD;if(a>=MOD) a%=MOD;b>>=1;}return ret;}

void precompute() {
POWER[0]=1;
for(int i=1;i<63;i++) POWER[i]=POWER[i-1]<<1LL;
}

int main()
{
std::ios_base::sync_with_stdio(false);
int start_s=clock();
int t;
cin >> t;
while(t--){
int n;
cin >> n;
vector<int> A(n);
for (int i=0;i<n;i++)
cin >> A[i];

sort(A.begin(),A.end());
int current=2;
for (int i=0;i<n;i++)
{
if (A[i]>=current)
{
current+=2;
}
}
cout << current << endl;
}

int stop_s=clock();
// cout << "time: " << (stop_s-start_s)/double(CLOCKS_PER_SEC)*1000 << endl;

return 0;
}

Second solution

#include <bits/stdc++.h>

#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define be begin()
#define en end()
#define all(x) (x).begin(),(x).end()
#define alli(a, n, k) (a+k),(a+n+k)
#define REP(i, a, b, k) for(__typeof(a) i = a;i < b;i += k)
#define REPI(i, a, b, k) for(__typeof(a) i = a;i > b;i -= k)
#define REPITER(it, a) for(__typeof(a.begin()) it = a.begin();it != a.end(); ++it)

#define y0 sdkfaslhagaklsldk
#define y1 aasdfasdfasdf
#define yn askfhwqriuperikldjk
#define j1 assdgsdgasghsf
#define tm sdfjahlfasfh
#define lr asgasgash
#define norm asdfasdgasdgsd
#define have adsgagshdshfhds

#define eps 1e-6
#define pi 3.141592653589793

using namespace std;

template<class T> inline T gcd(T a, T b) { while(b) b ^= a ^= b ^= a %= b; return a; }
template<class T> inline T mod(T x) { if(x < 0) return -x; else return x; }

typedef vector<int> VII;
typedef vector<ll> VLL;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef pair<int, PII > PPII;
typedef vector< PII > VPII;
typedef vector< PPII > VPPI;

const int MOD = 1e9 + 7;
const int INF = 1e9;
const int MAX = 1e5 + 5;
int a[MAX];

int main(int argc, char* argv[])
{
if(argc == 2 or argc == 3) freopen(argv[1], "r", stdin);
if(argc == 3) freopen(argv[2], "w", stdout);
ios::sync_with_stdio(false);
int t, n, x;
cin >> t;
assert(1 <= t and t <= 100);
while (t--) {
cin >> n;
assert(1 <= n and n <= 100000);
REP(i, 0, n, 1) {
cin >> a[i];
assert(0 <= a[i] and a[i] <= INF);
}
sort(alli(a, n, 0));
x = 2;
REP(i, 0, n, 1) {
if (a[i] >= x) {
x += 2;
}
}
cout << x << endl;
}
return 0;
}


Post a Comment

0 Comments