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 = 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++
#include <iostream>
using namespace std;
int findCount( int arr[], int N)
{
int counts = 0;
for ( int i = 0; i < N; i++) {
for ( int j = i + 1; j < N + 1; j++) {
for ( int k = i; k <= j; k++) {
if ((j - i) == arr[k])
counts += 1;
}
}
}
return counts;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int N = 5;
cout << findCount(arr, N);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int findCount( int [] arr, int N)
{
int counts = 0 ;
for ( int i = 0 ; i < N; i++) {
for ( int j = i + 1 ; j < N + 1 ; j++) {
for ( int k = i; k <= j; k++) {
if ((j - i) == arr[k])
counts += 1 ;
}
}
}
return counts;
}
public static void main (String[] args) {
int arr[] = { 1 , 2 , 3 , 4 , 5 };
int N = 5 ;
System.out.println( findCount(arr, N));
}
}
|
Python3
def findCount(arr, N):
counts = 0
for i in range (N):
for j in range (i + 1 , N + 1 ):
if j - i in arr[i: j]:
counts + = 1
return counts
if __name__ = = '__main__' :
arr = [ 1 , 2 , 3 , 4 , 5 ]
N = 5
print (findCount(arr, N))
|
C#
using System;
public class GFG
{
static int findCount( int [] arr, int N)
{
int counts = 0;
for ( int i = 0; i < N; i++) {
for ( int j = i + 1; j < N + 1; j++) {
for ( int k = i; k < j; k++) {
if ((j - i) == arr[k])
counts += 1;
}
}
}
return counts;
}
public static void Main ( string [] args) {
int []arr = { 1, 2, 3, 4, 5 };
int N = 5;
Console.WriteLine(findCount(arr, N));
}
}
|
Javascript
<script>
function findCount(arr, N) {
let counts = 0
for (let i = 0; i < N; i++) {
for (let j = i + 1; j < N + 1; j++) {
for (let k = i; k <= j; k++) {
if (arr[k] == j - i) {
counts++;
}
}
}
}
return counts
}
let arr = [1, 2, 3, 4, 5]
let N = 5
document.write(findCount(arr, N))
</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++
#include <bits/stdc++.h>
using namespace std;
int findCount( int arr[], int N)
{
int counts = 0;
for ( int i = 0; i < N; i++) {
unordered_map< int , int > mm;
for ( int j = i; j < N; j++) {
mm[arr[j]]++;
if (mm[j-i+1])counts++;
}
}
return counts;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int N = 5;
cout << findCount(arr, N);
return 0;
}
|
Java
import java.util.*;
public class Main
{
public static int findCount( int [] arr, int N) {
int counts = 0 ;
for ( int i = 0 ; i < N; i++)
{
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 );
}
if (mm.get(j-i+ 1 ) != null && mm.get(j-i+ 1 ) > 0 ) {
counts++;
}
}
}
return counts;
}
public static void main(String[] args) {
int [] arr = { 1 , 2 , 3 , 4 , 5 };
int N = 5 ;
System.out.println(findCount(arr, N));
}
}
|
Python3
def find_count(arr, N):
counts = 0
for i in range (N):
mm = {}
for j in range (i, N):
if arr[j] in mm:
mm[arr[j]] + = 1
else :
mm[arr[j]] = 1
if mm.get(j - i + 1 , 0 ):
counts + = 1
return counts
arr = [ 1 , 2 , 3 , 4 , 5 ]
N = 5
print (find_count(arr, N))
|
C#
using System;
using System.Collections.Generic;
public class Program
{
public static int findCount( int [] arr, int N) {
int counts = 0;
for ( int i = 0; i < N; i++)
{
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);
}
if (mm.ContainsKey(j - i + 1) && mm[j - i + 1] > 0) {
counts++;
}
}
}
return counts;
}
public static void Main( string [] args) {
int [] arr = { 1, 2, 3, 4, 5 };
int N = 5;
Console.WriteLine(findCount(arr, N));
}
}
|
Javascript
function findCount(arr, N) {
let counts = 0;
for (let i = 0; i < N; i++) {
let mm = {};
for (let j = i; j < N; j++) {
if (mm[arr[j]]) {
mm[arr[j]] += 1;
} else {
mm[arr[j]] = 1;
}
if (mm[j - i + 1]) {
counts += 1;
}
}
}
return counts;
}
let arr = [1, 2, 3, 4, 5];
let N = 5;
console.log(findCount(arr, N));
|
Time Complexity:- O(N^2)
Auxiliary Space:- O(N)
Last Updated :
01 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...