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 Easy Strong Permutation problem solution

YASH PAL, 31 July 2024
In this HackerEarth Easy Strong, Permutation problem solution Kevin has a sequence of integers a1, a2, …, an. Define the strength of the sequence to be
|a1 – a2| + |a2 – a3| + … + |an-1 – an| + |an – a1|.
Kevin wants to make his sequence stronger, so he reorders his sequence into a new sequence b1, b2, …, bn. He wants this new sequence to be as strong as possible. What is the largest possible strength of the resulting sequence?
HackerEarth Easy Strong Permutation problem solution

HackerEarth Easy Strong Permutation problem solution.

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll p = 1e9+7;
const int MAXN = 1e5+5;

ll nums[MAXN], n, sum;

int main() {
cin >> n; for (int i = 0; i < n; ++i)
cin >> nums[i];

sort(nums, nums+n);
for (int i = 0; i < n/2; ++i)
sum -= 2*nums[i], sum += 2*nums[n-1-i];

cout << sum << "n";
}

Second solution

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define MAXN 100005
int arr [MAXN];
main(){
ios_base::sync_with_stdio(0);
int n; cin >> n; assert(1<=n && n<=1e5); for (int g=1; g<=n; g++) {cin >> arr[g]; assert(0<=abs(arr[g]) && abs(arr[g])<=1e9);}
sort(arr+1, arr+n+1);
vector <int> a, b;
for (int g=1; g<=n/2; g++) a.push_back(arr[g]);
for (int g=n/2+1; g<=n; g++) b.push_back(arr[g]);
int ans = 0;
vector <int> c; //
if (a.size() >= b.size()){
int ans1 = 0;
c.clear();
for (int g=0; g<b.size(); g++) {c.push_back(a[g]); c.push_back(b[g]);}
if (a.size() > b.size()) c.push_back(a.back());
for (int g=0; g<c.size()-1; g++) ans1+=abs(c[g+1] - c[g]);
ans1+=abs(c.back() - c[0]);
ans = max(ans, ans1);
}
else{
int ans2 = 0;
c.clear();
for (int g=0; g<a.size(); g++){
c.push_back(b[g]);
c.push_back(a[g]);
}
if (b.size() > a.size()) c.push_back(b.back());
for (int g=0; g<c.size()-1; g++) ans2+=abs(c[g+1] - c[g]);
ans2+=abs(c.back() - c[0]);
ans = max(ans, ans2);
}//
cout << ans;
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 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
©2026 Programmingoneonone | WordPress Theme by SuperbThemes