Given an integer **X**, the task is to find the maximum value **N** such that the sum of first **N** natural numbers is not more than **X**.

**Examples:**

Input:X = 5

Output:2

2 is the maximum possible value of N because for N = 3, the sum of the series will exceed X

i.e. 1^{2}+ 2^{2}+ 3^{2}= 1 + 4 + 9 = 14

Input:X = 25

Output:3

**Simple Solution**: A simple solution is to run a loop from **1** till the maximum **N** such that **S(N) ≤ X**, where **S(N)** is the sum of square of first **N** natural numbers. Sum of square of first** N** natural numbers is given by the formula **S(N) = N * (N + 1) * (2 * N + 1) / 6**. The time complexity of this approach is **O(N)**.

**Efficient Approach**: An efficient solution is to use Binary Search to find the value of **N**. The time complexity of this approach is **O(log N)**.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `#define ll long long ` ` ` `// Function to return the sum of the squares ` `// of first N natural numbers ` `ll squareSum(ll N) ` `{ ` ` ` `ll sum = (ll)(N * (N + 1) * (2 * N + 1)) / 6; ` ` ` ` ` `return` `sum; ` `} ` ` ` `// Function to return the maximum N such that ` `// the sum of the squares of first N ` `// natural numbers is not more than X ` `ll findMaxN(ll X) ` `{ ` ` ` `ll low = 1, high = 100000; ` ` ` `int` `N = 0; ` ` ` ` ` `while` `(low <= high) { ` ` ` `ll mid = (high + low) / 2; ` ` ` ` ` `if` `(squareSum(mid) <= X) { ` ` ` `N = mid; ` ` ` `low = mid + 1; ` ` ` `} ` ` ` ` ` `else` ` ` `high = mid - 1; ` ` ` `} ` ` ` ` ` `return` `N; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `ll X = 25; ` ` ` `cout << findMaxN(X); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` `// Function to return the sum of the squares ` `// of first N natural numbers ` `static` `long` `squareSum(` `long` `N) ` `{ ` ` ` `long` `sum = (` `long` `)(N * (N + ` `1` `) * (` `2` `* N + ` `1` `)) / ` `6` `; ` ` ` ` ` `return` `sum; ` `} ` ` ` `// Function to return the maximum N such that ` `// the sum of the squares of first N ` `// natural numbers is not more than X ` `static` `long` `findMaxN(` `long` `X) ` `{ ` ` ` `long` `low = ` `1` `, high = ` `100000` `; ` ` ` `int` `N = ` `0` `; ` ` ` ` ` `while` `(low <= high) ` ` ` `{ ` ` ` `long` `mid = (high + low) / ` `2` `; ` ` ` ` ` `if` `(squareSum(mid) <= X) ` ` ` `{ ` ` ` `N = (` `int` `) mid; ` ` ` `low = mid + ` `1` `; ` ` ` `} ` ` ` ` ` `else` ` ` `high = mid - ` `1` `; ` ` ` `} ` ` ` ` ` `return` `N; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `long` `X = ` `25` `; ` ` ` `System.out.println(findMaxN(X)); ` `} ` `} ` ` ` `// This code contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the sum of the squares ` `// of first N natural numbers ` `static` `long` `squareSum(` `long` `N) ` `{ ` ` ` `long` `sum = (` `long` `)(N * (N + 1) * (2 * N + 1)) / 6; ` ` ` ` ` `return` `sum; ` `} ` ` ` `// Function to return the maximum N such that ` `// the sum of the squares of first N ` `// natural numbers is not more than X ` `static` `long` `findMaxN(` `long` `X) ` `{ ` ` ` `long` `low = 1, high = 100000; ` ` ` `int` `N = 0; ` ` ` ` ` `while` `(low <= high) ` ` ` `{ ` ` ` `long` `mid = (high + low) / 2; ` ` ` ` ` `if` `(squareSum(mid) <= X) ` ` ` `{ ` ` ` `N = (` `int` `) mid; ` ` ` `low = mid + 1; ` ` ` `} ` ` ` ` ` `else` ` ` `high = mid - 1; ` ` ` `} ` ` ` ` ` `return` `N; ` `} ` ` ` `// Driver code ` `static` `public` `void` `Main () ` `{ ` ` ` ` ` `long` `X = 25; ` ` ` `Console.WriteLine(findMaxN(X)); ` `} ` `} ` ` ` `// This code contributed by ajit ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the sum of the ` `# squares of first N natural numbers ` `def` `squareSum(N): ` ` ` ` ` `Sum` `=` `(N ` `*` `(N ` `+` `1` `) ` `*` `(` `2` `*` `N ` `+` `1` `)) ` `/` `/` `6` ` ` `return` `Sum` ` ` `# Function to return the maximum N such ` `# that the sum of the squares of first N ` `# natural numbers is not more than X ` `def` `findMaxN(X): ` ` ` ` ` `low, high, N ` `=` `1` `, ` `100000` `, ` `0` ` ` ` ` `while` `low <` `=` `high: ` ` ` `mid ` `=` `(high ` `+` `low) ` `/` `/` `2` ` ` ` ` `if` `squareSum(mid) <` `=` `X: ` ` ` `N ` `=` `mid ` ` ` `low ` `=` `mid ` `+` `1` ` ` ` ` `else` `: ` ` ` `high ` `=` `mid ` `-` `1` ` ` ` ` `return` `N ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `X ` `=` `25` ` ` `print` `(findMaxN(X)) ` ` ` `# This code is contributed ` `# by Rituraj Jain ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` ` ` `// Function to return the sum of the ` `// squares of first N natural numbers ` `function` `squareSum(` `$N` `) ` `{ ` ` ` `$sum` `= (` `$N` `* (int)(` `$N` `+ 1) * ` ` ` `(2 * ` `$N` `+ 1)) / 6; ` ` ` ` ` `return` `$sum` `; ` `} ` ` ` `// Function to return the maximum N such ` `// that the sum of the squares of first N ` `// natural numbers is not more than X ` `function` `findMaxN(` `$X` `) ` `{ ` ` ` `$low` `= 1; ` ` ` `$high` `= 100000; ` ` ` `$N` `= 0; ` ` ` ` ` `while` `(` `$low` `<= ` `$high` `) ` ` ` `{ ` ` ` `$mid` `= (int)(` `$high` `+ ` `$low` `) / 2; ` ` ` ` ` `if` `(squareSum(` `$mid` `) <= ` `$X` `) ` ` ` `{ ` ` ` `$N` `= ` `$mid` `; ` ` ` `$low` `= ` `$mid` `+ 1; ` ` ` `} ` ` ` ` ` `else` ` ` `$high` `= ` `$mid` `- 1; ` ` ` `} ` ` ` ` ` `return` `$N` `; ` `} ` ` ` `// Driver code ` `$X` `= 25; ` `echo` `findMaxN(` `$X` `); ` ` ` `// This code is contributed by akt_mit ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

3

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Length of longest subarray in which elements greater than K are more than elements not greater than K
- Find the permutation of first N natural numbers such that sum of i % P
_{i}is maximum possible - Minimum value of K such that sum of cubes of first K natural number is greater than equal to N
- Sum of square-sums of first n natural numbers
- Count of Numbers in Range where the number does not contain more than K non zero digits
- Sum of first N natural numbers which are not powers of K
- Fill the missing numbers in the array of N natural numbers such that arr[i] not equal to i
- Find the number of sub arrays in the permutation of first N natural numbers such that their median is M
- Maximum size of square such that all submatrices of that size have sum less than K
- Numbers having difference with digit sum more than s
- Count number of rotated strings which have more number of vowels in the first half than second half
- Sum of sum of all subsets of a set formed by first N natural numbers
- Difference between Sum of Cubes and Sum of First N Natural Numbers
- Sum of sum-series of first N Natural numbers
- Check if a given number can be expressed as pair-sum of sum of first X natural numbers
- Program to find sum of first n natural numbers
- Find if given number is sum of first n natural numbers
- Ways to place 4 items in n^2 positions such that no row/column contains more than one
- Remove minimum elements from the array such that 2*min becomes more than max
- Find smallest perfect square number A such that N + A is also a perfect square number

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.