logoNEURAL COOK
SearchAll PostsTagsNotesAuthor
logoNEURAL COOK

A posts created with Next.js and Tailwind.css

Explore

  • All posts
  • Notes
  • Snippets
  • Tags
  • RSS feed

Connect

instagramtwitterfacebookyoutubelinkedinmailgithub

© 2026 VIKI. All rights reserved.

Built with
Contents
Table Of Contents
Capacity To Ship Packages Within D DaysFind Nth Root of MFind Square Root of a NumberSmallest Divisor Given a ThresholdKoko Eating Bananas (Minimum Rate)Minimum Days to Make M BouquetsPainter's Partition ProblemSplit Array Largest Sum
Code Scratch/take u forward/binary search/on answers

Find Nth Root of M

6 min read
On this pageQuestion
QuestionSolutionPatternHow to IdentifyDescriptionThe IntuitionComplexityCodeCaveatsConcepts to Think AboutLogical Follow-up

# Question

Given two integers NNN and MMM, find the NNNth root of MMM. The NNNth root of MMM is defined as an integer XXX such that XN=MX^N = MXN=M. If the NNNth root is not an integer, return -1.

# Solution

# Pattern

Binary Search on Answer Space with Safe Evaluation We search the numerical range [1,M][1, M][1,M] for the correct root. Because raising a number to the power of NNN can easily overflow standard data types, we must implement a custom evaluation function that halts multiplication immediately if the running product exceeds MMM.

# How to Identify

  • The problem asks for an exact numerical value satisfying a monotonic mathematical condition (XN=MX^N = MXN=M).
  • A linear scan from 1 to MMM is too slow, demanding a logarithmic approach.
  • The combination of exponents and large bounds strongly signals that preventing integer overflow is the primary hidden challenge.

# Description

Step-by-step explanation:

  1. Handle base cases: If M=1M=1M=1 or N=1N=1N=1, return MMM.
  2. Initialize search boundaries: left = 1 and right = M.
  3. Loop while left <= right. Calculate mid safely.
  4. Call a helper function evaluatePower(mid, N, M) to safely calculate midNmid^NmidN.
    • Inside the helper: Loop NNN times, multiplying ans * mid.
    • The Safety Valve: After every multiplication, check if ans > M. If it is, return a flag indicating "Too Large" immediately. This prevents the number from growing out of bounds and crashing the program.
  5. Based on the helper's return value:
    • If exact match: Return mid.
    • If too small: Search higher (left = mid + 1).
    • If too large: Search lower (right = mid - 1).
  6. If the loop breaks without finding an exact match, return -1.

# The Intuition

Finding a root is a guessing game. If you want the 4th root of 10000, you know it's between 1 and 10000. If you guess 50, you need to calculate 50450^4504. But 50×50×50×50=6,250,00050 \times 50 \times 50 \times 50 = 6,250,00050×50×50×50=6,250,000. The trick is, you don't need to calculate the exact massive number. After just the second multiplication (50×50=250050 \times 50 = 250050×50=2500), you already know 2500 < 10000, but on the third multiplication (2500×50=125,0002500 \times 50 = 125,0002500×50=125,000), it dramatically exceeds 10,000. You stop right there. You don't need the fourth multiplication to know that 50 is too big. This early stopping is what prevents systems from crashing due to integer overflow during mathematical binary searches.

# Complexity

LabelWorstAverage
Time ComplexityO(Nlog⁡M)O(N \log M)O(NlogM)O(Nlog⁡M)O(N \log M)O(NlogM)
Space ComplexityO(1)O(1)O(1)O(1)O(1)O(1)

# Time Complexity

The binary search takes O(log⁡M)O(\log M)O(logM) iterations. In each iteration, we multiply mid by itself up to NNN times. The total time is O(Nlog⁡M)O(N \log M)O(NlogM). (Note: NNN is usually small in these constraints. If NNN were massive, you would use Binary Exponentiation to reduce this to O(log⁡Nlog⁡M)O(\log N \log M)O(logNlogM)).

# Space Complexity

We only allocate primitive scalar variables. Auxiliary space is strictly O(1)O(1)O(1).

# Code

class Solution {
    public int NthRoot(int N, int M) {
        if (M == 1 || N == 1) return M;
 
        int left = 1;
        int right = M;
 
        while (left <= right) {
            int mid = left + (right - left) / 2;
            int state = evaluatePower(mid, N, M);
 
            if (state == 1) {
                return mid; // Exact match found
            } else if (state == 0) {
                left = mid + 1; // Mid is too small
            } else {
                right = mid - 1; // Mid is too large
            }
        }
 
        return -1; 
    }
 
    // Helper to safely evaluate mid^n relative to m
    // Returns: 0 if < m, 1 if == m, 2 if &gt; m
    private int evaluatePower(int mid, int n, int m) {
        long ans = 1;
        for (int i = 1; i &lt;= n; i++) {
            ans *= mid;
            
            // HALT EARLY: Prevents overflow and wasted cycles
            if (ans &gt; m) return 2; 
        }
        
        if (ans == m) return 1;
        return 0; // ans &lt; m
    }
}

# Caveats

  • The Math.pow() Trap: Do not use Math.pow(mid, N). Math.pow uses floating-point arithmetic (double), which loses precision on large integers, causing exact equality checks to fail. Furthermore, it does not halt early, meaning it will calculate massive numbers, eventually hitting Infinity and breaking comparison logic entirely.
  • Binary Exponentiation: While the linear multiplication loop is sufficient if N≤30N \le 30N≤30, if the constraints allowed N≥105N \ge 10^5N≥105, you would be forced to use a modified Binary Exponentiation (Fast Power) algorithm to evaluate the threshold in O(log⁡N)O(\log N)O(logN) time instead of O(N)O(N)O(N).

# Concepts to Think About

  • Safe Mathematics: In software engineering, preventing overflow is often more critical than theoretical execution speed. Early halting is a standardized pattern when validating upper limits.
  • Search Space Reduction: You can mathematically optimize the initial right bound. The NNNth root of MMM is bounded by M/NM / NM/N or min⁡(M,reasonable maximum)\min(M, \text{reasonable maximum})min(M,reasonable maximum). However, initializing right = M is perfectly safe because binary search eliminates bloated spaces logarithmically fast anyway.
  • Integer vs. Double: When the problem asks for an integer root, avoid double to prevent precision errors.
  • Early Exit Pattern: In the check function, we don't need the full value of XNX^NXN if it's already larger than M. This is an L5 optimization.
  • Binary Exponentiation: Could you make the check function O(logN)O(logN)O(logN) instead of O(N)O(N)O(N)?
  • Overflow Limits: What is the maximum value mid can take before midNmid^NmidN overflows a long? (In Java, Long.MAX_VALUE is ≈9×1018≈9×10^18≈9×1018).

# Logical Follow-up

Question: Suppose you needed to return a decimal approximation of the NNNth root accurate to 5 decimal places. How does the algorithm change?

Solution: The binary search bounds become left = 1.0 and right = (double) M. The loop condition changes to a precision threshold: while (right - left &gt; 1e-6). We cannot use early halting for the multiplication since we need exact decimal comparisons to pinpoint the float, so we would use Math.pow or a custom double multiplier. Update bounds via left = mid or right = mid (no +1 or -1). Time complexity depends on the precision required PPP.

Question: "What if the problem asked for the NthN^{th}Nth root of MMM as a decimal with 10−610^{-6}10−6 precision?"

Solution:

  1. Change the search range to double.
  2. The while condition becomes while (high - low &gt; 1e-7).
  3. The result would be low (or high).
  4. Binary search works perfectly for continuous values as long as the function remains monotonic.

Question (Koko Eating Bananas): "Koko loves to eat bananas. There are n piles of bananas, the ithi^{th}ith pile has piles[i] bananas. The guards will be back in h hours. Koko can decide her bananas-per-hour eating speed of k. Each hour, she chooses some pile and eats k bananas from it. If the pile has less than k bananas, she eats them all and does not eat any more bananas during that hour. Return the minimum integer k such that she can eat all the bananas within h hours."

Solution:

  1. Search Range: The minimum speed is 111, the maximum speed is max(piles)max(piles)max(piles).
  2. Monotonicity: If Koko can finish at speed KKK, she can definitely finish at speed K+1K+1K+1. If she cannot finish at speed KKK, she definitely cannot finish at speed K−1K-1K−1.
  3. Check Function: For a given speed mid, calculate the total hours: ∑⌈piles[i]/mid⌉\sum \lceil piles[i] / mid \rceil∑⌈piles[i]/mid⌉.
  4. Optimization: This is the same "Answer Space" pattern. You are searching for the "Lower Bound" of speed KKK such that totalHours <= h.
←PreviousCapacity To Ship Packages Within D DaysNext→Find Square Root of a Number

On this page

QuestionSolutionPatternHow to IdentifyDescriptionThe IntuitionComplexityCodeCaveatsConcepts to Think AboutLogical Follow-up