Skip to content
Programmingoneonone
Programmingoneonone
  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • 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

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

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes