{"size": 1551, "ext": "js", "max_stars_repo_path": "Algorithms/Greedy/Largest Permutation/Solution.js", "max_stars_repo_name": "4ngelica/HackerRank", "max_stars_repo_stars_event_min_datetime": "2017-03-22T03:52:28.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-31T06:01:39.000Z", "max_issues_repo_path": "Algorithms/Greedy/Largest Permutation/Solution.js", "max_issues_repo_name": "4ngelica/HackerRank", "max_issues_repo_issues_event_min_datetime": "2017-03-15T20:01:28.000Z", "max_issues_repo_issues_event_max_datetime": "2021-07-12T14:42:21.000Z", "max_forks_repo_path": "Algorithms/Greedy/Largest Permutation/Solution.js", "max_forks_repo_name": "4ngelica/HackerRank", "max_forks_repo_forks_event_min_datetime": "2017-03-19T21:28:30.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-26T16:58:54.000Z"}
{"max_stars_count": 1122, "max_issues_count": 100, "max_forks_count": 799, "avg_line_length": 29.2641509434, "max_line_length": 85, "alphanum_fraction": 0.5886524823}
//Problem: https://www.hackerrank.com/challenges/largest-permutation
//JavaScript
/*
Initial Thoughts: 
We traverse the array making sure each index (i) is equal to N - i.  
When we find an index that does not equal N - i we swap it with the 
index that has the value N - i. We do this K number of times.

Optimization:
Traverse the array storing the index of each value in an auxiliary 
index array. This will allow us to know the index of N - i right 
away instead of having to look for it each time array[i] != N - i

Time Complexity: O(n) //we have to traverse the entire array to get each values index
Space Complexity: O(n) //We allocate two array to store the input and indexes
*/
function processData(input) {
    let setup = input.split("\n"),
        params = setup[0].split(' ').map(Number),
        n = params[0],
        sweeps = params[1],
        index = [],
        arr = setup[1].split(' ').map(Number);
    for (let int in arr) {
        index[arr[int]] = int;
    }
    for (let i = 0; i < n && sweeps > 0; i++) {
        if (arr[i] === n - i) {
            continue;
        }
        arr[index[n - i]] = arr[i];
        index[arr[i]] = index[n - i];
        arr[i] = n - i;
        index[n - i] = i;
        sweeps--; 
    }
    console.log(arr.join(" ").trim());
} 

/////////////// ignore below code \\\\\\\\\\\\\\\

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
    processData(_input);
});

