Header Ad

HackerEarth Sam Height HSBC problem solution

In this HackerEarth Sam Height <HSBC> problem solution, Sam among his friends wants to go to watch a movie in Armstord Cinema.
There is something special about Armstord cinema whenever people come in the group here. They will get seats accordingly their heights. Sam as a curious guy always wants to sit in the middle as cinema has the best view from the middle.
Now, Sam as the leader of his group decides who will join him for the movie.
Initially, he has N - 1 friends with him (N including him).
You are given N - 1 numbers that represent the heights of Sam's friends.
You are given the height of Sam as well.

Now, Sam can do two operations:
1. He can call a new friend of height H.
2. He can cancel any of his friend invitations.

Each operation will cost him a unit time.
He wants to do this as soon as possible.



HackerEarth Sam Height <HSBC> problem solution


HackerEarth Sam Height HSBC problem solution.

#include <bits/stdc++.h> 
#define mod 1000000007
#define INF INT_MAX
#define max_range 1<<20
#define ll long long
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
using namespace std;
ll power(ll x,ll y) {ll res=1; x =x%mod; while (y > 0) {if(y&1)res=(res*x)%mod; y=y>>1; x=(x*x)%mod;} return res;}
vector<vector<ll> > newdp(ll A, ll B){ vector<vector<ll> > dp; dp.resize(A+1);for(int i = 0; i < A+1; i++){dp[i].resize(B+1);for(int j = 0; j < B+1; j++)dp[i][j] = -1;} return dp;}
vector<ll> all_prime;
void prime(){ bool hash[1000000];memset(hash,false,sizeof hash); for (int p = 2; p * p < 1000000; p++)if (hash[p] == true)for (int i = p * 2; i < 1000000; i += p)hash[i] = false;
for(int i = 2;i < 1000000; i++) if(hash[i]) all_prime.push_back(i);}

int main(int argc, char const *argv[])
{

ll t; cin>>t;
while(t){

ll n,k; cin>>n>>k;
ll ar[n+1];

for(int i=0;i<n;i++) cin>>ar[i];
ar[n]=k;
sort(ar,ar+n+1);

ll prev=0, next=0;
for(int i=0;i<n+1;i++)
if(ar[i]==k) prev=i,next=(n+1)-(i+1);

cout<<abs(prev-next)<<endl; t--;
}

return 0;
}

Second solution

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 10000007ll
#define vll vector<ll>
#define pll pair<ll,ll>
#define vpll vector<pll>
#define pb push_back
#define mp make_pair
#define x first
#define y second
ll n, s;
int main()
{
ll t, i;
cin >> t;
assert(t >= 1 && t <= 100);
while(t --)
{
ll small = 0, ans = 0, eq = 0;
cin >> n >> s;
assert(n >= 1 && n <= 100000);
for(i = 0; i < n; i ++)
{
ll x;
cin >> x;
assert(x >= 1 && x <= 1000000000);
if(x < s)
small ++;
if(x == s)
eq ++;
}
if(((n + 1) % 2) == 0)
{
if(small < n - small)
{
small ++;
n ++;
}
else
n --;
ans ++;
}
assert(eq == 0);
cout << ans + abs(small - (n - small)) << endl;
}
return 0;
}


Post a Comment

0 Comments