Header Ad

HackerRank Sequence Equation problem solution

In this HackerRank Sequence Equation problem, you have given a sequence of n integers, p(1),p(2),...,p(n) where each element is distinct and satisfies 1<=p(x)<=n. For each x where 1<=x<=n, that is x increments from 1 to n, find any integer y such that p(p(y)) = x and keep a history of the values of y in a return array.

HackerRank Sequence Equation problem solution


Problem solution in Python programming.

# Enter your code here. Read input from STDIN. Print output to STDOUT
n=int(input())
fDict=dict()
fInvDict=dict()

L=input().split()
for i in range(n):
    fDict[i+1] = int(L[i])
    fInvDict[int(L[i])] = i+1
for x in range(1,n+1):
    print(fInvDict[fInvDict[x]])


Problem solution in Java Programming.

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {
    public static void main(String args[] ) throws Exception {
        Scanner in=new Scanner(System.in);
        int n=in.nextInt();
        int[] a=new int[n];
        for(int i=0;i<n;i++)
            a[i]=in.nextInt();
        for(int i=1;i<=n;i++){
            for(int j=0;j<n;j++){
                if(i==a[j]){
                  for(int k=0;k<n;k++)  {
                      if((j+1)==a[k])
                          System.out.println(k+1);
                  }
                }
            }
        }
    }
}


Problem solution in C++ programming.

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
typedef pair<int, int> II;

int main() {
    #ifdef LOCAL
        freopen("Data.inp", "r", stdin);
        freopen("Data.out", "w", stdout);
    #endif

    int n, a[100];
    cin >> n;
    for (int i = 1; i <= n; ++i) cin >> a[i];

    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= n; ++j) if (a[a[j]] == i) {
            cout << j << endl;
            break;
        }
    }

    return 0;
}


Problem solution in C programming.

#include<stdio.h>
#include<stdlib.h>
int main(){
    int n;
    scanf("%d",&n);
    int *a = (int*)malloc(sizeof(int)*n+1);
    for(int i = 1; i < n+1; i++){
        scanf("%d",&a[i]);
    }
    for(int x = 1; x < n+1; x++){
        for(int j = 1; j < n+1; j++){
            if(x == a[j]){
                for(int i = 1; i < n+1; i++){
                    
                    if(j == a[i]){
                        printf("%d\n",i);
                    }
                }
            }
        }
    }
    return 0;
}


Problem solution in JavaScript programming.

process.stdin.resume();
process.stdin.setEncoding("ascii");
var input = "";
process.stdin.on("data", function (chunk) {
    input += chunk;
});
process.stdin.on("end", function () {
    // now we can read/parse input
    main(input);
});
function main(inp) {
    inp=inp.split('\n');
    var n=inp[0];
    var a=inp[1].split(' ');
    for (var i=0; i<n; i++) a[i]=parseInt(a[i]);
    for (var i=1; i<=n; i++) {
        var ind=a.indexOf(i);
        console.log(a.indexOf(ind+1)+1);
    }
    
}


Post a Comment

0 Comments