给定一个大小为N的数组arr[]和一个数组range[] ,任务是检查子数组{range[0], .. , range[1]} 的和是否是一个完全平方数。如果总和是完全平方数,则打印总和的平方根。否则,打印-1。
例子 :
Input: arr[] = {2, 19, 33, 48, 90, 100}, range = [1, 3]
Output: 10
Explanation:
The sum of element from position 1 to position 3 is 19 + 33 + 48 = 100, which is a perfect square of 10.
Input: arr[] = {13, 15, 30, 55, 87}, range = [0, 1]
Output: -1
朴素的方法:最简单的方法是遍历子数组并检查子数组的和是否为完全平方数。
时间复杂度: O(N)
辅助空间: O(1)
高效方法:上述方法的优化思路是使用二分查找来计算子数组和的平方根。请按照以下步骤操作:
- 计算给定range[]中子数组的总和。
- 现在,使用二分搜索在 0 到 sum 的范围内找到和的平方根。
- 从开始和最后一个值中找到 mid 元素,并将mid 2的值与sum 进行比较。
- 如果值 mid 2等于总和,找到一个完全平方,然后返回mid。
- 如果mid 2的值大于总和,则答案只能位于 mid 元素之后范围的右侧。因此,向右重复并将搜索空间减少到0到mid – 1 。
- 如果mid 2的值小于sum ,则将搜索空间缩小到mid + 1以求和。
下面是上述方法的实现。
C++
// C++ implementation of
// the above approach
#include
using namespace std;
// Function to calculate the square
// root of the sum of a subarray in
// a given range
int checkForPerfectSquare(vector arr,
int i, int j)
{
int mid, sum = 0;
// Calculate the sum of array
// elements within a given range
for (int m = i; m <= j; m++) {
sum += arr[m];
}
// Finding the square root
int low = 0, high = sum / 2;
while (low <= high) {
mid = low + (high - low) / 2;
// If a perfect square is found
if (mid * mid == sum) {
return mid;
}
// Reduce the search space if
// the value is greater than sum
else if (mid * mid > sum) {
high = mid - 1;
}
// Reduce the search space if
// the value if smaller than sum
else {
low = mid + 1;
}
}
return -1;
}
// Driver Code
int main()
{
// Given Array
vector arr;
arr = { 2, 19, 33, 48, 90, 100 };
// Given range
int L = 1, R = 3;
// Function Call
cout << checkForPerfectSquare(arr, L, R);
return 0;
}
Java
// Java implementation of
// the above approach
import java.util.*;
class GFG{
// Function to calculate the square
// root of the sum of a subarray in
// a given range
static int checkForPerfectSquare(int []arr,
int i, int j)
{
int mid, sum = 0;
// Calculate the sum of array
// elements within a given range
for (int m = i; m <= j; m++)
{
sum += arr[m];
}
// Finding the square root
int low = 0, high = sum / 2;
while (low <= high)
{
mid = low + (high - low) / 2;
// If a perfect square
// is found
if (mid * mid == sum)
{
return mid;
}
// Reduce the search space
// if the value is greater
// than sum
else if (mid * mid > sum)
{
high = mid - 1;
}
// Reduce the search space
// if the value if smaller
// than sum
else
{
low = mid + 1;
}
}
return -1;
}
// Driver Code
public static void main(String[] args)
{
// Given Array
int []arr = {2, 19, 33,
48, 90, 100};
// Given range
int L = 1, R = 3;
// Function Call
System.out.print(
checkForPerfectSquare(arr, L, R));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 implementation of
# the above approach
# Function to calculate the square
# root of the sum of a subarray in
# a given range
def checkForPerfectSquare(arr, i, j):
mid, sum = 0, 0
# Calculate the sum of array
# elements within a given range
for m in range(i, j + 1):
sum += arr[m]
# Finding the square root
low = 0
high = sum // 2
while (low <= high):
mid = low + (high - low) // 2
# If a perfect square is found
if (mid * mid == sum):
return mid
# Reduce the search space if
# the value is greater than sum
elif (mid * mid > sum):
high = mid - 1
# Reduce the search space if
# the value if smaller than sum
else:
low = mid + 1
return -1
# Driver Code
if __name__ == '__main__':
# Given Array
arr = [ 2, 19, 33, 48, 90, 100 ]
# Given range
L = 1
R = 3
# Function call
print(checkForPerfectSquare(arr, L, R))
# This code is contributed by mohit kumar 29
C#
// C# implementation of
// the above approach
using System;
class GFG{
// Function to calculate the square
// root of the sum of a subarray in
// a given range
static int checkForPerfectSquare(int []arr,
int i, int j)
{
int mid, sum = 0;
// Calculate the sum of array
// elements within a given range
for (int m = i; m <= j; m++)
{
sum += arr[m];
}
// Finding the square root
int low = 0, high = sum / 2;
while (low <= high)
{
mid = low + (high - low) / 2;
// If a perfect square
// is found
if (mid * mid == sum)
{
return mid;
}
// Reduce the search space
// if the value is greater
// than sum
else if (mid * mid > sum)
{
high = mid - 1;
}
// Reduce the search space
// if the value if smaller
// than sum
else
{
low = mid + 1;
}
}
return -1;
}
// Driver Code
public static void Main(String[] args)
{
// Given Array
int []arr = {2, 19, 33,
48, 90, 100};
// Given range
int L = 1, R = 3;
// Function Call
Console.Write(checkForPerfectSquare(arr,
L, R));}
}
// This code is contributed by 29AjayKumar
Javascript
输出:
10
时间复杂度: O(max(log(sum), N))
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live