给定一个大小为N的数组arr[] ,任务是找到长度至少为 2 的子数组的计数,使得这些子数组的连续元素之间的差异在整个过程中保持相同,即子数组的元素形成一个 AP。
例子:
Input: arr[] = {8, 7, 4, 1, 0}
Output: 5
Explanation:
All subarrays of size greater than 1 which form an AP are [8, 7], [7, 4], [4, 1], [1, 0], [7, 4, 1]
Input: arr[] = {4, 2}
Output: 1
方法:想法是从给定的数组中生成所有可能的子数组,对于每个子数组,检查相邻元素之间的差异对于生成的子数组是否相同。以下是步骤:
- 使用两个循环迭代每个长度至少为 2 的子数组。
- 设i为子数组的起始索引, j为子数组的结束索引。
- 如果从索引 i 到 j 的数组中每对相邻元素之间的差异都相同,则增加总计数。
- 否则,对下一个子数组重复上述过程。
下面是上述方法的实现:
C++
// C++ implementation of
// the above approach
#include
using namespace std;
// Function to find the
// total count of subarrays
int calcSubarray(int A[], int N)
{
int count = 0;
// Iterate over each subarray
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N; j++) {
bool flag = true;
// Difference between first
// two terms of subarray
int comm_diff = A[i + 1] - A[i];
// Iterate over the subarray
// from i to j
for (int k = i; k < j; k++) {
// Check if the difference
// of all adjacent elements
// is same
if (A[k + 1] - A[k] == comm_diff) {
continue;
}
else {
flag = false;
break;
}
}
if (flag) {
count++;
}
}
}
return count;
}
// Driver Code
int main()
{
// Given array
int A[5] = { 8, 7, 4, 1, 0 };
int N = sizeof(A) / sizeof(int);
// Function Call
cout << calcSubarray(A, N);
}
Java
// Java implementation of
// the above approach
import java.util.*;
class GFG{
// Function to find the
// total count of subarrays
static int calcSubarray(int A[],
int N)
{
int count = 0;
// Iterate over each subarray
for (int i = 0; i < N; i++)
{
for (int j = i + 1; j < N; j++)
{
boolean flag = true;
// Difference between first
// two terms of subarray
int comm_diff = A[i + 1] - A[i];
// Iterate over the subarray
// from i to j
for (int k = i; k < j; k++)
{
// Check if the difference
// of all adjacent elements
// is same
if (A[k + 1] - A[k] == comm_diff)
{
continue;
}
else
{
flag = false;
break;
}
}
if (flag)
{
count++;
}
}
}
return count;
}
// Driver Code
public static void main(String[] args)
{
// Given array
int []A = {8, 7, 4, 1, 0};
int N = A.length;
// Function Call
System.out.print(calcSubarray(A, N));
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 implementation of
# the above approach
# Function to find the
# total count of subarrays
def calcSubarray(A, N):
count = 0
# Iterate over each subarray
for i in range(N):
for j in range(i + 1, N):
flag = True
# Difference between first
# two terms of subarray
comm_diff = A[i + 1] - A[i]
# Iterate over the subarray
# from i to j
for k in range(i, j):
# Check if the difference
# of all adjacent elements
# is same
if (A[k + 1] - A[k] == comm_diff):
continue
else:
flag = False
break
if (flag):
count += 1
return count
# Driver Code
if __name__ == '__main__':
# Given array
A = [ 8, 7, 4, 1, 0 ]
N = len(A)
# Function call
print(calcSubarray(A, N))
# This code is contributed by mohit kumar 29
C#
// C# implementation of
// the above approach
using System;
class GFG{
// Function to find the
// total count of subarrays
static int calcSubarray(int []A,
int N)
{
int count = 0;
// Iterate over each subarray
for (int i = 0; i < N; i++)
{
for (int j = i + 1; j < N; j++)
{
bool flag = true;
// Difference between first
// two terms of subarray
int comm_diff = A[i + 1] - A[i];
// Iterate over the subarray
// from i to j
for (int k = i; k < j; k++)
{
// Check if the difference
// of all adjacent elements
// is same
if (A[k + 1] - A[k] == comm_diff)
{
continue;
}
else
{
flag = false;
break;
}
}
if (flag)
{
count++;
}
}
}
return count;
}
// Driver Code
public static void Main(String[] args)
{
// Given array
int []A = {8, 7, 4, 1, 0};
int N = A.Length;
// Function Call
Console.Write(calcSubarray(A, N));
}
}
// This code is contributed by 29AjayKumar
Javascript
输出:
5
时间复杂度: O(N 3 )
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live