Distinct elements in subarray using Mo’s Algorithm
Given an array ‘a[]’ of size n and number of queries q. Each query can be represented by two integers l and r. Your task is to print the number of distinct integers in the subarray l to r. Given a[i] <=
*** QuickLaTeX cannot compile formula:
10^6
*** Error message:
Cannot connect to QuickLaTeX server: cURL error 28: Connection timed out after 10000 milliseconds
Please make sure your server/PHP settings allow HTTP requests to external resources ("allow_url_fopen", etc.)
These links might help in finding solution:
http://wordpress.org/extend/plugins/core-control/
http://wordpress.org/support/topic/an-unexpected-http-error-occurred-during-the-api-request-on-wordpress-3?replies=37
Examples :
Input : a[] = {1, 1, 2, 1, 2, 3}
q = 3
0 4
1 3
2 5
Output : 2
2
3
In query 1, number of distinct integers
in a[0...4] is 2 (1, 2)
In query 2, number of distinct integers
in a[1..3] is 2 (1, 2)
In query 3, number of distinct integers
in a[2..5] is 3 (1, 2, 3)
Input : a[] = {7, 3, 5, 9, 7, 6, 4, 3, 2}
q = 4
1 5
0 4
0 7
1 8
Output : 5
4
6
7
Let a[0…n-1] be input array and q[0..m-1] be array of queries.
Approach :
- Sort all queries in a way that queries with L values from 0 to
*** QuickLaTeX cannot compile formula:
\sqrt(n) - 1
*** Error message:
Cannot connect to QuickLaTeX server: cURL error 28: Connection timed out after 10000 milliseconds
Please make sure your server/PHP settings allow HTTP requests to external resources ("allow_url_fopen", etc.)
These links might help in finding solution:
http://wordpress.org/extend/plugins/core-control/
http://wordpress.org/support/topic/an-unexpected-http-error-occurred-during-the-api-request-on-wordpress-3?replies=37
are put together, then all queries from *** QuickLaTeX cannot compile formula:
\sqrt(n)
*** Error message:
Cannot connect to QuickLaTeX server: cURL error 28: Connection timed out after 10001 milliseconds
Please make sure your server/PHP settings allow HTTP requests to external resources ("allow_url_fopen", etc.)
These links might help in finding solution:
http://wordpress.org/extend/plugins/core-control/
http://wordpress.org/support/topic/an-unexpected-http-error-occurred-during-the-api-request-on-wordpress-3?replies=37
to *** QuickLaTeX cannot compile formula:
2*\sqrt(n) - 1
*** Error message:
Cannot connect to QuickLaTeX server: cURL error 28: Connection timed out after 10001 milliseconds
Please make sure your server/PHP settings allow HTTP requests to external resources ("allow_url_fopen", etc.)
These links might help in finding solution:
http://wordpress.org/extend/plugins/core-control/
http://wordpress.org/support/topic/an-unexpected-http-error-occurred-during-the-api-request-on-wordpress-3?replies=37
, and so on. All queries within a block are sorted in increasing order of R values. - Initialize an array freq[] of size
*** QuickLaTeX cannot compile formula:
10^6
*** Error message:
Cannot connect to QuickLaTeX server: cURL error 28: Connection timed out after 10001 milliseconds
Please make sure your server/PHP settings allow HTTP requests to external resources ("allow_url_fopen", etc.)
These links might help in finding solution:
http://wordpress.org/extend/plugins/core-control/
http://wordpress.org/support/topic/an-unexpected-http-error-occurred-during-the-api-request-on-wordpress-3?replies=37
with 0 . freq[] array keep count of frequencies of all the elements in lying in a given range. - Process all queries one by one in a way that every query uses number of different elements and frequency array computed in previous query and stores the result in structure.
- Let ‘curr_Diff_element’ be number of different elements of previous query.
- Remove extra elements of previous query. For example if previous query is [0, 8] and current query is [3, 9], then remove a[0], a[1] and a[2]
- Add new elements of current query. In the same example as above, add a[9].
- Sort the queries in the same order as they were provided earlier and print their stored results
Adding elements()
- Increase the frequency of element to be added(freq[a[i]]) by 1.
- If frequency of element a[i] is 1.Increase curr_diff_element by 1 as 1 new element has been added in range.
Removing elements()
- Decrease frequency of element to be removed (a[i]) by 1.
- if frequency of an element a[i] is 0.Just decrease curr_diff_element by 1 as 1 element has been completely removed from the range.
Note :
In this algorithm, in step 2, index variable for R change at most O(n *
*** QuickLaTeX cannot compile formula:
\sqrt(n)
*** Error message:
Cannot connect to QuickLaTeX server: cURL error 28: Connection timed out after 10001 milliseconds
Please make sure your server/PHP settings allow HTTP requests to external resources ("allow_url_fopen", etc.)
These links might help in finding solution:
http://wordpress.org/extend/plugins/core-control/
http://wordpress.org/support/topic/an-unexpected-http-error-occurred-during-the-api-request-on-wordpress-3?replies=37
) times throughout the run and same for L changes its value at most O(m * *** QuickLaTeX cannot compile formula:
\sqrt(n)
*** Error message:
Cannot connect to QuickLaTeX server: cURL error 28: Connection timed out after 10001 milliseconds
Please make sure your server/PHP settings allow HTTP requests to external resources ("allow_url_fopen", etc.)
These links might help in finding solution:
http://wordpress.org/extend/plugins/core-control/
http://wordpress.org/support/topic/an-unexpected-http-error-occurred-during-the-api-request-on-wordpress-3?replies=37
) times. All these bounds are possible only because sorted queries first in blocks of *** QuickLaTeX cannot compile formula:
\sqrt(n)
*** Error message:
Cannot connect to QuickLaTeX server: cURL error 28: Connection timed out after 10001 milliseconds
Please make sure your server/PHP settings allow HTTP requests to external resources ("allow_url_fopen", etc.)
These links might help in finding solution:
http://wordpress.org/extend/plugins/core-control/
http://wordpress.org/support/topic/an-unexpected-http-error-occurred-during-the-api-request-on-wordpress-3?replies=37
size. The preprocessing part takes O(m Log m) time. Processing all queries takes O(n * *** QuickLaTeX cannot compile formula:
\sqrt(n)
*** Error message:
Cannot connect to QuickLaTeX server: cURL error 28: Connection timed out after 10000 milliseconds
Please make sure your server/PHP settings allow HTTP requests to external resources ("allow_url_fopen", etc.)
These links might help in finding solution:
http://wordpress.org/extend/plugins/core-control/
http://wordpress.org/support/topic/an-unexpected-http-error-occurred-during-the-api-request-on-wordpress-3?replies=37
) + O(m * *** QuickLaTeX cannot compile formula:
\sqrt(n)
*** Error message:
Cannot connect to QuickLaTeX server: cURL error 28: Connection timed out after 10000 milliseconds
Please make sure your server/PHP settings allow HTTP requests to external resources ("allow_url_fopen", etc.)
These links might help in finding solution:
http://wordpress.org/extend/plugins/core-control/
http://wordpress.org/support/topic/an-unexpected-http-error-occurred-during-the-api-request-on-wordpress-3?replies=37
) = O((m+n) **** QuickLaTeX cannot compile formula:
\sqrt(n)
*** Error message:
Cannot connect to QuickLaTeX server: cURL error 28: Connection timed out after 10001 milliseconds
Please make sure your server/PHP settings allow HTTP requests to external resources ("allow_url_fopen", etc.)
These links might help in finding solution:
http://wordpress.org/extend/plugins/core-control/
http://wordpress.org/support/topic/an-unexpected-http-error-occurred-during-the-api-request-on-wordpress-3?replies=37
) time.
Below is the implementation of above approach :
CPP
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1000000;
int block;
struct Query {
int L, R, index, result;
};
bool compare(Query x, Query y)
{
if (x.L / block != y.L / block)
return x.L / block < y.L / block;
return x.R < y.R;
}
bool compare1(Query x, Query y)
{
return x.index < y.index;
}
void queryResults( int a[], int n, Query q[], int m)
{
block = ( int ) sqrt (n);
sort(q, q + m, compare);
int currL = 0, currR = 0;
int curr_Diff_elements = 0;
int freq[MAX] = { 0 };
for ( int i = 0; i < m; i++) {
int L = q[i].L, R = q[i].R;
while (currL < L) {
freq[a[currL]]--;
if (freq[a[currL]] == 0)
curr_Diff_elements--;
currL++;
}
while (currL > L) {
freq[a[currL - 1]]++;
if (freq[a[currL - 1]] == 1)
curr_Diff_elements++;
currL--;
}
while (currR <= R) {
freq[a[currR]]++;
if (freq[a[currR]] == 1)
curr_Diff_elements++;
currR++;
}
while (currR > R + 1) {
freq[a[currR - 1]]--;
if (freq[a[currR - 1]] == 0)
curr_Diff_elements--;
currR--;
}
q[i].result = curr_Diff_elements;
}
}
void printResults(Query q[], int m)
{
sort(q, q + m, compare1);
for ( int i = 0; i < m; i++) {
cout << "Number of different elements" <<
" in range " << q[i].L << " to "
<< q[i].R << " are " << q[i].result << endl;
}
}
int main()
{
int a[] = { 1, 1, 2, 1, 3, 4, 5, 2, 8 };
int n = sizeof (a) / sizeof (a[0]);
Query q[] = { { 0, 4, 0, 0 }, { 1, 3, 1, 0 },
{ 2, 4, 2, 0 } };
int m = sizeof (q) / sizeof (q[0]);
queryResults(a, n, q, m);
printResults(q, m);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static class Query{
int L, R, index, result;
Query( int l, int r, int i, int res){
this .L = l;
this .R = r;
this .index = i;
this .result = res;
}
}
public static int MAX = 1000000 ;
public static int block;
public static void queryResults( int a[], int n, Query q[], int m)
{
block = ( int )Math.sqrt(n);
Arrays.sort(q, new Comparator<Query>() {
public int compare(Query x, Query y) {
if (x.L / block != y.L / block)
return x.L / block < y.L / block? 1 :- 1 ;
return x.R < y.R? 1 :- 1 ;
}
});
int currL = 0 , currR = 0 ;
int curr_Diff_elements = 0 ;
int freq[] = new int [MAX];
Arrays.fill(freq, 0 );
for ( int i = 0 ; i < m; i++) {
int L = q[i].L, R = q[i].R;
while (currL < L) {
freq[a[currL]]--;
if (freq[a[currL]] == 0 ) {
curr_Diff_elements--;
}
currL++;
}
while (currL > L) {
freq[a[currL - 1 ]]++;
if (freq[a[currL - 1 ]] == 1 ) {
curr_Diff_elements++;
}
currL--;
}
while (currR <= R) {
freq[a[currR]]++;
if (freq[a[currR]] == 1 ){
curr_Diff_elements++;
}
currR++;
}
while (currR > R + 1 ) {
freq[a[currR - 1 ]]--;
if (freq[a[currR - 1 ]] == 0 ){
curr_Diff_elements--;
}
currR--;
}
q[i].result = curr_Diff_elements;
}
}
public static void printResults(Query q[], int m)
{
Arrays.sort(q, new Comparator<Query>() {
public int compare(Query x, Query y) {
return (x.index < y.index)?- 1 : 1 ;
}
});
for ( int i = 0 ; i < m; i++) {
System.out.println( "Number of different elements in range " + q[i].L + " to " + q[i].R + " are " + q[i].result);
}
}
public static void main (String[] args) {
int a[] = { 1 , 1 , 2 , 1 , 3 , 4 , 5 , 2 , 8 };
int n = a.length;
Query q[] = new Query[ 3 ];
q[ 0 ]= new Query( 0 , 4 , 0 , 0 );
q[ 1 ] = new Query( 1 , 3 , 1 , 0 );
q[ 2 ] = new Query( 2 , 4 , 2 , 0 );
int m = q.length;
queryResults(a, n, q, m);
printResults(q, m);
}
}
|
Python3
import math
MAX = 1000000
class Query:
def __init__( self , L, R, index, result):
self .L = L
self .R = R
self .index = index
self .result = result
def compare(x, y):
if x.L / / block ! = y.L / / block:
return x.L / / block < y.L / / block
return x.R < y.R
def compare1(x, y):
return x.index < y.index
def queryResults(a, n, q, m):
global block
block = int (math.sqrt(n))
q.sort(key = lambda x: (x.L / / block, x.R))
currL = 0
currR = 0
curr_Diff_elements = 0
freq = [ 0 ] * ( MAX + 1 )
for i in range (m):
L, R = q[i].L, q[i].R
while currL < L:
freq[a[currL]] - = 1
if freq[a[currL]] = = 0 :
curr_Diff_elements - = 1
currL + = 1
while currL > L:
freq[a[currL - 1 ]] + = 1
if freq[a[currL - 1 ]] = = 1 :
curr_Diff_elements + = 1
currL - = 1
while currR < = R:
freq[a[currR]] + = 1
if freq[a[currR]] = = 1 :
curr_Diff_elements + = 1
currR + = 1
while currR > R + 1 :
freq[a[currR - 1 ]] - = 1
if freq[a[currR - 1 ]] = = 0 :
curr_Diff_elements - = 1
currR - = 1
q[i].result = curr_Diff_elements
def printResults(q, m):
q.sort(key = lambda x: x.index)
for i in range (m):
print ( "Number of different elements in range" , q[i].L, "to" , q[i].R, "are" , q[i].result)
if __name__ = = "__main__" :
a = [ 1 , 1 , 2 , 1 , 3 , 4 , 5 , 2 , 8 ]
n = len (a)
q = [Query( 0 , 4 , 0 , 0 ), Query( 1 , 3 , 1 , 0 ), Query( 2 , 4 , 2 , 0 )]
m = len (q)
queryResults(a, n, q, m)
printResults(q, m)
|
OutputNumber of different elements in range 0 to 4 are 3
Number of different elements in range 1 to 3 are 2
Number of different elements in range 2 to 4 are 3
Last Updated :
10 Feb, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...