how to swap and please refer some method
Need help to complete
@deepgarg46 below explaination will help you :
The following problem can be easily solved by using hashmap or unordered map. The hint to solve the problem is that the given array will always contain permutation of the first N natural numbers. So we will prepare a map which will contain element as the key and the index at which it is present as the value.
Algorithm
- Create the map by filling the number as the key and the index at which it is present as the value
- Iterate over the whole array.
- For element at a particular index m we will check if it is present at its best spot or not.
- The best spot of any number num is the N-num index of the array.
- If the number is not present at its best spot then we will swap the number with element present at that spot.
- As we have stored the index of every element in the map we can easily retrieve that from the map to get the swap done.
Java Code
import java.util.HashMap;
import java.util.Scanner;
public class unlock {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int k=scn.nextInt();
int[] arr = new int[n];
HashMap<Integer, Integer> map = new HashMap();
for (int i = 0; i < n; i++) {
arr[i] = scn.nextInt();
map.put(arr[i], i);
}
int num=n;
int m=0;
while(m<n && k>0) {
int hold = map.get(num-m);
if(hold==n-num+m) { // Check if the number is present at the best spot
m++;
continue;
} else { // If the number is not present at the best spot then swap the number with the element at that spot
int temp=arr[m];
arr[m]=arr[hold];
arr[hold]=temp;
map.put(arr[hold], hold);
map.put(arr[m],m);
m++;
k--;
}
}
for(int i=0;i<arr.length;i++) {
System.out.print(arr[i]+" ");
}
}
}
Note: Main.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details.
@deepgarg46 you must have copy pasted the code and while doing so some things like " would have become any thing else which is giving you error so look for these.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.