Friday, June 3, 2022
HomeWordPress DevelopmentDepend of Subarrays which Include the Size of that Subarray

Depend of Subarrays which Include the Size of that Subarray


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

  

def findCount(arr, N):

    counts = 0

      

    

    for i in vary(N):

        for j in vary(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))

Time complexity: O(N3)
Auxiliary Area: O(1)

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments