Find maximum N such that the sum of square of first N natural numbers is not more than X
Last Updated :
15 Jun, 2022
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. 12 + 22 + 32 = 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).
Below is the implementation of the above approach :
C++
#include <bits/stdc++.h>
using namespace std;
int squareSum( int N)
{
int sum = ( int )(N * (N + 1) * (2 * N + 1)) / 6;
return sum;
}
int findMaxN( int X)
{
int N = sqrt (X);
for ( int i = 1; i <= N; i++)
{
if (squareSum(i) > X)
return i - 1;
}
return -1L;
}
int main()
{
int X = 25;
cout << findMaxN(X);
return 0;
}
|
Java
class GFG
{
static long squareSum( long N)
{
long sum = ( long )(N * (N + 1 )
* ( 2 * N + 1 )) / 6 ;
return sum;
}
static long findMaxN( long X)
{
long N = ( long )Math.sqrt(X);
for ( long i = 1 ; i <= N; i++)
{
if (squareSum(i) > X)
return i - 1 ;
}
return - 1 ;
}
public static void main(String[] args)
{
long X = 25 ;
System.out.println(findMaxN(X));
}
}
|
Python3
import math
def squareSum(N):
sum = (N * (N + 1 ) * ( 2 * N + 1 )) / / 6
return sum
def findMaxN(X):
N = ( int )(math.sqrt(X))
for i in range ( 1 , N + 1 ):
if (squareSum(i) > X):
return i - 1
return - 1
X = 25
print (findMaxN(X))
|
C#
using System;
class GFG{
static long squareSum( long N)
{
long sum = ( long )(N * (N + 1) *
(2 * N + 1)) / 6;
return sum;
}
static long findMaxN( long X)
{
long N = ( long )Math.Sqrt(X);
for ( long i = 1; i <= N; i++)
{
if (squareSum(i) > X)
return i - 1;
}
return -1;
}
public static void Main( string [] args)
{
long X = 25;
Console.WriteLine(findMaxN(X));
}
}
|
Javascript
<script>
function squareSum(N)
{
let sum = parseInt((N * (N + 1) *
(2 * N + 1)) / 6);
return sum;
}
function findMaxN(X)
{
let N = Math.sqrt(X);
for (let i = 1; i <= N; i++)
{
if (squareSum(i) > X)
return i - 1;
}
return -1;
}
let X = 25;
document.write(findMaxN(X));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
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++
#include <bits/stdc++.h>
using namespace std;
int squareSum( int N)
{
int sum = ( int )(N * (N + 1) * (2 * N + 1)) / 6;
return sum;
}
int findMaxN( int X)
{
int low = 1, high = 100000;
int N = 0;
while (low <= high)
{
int mid = (high + low) / 2;
if (squareSum(mid) <= X)
{
N = mid;
low = mid + 1;
}
else
high = mid - 1;
}
return N;
}
int main()
{
int X = 25;
cout << findMaxN(X);
return 0;
}
|
Java
class GFG {
static long squareSum( long N)
{
long sum = ( long )(N * (N + 1 )
* ( 2 * N + 1 )) / 6 ;
return sum;
}
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;
}
public static void main(String[] args)
{
long X = 25 ;
System.out.println(findMaxN(X));
}
}
|
C#
using System;
class GFG {
static long squareSum( long N)
{
long sum = ( long )(N * (N + 1)
* (2 * N + 1)) / 6;
return sum;
}
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;
}
static public void Main()
{
long X = 25;
Console.WriteLine(findMaxN(X));
}
}
|
Python3
def squareSum(N):
Sum = (N * (N + 1 ) * ( 2 * N + 1 )) / / 6
return Sum
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
if __name__ = = "__main__" :
X = 25
print (findMaxN(X))
|
PHP
<?php
function squareSum( $N )
{
$sum = ( $N * (int)( $N + 1) *
(2 * $N + 1)) / 6;
return $sum ;
}
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 ;
}
$X = 25;
echo findMaxN( $X );
?>
|
Javascript
<script>
function squareSum(N)
{
var sum = parseInt((N * (N + 1) *
(2 * N + 1)) / 6);
return sum;
}
function findMaxN(X)
{
var low = 1, high = 100000;
var N = 0;
while (low <= high)
{
var mid = (high + low) / 2;
if (squareSum(mid) <= X)
{
N = parseInt(mid);
low = mid + 1;
}
else
high = mid - 1;
}
return N;
}
var X = 25;
document.write(findMaxN(X));
</script>
|
Time Complexity: O(logn)
Auxiliary Space: O(1)
Alternate Approach :
The idea is to subtract the consecutive squares from N while N>=(i*i) where i starts from 1 and loop ends when N<(i*i).
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll findMaxN(ll X)
{
ll i = 1;
ll count = 0;
ll N = X;
while (N >= (i * i)) {
N -= (i * i);
count += 1;
i += 1;
}
return count;
}
int main()
{
ll X = 25;
cout << findMaxN(X);
return 0;
}
|
Java
class GFG {
static long findMaxN( long X)
{
long N = X;
int i = 1 ;
while (N >= (i * i)) {
N -= (i * i);
i += 1 ;
}
return i - 1 ;
}
public static void main(String[] args)
{
long X = 25 ;
System.out.println(findMaxN(X));
}
}
|
Python3
def findMaxN(X):
i = 1
count = 0
N = X
while (N > = (i * i)):
N - = (i * i)
count + = 1
i + = 1
return count
X = 25
print (findMaxN(X))
|
C#
using System;
class GFG{
static long findMaxN( long X)
{
long N = X;
int i = 1;
while (N >= (i * i))
{
N -= (i * i);
i += 1;
}
return i - 1;
}
public static void Main()
{
long X = 25;
Console.Write(findMaxN(X));
}
}
|
Javascript
<script>
function findMaxN(X)
{
var i = 1;
var count = 0;
var N = X;
while (N >= (i * i))
{
N -= (i * i);
count += 1;
i += 1;
}
return count;
}
var X = 25;
document.write(findMaxN(X));
</script>
|
Time Complexity: O(sqrtn)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...