-
Notifications
You must be signed in to change notification settings - Fork 617
/
Copy pathinverseofarray.java
48 lines (40 loc) · 1.15 KB
/
inverseofarray.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
---------------- INVERSE OF AN ARRAY ----------------------
If the array elements are swapped with their corresponding indices,
then the array finally results is called as inverse of an array.
*/
package peakval;
import java.util.Scanner;
public class peakvalueinarray {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter Size of Arraay");
int n = scan.nextInt();
int[] arr = new int[n];
// Taking Array Elements
for (int i = 0; i < n; i++) {
arr[i] = scan.nextInt();
}
inversearrray(arr);
}
public static void inversearrray(int[] arr){
int[] ans = new int[arr.length];
for (int i = 0; i<arr.length; i++){
ans[arr[i]] = i;
}
for (int i = 0; i<ans.length; i++){
System.out.print(ans[i]+" ");
}
}
}
/*
Test Cases:
Input : 5
4 0 2 3 1
Output: 1 4 2 3 0
Input : 3
2 0 1
Output: 1 2 0
Time Complexity: O(n) where n is Length of Array
Space Complexity: O(1)
*/