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
Inserting a Node Into a Sorted Doubly Linked List
Given a reference to the head of a doubly-linked list and an integer ,data , create a new DoublyLinkedListNode object having data value data and insert it at the proper location to maintain the sort. Example head refers to the list 1 <-> 2 <-> 4 - > NULL. data = 3 Return a reference to the new list: 1 <-> 2 <-> 4 - > NULL , Function Description Complete the sortedInsert function
View Solution →Reverse a doubly linked list
This challenge is part of a tutorial track by MyCodeSchool Given the pointer to the head node of a doubly linked list, reverse the order of the nodes in place. That is, change the next and prev pointers of the nodes so that the direction of the list is reversed. Return a reference to the head node of the reversed list. Note: The head node might be NULL to indicate that the list is empty.
View Solution →Tree: Preorder Traversal
Complete the preorder function in the editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's preorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the preOrder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the tree's
View Solution →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 →