Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Check Whether a Number Is a Kaprekar Number

Basic Acceptance 88.00% Points 10.00

You are given a positive integer n. Your task is to determine whether it is a Kaprekar number.

A number is a Kaprekar number if its square can be split into two parts such that the sum of the left and right parts equals the original number. The right part must be non-zero, and the split must preserve the digit positions of the squared number.

Return Yes if n satisfies this property; otherwise, return No.

Examples
Example 1

Input: n = 45
Output: Yes
Explanation: The square of 45 is 2025. Splitting it into 20 and 25 gives 20 + 25 = 45.

Example 2

Input: n = 9
Output: Yes
Explanation: The square of 9 is 81. Splitting it into 8 and 1 gives 8 + 1 = 9.

Example 3

Input: n = 10
Output: No
Explanation: The square of 10 is 100. No valid split produces two parts whose sum equals 10.

Constraints
  • 1 = n = 105*
Companies
Apple Morgan Stanly
Topics
Data Structures Basics
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions