Count of Subarrays which Contain the Length of that Subarray
Last Updated :
01 Mar, 2023
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 = 3
Output: 1
Explanation: Only the subarray {1}, with a length 1, contains its own length.
Input: A = [1, 2, 3, 4, 5], N = 5
Output: 9
Explanation: The subarrays {1}, {1, 2}, {2, 3}, {1, 2, 3}, {2, 3, 4}, {3, 4, 5},
{1, 2, 3, 4}, {2, 3, 4, 5}, {1, 2, 3, 4, 5} contain their own length.
Approach: Follow the below idea to solve the problem:
First, form each and every subarray of A. Then, check if the length of the subarray is present in that subarray.
Follow the steps mentioned below to implement the idea:
- Iterate over the array from i = 0 to N:
- Iterate in a nested loop from j = i to N:
- The subarray created is from i to j.
- Traverse the subarray and check if the length is present in the subarray.
- If present, then increment the count.
- The final count is the required answer.
Below is the implementation for the above approach:
C++
// C++ code to implement the approach
#include <iostream>
using namespace std;
// Function to find the count of the subarrays
// that contain their own length
int findCount(int arr[], int N)
{
int counts = 0;
// Forming all possible subarrays
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N + 1; j++) {
// Checking if the length is present
// in the subarray
for (int k = i; k <= j; k++) {
if ((j - i) == arr[k])
counts += 1;
}
}
}
return counts;
}
// Driver Code
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int N = 5;
// Function Call
cout << findCount(arr, N);
return 0;
}
// This code is contributed by Rohit Pradhan
Java
// Java code to implement the approach
import java.io.*;
class GFG
{
// Function to find the count of the subarrays
// that contain their own length
static int findCount(int[] arr, int N)
{
int counts = 0;
// Forming all possible subarrays
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N + 1; j++) {
// Checking if the length is present
// in the subarray
for (int k = i; k <= j; k++) {
if ((j - i) == arr[k])
counts += 1;
}
}
}
return counts;
}
// Driver Code
public static void main (String[] args) {
int arr[] = { 1, 2, 3, 4, 5 };
int N = 5;
// Function Call
System.out.println( findCount(arr, N));
}
}
// This code is contributed by hrithikgarg03188.
Python3
# Python3 code to implement the approach
# Function to find the count of the subarrays
# that contain their own length
def findCount(arr, N):
counts = 0
# Forming all possible subarrays
for i in range(N):
for j in range(i + 1, N + 1):
# Checking if the length is present
# in the subarray
if j - i in arr[i: j]:
counts += 1
return counts
# Driver Code
if __name__ == '__main__':
arr = [1, 2, 3, 4, 5]
N = 5
# Function Call
print(findCount(arr, N))
C#
// C# code to implement the above approach
using System;
public class GFG
{
// Function to find the count of the subarrays
// that contain their own length
static int findCount(int[] arr, int N)
{
int counts = 0;
// Forming all possible subarrays
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N + 1; j++) {
// Checking if the length is present
// in the subarray
for (int k = i; k < j; k++) {
if ((j - i) == arr[k])
counts += 1;
}
}
}
return counts;
}
// Driver Code
public static void Main (string[] args) {
int []arr = { 1, 2, 3, 4, 5 };
int N = 5;
// Function Call
Console.WriteLine(findCount(arr, N));
}
}
// This code is contributed by AnkThon
JavaScript
<script>
// JavaScript code for the above approach
// Function to find the count of the subarrays
// that contain their own length
function findCount(arr, N) {
let counts = 0
// Forming all possible subarrays
for (let i = 0; i < N; i++) {
for (let j = i + 1; j < N + 1; j++) {
// Checking if the length is present
// in the subarray
for (let k = i; k <= j; k++) {
if (arr[k] == j - i) {
counts++;
}
}
}
}
return counts
}
// Driver Code
let arr = [1, 2, 3, 4, 5]
let N = 5
// Function Call
document.write(findCount(arr, N))
// This code is contributed by Potta Lokesh
</script>
Time complexity: O(N3)
Auxiliary Space: O(1)
Efficient Approach:-
- As the previous approach running in O(N^3) time
- We can optimize the 3rd for loop of that approach by observing that we are finding length only in that loop but if we use hashmap to store element for each subarray then we can find the length in O(1) time
- So in this approach we will be using hashmap to store elements and then find the length of subarray present or not
Implementation:-
C++
// C++ code to implement the approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the count of the subarrays
// that contain their own length
int findCount(int arr[], int N)
{
int counts = 0;
// Forming all possible subarrays
for (int i = 0; i < N; i++) {
//map to store frequency
unordered_map<int,int> mm;
for (int j = i; j < N; j++) {
mm[arr[j]]++;
//finding length is present or not
if(mm[j-i+1])counts++;
}
}
return counts;
}
// Driver Code
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int N = 5;
// Function Call
cout << findCount(arr, N);
return 0;
}
// This code is contributed by shubhamrajput6156
Java
// Java code to implement the approach
import java.util.*;
public class Main
{
// Function to find the count of the subarrays
// that contain their own length
public static int findCount(int[] arr, int N) {
int counts = 0;
// Forming all possible subarrays
for (int i = 0; i < N; i++)
{
// Map to store frequency
Map<Integer, Integer> mm = new HashMap<>();
for (int j = i; j < N; j++) {
if (mm.containsKey(arr[j])) {
mm.put(arr[j], mm.get(arr[j]) + 1);
} else {
mm.put(arr[j], 1);
}
// finding length is present or not
if (mm.get(j-i+1) != null && mm.get(j-i+1) > 0) {
counts++;
}
}
}
return counts;
}
// Driver Code
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
int N = 5;
// Function Call
System.out.println(findCount(arr, N));
}
}
Python3
# Python code to implement the approach
# Function to find the count of the subarrays
# that contain their own length
def find_count(arr, N):
counts = 0
# Forming all possible subarrays
for i in range(N):
# dict to store frequency
mm = {}
for j in range(i, N):
if arr[j] in mm:
mm[arr[j]] += 1
else:
mm[arr[j]] = 1
# finding length is present or not
if mm.get(j-i+1, 0):
counts += 1
return counts
# Driver Code
arr = [1, 2, 3, 4, 5]
N = 5
# Function Call
print(find_count(arr, N))
C#
//C# equivalent code
using System;
using System.Collections.Generic;
public class Program
{
// Function to find the count of the subarrays
// that contain their own length
public static int findCount(int[] arr, int N) {
int counts = 0;
// Forming all possible subarrays
for (int i = 0; i < N; i++)
{
// Dictionary to store frequency
Dictionary<int, int> mm = new Dictionary<int, int>();
for (int j = i; j < N; j++) {
if (mm.ContainsKey(arr[j])) {
mm[arr[j]] = mm[arr[j]] + 1;
} else {
mm.Add(arr[j], 1);
}
// finding length is present or not
if (mm.ContainsKey(j - i + 1) && mm[j - i + 1] > 0) {
counts++;
}
}
}
return counts;
}
// Driver Code
public static void Main(string[] args) {
int[] arr = { 1, 2, 3, 4, 5 };
int N = 5;
// Function Call
Console.WriteLine(findCount(arr, N));
}
}
JavaScript
function findCount(arr, N) {
let counts = 0;
// Forming all possible subarrays
for (let i = 0; i < N; i++) {
// object to store frequency
let mm = {};
for (let j = i; j < N; j++) {
if (mm[arr[j]]) {
mm[arr[j]] += 1;
} else {
mm[arr[j]] = 1;
}
// finding length is present or not
if (mm[j - i + 1]) {
counts += 1;
}
}
}
return counts;
}
// Driver Code
let arr = [1, 2, 3, 4, 5];
let N = 5;
// Function Call
console.log(findCount(arr, N));
Time Complexity:- O(N^2)
Auxiliary Space:- O(N)
Similar Reads
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 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
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 of Subarrays in an array containing numbers from 1 to the length of subarray 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
7 min read
Count of subarrays which start and end with the same element Given an array A of size N where the array elements contain values from 1 to N with duplicates, the task is to find the total number of subarrays that start and end with the same element. Examples: Input: A[] = {1, 2, 1, 5, 2} Output: 7 Explanation: Total 7 sub-array of the given array are {1}, {2},
10 min read
Count of elements which is the sum of a subarray of the given Array Given an array arr[], the task is to count elements in an array such that there exists a subarray whose sum is equal to this element.Note: Length of subarray must be greater than 1. Examples: Input: arr[] = {1, 2, 3, 4, 5, 6, 7} Output: 4 Explanation: There are 4 such elements in array - arr[2] = 3
7 min read
Count of subarrays which contains a given number exactly K times Given an array A[] of N elements consisting of values from 1 to N with duplicates, the task is to find the total number of subarrays that contain a given number num exactly K times. Examples: Input: A[] = {1, 2, 1, 5, 1}, num = 1, K = 2 Output: 2 Explanation: Subarrays {1, 2, 1, 5}, {1, 2, 1}, {2, 1
8 min read
Count of Subarray with B times Sum equal to C times Length Given, an array A[] of N integers, and also given two positive integers B and C, the task is to find the number of subarrays, such that B* Sum of elements of the subarray = C * Length of the subarray (here * describes multiplication). Examples: Input: N = 3, A[] = {3, -1, 1}, B = 1, C = 1Output: 3Ex
7 min read
Count of subsequences consisting of the same element Given an array A[] consisting of N integers, the task is to find the total number of subsequence which contain only one distinct number repeated throughout the subsequence. Examples: Input: A[] = {1, 2, 1, 5, 2} Output: 7 Explanation: Subsequences {1}, {2}, {1}, {5}, {2}, {1, 1} and {2, 2} satisfy t
4 min read
Count subarrays with same even and odd elements Given an array of N integers, count number of even-odd subarrays. An even - odd subarray is a subarray that contains the same number of even as well as odd integers. Examples : Input : arr[] = {2, 5, 7, 8} Output : 3 Explanation : There are total 3 even-odd subarrays. 1) {2, 5} 2) {7, 8} 3) {2, 5, 7
15+ min read