Find Largest Number in C++ (Time Complexity: O(n))


Given a positive number S, find the largest number n such that the sum
12 + 22 + 32 + ... + n2 is less than or equal to S.

For your information, n2 = n * n

Example:

Input: S = 35

Output: 4

Explanation:
12 + 22 + 32 + 42 = 1 + 4 + 9 + 16 = 30
and 30 <= 35
if we add 52 we end up with 55, which exceeds 35
therefore 4 is the answer

Understanding the Problem

The core challenge of this problem is to find the largest integer n such that the sum of squares from 1 to n does not exceed a given number S. This problem is significant in various mathematical and computational applications, such as optimization problems and resource allocation.

Potential pitfalls include misunderstanding the sum of squares and not correctly iterating through the numbers to find the largest n.

Approach

To solve this problem, we can start with a simple iterative approach:

  1. Initialize a variable to keep track of the total sum of squares.
  2. Iterate through numbers starting from 1, adding the square of each number to the total sum.
  3. Stop when adding the next square would exceed S.

This naive approach is straightforward but can be optimized further. However, for this problem, the naive approach is efficient enough given the constraints.

Algorithm

Here is a step-by-step breakdown of the algorithm:

  1. Initialize totalSum to 0 and n to 0.
  2. Iterate while totalSum + (n+1)*(n+1) <= S:
    • Increment n.
    • Add (n*n) to totalSum.
  3. Return n as the result.

Code Implementation

#include <iostream>
using namespace std;

int findLargestN(int S) {
    int totalSum = 0;
    int n = 0;
    
    // Iterate to find the largest n
    while (totalSum + (n + 1) * (n + 1) <= S) {
        n++;
        totalSum += n * n;
    }
    
    return n;
}

int main() {
    int S = 35;
    cout << "The largest n for S = " << S << " is " << findLargestN(S) << endl;
    return 0;
}

Complexity Analysis

The time complexity of this approach is O(n), where n is the largest number such that the sum of squares is less than or equal to S. The space complexity is O(1) as we are using a constant amount of extra space.

Edge Cases

Potential edge cases include:

For example, if S = 1, the output should be 1 because 12 = 1. If S = 2, the output should still be 1 because 12 = 1 and 22 = 4 exceeds 2.

Testing

To test the solution comprehensively, consider the following test cases:

Using a testing framework like Google Test can help automate and manage these test cases effectively.

Thinking and Problem-Solving Tips

When approaching such problems, consider the following tips:

Conclusion

In this blog post, we discussed how to find the largest number n such that the sum of squares from 1 to n is less than or equal to a given number S. We explored a simple iterative approach, provided a detailed algorithm, and analyzed its complexity. Understanding and solving such problems is crucial for developing strong problem-solving skills in programming.

Additional Resources

For further reading and practice, consider the following resources: