Skip to content
Programmingoneonone
Programmingoneonone

LEARN EVERYTHING ABOUT PROGRAMMING

  • Home
  • CS Subjects
    • IoT ? Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone

LEARN EVERYTHING ABOUT PROGRAMMING

HackerRank New Year Game problem solution

YASH PAL, 31 July 2024

In this HackerRank New Year Game problem solution, It’s New Year’s Day, and Balsa and Koca are stuck inside watching the rain. They decide to invent a game, the rules for which are described below.

Given array A containing N integers, they take turns making a single move. Balsa always moves first, and both players are moving optimally (playing to win and making no mistakes).

During each move, the current player chooses one element from A, adds it to their own score, and deletes the element from A; because the size of A decreases by 1 after each move, A’s size will be 0 after N moves and the game ends (as all elements were deleted from A). We refer to Balsa’s score as Sb and Koca’s score as Sk. Koca wins the game if |Sb-Sk| is divisible by 3; otherwise Balsa wins.

Given A, determine the winner.

HackerRank New Year Game problem solution

Topics we are covering

Toggle
  • Problem solution in Python.
  • Problem solution in Java.
  • Problem solution in C++.
  • Problem solution in C.

Problem solution in Python.

def dis_winner(arr):
    arr = [x % 3 for x in arr]
    arr = filter(lambda k: k != 0, arr)
    freq = {1: 0, 2: 0}
    for i in arr:
        freq[i] += 1
    if freq[1] % 2 == 0 and freq[2] % 2 == 0:
        print('Koca')
    else:
        print('Balsa')


def main():
    num_tests = int(input())
    for test in range(num_tests):
        size = int(input())
        arr = [int(x) for x in input().split(' ')]
        dis_winner(arr)


if __name__ == '__main__':
    main()

Problem solution in Java.

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*; 
import java.util.*;
import java.util.regex.*;

public class NewYearGame {
	private static BufferedReader br;
	private static StringTokenizer st;
	private static PrintWriter pw;

	public static void main(String[] args) throws Exception {
		br = new BufferedReader(new InputStreamReader(System.in));
		pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
		int qq = readInt();
		for(int casenum = 1; casenum <= qq; casenum++)	{
			int n = readInt();
			int o = 0;
			int t = 0;
			while(n-- > 0) {
				int curr = readInt();
				if(curr % 3 == 1) o++;
				if(curr % 3 == 2) t++;
			}
			if(o%2 == 0 && t%2 == 0) pw.println("Koca");
			else pw.println("Balsa");
		}
		exitImmediately();
	}

	private static void exitImmediately() {
		pw.close();
		System.exit(0);
	}

	private static long readLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	private static double readDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}

	private static int readInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	private static String nextLine() throws IOException  {
		if(!br.ready()) {
			exitImmediately();
		}
		st = null;
		return br.readLine();
	}

	private static String nextToken() throws IOException  {
		while(st == null || !st.hasMoreTokens())  {
			if(!br.ready()) {
				exitImmediately();
			}
			st = new StringTokenizer(br.readLine().trim());
		}
		return st.nextToken();
	}
}

Problem solution in C++.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <set>
using namespace std;

typedef long long lolo;

const int MAXN = 1e5 + 100;

int cnt[5];

int main() {
  ios_base::sync_with_stdio(0);
  int t;
  cin >> t;
  while (t--) {
    int n, x;
    cin >> n;
    fill(cnt, cnt + 3, 0);
    for (int i = 1; i <= n; ++i) {
      cin >> x;
      ++cnt[x % 3];
    }
    bool ok = false;
    for (int i = 1; i < 3; ++i) ok |= cnt[i] & 1;
    cout << (ok ? "Balsa" : "Koca") << "n";
  }
  return 0;
}

Problem solution in C.

#include<stdio.h>
int main (){
    int t,k;
    scanf("%d",&t);
    for(k=0;k<t;k++){
        int n;
        scanf("%d",&n);
        int a[n],i;
        for(i=0;i<n;i++)
        scanf("%d",&a[i]);
        int c1=0,c2=0,c3=0;
        for(i=0;i<n;i++){
        if(a[i]%3==0) c1++;
        if(a[i]%3==1) c2++;
        if(a[i]%3 ==2)c3++;
        }
    
    
    if(c2 %2==0 && c3%2==0)
    printf("Kocan");
    else printf("Balsan");

}
}

Algorithms coding problems solutions

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes