Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • 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

Learn everything about programming

HackerEarth Capitals and cities problem solution

YASH PAL, 31 July 2024
In this HackerEarth Capitals and cities problem solution Suppose we have n cities with integer coordinate on a line. First, we have to select a city to be the capital. Then in each operation, we have to choose a non-capital city and change it’s coordinate by 1 (-1 or +1). We have to pick the capital and do the operations exactly k time so that the sum of distances from cities to the capital is minimized. Print the index of the selected capital and the desired sum after exactly k operations. If there are multiple such choices, output the smallest index of an optimal capital. 
You are required to print the index of the selected capital and required sum after k operations. If there are multiple such choices, print the smallest index of an optimal capital.
HackerEarth Capitals and cities problem solution

HackerEarth Capitals and cities problem solution.

#include<bits/stdc++.h>
using namespace std;
const int N = 300005;
int n, k, id = N, A[N], P[N];
long long tot, Mn = LLONG_MAX;
bool CMP(int i, int j) {return (A[i] < A[j]);}
int main()
{
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++)
scanf("%d", &A[i]), P[i] = i;
sort(P + 1, P + n + 1, CMP);
for (int i = 1; i <= n; i++)
tot += A[P[i]] - A[P[1]];
for (int i = 1; i <= n; i++)
{
if (tot >= k && Mn > tot - k)
Mn = tot - k, id = P[i];
if (tot >= k && Mn >= tot - k)
Mn = tot - k, id = min(id, P[i]);
if (tot < k && Mn > ((k - tot) & 1))
Mn = (k - tot) & 1, id = P[i];
if (tot < k && Mn >= ((k - tot) & 1))
Mn = (k - tot) & 1, id = min(id, P[i]);
tot += (A[P[i + 1]] - A[P[i]]) * 1LL * (i + i - n);
}
return !printf("%d %lldn", id, Mn);
}

second solution

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 3e5 + 14;

int n, k;
pair<int, int> a[maxn];
ll suf[maxn];
int main(){
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> k;
for(int i = 0; i < n; i++)
cin >> a[i].first, a[i].second = i;
sort(a, a + n);
for(int i = n - 1; i >= 0; i--)
suf[i] = suf[i + 1] + a[i].first;
ll pre = 0, ans = 1e18;
int cer;
for(int i = 0; i < n; i++){
ll me = (ll) i * a[i].first - pre + suf[i + 1] - (ll) (n - i - 1) * a[i].first - k;
me = max(0ll, me);
if(((ll) i * a[i].first - pre + suf[i + 1] - (ll) (n - i - 1) * a[i].first - k) % 2)
me = max(me, 1ll);
if(me < ans || ans == me && cer > a[i].second){
ans = me;
cer = a[i].second;
}
pre += a[i].first;
}
cout << cer + 1 << ' ' << ans << 'n';
}
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 300 Rupees. Ask all your doubts and questions related to your career 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
©2025 Programmingoneonone | WordPress Theme by SuperbThemes