Find Largest Number in Java (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 iterative 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. While the total sum plus the square of the next number is less than or equal to S, increment n and add the square of n to totalSum.
  3. Return n as the result.

Code Implementation

public class LargestNumber {
    public static int findLargestNumber(int S) {
        int totalSum = 0;
        int n = 0;
        
        // Iterate while the sum of squares is less than or equal to S
        while (totalSum + (n + 1) * (n + 1) <= S) {
            n++;
            totalSum += n * n;
        }
        
        return n;
    }

    public static void main(String[] args) {
        int S = 35;
        System.out.println("The largest number n such that the sum of squares is <= " + S + " is: " + findLargestNumber(S));
    }
}

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:

These cases are handled by the algorithm as it iterates and checks the sum condition.

Testing

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

Input: S = 1
Output: 1

Input: S = 14
Output: 3

Input: S = 50
Output: 5

Use a testing framework like JUnit to automate and validate these test cases.

Thinking and Problem-Solving Tips

When approaching such problems:

Conclusion

Understanding and solving this problem helps in developing a strong foundation in iterative algorithms and optimization techniques. Practice and exploration of similar problems can further enhance problem-solving skills.

Additional Resources

For further reading and practice: