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
13. Roman to Integer14. Longest Common Prefix20. Valid Parentheses26. Remove Duplicates from Sorted Array35. Search Insert Position69. Sqrt(x)70. Climbing Stairs88. Merge Sorted Array (In-Place)104. Maximum Depth of Binary Tree118. Pascal's Triangle121. Best Time to Buy and Sell Stock205. Isomorphic Strings242. Valid Anagram283. Move Zeroes345. Reverse Vowels of a String605. Can Place Flowers704. Binary Search796. Rotate String1071. Greatest Common Divisor of Strings1299. Replace Elements with Greatest Element on Right Side1431. Kids With the Greatest Number of Candies1614. Maximum Nesting Depth of the Parentheses1752. Check if Array Is Sorted and Rotated1768. Merge Strings Alternately1903. Largest Odd Number in String2965. Find Missing and Repeated Values
Code Scratch/leetcode/easy

2965. Find Missing and Repeated Values

2 min read
On this pageIntuition
IntuitionComplexityCode

# Intuition

We're given a 2D n x n grid filled with values from 1 to n^2. One number is repeated (A) and one number is missing (B).


To find the missing and repeated numbers, we use mathematical formulas for the sum and sum of squares of the first n^2natural numbers.

the sum of the first n natural numbers:

∑i=1ni=n(n+1)2\sum_{i=1}^{n} i = \frac{n(n+1)}{2}i=1∑n​i=2n(n+1)​

the sum of squares of the first n natural numbers:

∑i=1ni2=n(n+1)(2n+1)6\sum_{i=1}^{n} i^2 = \frac{n(n+1)(2n+1)}{6}i=1∑n​i2=6n(n+1)(2n+1)​

Comparing the actual and expected sums helps us form equations to solve for A and B.

Let:

  • A be the repeating number
  • B be the missing number

We know:

  • currentSum = expectedSum - B + A
  • currentSqSum = expectedSqSum - B² + A²

From which:

  • AMinusB = currentSum - expectedSum = A - B
  • ASquaredMinusBSquared = currentSqSum - expectedSqSum = A² - B² = (A - B)(A + B)

So:

  • APlusB = ASquaredMinusBSquared / AMinusB
  • Then solve:
    A = (APlusB + AMinusB) / 2
    B = APlusB - A

# Complexity

Space ComplexityTime Complexity
O(1)\text{O}(1)O(1)O(n2)\text{O}(n^2)O(n2)

# Code

public int[] findMissingAndRepeatedValues(int[][] grid) {
    long gridSize = grid.length;
    long currentSum = 0;
    long currentSquaredSum = 0;
 
    // Step 1: Compute current sum and current squared sum from the grid
    for (int row = 0; row < gridSize; ++row) {
        for (int col = 0; col < gridSize; ++col) {
            currentSum += grid[row][col];
            currentSquaredSum += (grid[row][col] * grid[row][col]);
        }
    }
 
    // Step 2: Calculate expected sum and expected squared sum for numbers from 1 to n^2
    long totalNumbers = gridSize * gridSize;
    long expectedSum = (totalNumbers * (totalNumbers + 1)) / 2;
    long expectedSquaredSum = (totalNumbers * (totalNumbers + 1) * (2 * totalNumbers + 1)) / 6;
 
    // Step 3: Derive A - B and A + B
    long aMinusB = currentSum - expectedSum;
    long aSquaredMinusBSquared = currentSquaredSum - expectedSquaredSum;
    long aPlusB = aSquaredMinusBSquared / aMinusB;
 
    // Step 4: Solve equations to get A and B
    int repeated = (int) (aMinusB + aPlusB) / 2;
    int missing = (int) (aPlusB - repeated);
 
    // Step 5: Return result
    return new int[]{repeated, missing};
}
←Previous1903. Largest Odd Number in StringNext→25. Reverse Nodes in k-Group

On this page

IntuitionComplexityCode