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

YASH PAL, 31 July 2024
In this HackerEarth Permutation problem solution A permutation is a list of K numbers, each between 1 and K (both inclusive), that has no duplicate elements.
Permutation X is lexicographically smaller than Permutation Y if for some i <= K:
All of the first i-1 elements of X are equal to first i-1 elements of Y.
ith element of X is smaller than ith element of Y.
You are given a permutation P, you can exchange some of its elements as many times as you want in any order. You have to find the lexicographically smallest Permutation that you can obtain from P.
K is less than 101.
HackerEarth Permutation problem solution

HackerEarth Permutation problem solution.

#include <bits/stdc++.h>

using namespace std;

int arr [100 + 10];
char board [100 + 10][100 + 10];
bool chosen [100 + 10];
bool vis[100 + 10];
vector < int > v[100 + 10];
int pos [100 + 10];

int ans = -1;

void dfs(int index){

vis[ index ] = true;
if(!chosen[index]) ans = min(ans , arr[index]); //check if the number isn't chosen before

for(int i = 0; i < (int) v[index].size(); i++){

if(vis[ v[index][i] ] ) continue; // check if the number was visited before
int e = v[index][i]; // check the available transitions
dfs( e );
}
}

int main()
{

int n;

cin >> n;

for(int i = 0; i < n; i++){

cin >> arr[i];
pos[ arr[i] ] = i; // store the original index of eah number
}

for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){

cin >> board[i][j];
if(board[i][j] == 'Y')
v[i].push_back(j);
}
}

for(int i = 0; i < n; i++){

memset(vis, false, sizeof vis); // set all numbers as unvisited
ans = 1e9;
dfs(i);
chosen[ pos[ans] ] = true; // put this index as already chosen
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
©2025 Programmingoneonone | WordPress Theme by SuperbThemes