There are many ways to order a list of integers from 1 to n. For example, if n = 3, the list could be :
[3 1 2].
But there is a special way to create another list from the given list of integers. In this list, position of integer i is the i-th number in the given list. So following this rule, the given list will be written as:
[2 3 1].
This list is called inverse list. Now there exists some list whose inverse list is identical. For example, inverse list of [1 2 3] is same as given list. Given a list of integers you have to determine whether the list is inverse or not.
The input contains several test cases. The first line is the number of test cases t (1 <= t <= 100) . The first line of each test case contains an integer n (1 <= n <= 100000). Then a list of the integers 1 to n follows in the next line.
/*
* uncomment this if you want to read input.
import java.io.BufferedReader;
import java.io.InputStreamReader;
*/
import java.util.Arrays;
import java.util.Scanner;
public class TestClass {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
in.nextLine();
StringBuilder sb = new StringBuilder();
while (N > 0) {
N--;
int T = in.nextInt();
int a[] = new int[T+1];
int b[] = new int[T+1];
for (int i = 1; i <= T; i++) {
a[i] = in.nextInt();
b[a[i]] = i;
}
if (Arrays.equals(a, b))
sb.append("inverse\n");
else
sb.append("not inverse\n");
}
System.out.println(sb);
}
}
[3 1 2].
But there is a special way to create another list from the given list of integers. In this list, position of integer i is the i-th number in the given list. So following this rule, the given list will be written as:
[2 3 1].
This list is called inverse list. Now there exists some list whose inverse list is identical. For example, inverse list of [1 2 3] is same as given list. Given a list of integers you have to determine whether the list is inverse or not.
The input contains several test cases. The first line is the number of test cases t (1 <= t <= 100) . The first line of each test case contains an integer n (1 <= n <= 100000). Then a list of the integers 1 to n follows in the next line.
Sample Input
2 3 3 1 2 3 1 2 3
Sample Output
not inverse inverse
Time Limit:
2 sec(s)
for all input files combined.
Memory Limit:
256 MB
Source Limit:
1024 KB
Code:
/*
* uncomment this if you want to read input.
import java.io.BufferedReader;
import java.io.InputStreamReader;
*/
import java.util.Arrays;
import java.util.Scanner;
public class TestClass {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
in.nextLine();
StringBuilder sb = new StringBuilder();
while (N > 0) {
N--;
int T = in.nextInt();
int a[] = new int[T+1];
int b[] = new int[T+1];
for (int i = 1; i <= T; i++) {
a[i] = in.nextInt();
b[a[i]] = i;
}
if (Arrays.equals(a, b))
sb.append("inverse\n");
else
sb.append("not inverse\n");
}
System.out.println(sb);
}
}
0 comments:
Post a Comment