Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

HackerEarth Guess the permutation problem solution

YASH PAL, 31 July 2024
In this HackerEarth Guess the permutation problem solution In this problem your goal is to guess some secret permutation A of integers from 1 to 16.
There are 17 tests in this problem. Test number i for 1 <= i <= 16 will have the following form:
the first line of the input contains string “ELEMENT” (without quotes)
the second line of the input of the test i contains two integers from 1 to 16 each – i and element A[i] of the secret permutation
the only line of the output file contains one integer – A[i] too.
Input of the last (the 17-th test) will contain the only string “PERMUTATION” (without quotes). For this test your program should output some permutation of integers from 1 to 16 – your variant of the secret permutation.
If this permutation equals to the secret permuation A and passes all the 17 tests with your solution , you will receive 100 points. Otherwise you will receive 0 points. Note that for each wrong try you will receive 20 minutes penalty time after you solved this problem (as in ACM ICPC format)
Let us remind you that after you submit solution you can see a detailed feedback for each test separately.
HackerEarth Guess the permutation problem solution

HackerEarth Guess the permutation problem solution.

#include <cstdio>
#include <algorithm>
#include <vector>
#include <iostream>
#include <string>
#include <string.h>
#include <cmath>
#include <set>
#include <map>
#include <bitset>
#include <iomanip>

#define X first
#define Y second
#define mp make_pair
#define pb push_back

typedef long long ll;

using namespace std;


int perm[] = {
13,
15,
1,
6,
14,
4,
5,
7,
2,
16,
9,
11,
10,
12,
8,
3,};

int main() {
string s;
cin>>s;
if (s == "ELEMENT") {
int a, b;
cin>>a>>b;
cout<<b<<endl;
if (perm[a - 1] != b) {
return 11;
}
}
else {
for (int i = 0; i < 16; i++) {
cout<<perm[i]<<" ";
}
}
return 0;
}

Second solution

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cctype>
#include<cstdlib>
#include<algorithm>
#include<bitset>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<sstream>
#include<fstream>
#include<iomanip>
#include<ctime>
#include<complex>
#include<functional>
#include<climits>
#include<cassert>
#include<iterator>
#include<unordered_set>
#include<unordered_map>
using namespace std;

namespace test {
void end_test() {
int val;
if (cin >> val) {
exit(1);
}
}
void range_test(int t, int l, int r) {
if (t < l || r < t) {
exit(1);
}
}
}

int main()
{
int a[]={13,15,1,6,14,4,5,7,2,16,9,11,10,12,8,3,2
};
string s;
cin>>s;
if(s=="ELEMENT"){
int aa;
scanf("%d",&aa);
aa--;
test::range_test(aa,0,15);
cout<<a[aa]<<endl;
int b;
scanf("%d",&b);
test::range_test(b,1,16);
}
else{
if(s=="PERMUTATION"){
for(int i=0;i<16;i++){
if(i){
printf(" ");
}
printf("%d",a[i]);
}
puts("");
}
else{
exit(1);
}
}
test::end_test();
return 0;
}
coding problems

Post navigation

Previous post
Next post
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
  • Hackerrank Day 14 scope 30 days of code solution
©2025 Programming101 | WordPress Theme by SuperbThemes