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
Cube Summation
You are given a 3-D Matrix in which each block contains 0 initially. The first block is defined by the coordinate (1,1,1) and the last block is defined by the coordinate (N,N,N). There are two types of queries. UPDATE x y z W updates the value of block (x,y,z) to W. QUERY x1 y1 z1 x2 y2 z2 calculates the sum of the value of blocks whose x coordinate is between x1 and x2 (inclusive), y coor
View Solution →Direct Connections
Enter-View ( EV ) is a linear, street-like country. By linear, we mean all the cities of the country are placed on a single straight line - the x -axis. Thus every city's position can be defined by a single coordinate, xi, the distance from the left borderline of the country. You can treat all cities as single points. Unfortunately, the dictator of telecommunication of EV (Mr. S. Treat Jr.) do
View Solution →Subsequence Weighting
A subsequence of a sequence is a sequence which is obtained by deleting zero or more elements from the sequence. You are given a sequence A in which every element is a pair of integers i.e A = [(a1, w1), (a2, w2),..., (aN, wN)]. For a subseqence B = [(b1, v1), (b2, v2), ...., (bM, vM)] of the given sequence : We call it increasing if for every i (1 <= i < M ) , bi < bi+1. Weight(B) =
View Solution →Kindergarten Adventures
Meera teaches a class of n students, and every day in her classroom is an adventure. Today is drawing day! The students are sitting around a round table, and they are numbered from 1 to n in the clockwise direction. This means that the students are numbered 1, 2, 3, . . . , n-1, n, and students 1 and n are sitting next to each other. After letting the students draw for a certain period of ti
View Solution →Mr. X and His Shots
A cricket match is going to be held. The field is represented by a 1D plane. A cricketer, Mr. X has N favorite shots. Each shot has a particular range. The range of the ith shot is from Ai to Bi. That means his favorite shot can be anywhere in this range. Each player on the opposite team can field only in a particular range. Player i can field from Ci to Di. You are given the N favorite shots of M
View Solution →Jim and the Skyscrapers
Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently. Let us describe the problem in one dimensional space
View Solution →