We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Gaming Array
Gaming Array
+ 9 comments 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 in = new Scanner(System.in); int g = in.nextInt(); for(int a0 = 0; a0 < g; a0++){ int n = in.nextInt(); int count = 0; int max = 0; for (int i = 0; i < n; i++) { int number = in.nextInt(); if (max < number) { max = number; count++; } } System.out.println(count % 2 == 0 ? "ANDY" : "BOB"); } } }
+ 3 comments Guys here's my code: Just keep traversing from left to right and keep track of the number larger than your previous maximum.
def gamingArray(arr): count=0 //Keeps track of number of turns played m=0 //Maximum Number for i in arr: if i>m: m=i count+=1 if count%2==0: return 'ANDY' return 'BOB'
+ 2 comments Can be done using Stack
stack<int>s; for(auto x : a){ if(!s.empty() && s.top() > x ) continue; s.push(x); } if(s.size()%2 == 0) return "ANDY"; else return "BOB";
+ 0 comments This question had a trick right up its sleeve :)
+ 0 comments JavaScript solution
function gamingArray(arr) { var maxInt = arr[0]; var counter = 1; if(arr.length === 1){ return "BOB"; } for(let i = 1; i < arr.length; i++){ if(arr[i] > maxInt){ maxInt = arr[i]; counter++ } } if(counter%2 === 0){ return "ANDY"; } else{ return "BOB" } }
Load more conversations
Sort 108 Discussions, By:
Please Login in order to post a comment