给定整数X ,任务是找到最大值N ,使得前N个自然数的总和不超过X。
例子:
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
一个简单的解决方案:一个简单的解决方案是运行一个从1到最大N的循环,使得S(N)≤X ,其中S(N)是前N个自然数的平方和。前N个自然数的平方和由公式S(N)= N *(N + 1)*(2 * N + 1)/ 6给出。该方法的时间复杂度为O(N) 。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the sum of the squares
// of first N natural numbers
int squareSum(int N)
{
int sum = (int)(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
int findMaxN(int X)
{
int N = sqrt(X);
// Iterate till maxvalue of N
for (int i = 1; i <= N; i++)
{
// If the condition fails then return the i-1 i.e
// sum of squares till i-1
if (squareSum(i) > X)
return i - 1;
}
return -1L;
}
// Driver code
int main()
{
int X = 25;
cout << findMaxN(X);
return 0;
}
// This code is contributed by hemanth gadarla
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 N = (long)Math.sqrt(X);
// Iterate through the maxvalue
// of N and find the
// ith position
for (long i = 1; i <= N; i++)
{
if (squareSum(i) > X)
return i - 1;
}
return -1;
}
// Driver code
public static void main(String[] args)
{
long X = 25;
System.out.println(findMaxN(X));
}
}
// This code contributed by Hemanth gadarla
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 N = (long)Math.Sqrt(X);
// Iterate through the maxvalue
// of N and find the
// ith position
for(long i = 1; i <= N; i++)
{
if (squareSum(i) > X)
return i - 1;
}
return -1;
}
// Driver code
public static void Main(string[] args)
{
long X = 25;
Console.WriteLine(findMaxN(X));
}
}
// This code is contributed by ukasp
Javascript
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the sum of the squares
// of first N natural numbers
int squareSum(int N)
{
int sum = (int)(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
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;
}
// Driver code
int main()
{
int X = 25;
cout << findMaxN(X);
return 0;
}
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
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
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
PHP
C++
// C++ implementation of the approach
#include
using namespace std;
#define ll long long
// 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 i = 1; // To keep track of all squares in the series
ll count = 0; // to count the number of squares used to
// reach N
ll N = X;
while (N >= (i * i)) {
// Subtract the square of current number
N -= (i * i);
// Increment count
count += 1;
// increment i for next square number
i += 1;
}
return count;
}
// Driver code
int main()
{
ll X = 25;
cout << findMaxN(X);
return 0;
}
// This code is contributed by hemanth gadarla
Java
// Java implementation of the approach
class GFG {
// 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 N = X;
// To keep track of the squares of consecutive
// numbers
int i = 1;
while (N >= (i * i)) {
N -= (i * i);
i += 1;
}
return i - 1;
}
// Driver code
public static void main(String[] args)
{
long X = 25;
System.out.println(findMaxN(X));
}
}
// This code contributed by Hemanth gadarla
C#
// C# implementation of the approach
using System;
class GFG{
// 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 N = X;
// To keep track of the squares
// of consecutive numbers
int i = 1;
while (N >= (i * i))
{
N -= (i * i);
i += 1;
}
return i - 1;
}
// Driver code
public static void Main()
{
long X = 25;
Console.Write(findMaxN(X));
}
}
// This code is contributed by subhammahato348
输出
3
高效的方法:
一个有效的解决方案是使用二进制搜索来找到N的值。该方法的时间复杂度为O(log N) 。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the sum of the squares
// of first N natural numbers
int squareSum(int N)
{
int sum = (int)(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
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;
}
// Driver code
int main()
{
int X = 25;
cout << findMaxN(X);
return 0;
}
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
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
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
的PHP
输出
3
替代方法:
这个想法是从N中减去连续的平方,而N> =(i * i) ,其中i从1开始,当N <(i * i)时,循环结束。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
#define ll long long
// 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 i = 1; // To keep track of all squares in the series
ll count = 0; // to count the number of squares used to
// reach N
ll N = X;
while (N >= (i * i)) {
// Subtract the square of current number
N -= (i * i);
// Increment count
count += 1;
// increment i for next square number
i += 1;
}
return count;
}
// Driver code
int main()
{
ll X = 25;
cout << findMaxN(X);
return 0;
}
// This code is contributed by hemanth gadarla
Java
// Java implementation of the approach
class GFG {
// 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 N = X;
// To keep track of the squares of consecutive
// numbers
int i = 1;
while (N >= (i * i)) {
N -= (i * i);
i += 1;
}
return i - 1;
}
// Driver code
public static void main(String[] args)
{
long X = 25;
System.out.println(findMaxN(X));
}
}
// This code contributed by Hemanth gadarla
C#
// C# implementation of the approach
using System;
class GFG{
// 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 N = X;
// To keep track of the squares
// of consecutive numbers
int i = 1;
while (N >= (i * i))
{
N -= (i * i);
i += 1;
}
return i - 1;
}
// Driver code
public static void Main()
{
long X = 25;
Console.Write(findMaxN(X));
}
}
// This code is contributed by subhammahato348
输出
3