Count of Subarrays in an array containing numbers from 1 to the length of subarray
Last Updated :
30 Aug, 2022
Given an array arr[] of length N containing all elements from 1 to N, the task is to find the number of sub-arrays that contain numbers from 1 to M, where M is the length of the sub-array.
Examples:
Input: arr[] = {4, 1, 3, 2, 5, 6}
Output: 5
Explanation:
Desired Sub-arrays = { {4, 1, 3, 2}, {1}, {1, 3, 2}, {4, 1, 3, 2, 5}, {4, 1, 3, 2, 5, 6} }
Count(Sub-arrays) = 5
Input: arr[] = {3, 2, 4, 1}
Output: 2
Explanation:
Desired Sub-arrays = { {1}, {3, 2, 4, 1} }
Count(Sub-arrays) = 2
Naive Approach: Generate all subarrays of the array and check for each subarray that it contains each element 1 to the length of the subarray.
Efficient Approach: Create a vector that maps each element of the array with its index in sorted order. Now iterate over this vector and check whether the difference of maximum and minimum index till the ith element is less than the number of elements iterated till now, which is the value of i itself.
Below is the implementation of the above approach
C++
// C++ Implementation to Count the no. of
// Sub-arrays which contains all elements
// from 1 to length of subarray
#include <bits/stdc++.h>
using namespace std;
// Function to count the number
// Sub-arrays which contains all elements
// 1 to length of subarray
int countOfSubarrays(int* arr, int n)
{
int count = 0;
vector<int> v(n + 1);
// Map all elements of array with their index
for (int i = 0; i < n; i++)
v[arr[i]] = i;
// Set the max and min index equal to the
// min and max value of integer respectively.
int maximum = INT_MIN;
int minimum = INT_MAX;
for (int i = 1; i <= n; i++) {
// Update the value of maximum index
maximum = max(maximum, v[i]);
// Update the value of minimum index
minimum = min(minimum, v[i]);
// Increase the counter if difference of
// max. and min. index is less than the
// elements iterated till now
if (maximum - minimum < i)
count = count + 1;
}
return count;
}
// Driver Function
int main()
{
int arr[] = { 4, 1, 3, 2, 5, 6 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << countOfSubarrays(arr, n) << endl;
return 0;
}
Java
// Java Implementation to Count the no. of
// Sub-arrays which contains all elements
// from 1 to length of subarray
class GFG
{
// Function to count the number
// Sub-arrays which contains all elements
// 1 to length of subarray
static int countOfSubarrays(int []arr, int n)
{
int count = 0;
int []v = new int[n + 1];
// Map all elements of array with their index
for (int i = 0; i < n; i++)
v[arr[i]] = i;
// Set the max and min index equal to the
// min and max value of integer respectively.
int maximum = Integer.MIN_VALUE;
int minimum = Integer.MAX_VALUE;
for (int i = 1; i <= n; i++)
{
// Update the value of maximum index
maximum = Math.max(maximum, v[i]);
// Update the value of minimum index
minimum = Math.min(minimum, v[i]);
// Increase the counter if difference of
// max. and min. index is less than the
// elements iterated till now
if (maximum - minimum < i)
count = count + 1;
}
return count;
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 4, 1, 3, 2, 5, 6 };
int n = arr.length;
System.out.print(countOfSubarrays(arr, n) +"\n");
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python3 Implementation to Count the no. of
# Sub-arrays which contains all elements
# from 1 to length of subarray
import sys
INT_MAX = sys.maxsize;
INT_MIN = -(sys.maxsize - 1);
# Function to count the number
# Sub-arrays which contains all elements
# 1 to length of subarray
def countOfSubarrays(arr, n) :
count = 0;
v = [0]*(n + 1);
# Map all elements of array with their index
for i in range(n) :
v[arr[i]] = i;
# Set the max and min index equal to the
# min and max value of integer respectively.
maximum = INT_MIN;
minimum = INT_MAX;
for i in range(1, n + 1) :
# Update the value of maximum index
maximum = max(maximum, v[i]);
# Update the value of minimum index
minimum = min(minimum, v[i]);
# Increase the counter if difference of
# max. and min. index is less than the
# elements iterated till now
if (maximum - minimum < i) :
count = count + 1;
return count;
# Driver code
if __name__ == "__main__" :
arr = [ 4, 1, 3, 2, 5, 6 ];
n = len(arr);
print(countOfSubarrays(arr, n));
# This code is contributed by AnkitRai01
C#
// C# Implementation to Count the no. of
// Sub-arrays which contains all elements
// from 1 to length of subarray
using System;
class GFG
{
// Function to count the number
// Sub-arrays which contains all elements
// 1 to length of subarray
static int countOfSubarrays(int []arr, int n)
{
int count = 0;
int []v = new int[n + 1];
// Map all elements of array with their index
for (int i = 0; i < n; i++)
v[arr[i]] = i;
// Set the max and min index equal to the
// min and max value of integer respectively.
int maximum = int.MinValue;
int minimum = int.MaxValue;
for (int i = 1; i <= n; i++)
{
// Update the value of maximum index
maximum = Math.Max(maximum, v[i]);
// Update the value of minimum index
minimum = Math.Min(minimum, v[i]);
// Increase the counter if difference of
// max. and min. index is less than the
// elements iterated till now
if (maximum - minimum < i)
count = count + 1;
}
return count;
}
// Driver code
public static void Main(String[] args)
{
int []arr = { 4, 1, 3, 2, 5, 6 };
int n = arr.Length;
Console.Write(countOfSubarrays(arr, n) +"\n");
}
}
// This code is contributed by PrinciRaj1992
JavaScript
<script>
// Javascript Implementation to Count the no. of
// Sub-arrays which contains all elements
// from 1 to length of subarray
// Function to count the number
// Sub-arrays which contains all elements
// 1 to length of subarray
function countOfSubarrays(arr, n)
{
var count = 0;
var v = Array(n + 1);
// Map all elements of array with their index
for (var i = 0; i < n; i++)
v[arr[i]] = i;
// Set the max and min index equal to the
// min and max value of integer respectively.
var maximum = -1000000000;
var minimum = 10000000000;
for (var i = 1; i <= n; i++) {
// Update the value of maximum index
maximum = Math.max(maximum, v[i]);
// Update the value of minimum index
minimum = Math.min(minimum, v[i]);
// Increase the counter if difference of
// max. and min. index is less than the
// elements iterated till now
if (maximum - minimum < i)
count = count + 1;
}
return count;
}
// Driver Function
var arr = [4, 1, 3, 2, 5, 6 ];
var n = arr.length;
document.write( countOfSubarrays(arr, n) );
// This code is contributed by importantly.
</script>
Time Complexity: O(N), for traversal over the array.
Auxiliary Space: O(N), for creating an extra array of size N + 1.
Similar Reads
Count of Subarrays which Contain the Length of that Subarray Given an array A[] of length N, the task is to count the number of subarrays of A[] that contain the length of that subarray. Examples: Input: A = {10, 11, 1}, N = 3Output: 1Explanation: Only the subarray {1}, with a length 1, contains its own length. Input: A = [1, 2, 3, 4, 5], N = 5Output: 9Explan
8 min read
Count of subarray that does not contain any subarray with sum 0 Given an array arr, the task is to find the total number of subarrays of the given array which do not contain any subarray whose sum of elements is equal to zero. All the array elements may not be distinct.Examples: Input: arr = {2, 4, -6} Output: 5 Explanation: There are 5 subarrays which do not co
11 min read
Count of Subarrays not containing all elements of another Array Given two arrays nums[] of size N and target[]. The task is to find the number of non-empty subarrays of nums[] that do not contain every number in the target[]. As the answer can be very large, calculate the result modulo 109+7. Examples: Input: nums = {1, 2, 2}, target = {1, 2}Output: 4Explanation
12 min read
Count subarrays having sum modulo K same as the length of the subarray Given an integer K and an array arr[] consisting of N positive integers, the task is to find the number of subarrays whose sum modulo K is equal to the size of the subarray. Examples: Input: arr[] = {1, 4, 3, 2}, K = 3Output: 4Explanation: 1 % 3 = 1 (1 + 4) % 3 = 2 4 % 3 = 1 (3 + 2) % 3 = 2 Therefor
15 min read
Count Subarrays with product of sum and subarray length less than K Given an array of positive elements arr[] of length N, the task is to count all the subarrays such that the product of the subarray sum and length of the subarray should be strictly less than K. Examples: Input: arr[] = {6, 2, 1, 4, 3}, K = 10Output: 6Explanation: There are six valid subarrays: {6},
13 min read
Find sum of count of duplicate numbers in all subarrays of given array Given an array arr[] of size N. The task it to find the sum of count of duplicate numbers in all subarrays of given array arr[]. For example array {1, 2, 3, 2, 3, 2} has two duplicate elements (i.e, 2 and 3 come more than one time in the array). Examples:Input: N = 2, arr = {3,3}Output: 1Explanation
6 min read
Queries to count subarrays consisting of given integer as the last element Given an array arr[] and an array query[] consisting of Q queries, the task for every ith query is to count the number of subarrays having query[i] as the last element. Note: Subarrays will be considered to be different for different occurrences of X. Examples: Input: arr[] = {1, 5, 4, 5, 6}, Q=3, q
11 min read
Count of subarrays starting or ending at an index i such that arr[i] is maximum in subarray Given an array arr[] consisting of N integers, the task is to find the number of subarrays starting or ending at an index i such that arr[i] is the maximum element of the subarray. Examples: Input: arr[] = {3, 4, 1, 6, 2}Output: 1 3 1 5 1Explanation: The subarray starting or ending at index 0 and wi
11 min read
Count of Subarrays of given Array with median at least X Given an array arr[]of integers with length N and an integer X, the task is to calculate the number of subarrays with median greater than or equal to the given integer X. Examples: Input: N=4, A = [5, 2, 4, 1], X = 4Output: 7Explanation: For subarray [5], median is 5. (>= 4)For subarray [5, 2], m
9 min read
Number of non-decreasing sub-arrays of length greater than or equal to K Given an array arr[] of N elements and an integer K, the task is to find the number of non-decreasing sub-arrays of length greater than or equal to K.Examples: Input: arr[] = {1, 2, 3}, K = 2 Output: 3 {1, 2}, {2, 3} and {1, 2, 3} are the valid subarrays.Input: arr[] = {3, 2, 1}, K = 1 Output: 3 Nai
6 min read