**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 : 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 →## Tree: Huffman Decoding

Huffman coding assigns variable length codewords to fixed length input characters based on their frequencies. More frequent characters are assigned shorter codewords and less frequent characters are assigned longer codewords. All edges along the path to a character contain a code digit. If they are on the left side of the tree, they will be a 0 (zero). If on the right, they'll be a 1 (one). Only t

View Solution →## Binary Search Tree : Lowest Common Ancestor

You are given pointer to the root of the binary search tree and two values v1 and v2. You need to return the lowest common ancestor (LCA) of v1 and v2 in the binary search tree. In the diagram above, the lowest common ancestor of the nodes 4 and 6 is the node 3. Node 3 is the lowest node which has nodes and as descendants. Function Description Complete the function lca in the editor b

View Solution →## Swap Nodes [Algo]

A binary tree is a tree which is characterized by one of the following properties: It can be empty (null). It contains a root node only. It contains a root node with a left subtree, a right subtree, or both. These subtrees are also binary trees. In-order traversal is performed as Traverse the left subtree. Visit root. Traverse the right subtree. For this in-order traversal, start from

View Solution →