Sort by

recency

|

1526 Discussions

|

  • + 0 comments

    java 8

    public static List<Integer> permutationEquation(List<Integer> p) {
        // Write your code here
    
        List<Integer> newList = new ArrayList<>();
        int x = 1;
        while (x <= p.size()) {
            for (int i = 0; i < p.size(); i++) {
                if (p.get(i) == x) {
                    newList.add((p.indexOf(i + 1)) + 1);
                }
            }
            x++;
    
        }
        return newList;
    }
    
  • + 0 comments

    using reduce in JS/Javascript:-

    function permutationEquation(p) {
       return p.reduce((acc, _, index) => {
        acc.push(p.indexOf(p.indexOf(index + 1) + 1) + 1)
        return acc
       }, [])
    }
    
  • + 0 comments

    n=int(input())

    m=list(map(int,input().split())) m.insert(0,0)

    for i in range(1,n+1): a=m[i] b=m.index(a) c=m.index(b) d=m.index(c) print(d)

  • + 0 comments

    TS solution O(n):

    function permutationEquation(p: number[]): number[] {
        // Write your code here
        
        let map: {[key: number]: number} = {}
        
        for(let i = 0; i < p.length; i++) {
            map[p[i]] = i + 1
        }
        
        let result: number[] = []
        
        for(let i = 1; i <= p.length; i++) {
            let pypy: number = map[map[i]]
            result.push(pypy)
        }
        
        return result
    }
    
  • + 0 comments

    here is my c# solution ,

    public static List<int> permutationEquation(List<int> p)
        {
    		// Sort the list and keep track of the original indices
            var sortedListWithIndices = p
        .Select((value, index) => new { Value = value, OriginalIndex = index + 1 })
        .OrderBy(x => x.Value)
        .ToList();
    
    // Create a list to store the transformed indices
    var tempList1 = sortedListWithIndices
        .Select(x => x.OriginalIndex)
        .ToList();
    
    // Create a list to store the final result
    var tempList2 = tempList1
        .Select(index => p.IndexOf(index) + 1)
        .ToList();
    
    return tempList2;
        }