Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerRank New Year Game problem solution

YASH PAL, 31 July 202426 January 2026

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

HackerRank New Year Game 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()

New Year Game 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 AlgorithmsHackerRank

Post navigation

Previous post
Next post

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes