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.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Constructive Algorithms
  4. Gaming Array
  5. Discussions

Gaming Array

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 108 Discussions, By:

votes

Please Login in order to post a comment

  • anatul
    5 years ago+ 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");
            }
        }
    }
    
    25|
    Permalink
    View more Comments..
  • mohdt786
    2 years ago+ 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'
    
    12|
    Permalink
  • Chatorikar
    2 years ago+ 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";
    
    7|
    Permalink
  • kartikey21
    5 years ago+ 0 comments

    This question had a trick right up its sleeve :)

    4|
    Permalink
  • zkobaladze002
    2 years ago+ 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"
        }
    }
    
    2|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature