给定一个数组arr [] ,任务是对所有可以求和的子数组计数为两个整数的平方之差。
例子:
Input: arr[] = {1, 3, 5}
Output: 6
Explanation:
There are six subarrays which can be formed from the array whose sum can be split as the difference of squares of two integers. They are:
Sum of the subarray {1} = 1 = 12 – 02
Sum of the subarray {3} = 3 = 22 – 12
Sum of the subarray {5} = 5 = 32 – 22
Sum of the subarray {1, 3} = 4 = 22 – 02
Sum of the subarray {3, 5} = 8 = 32 – 12
Sum of the subarray {1, 3, 5} = 9 = 52 – 42
Input: arr[] = {1, 2, 3, 4, 5}
Output: 11
方法:为了解决此问题,需要进行观察。除了可以以((4 * N)+ 2)形式表示的那些平方以外,任何数字都可以表示为两个平方的差,其中N是整数。因此,可以按照以下步骤计算答案:
- 遍历数组以找到给定数组的所有可能的子数组。
- 如果数字K可以表示为((4 * N)+ 2) ,其中N是某个整数,则K + 2始终是4的倍数。
- 因此,对于子数组K的每个和,检查(K + 2)是否为4的倍数。
- 如果是,则该特定值不能表示为平方差。
下面是上述方法的实现:
C++
// C++ program to count all the non-contiguous
// subarrays whose sum can be split
// as the difference of the squares
#include
using namespace std;
// Function to count all the non-contiguous
// subarrays whose sum can be split
// as the difference of the squares
int Solve(int arr[], int n)
{
int temp = 0, count = 0;
// Loop to iterate over all the possible
// subsequences of the array
for (int i = 0; i < n; i++) {
temp = 0;
for (int j = i; j < n; j++) {
// Finding the sum of all the
// possible subsequences
temp += arr[j];
// Condition to check whether
// the number can be split
// as difference of squares
if ((temp + 2) % 4 != 0)
count++;
}
}
return count;
}
// Driver code
int main()
{
int arr[] = { 1, 2, 3, 4, 5,
6, 7, 8, 9, 10 };
int N = sizeof(arr) / sizeof(int);
cout << Solve(arr, N);
return 0;
}
Java
// Java program to count all the
// non-contiguous subarrays whose
// sum can be split as the
// difference of the squares
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
// Function to count all the non-contiguous
// subarrays whose sum can be split
// as the difference of the squares
static int Solve(int arr[], int n)
{
int temp = 0, count = 0;
// Loop to iterate over all the possible
// subsequences of the array
for(int i = 0; i < n; i++)
{
temp = 0;
for(int j = i; j < n; j++)
{
// Finding the sum of all the
// possible subsequences
temp += arr[j];
// Condition to check whether
// the number can be split
// as difference of squares
if ((temp + 2) % 4 != 0)
count++;
}
}
return count;
}
// Driver Code
public static void main(String [] args)
{
int arr[] = { 1, 2, 3, 4, 5,
6, 7, 8, 9, 10 };
int N = arr.length;
System.out.println(Solve(arr, N));
}
}
// This code is contributed by shivanisinghss2110
Python3
# Python3 program to count all the non-contiguous
# subarrays whose sum can be split
# as the difference of the squares
# Function to count all the non-contiguous
# subarrays whose sum can be split
# as the difference of the squares
def Solve(arr, n):
temp = 0; count = 0;
# Loop to iterate over all the possible
# subsequences of the array
for i in range(0, n):
temp = 0;
for j in range(i, n):
# Finding the sum of all the
# possible subsequences
temp = temp + arr[j];
# Condition to check whether
# the number can be split
# as difference of squares
if ((temp + 2) % 4 != 0):
count += 1;
return count;
# Driver code
arr = [ 1, 2, 3, 4, 5,
6, 7, 8, 9, 10 ];
N = len(arr);
print(Solve(arr, N));
# This code is contributed by Code_Mech
C#
// C# program to count all the
// non-contiguous subarrays whose
// sum can be split as the
// difference of the squares
using System;
class GFG{
// Function to count all the
// non-contiguous subarrays whose
// sum can be split as the
// difference of the squares
static int Solve(int []arr, int n)
{
int temp = 0, count = 0;
// Loop to iterate over all
// the possible subsequences
// of the array
for(int i = 0; i < n; i++)
{
temp = 0;
for(int j = i; j < n; j++)
{
// Finding the sum of all the
// possible subsequences
temp += arr[j];
// Condition to check whether
// the number can be split
// as difference of squares
if ((temp + 2) % 4 != 0)
count++;
}
}
return count;
}
// Driver code
public static void Main(String[] args)
{
int []arr = { 1, 2, 3, 4, 5,
6, 7, 8, 9, 10 };
int N = arr.Length;
Console.Write(Solve(arr, N));
}
}
// This code is contributed by shivanisinghss2110
Javascript
40
时间复杂度: O(N) 2 ,其中N是数组的大小