Count of contiguous subarrays possible for every index by including the element at that index

Given a number N which represents the size of the array A[], the task is to find the number of contiguous subarrays that can be formed for every index of the array by including the element at that index in the original array.

Examples:

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}}

Approach: The idea is to use the concept of permutation and combination. It can be observed that the number of subarrays possible including the element at the ith index is always equal to the number of subarrays possible including the element at the index (N – i) where N is the length of the array.



  • Iterate through the first half of the array.
  • The number of subarrays including the element at the ith index is always equal to the number of subarrays including the element at the index N – i. Therefore, simultaneously update the count for both the indices.
  • To calculate the number of subarrays that include the element at the ith index, we simply subtract the number of subarrays not including the element at the ith index from the total number of ways.
  • Therefore, the formula to calculate the required value is:
    Count of possible subarrays = N * (i + 1) - i * (i + 1)
    

    where i is the current index.

  • Calculate and store the above values for every index.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find the number of
// contiguous subarrays including
// the element at every index
// of the array of size N
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the number of
// subarrays including the element
// at every index of the array
vector<int> calculateWays(int N)
{
    int x = 0;
    vector<int> 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<int> v)
{
    for (int i = 0; i < v.size(); i++)
        cout << v[i] << " ";
}
  
// Driver code
int main()
{
    vector<int> v;
    v = calculateWays(4);
  
    printArray(v);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output:

4 6 6 4

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : AnkitRai01, sayesha, rutvik_56