Given an array A[] of size N, the duty is to depend the variety of subarrays of A[] that include the size of that subarray.
Examples:
Enter: A = {10, 11, 1}, N = 3
Output: 1
Clarification: Solely the subarray {1}, with a size 1, incorporates its personal size.Enter: A = [1, 2, 3, 4, 5], N = 5
Output: 9
Clarification: 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} include their very own size.
Method: Comply with the under concept to unravel the issue:
First, kind each subarray of A. Then, verify if the size of the subarray is current in that subarray.
Comply with the steps talked about under to implement the thought:
- 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 verify if the size is current within the subarray.
- If current, then increment the depend.
- The ultimate depend is the required reply.
Under is the implementation for the above strategy:
Python3
|
Time complexity: O(N3)
Auxiliary Area: O(1)