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++
#include <bits/stdc++.h>
using namespace std;
vector< int > calculateWays( int N)
{
int x = 0;
vector< int > v;
for ( int i = 0; i < N; i++)
v.push_back(0);
for ( int i = 0; i <= N / 2; i++) {
if (N % 2 == 0 && i == N / 2)
break ;
x = N * (i + 1) - (i + 1) * i;
v[i] = x;
v[N - i - 1] = x;
}
return v;
}
void printArray(vector< int > v)
{
for ( int i = 0; i < v.size(); i++)
cout << v[i] << " " ;
}
int main()
{
vector< int > v;
v = calculateWays(4);
printArray(v);
return 0;
}
|
Java
import java.util.Scanner;
class contiguous_subarrays{
public static int [] calculateWays( int n)
{
int x = 0 ;
int []v = new int [n];
for ( int i = 0 ; i < n; i++)
v[i] = 0 ;
for ( int i = 0 ; i < n / 2 ; i++)
{
if (n % 2 == 0 && i == n / 2 )
break ;
x = n * (i + 1 ) - (i + 1 ) * i;
v[i] = x;
v[n - i - 1 ] = x;
}
return v;
}
public static void printArray( int []v)
{
for ( int i = 0 ; i < v.length; i++)
System.out.print(v[i] + " " );
}
public static void main (String args[])
{
int []v;
v = calculateWays( 4 );
printArray(v);
}
}
|
Python3
def calculateWays(N):
x = 0 ;
v = [];
for i in range (N):
v.append( 0 );
for i in range (N / / 2 + 1 ):
if (N % 2 = = 0 and i = = N / / 2 ):
break ;
x = N * (i + 1 ) - (i + 1 ) * i;
v[i] = x;
v[N - i - 1 ] = x;
return v;
def printArray(v):
for i in range ( len (v)):
print (v[i], end = " " );
if __name__ = = "__main__" :
v = calculateWays( 4 );
printArray(v);
|
C#
using System;
class GFG{
public static int [] calculateWays( int N)
{
int x = 0;
int []v = new int [N];
for ( int i = 0; i < N; i++)
v[i] = 0;
for ( int i = 0; i < N / 2; i++)
{
if (N % 2 == 0 && i == N / 2)
break ;
x = N * (i + 1) - (i + 1) * i;
v[i] = x;
v[N - i - 1] = x;
}
return v;
}
public static void printArray( int []v)
{
for ( int i = 0; i < v.Length; i++)
{
Console.Write(v[i] + " " );
}
}
public static void Main ( string []args)
{
int []v;
v = calculateWays(4);
printArray(v);
}
}
|
Javascript
<script>
function calculateWays(n)
{
let x = 0;
let v = Array.from({length: n}, (_, i) => 0);
for (let i = 0; i < n; i++)
v[i] = 0;
for (let i = 0; i < n / 2; i++)
{
if (n % 2 == 0 && i == n / 2)
break ;
x = n * (i + 1) - (i + 1) * i;
v[i] = x;
v[n - i - 1] = x;
}
return v;
}
function printArray(v)
{
for (let i = 0; i < v.length; i++)
document.write(v[i] + " " );
}
let v;
v = calculateWays(4);
printArray(v);
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Last Updated :
07 Jan, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...