Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

HackerEarth Sam Height HSBC problem solution

YASH PAL, 31 July 2024
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;
}
coding problems solutions

Post navigation

Previous post
Next post

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for just 7 dollars. Ask all your career-related questions to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes