Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • 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
Programmingoneonone

Learn everything about programming

Leetcode Nim Game problem solution

YASH PAL, 31 July 202418 January 2026

In this Leetcode Nim Game problem solution You are playing the following Nim Game with your friend:

Initially, there is a heap of stones on the table.

  1. You and your friend will alternate taking turns, and you go first.
  2. On each turn, the person whose turn it is will remove 1 to 3 stones from the heap.
  3. The one who removes the last stone is the winner.

Given n, the number of stones in the heap, return true if you can win the game assuming both you and your friend play optimally, otherwise return false.

Leetcode Nim Game problem solution

Problem solution in Python.

class Solution:
    def canWinNim(self, n: int) -> bool:
        if n%4 == 0:
            return False
        else:
            return True

Problem solution in Java.

public boolean canWinNim(int n) {

    if(n>100000000) return !(n%4==0);
    
    boolean[] mem = new boolean[n+1];
    return helper(n, mem);
}

Problem solution in C++.

boolean helper(int n, boolean[] mem) { 
    if(n<=3) return true;
    if(n==4) return false;
            
    if(mem[n]==true) return mem[n];
     
    mem[n] = !helper(n-1, mem) || !helper(n-2, mem) || !helper(n-3, mem);
    return mem[n];
}

Problem solution in C.

bool canWinNim(int n) {
    if(n%4==0)
        return false;
    return true;
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

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