Given an array of size N containing all elements as 0 initially, and a Q queries containing range in the form of [L, R]. The task is to modify the array by adding 1 to each element in the range [L, R] for Q queries and then print the size of minimum length subarray containing all unique elements.
Note: 1-based indexing is used in the range [L, R].
Examples:
Input: N = 6, Q[4][] = {{1, 3}, {4, 6}, {3, 4}, {3, 3}}
Output: 3
Explanation:
Initial array: arr[] = { 0, 0, 0, 0, 0, 0 }
Query 1: updated arr[] = { 1, 1, 1, 0, 0, 0 }.
Query 2: updated arr[] = { 1, 1, 1, 1, 1, 1 }.
Query 3: updated arr[] = { 1, 1, 2, 2, 1, 1 }.
Query 4: updated arr[] = { 1, 1, 3, 2, 1, 1 }.
The subarray { 1, 3, 2 } is minimum subarray which contains all unique elements. Thus, the answer is 3.
Input: N = 8, Q[6][] = {{1, 4}, {3, 4}, {4, 5}, {5, 5}, {7, 8}, {8, 8}}
Output: 4
Explanation:
After processing all queries, the array becomes = { 1, 1, 2, 3, 2, 0, 1, 2 }.
The subarray { 3, 2, 0, 1 } is minimum subarray which contains all unique elements. Thus, the answer is 4.
Approach: The idea is to use the concept of Prefix sum array and Two pointers approach to this problem.
- Final Array after the queries can be computed by incrementing the value at the array by 1 at the index L and decrementing the value by 1 at the index R + 1.
Processing of a Query:
arr[L] += 1
arr[R + 1] -= 1
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int subarrayLength( int A[], int R[][2],
int N, int M)
{
for ( int i = 0; i < M; ++i) {
int l = R[i][0], r = R[i][1] + 1;
l--;
r--;
A[l]++;
if (r < N)
A[r]--;
}
for ( int i = 1; i < N; ++i) {
A[i] += A[i - 1];
}
int count = 0;
unordered_set< int > s;
for ( int i = 0; i < N; ++i) {
if (s.find(A[i]) == s.end())
count++;
s.insert(A[i]);
}
vector< int > repeat(count + 1, 0);
int ans = N;
int counter = 0, left = 0, right = 0;
while (right < N) {
int cur_element = A[right];
repeat[cur_element] += 1;
if (repeat[cur_element] == 1)
++counter;
while (counter == count) {
ans = min(ans, right - left + 1);
cur_element = A[left];
repeat[cur_element] -= 1;
++left;
if (repeat[cur_element] == 0)
--counter;
}
++right;
}
return ans;
}
int main()
{
int N = 8, queries = 6;
int Q[][2]
= {
{ 1, 4 }, { 3, 4 }, { 4, 5 },
{ 5, 5 }, { 7, 8 }, { 8, 8 }
};
int A[N] = { 0 };
cout << subarrayLength(A, Q, N, queries);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int subarrayLength( int A[], int R[][],
int N, int M)
{
for ( int i = 0 ; i < M; ++i)
{
int l = R[i][ 0 ], r = R[i][ 1 ] + 1 ;
l--;
r--;
A[l]++;
if (r < N)
A[r]--;
}
for ( int i = 1 ; i < N; ++i)
{
A[i] += A[i - 1 ];
}
int count = 0 ;
HashSet<Integer> s = new HashSet<Integer>();
for ( int i = 0 ; i < N; ++i)
{
if (!s.contains(A[i]))
count++;
s.add(A[i]);
}
int []repeat = new int [count + 1 ];
int ans = N;
int counter = 0 , left = 0 , right = 0 ;
while (right < N)
{
int cur_element = A[right];
repeat[cur_element] += 1 ;
if (repeat[cur_element] == 1 )
++counter;
while (counter == count)
{
ans = Math.min(ans,
right - left + 1 );
cur_element = A[left];
repeat[cur_element] -= 1 ;
++left;
if (repeat[cur_element] == 0 )
--counter;
}
++right;
}
return ans;
}
public static void main(String[] args)
{
int N = 8 , queries = 6 ;
int Q[][] = {{ 1 , 4 }, { 3 , 4 }, { 4 , 5 },
{ 5 , 5 }, { 7 , 8 }, { 8 , 8 }};
int A[] = new int [N];
System.out.print(subarrayLength(A, Q,
N, queries));
}
}
|
Python3
def subarrayLength(A, R, N, M):
for i in range (M):
l = R[i][ 0 ]
r = R[i][ 1 ] + 1
l - = 1
r - = 1
A[l] + = 1
if (r < N):
A[r] - = 1
for i in range ( 1 , N):
A[i] + = A[i - 1 ]
count = 0
s = []
for i in range (N):
if (A[i] not in s):
count + = 1
s.append(A[i])
repeat = [ 0 ] * (count + 1 )
ans = N
counter, left, right = 0 , 0 , 0
while (right < N):
cur_element = A[right]
repeat[cur_element] + = 1
if (repeat[cur_element] = = 1 ):
counter + = 1
while (counter = = count):
ans = min (ans, right - left + 1 )
cur_element = A[left]
repeat[cur_element] - = 1
left + = 1
if (repeat[cur_element] = = 0 ):
counter - = 1
right + = 1
return ans
if __name__ = = "__main__" :
N , queries = 8 , 6
Q = [ [ 1 , 4 ], [ 3 , 4 ], [ 4 , 5 ],
[ 5 , 5 ], [ 7 , 8 ], [ 8 , 8 ] ]
A = [ 0 ] * N
print (subarrayLength(A, Q, N, queries))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int subarrayLength( int []A, int [,]R,
int N, int M)
{
for ( int i = 0; i < M; ++i)
{
int l = R[i, 0], r = R[i, 1] + 1;
l--;
r--;
A[l]++;
if (r < N)
A[r]--;
}
for ( int i = 1; i < N; ++i)
{
A[i] += A[i - 1];
}
int count = 0;
HashSet< int > s = new HashSet< int >();
for ( int i = 0; i < N; ++i)
{
if (!s.Contains(A[i]))
count++;
s.Add(A[i]);
}
int []repeat = new int [count + 1];
int ans = N;
int counter = 0, left = 0, right = 0;
while (right < N)
{
int cur_element = A[right];
repeat[cur_element] += 1;
if (repeat[cur_element] == 1)
++counter;
while (counter == count)
{
ans = Math.Min(ans,
right - left + 1);
cur_element = A[left];
repeat[cur_element] -= 1;
++left;
if (repeat[cur_element] == 0)
--counter;
}
++right;
}
return ans;
}
public static void Main(String[] args)
{
int N = 8, queries = 6;
int [,]Q = { { 1, 4 }, { 3, 4 }, { 4, 5 },
{ 5, 5 }, { 7, 8 }, { 8, 8 } };
int []A = new int [N];
Console.Write(subarrayLength(A, Q,
N, queries));
}
}
|
Javascript
<script>
function subarrayLength(A, R, N, M)
{
for (let i = 0; i < M; ++i)
{
let l = R[i][0], r = R[i][1] + 1;
l--;
r--;
A[l]++;
if (r < N)
A[r]--;
}
for (let i = 1; i < N; ++i)
{
A[i] += A[i - 1];
}
let count = 0;
let s = new Set();
for (let i = 0; i < N; ++i)
{
if (!s.has(A[i]))
count++;
s.add(A[i]);
}
let repeat = Array.from({length: count + 1}, (_, i) => 0);
let ans = N;
let counter = 0, left = 0, right = 0;
while (right < N)
{
let cur_element = A[right];
repeat[cur_element] += 1;
if (repeat[cur_element] == 1)
++counter;
while (counter == count)
{
ans = Math.min(ans,
right - left + 1);
cur_element = A[left];
repeat[cur_element] -= 1;
++left;
if (repeat[cur_element] == 0)
--counter;
}
++right;
}
return ans;
}
let N = 8, queries = 6;
let Q = [[ 1, 4 ], [ 3, 4 ], [ 4, 5 ],
[ 5, 5 ], [ 7, 8 ], [ 8, 8 ]];
let A = Array.from({length: N}, (_, i) => 0);
document.write(subarrayLength(A, Q,
N, queries));
</script>
|
Time Complexity: O(N)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!