Open In App

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

Last Updated : 07 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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:
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++




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


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




<script>
 
// Javascript 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
function calculateWays(n)
{
    let x = 0;
           
    // Creating an array of size N
    let v = Array.from({length: n}, (_, i) => 0);
       
    for(let i = 0; i < n; i++)
        v[i] = 0;
           
    // The loop is iterated till half the
    // length of the array
    for(let 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
function printArray(v)
{
    for(let i = 0; i < v.length; i++)
        document.write(v[i] + " ");
}
 
// Driver Code
let v;
v = calculateWays(4);
   
printArray(v);
 
// This code is contributed by target_2
 
</script>


Output

4 6 6 4

Time Complexity: O(n)

Auxiliary Space: O(n)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads