给定一个表示数组A[]大小的数字N ,任务是通过在原始数组中包含该索引处的元素来找到可以为数组的每个索引形成的连续子数组的数量。
例子:
Input: N = 4
Output: {4, 6, 6, 4}
Explanation:
Since the size of the array is given as 4. Let’s assume the array to be {1, 2, 3, 4}.
The number of subarrays that contain 1 are: {{1}, {1, 2}, {1, 2, 3}, {1, 2, 3, 4}}
The number of subarrays that contain 2 are: {{2}, {2, 3}, {2, 3, 4}, {1, 2}, {1, 2, 3}, {1, 2, 3, 4}}
The number of subarrays that contain 3 are: {{3}, {2, 3}, {2, 3, 4}, {3, 4}, {1, 2, 3}, {1, 2, 3, 4}}
The number of subarrays that contain 4 are: {{4}, {3, 4}, {2, 3, 4}, {1, 2, 3, 4}}
Input: 3
Output: {3, 4, 3}
Explanation:
Since the size of the array is given as 3. Lets assume the array to be {1, 2, 3}.
The number of subarrays that contain 1 are: {{1}, {1, 2}, {1, 2, 3}}
The number of subarrays that contain 2 are: {{2}, {1, 2}, {2, 3}, {1, 2, 3}}
The number of subarrays that contain 3 are: {{3}, {2, 3}, {1, 2, 3}}
做法:思路是利用排列组合的概念。可以观察到,包含第i个索引处的元素的可能子数组的数量始终等于可能包含索引 ( N – i ) 处的元素的子数组的数量,其中N是数组的长度。
- 遍历数组的前半部分。
- 包含第i个索引处的元素的子数组的数量总是等于包含索引N – i处的元素的子数组的数量。因此,同时更新两个索引的计数。
- 要计算包含第i个索引处的元素的子数组的数量,我们只需从总路数中减去不包含第i个索引处的元素的子数组的数量。
- 因此,计算所需值的公式为:
Count of possible subarrays = N * (i + 1) - i * (i + 1)
其中 i 是当前索引。
- 计算并存储每个索引的上述值。
下面是上述方法的实现:
C++
// C++ program to find the number of
// contiguous subarrays including
// the element at every index
// of the array of size N
#include
using namespace std;
// Function to find the number of
// subarrays including the element
// at every index of the array
vector calculateWays(int N)
{
int x = 0;
vector v;
// Creating an array of size N
for (int i = 0; i < N; i++)
v.push_back(0);
// The loop is iterated till half the
// length of the array
for (int i = 0; i <= N / 2; i++) {
// Condition to avoid overwriting
// the middle element for the
// array with even length.
if (N % 2 == 0 && i == N / 2)
break;
// Computing the number of subarrays
x = N * (i + 1) - (i + 1) * i;
// The ith element from the beginning
// and the ending have the same
// number of possible subarrays
v[i] = x;
v[N - i - 1] = x;
}
return v;
}
// Function to print the vector
void printArray(vector v)
{
for (int i = 0; i < v.size(); i++)
cout << v[i] << " ";
}
// Driver code
int main()
{
vector v;
v = calculateWays(4);
printArray(v);
return 0;
}
Java
// Java program to find the number
// of contiguous subarrays including
// the element at every index
// of the array of size N
import java.util.Scanner;
class contiguous_subarrays{
// Function to find the number of
// subarrays including the element
// at every index of the array
public static int[] calculateWays(int n)
{
int x = 0;
// Creating an array of size N
int[]v = new int[n];
for(int i = 0; i < n; i++)
v[i] = 0;
// The loop is iterated till half the
// length of the array
for(int i = 0; i < n / 2; i++)
{
// Condition to avoid overwriting
// the middle element for the
// array with even length.
if(n % 2 == 0 && i == n / 2)
break;
// Computing the number of subarrays
x = n * (i + 1) - (i + 1) * i;
// The ith element from the beginning
// and the ending have the same
// number of possible subarray
v[i] = x;
v[n - i - 1] = x;
}
return v;
}
// Function to print the vector
public static void printArray(int[]v)
{
for(int i = 0; i < v.length; i++)
System.out.print(v[i] + " ");
}
// Driver code
public static void main (String args[])
{
int[]v;
v = calculateWays(4);
printArray(v);
}
}
// This code is contributed by sayesha
Python3
# Python3 program to find the number of
# contiguous subarrays including
# the element at every index
# of the array of size N
# Function to find the number of
# subarrays including the element
# at every index of the array
def calculateWays(N):
x = 0;
v = [];
# Creating an array of size N
for i in range(N):
v.append(0);
# The loop is iterated till half the
# length of the array
for i in range(N // 2 + 1):
# Condition to avoid overwriting
# the middle element for the
# array with even length.
if (N % 2 == 0 and i == N // 2):
break;
# Computing the number of subarrays
x = N * (i + 1) - (i + 1) * i;
# The ith element from the beginning
# and the ending have the same
# number of possible subarrays
v[i] = x;
v[N - i - 1] = x;
return v;
# Function to print the vector
def printArray(v):
for i in range(len(v)):
print(v[i], end = " ");
# Driver code
if __name__ == "__main__":
v = calculateWays(4);
printArray(v);
# This code is contributed by AnkitRai01
C#
// C# program to find the number
// of contiguous subarrays including
// the element at every index
// of the array of size N
using System;
class GFG{
// Function to find the number of
// subarrays including the element
// at every index of the array
public static int[] calculateWays(int N)
{
int x = 0;
// Creating an array of size N
int[]v = new int[N];
for(int i = 0; i < N; i++)
v[i] = 0;
// The loop is iterated till half
// the length of the array
for(int i = 0; i < N / 2; i++)
{
// Condition to avoid overwriting
// the middle element for the
// array with even length.
if(N % 2 == 0 && i == N / 2)
break;
// Computing the number of subarrays
x = N * (i + 1) - (i + 1) * i;
// The ith element from the beginning
// and the ending have the same
// number of possible subarray
v[i] = x;
v[N - i - 1] = x;
}
return v;
}
// Function to print the vector
public static void printArray(int []v)
{
for(int i = 0; i < v.Length; i++)
{
Console.Write(v[i] + " ");
}
}
// Driver code
public static void Main (string []args)
{
int []v;
v = calculateWays(4);
printArray(v);
}
}
// This code is contributed by rutvik_56
Javascript
4 6 6 4
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live