📌  相关文章
📜  通过在每个索引处包含元素,可以为每个索引计算连续的子数组的数目

📅  最后修改于: 2021-04-26 08:49:34             🧑  作者: Mango

给定一个代表数组A []大小的数字N ,任务是通过在原始数组中包含该索引处的元素,找到可以为数组的每个索引形成的连续子数组的数目。

例子:

方法:想法是使用置换和组合的概念。可以观察到,包括第i索引处的元素的可能子数组的数量始终等于包括索引( N – i )处的元素的可能子数组的数量,其中N是数组的长度。

  • 遍历数组的前半部分。
  • 包括在第i索引处的元素的子数组的数量始终等于包括在索引N – i处的元素的子数组的数量。因此,同时更新两个索引的计数。
  • 为了计算在第i索引处包含元素的子数组的数量,我们只需从总数中减去不包含在第i索引处的元素的子数组的数量。
  • 因此,计算所需值的公式为:
    Count of possible subarrays = N * (i + 1) - i * (i + 1)
    

    其中我是当前指数。

  • 计算并存储每个索引的上述值。

下面是上述方法的实现:

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


输出:
4 6 6 4