John and Olivia once start fighting when Olivia sees John talking with another girl. So, you tell them to play the game of love in which you give them an array A of size N. Now, the game will play in turns in which a player must decrease the value at the smallest index (with a non-zero value) in A by x (x > 0). The player who cannot make a move will lose the game.
You being a loyal friend of Olivia, wants her to win. So, tell Olivia whether she should move first or second to win this game.
Assume both players will play optimally at every step of the game.
Input
The first line contains a single integer T (1 ≤ T ≤ 1000) — the number of test cases.
The first line of each test case contains a single integer N (1 ≤ N ≤ 100) — the size of the array A on which the game is to be played.
.
The second line contains n integers A1, A2, …, AN (1 ≤ Ai ≤ 109).
Output
For each test case, print on a new line whether Olivia should move “first” or “second” (without quotes) to win the game.
Example
Sample Input:
2
3
2 1 3
2
1 1
Sample Output:
first
second
Sample Explanation:
For the first test case, Olivia will remove 2 from the 1st index, then John has to remove 1 from the 2nd index, and finally, Olivia will remove 3 from the 3rd index.
For the second test case, John has to remove 1 from the 1st index, then Olivia removes 1 from the 2nd index.
Solution
import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework
// don't change the name of this class
// you can add inner classes if needed
class Main {
public static void main (String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
while (t-- > 0) {
int n = scanner.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = scanner.nextInt();
}
boolean firstWins = false;
int i = 0;
while (i < n && a[i] == 1) {
i++;
}
if (i == n) {
firstWins = (n % 2 == 1);
} else {
firstWins = (i % 2 == 0);
}
System.out.println(firstWins ? "first" : "second");
}
}
}
Happy Learning – If you require any further information, feel free to contact me.