HackerRank Absolute Permutation problem solution YASH PAL, 31 July 2024 In this HackerRank Absolute Permutation problem, you have Given n and k, print the lexicographically smallest absolute permutation P. If no absolute permutation exists, print -1. Problem solution in Python programming. #include <bits/stdc++.h> using namespace std; #define pb push_back #define mp make_pair #define REP(i, n) for (int i = 0; i < (int)(n); ++i) typedef long long LL; typedef pair<int, int> PII; int tt; int n, k; int a[100000]; int main() { scanf("%d", &tt); REP(test, tt) { scanf("%d%d", &n, &k); if (k == 0) { REP(i, n) printf("%d ", i + 1); printf("n"); continue; } if (n % (2 * k) != 0) { printf("-1n"); continue; } for (int pos = 0; pos < n; pos += 2 * k) { REP(i, k) { a[pos + i] = pos + k + i + 1; a[pos + k + i] = pos + i + 1; } } REP(i, n) printf("%d ", a[i]); printf("n"); } return 0; } Problem solution in Java Programming. import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); for (int tt = 0; tt < t; tt++) { solve(in.nextInt(), in.nextInt()); System.out.println(); } } public static void solve( int n, int k) { if (k == 0) { for (int i = 1; i <= n; i++) { System.out.print(i + " "); } return; } if (n%2 == 1 || k > n/2) { System.out.print("-1"); return; } solveAns(n,k); } static void solveAns(int n, int k) { boolean seen[] = new boolean[n+1]; // 1 based indexing reminder! StringBuilder ans = new StringBuilder(); for (int i = 1; i <= n; i++) { // try subtracting if ((i-k) > 0 && !seen[(i-k)]) { seen[(i-k)] = true; ans.append(i-k); ans.append(' '); } else if ((i+k) <= n && !seen[(i+k)]) { seen[(i+k)] = true; ans.append(i+k); ans.append(' '); } else { // impossible? System.out.print("-1"); return; } } System.out.print(ans.toString()); } } Problem solution in C++ programming. #include <iostream> #include <stdio.h> #include <string> #include <algorithm> #include <stdlib.h> #include <memory.h> using namespace std; const int maxn=1000010; int test,n,k,a[maxn]; bool kt[maxn]; int main() { cin>>test; for (int t=1;t<=test;++t) { memset(kt,false,sizeof(kt)); cin>>n>>k; if (k==0) for (int i=1;i<=n;++i) cout<<i<<" "; else if (n%(2*k)!=0) cout<<-1; else { for (int i=1;i<=n;++i) a[i]=i; for (int i=1;i<=n;++i) if (!kt[i]) { swap(a[i],a[i+k]); kt[i]=true; kt[i+k]=true; } for (int i=1;i<=n;++i) cout<<a[i]<<" "; } cout<<"n"; } return 0; } Problem solution in C programming. #include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ int t; scanf("%d",&t); while(t--) { int n,k; scanf("%d %d",&n,&k); int i,a[n+1],flag=0; if(k==0) { for(i=1;i<=n;i++) printf("%d ",i); printf("n"); } else { for(i=1;i<k+1;i++) { int temp=((n-i)/k)+1; if(temp%2) { flag=1; break; } } if(flag==1) printf("-1n"); else { int j; for(i=1;i<k+1;i++) { for(j=i;j<=n-k;j+=2*k) { a[j]=j+k; a[j+k]=j; } } for(i=1;i<=n;i++) printf("%d ",a[i]); printf("n"); } } } return 0; } Problem solution in JavaScript programming. process.stdin.resume(); process.stdin.setEncoding('ascii'); var input_stdin = ""; var input_stdin_array = ""; var input_currentline = 0; process.stdin.on('data', function (data) { input_stdin += data; }); process.stdin.on('end', function () { input_stdin_array = input_stdin.split("n"); main(); }); function readLine() { return input_stdin_array[input_currentline++]; } /////////////// ignore above this line //////////////////// function main() { var t = parseInt(readLine()); for(var a0 = 0; a0 < t; a0++){ var n_temp = readLine().split(' '); var n = parseInt(n_temp[0]); var k = parseInt(n_temp[1]); if(k==0){ console.log(Array(n).fill(0).map((e,i)=>i+1).toString().replace(/,/g," ")); } else if((n/k)%2==0){ let ar = Array(n).fill(0).map((e,i)=>i+1); for(let i = 0; i < n; i+=2*k){ let j = i + k; let L = ar.slice(i,i+k); let R = ar.slice(j,j+k); ar.splice(i,k,...R); ar.splice(j,k,...L); } console.log(ar.toString().replace(/,/g," ")); } else{ console.log(-1); } } } algorithm coding problems