给定一个大小为N的数组arr [] ,任务是打印所有和为完美平方的子数组的开始和结束索引。
例子:
Input: arr[] = {65, 79, 81}
Output: (0, 1) (0, 2) (2, 2)
Explanation:
Subarray sum whose start and end index is (0, 1) = 65 + 79 = 144 = 122
Subarray sum whose start and end index is (0, 2} = 65 + 79 + 81 = 225 = 152
Subarray sum whose start and end index is {2, 2} = 81 = 92
Input: arr[] = {1, 2, 3, 4, 5}
Output: (0, 0) (1, 3) (3, 3) (3, 4)
方法:可以使用“前缀和数组”技术解决该问题。这个想法是使用前缀求和数组来找到所有子数组的和。对于每个子数组,请检查子数组的总和是否为理想平方。如果发现对任何子数组都是正确的,则打印该子数组的开始和结束索引。请按照以下步骤解决问题。
- 初始化一个变量,例如currSubSum,以存储当前的子数组总和。
- 遍历数组以生成给定数组的所有可能的子数组。
- 计算每个子数组的总和,对于每个子数组的总和,检查它是否为理想平方。
- 如果发现对任何子数组都是正确的,则打印子数组的开始和结束索引。
下面是上述方法的实现:
C++14
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to print the start and end
// indices of all subarrays whose sum
// is a perfect square
void PrintIndexes(int arr[], int N)
{
for (int i = 0; i < N; i++) {
// Stores the current
// subarray sum
int currSubSum = 0;
for (int j = i; j < N; j++) {
// Update current subarray sum
currSubSum += arr[j];
// Stores the square root
// of currSubSum
int sq = sqrt(currSubSum);
// Check if currSubSum is
// a perfect square or not
if (sq * sq == currSubSum) {
cout << "(" << i << ", "
<< j << ") ";
}
}
}
}
// Driver Code
int main()
{
int arr[] = { 65, 79, 81 };
int N = sizeof(arr) / sizeof(arr[0]);
PrintIndexes(arr, N);
}
Java
// Java program to implement
// the above approach
import java.io.*;
class GFG{
// Function to print the start and end
// indices of all subarrays whose sum
// is a perfect square
static void PrintIndexes(int arr[], int N)
{
for(int i = 0; i < N; i++)
{
// Stores the current
// subarray sum
int currSubSum = 0;
for(int j = i; j < N; j++)
{
// Update current subarray sum
currSubSum += arr[j];
// Stores the square root
// of currSubSum
int sq = (int)Math.sqrt(currSubSum);
// Check if currSubSum is
// a perfect square or not
if (sq * sq == currSubSum)
{
System.out.print("(" + i + "," +
j + ")" + " ");
}
}
}
}
// Driver code
public static void main (String[] args)
throws java.lang.Exception
{
int arr[] = { 65, 79, 81 };
PrintIndexes(arr, arr.length);
}
}
// This code is contributed by bikram2001jha
Python3
# Python3 program to implement
# the above approach
import math
# Function to print the start and end
# indices of all subarrays whose sum
# is a perfect square
def PrintIndexes(arr, N):
for i in range(N):
# Stores the current
# subarray sum
currSubSum = 0
for j in range(i, N, 1):
# Update current subarray sum
currSubSum += arr[j]
# Stores the square root
# of currSubSum
sq = int(math.sqrt(currSubSum))
# Check if currSubSum is
# a perfect square or not
if (sq * sq == currSubSum):
print("(", i, ",",
j, ")", end = " ")
# Driver Code
arr = [ 65, 79, 81 ]
N = len(arr)
PrintIndexes(arr, N)
# This code is contributed by sanjoy_62
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to print the start
// and end indices of all
// subarrays whose sum
// is a perfect square
static void PrintIndexes(int []arr,
int N)
{
for(int i = 0; i < N; i++)
{
// Stores the current
// subarray sum
int currSubSum = 0;
for(int j = i; j < N; j++)
{
// Update current subarray sum
currSubSum += arr[j];
// Stores the square root
// of currSubSum
int sq = (int)Math.Sqrt(currSubSum);
// Check if currSubSum is
// a perfect square or not
if (sq * sq == currSubSum)
{
Console.Write("(" + i + "," +
j + ")" + " ");
}
}
}
}
// Driver code
public static void Main(String[] args)
{
int []arr = {65, 79, 81};
PrintIndexes(arr, arr.Length);
}
}
// This code is contributed by shikhasingrajput
输出:
(0, 1) (0, 2) (2, 2)
时间复杂度: O(N 2 )
辅助空间: O(1)