**Gaming Array**

### Problem Statement :

Andy wants to play a game with his little brother, Bob. The game starts with an array of distinct integers and the rules are as follows: Bob always plays first. In a single move, a player chooses the maximum element in the array. He removes it and all elements to its right. For example, if the starting array arr = [2,3,5,4,1], then it becomes arr' = [2,3] after removing [5,4,1]. The two players alternate turns. The last player who can make a move wins. Andy and Bob play g games. Given the initial array for each game, find and print the name of the winner on a new line. If Andy wins, print ANDY; if Bob wins, print BOB. To continue the example above, in the next move Andy will remove 3. Bob will then remove 2 and win because there are no more integers to remove. Function Description Complete the gamingArray function in the editor below. gamingArray has the following parameter(s): int arr[n]: an array of integers Returns - string: either ANDY or BOB Input Format The first line contains a single integer g, the number of games. Each of the next g pairs of lines is as follows: The first line contains a single integer, n, the number of elements in arr. The second line contains n distinct space-separated integers arr[i] where 0 <= i <= n. Constraints Array arr contains n distinct integers. For 35% of the maximum score: 1 <= g <= 10 1 <= n <= 1000 1<= arr[i] <= 10^5 The sum of n over all games does not exceed 1000. For 100% of the maximum score: 1 <= g <= 100 1 <= n <= 10^5 1 <= ai <= 10^9 The sum of n over all games does not exceed 10^5.

### Solution :

` ````
Solution in C :
In C++ :
#include <bits/stdc++.h>
using namespace std;
int main()
{
int Q;
scanf("%d", &Q);
while(Q--)
{
int N;
scanf("%d", &N);
int last=0, ans=0;
for(int i=0; i<N; i++)
{
int x;
scanf("%d", &x);
if(x>last)
last=x, ans^=1;
}
if(ans==0)
printf("ANDY\n");
else
printf("BOB\n");
}
return 0;
}
In Java :
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int g = sc.nextInt();
for(int a0 = 0; a0 < g; a0++){
int n = sc.nextInt();
int[] a = new int[n];
TreeMap<Integer, Integer> tm = new TreeMap<Integer, Integer>();
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
tm.put(a[i], i);
}
int lastIndex = n;
int ans = 0;
while (!tm.isEmpty()) {
Map.Entry<Integer, Integer> e = tm.pollLastEntry();
if (e.getValue() < lastIndex) {
lastIndex = e.getValue();
ans++;
}
}
System.out.println(ans%2==0?"ANDY":"BOB");
}
}
}
In C :
#include<stdio.h>
typedef unsigned u;
int main()
{
u t,n,p,a,r;
for(scanf("%u",&t);t--;printf(r?"BOB\n":"ANDY\n"))
for(scanf("%u",&n),p=r=0;n--;)
{
scanf("%u",&a);
if(a>p){p=a;r^=1;}
}
return 0;
}
In Python3 :
#!/bin/python3
import sys
g = int(input().strip())
for a0 in range(g):
n = int(input().strip())
a = list(map(int, input().split()))
maxtotheleft = []
curmaxidx = None
for i in range(n):
if curmaxidx == None or a[curmaxidx] < a[i]:
curmaxidx = i
maxtotheleft.append(curmaxidx)
curplayer = True
for i in reversed(range(n)):
if maxtotheleft[i] == i:
curplayer = not curplayer
if curplayer:
print("ANDY")
else:
print("BOB")
```

## View More Similar Problems

## Tree: Postorder Traversal

Complete the postorder function in the editor below. It received 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the postorder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the

View Solution →## Tree: Inorder Traversal

In this challenge, you are required to implement inorder traversal of a tree. Complete the inorder function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's inorder traversal as a single line of space-separated values. Input Format Our hidden tester code passes the root node of a binary tree to your $inOrder* func

View Solution →## Tree: Height of a Binary Tree

The height of a binary tree is the number of edges between the tree's root and its furthest leaf. For example, the following binary tree is of height : image Function Description Complete the getHeight or height function in the editor. It must return the height of a binary tree as an integer. getHeight or height has the following parameter(s): root: a reference to the root of a binary

View Solution →## Tree : Top View

Given a pointer to the root of a binary tree, print the top view of the binary tree. The tree as seen from the top the nodes, is called the top view of the tree. For example : 1 \ 2 \ 5 / \ 3 6 \ 4 Top View : 1 -> 2 -> 5 -> 6 Complete the function topView and print the resulting values on a single line separated by space.

View Solution →## Tree: Level Order Traversal

Given a pointer to the root of a binary tree, you need to print the level order traversal of this tree. In level-order traversal, nodes are visited level by level from left to right. Complete the function levelOrder and print the values in a single line separated by a space. For example: 1 \ 2 \ 5 / \ 3 6 \ 4 F

View Solution →## Binary Search Tree : Insertion

You are given a pointer to the root of a binary search tree and values to be inserted into the tree. Insert the values into their appropriate position in the binary search tree and return the root of the updated binary tree. You just have to complete the function. Input Format You are given a function, Node * insert (Node * root ,int data) { } Constraints No. of nodes in the tree <

View Solution →