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 Kitty and Katty problem solution

YASH PAL, 31 July 202426 January 2026

In this HackerRank Kitty and Katty problem solution, Kitty and Katty have N plastic blocks. They label the blocks with sequential numbers from 1 to N and begin playing a game in turns, with Kitty always taking the first turn. The game’s rules are as follows:

For each turn, the player removes 2 blocks, A and B, from the set. They calculate A – B, write the result on a new block, and insert the new block into the set.
The game ends when only 1 block is left. The winner is determined by the value written on the final block, X:

  • If X%3=1, then Kitty wins.
  • If X%3=2, then Katty wins.
  • If X%3=0, then the player who moved last wins.

we have given the value of N plastic blocks. Do we need to find and print the name of the winner? Assume both plays optimally.

HackerRank Kitty and Katty problem solution

HackerRank Kitty and Katty problem solution in Python.

#!/bin/python3

import math
import os
import random
import re
import sys



if __name__ == '__main__':
    T = int(input())

    for T_itr in range(T):
        n = int(input())
        if(n==1):
            print("Kitty")
        elif(n%2==0):
            print("Kitty")
        else:
            print("Katty")

Kitty and Katty problem solution in Java.

import java.io.*;
import java.util.*;

public class KittyAndKatty {
	BufferedReader br;
	PrintWriter out;
	StringTokenizer st;
	boolean eof;

	void solve() throws IOException {
		int t = nextInt();
		while (t-- > 0) {
			int n = nextInt();
			out.println((n > 1 && n % 2 == 1) ? "Katty" : "Kitty");
		}
	}

	KittyAndKatty() throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		out = new PrintWriter(System.out);
		solve();
		out.close();
	}

	public static void main(String[] args) throws IOException {
		new KittyAndKatty();
	}

	String nextToken() {
		while (st == null || !st.hasMoreTokens()) {
			try {
				st = new StringTokenizer(br.readLine());
			} catch (Exception e) {
				eof = true;
				return null;
			}
		}
		return st.nextToken();
	}

	String nextString() {
		try {
			return br.readLine();
		} catch (IOException e) {
			eof = true;
			return null;
		}
	}

	int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}
}

Problem solution in C++.

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>

using namespace std;


int main(){
    int T;
    cin >> T;
    for(int a0 = 0; a0 < T; a0++){
        int n;
        cin >> n;
        if(n==1)
            cout<<"Kitty"<<endl;
        else if(n%2==0)
            cout<<"Kitty"<<endl;
        else
            cout<<"Katty"<<endl;
    }
    return 0;
}

Problem solution in C.

#include<stdio.h>
#include<string.h>
main()
{
    int u;
    scanf("%d",&u);
    while(u!=0)
    {
    int n;
    scanf("%d",&n);
     
        if(n==1){
          printf("Kittyn");  
        }
        else if(n%2==0){
          printf("Kittyn");   
        }
        else{
            printf("Kattyn");  
         
        }

    u--;
    }

}

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