Solution 4 for Scaler Topics Fortnightly Contest - 19

Learn via video course
FREE
View all courses
DSA Problem Solving for Interviews using Java
DSA Problem Solving for Interviews using Java
by Jitender Punia
1000
4.9
Start Learning
DSA Problem Solving for Interviews using Java
DSA Problem Solving for Interviews using Java
by Jitender Punia
1000
4.9
Start Learning
Topics Covered

Play with friend Complete Solution

This article is part of the Scaler Topics Fortnightly Contest - 19

Build an AI-First Career, Master the Complete Skillset

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program
NSDC Certified

AI Forward Deployed Engineer Program

Full-stack engineering, production AI and client-facing consulting

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program

Solution Approach

  • We'll use the concepts of Grundy numbers and Sprague-Grundy's Theorem in this solution.
  • The idea is that every game state can be assigned an integer number, and if there are many elements of a game, then the value assigned to that total game state is the xor of the values of each element individually.
  • The Grundy number of a state is the minimum number that is not achieved among any state that the state can move to.
  • we have to separate the problem into 2 cases, B even and odd.
  • Let f(n) denote the Grundy number of a element of value n. By definition f(0)=0.
  • If B is even, then when you split the element of value 2n into B elements of value n, the resulting Grundy number of that state is (f(n) xor'ed n times = 0).
  • as B is even. Given this, it is easy to compute that f(0)=0,f(1)=1,f(2)=2,f(3)=0, f(4)=1. Now I will show by induction that for n>=2,f(2n-1)=0,f(2n)=1.
  • The case where B is odd is similar but requires more work. Let's look at the splitting operation first. This time, from a element of value 2n we can move to B element of value n, with Grundy number (f(n) xor'ed n times = f(n)) as B is odd.
  • So from 2n we can achieve the Grundy numbers f(2n-1) and f(n).

Time Complexity - O(|A|*log(max(A[i])) Space Complexity - O(1)

C++ Implementation

Sharpen Your Fundamentals with Free Learning

Java Implementation

How Scaler Transformed Careers in Different Fields

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Python Implementation