Wednesday, 27 Oct 2021

# Count of index range [L, R] in Array such that removing all its instances sorts the Array

Given an array arr[] of length N, the task is to find the number of Good Ranges in the array arr[].A Good Range is defined as the range of left and right indices, i.e, [L. R] in the array arr[] such that by removing all the numbers in the range [L, R] of the array arr[] and the appearances of those elements outside the range, the array arr[] becomes sorted in non-decreasing order.Example:Input: N=3, arr[] = {9, 8, 7}Output: 3Explanation: The good ranges are: (2, 3), (1, 3), (1, 2).(2, 3) is a good range as the resultant array  is sorted (we deleted 8, 7).(1, 3) is a good range as the resultant array [] which is sorted (we deleted 9, 8, 7)(1, 2) is a good range as the resultant array  is sorted (we deleted 9, 8).Input: N=5, arr[] = {5, 3, 1, 5, 2}Output: 7Explanation: The good ranges are: (1, 2), (1, 3), (1, 4), (1, 5), (2, 4), (2, 5), (3, 5).(1, 2) is a good range as the resultant array [1, 2] is sorted (1, 3) is a good range as the resultant array  is sorted (1, 4) is a good range as the resultant array  is sorted (1, 5) is a good range as the resultant array [] is sorted (2, 4) is a good range as the resultant array  is sorted (2, 5) is a good range as the resultant array [] is sorted (3, 5) is a good range as the resultant array  is sorted Approach: The approach is to find every subarray [l, r] and check if the remaining array is sorted or not. If the array is sorted, then, with the left part of the range at l and right part from r to the end, every subarray will be the answer. Below is the implementation of the above approach:Initialize the variable count as 0 to store the number of such subarrays.Define a function chk_sorted(l, r, a) to check if the remaining array a[] is sorted or not:Iterate over the range [0, N] using variable i and perform the following steps:Iterate over the range [i+1, N] using variable i and perform the following steps:Call the function chk_sorted(i, j, a) and if the function returns true, then, increase the value of count by len(a)-j and break the loop.Return the value of count as the answer.Below is the implementation of the above approach.Python3  def chk_sorted(l, r, a):              l = list(a[l:r + 1])      chk = []    for i in a:                        if(i not in l):            chk.append(i)    chk1 = list(chk)    chk1.sort()        if(chk1 == chk):        return True    else:        return False    def countp(a):          count = 0          for i in range(len(a)):        for j in range(i + 1, len(a)):                          if(chk_sorted(i, j, a)):                                                  count += len(a)-j                break    return count    N = 5A = [5, 3, 1, 5, 2]print(countp(A))Time Complexity: O(N*N*N)Auxiliary Space: O(N)Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.