Count of index range [L, R] in Array such that removing all its instances sorts the Array
Last Updated :
12 Oct, 2022
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: 3
Explanation: The good ranges are: (2, 3), (1, 3), (1, 2).
(2, 3) is a good range as the resultant array [9] 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 [7] is sorted (we deleted 9, 8).
Input: N=5, arr[] = {5, 3, 1, 5, 2}
Output: 7
Explanation: 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 [2] is sorted
(1, 4) is a good range as the resultant array [2] is sorted
(1, 5) is a good range as the resultant array [] is sorted
(2, 4) is a good range as the resultant array [2] is sorted
(2, 5) is a good range as the resultant array [] is sorted
(3, 5) is a good range as the resultant array [3] 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.
C++
#include <bits/stdc++.h>
using namespace std;
bool chk_sorted( int l, int r, vector< int > a)
{
vector< int > temp;
unordered_set< int > s;
for ( int i = l; i <= r; i++)
{
temp.push_back(a[i]);
s.insert(a[i]);
}
vector< int > chk;
for ( int i = 0; i < a.size(); i++)
{
if (s.find(a[i]) == s.end())
{
chk.push_back(a[i]);
}
}
vector< int > chk1 = chk;
sort(chk1.begin(), chk1.end());
for ( int i = 0; i < chk.size(); i++)
{
if (chk[i] != chk1[i])
{
return false ;
}
}
return true ;
}
int countp(vector< int > a)
{
int count = 0;
for ( int i = 0; i < a.size(); i++)
{
for ( int j = i + 1; j < a.size(); j++)
{
if (chk_sorted(i, j, a))
{
count += a.size() - j;
break ;
}
}
}
return count;
}
int main()
{
int N = 5;
vector< int > A = { 5, 3, 1, 5, 2 };
cout << (countp(A));
}
|
Java
import java.util.*;
class GFG{
static boolean chk_sorted( int l, int r, int []a)
{
Vector<Integer> temp = new Vector<Integer>();
HashSet<Integer> s = new HashSet<Integer>();
for ( int i = l; i <= r; i++)
{
temp.add(a[i]);
s.add(a[i]);
}
Vector<Integer> chk= new Vector<Integer>();
for ( int i = 0 ; i < a.length; i++)
{
if (!s.contains(a[i]))
{
chk.add(a[i]);
}
}
Vector<Integer> chk1 = new Vector<Integer>(chk);
Collections.sort(chk1);
for ( int i = 0 ; i < chk.size(); i++)
{
if (chk.get(i) != chk1.get(i))
{
return false ;
}
}
return true ;
}
static int countp( int []a)
{
int count = 0 ;
for ( int i = 0 ; i < a.length; i++)
{
for ( int j = i + 1 ; j < a.length; j++)
{
if (chk_sorted(i, j, a))
{
count += a.length - j;
break ;
}
}
}
return count;
}
public static void main(String[] args)
{
int N = 5 ;
int [] A = { 5 , 3 , 1 , 5 , 2 };
System.out.print(countp(A));
}
}
|
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 = 5
A = [ 5 , 3 , 1 , 5 , 2 ]
print (countp(A))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static bool chk_sorted( int l, int r, int [] a)
{
List< int > temp = new List< int >();
HashSet< int > s = new HashSet< int >();
for ( int i = l; i <= r; i++) {
temp.Add(a[i]);
s.Add(a[i]);
}
List< int > chk = new List< int >();
for ( int i = 0; i < a.Length; i++) {
if (!s.Contains(a[i])) {
chk.Add(a[i]);
}
}
List< int > chk1 = new List< int >(chk);
chk1.Sort();
for ( int i = 0; i < chk.Count; i++) {
if (chk[i] != chk1[i]) {
return false ;
}
}
return true ;
}
static int countp( int [] a)
{
int count = 0;
for ( int i = 0; i < a.Length; i++) {
for ( int j = i + 1; j < a.Length; j++) {
if (chk_sorted(i, j, a)) {
count += a.Length - j;
break ;
}
}
}
return count;
}
public static void Main( string [] args)
{
int [] A = { 5, 3, 1, 5, 2 };
Console.WriteLine(countp(A));
}
}
|
Javascript
<script>
function chk_sorted(l, r, a){
l = a.slice(l, r + 1)
let chk = []
for (let i of a){
if (!l.includes(i)){
chk.push(i)
}
}
let chk1 = [...chk]
chk1.sort()
if (chk1.every((val, index) => val == chk[index]))
return true
else
return false
}
function countp(a){
let count = 0
for (let i = 0; i < a.length; i++){
for (let j = i + 1; j < a.length; j++){
if (chk_sorted(i, j, a)){
count += a.length - j
break
}
}
}
return count
}
let N = 5
let A = [5, 3, 1, 5, 2]
document.write(countp(A))
</script>
|
Time Complexity: O(N*N*N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...