Print all Repetitive elements in given Array within value range [A, B] for Q queries
Last Updated :
26 Oct, 2023
Given an array arr[] of size N, and Q queries of the form [A, B], the task is to find all the unique elements from the array which are repetitive and their values lie between A and B (both inclusive) for each of the Q queries.
Examples:
Input: arr[] = { 1, 5, 1, 2, 3, 3, 4, 0, 0 }, Q = 2, queries={ { 1, 3 }, { 0, 0 } }
Output: {3, 1}, { 0 }
Explanation: For the first query the elements 1 and 3 lie in the given range and occurs more than once.
For the second queryonly 0 lies in the range and is repetitive in nature.
Input: arr[] = {1, 5, 1, 2, 3, 4, 0, 0}, Q = 1, queries={ { 1, 2 } }
Output: 1
Naive Approach: The basic approach to solve this problem is to use nested loops. A traversal of all elements is performed by the outer loop, and checking whether the element picked by the outer loop appears anywhere else is performed by the inner loop. Then, if it appears elsewhere, check if it is lying within the range of [A, B]. If yes, then print it.
Below is the implementation of the above approach.
C++14
#include <bits/stdc++.h>
using namespace std;
void printElements( int arr[], int N, int A, int B)
{
bool flag = false ;
for ( int i = 0; i < N; i++) {
if (arr[i] >= A && arr[i] <= B) {
bool repeat = false ;
for ( int j = i + 1; j < N; j++) {
if (arr[i] == arr[j]) {
repeat = true ;
break ;
}
}
if (repeat) {
if (flag)
cout << " " ;
cout << arr[i];
flag = true ;
}
}
}
if (!flag)
cout << -1;
}
void findElements( int arr[], int N, int Q,
vector<pair< int , int > >& queries)
{
for ( int i = 0; i < Q; i++) {
int A = queries[i].first;
int B = queries[i].second;
printElements(arr, N, A, B);
cout << endl;
}
}
int main()
{
int arr[] = { 1, 5, 1, 2, 3, 3, 4, 0, 0 };
int N = sizeof (arr) / sizeof (arr[0]);
int Q = 2;
vector<pair< int , int > > queries
= { { 1, 3 }, { 0, 0 } };
findElements(arr, N, Q, queries);
return 0;
}
|
Java
import java.util.*;
public class Main {
static class Pair<F, S> {
F first;
S second;
Pair(F first, S second) {
this .first = first;
this .second = second;
}
}
static void printElements( int [] arr, int N, int A, int B) {
boolean flag = false ;
for ( int i = 0 ; i < N; i++) {
if (arr[i] >= A && arr[i] <= B) {
boolean repeat = false ;
for ( int j = i + 1 ; j < N; j++) {
if (arr[i] == arr[j]) {
repeat = true ;
break ;
}
}
if (repeat) {
if (flag)
System.out.print( " " );
System.out.print(arr[i]);
flag = true ;
}
}
}
if (!flag)
System.out.print(- 1 );
System.out.println();
}
static void findElements( int [] arr, int N, int Q,
ArrayList<Pair<Integer, Integer>> queries) {
for ( int i = 0 ; i < Q; i++) {
int A = queries.get(i).first;
int B = queries.get(i).second;
printElements(arr, N, A, B);
}
}
public static void main(String[] args) {
int [] arr = { 1 , 5 , 1 , 2 , 3 , 3 , 4 , 0 , 0 };
int N = arr.length;
int Q = 2 ;
ArrayList<Pair<Integer, Integer>> queries = new ArrayList<>();
queries.add( new Pair<>( 1 , 3 ));
queries.add( new Pair<>( 0 , 0 ));
findElements(arr, N, Q, queries);
}
}
|
Python3
def printElements(arr, A, B):
flag = False
for i in range ( len (arr)):
if A < = arr[i] < = B:
repeat = False
for j in range (i + 1 , len (arr)):
if arr[i] = = arr[j]:
repeat = True
break
if repeat:
if flag:
print ( " " , end = "")
print (arr[i], end = "")
flag = True
if not flag:
print ( - 1 )
def findElements(arr, queries):
for query in queries:
A, B = query
printElements(arr, A, B)
print ()
if __name__ = = "__main__" :
arr = [ 1 , 5 , 1 , 2 , 3 , 3 , 4 , 0 , 0 ]
Q = 2
queries = [( 1 , 3 ), ( 0 , 0 )]
findElements(arr, queries)
|
C#
using System;
using System.Collections.Generic;
class Program
{
static void PrintElements( int [] arr, int N, int A, int B)
{
bool flag = false ;
for ( int i = 0; i < N; i++)
{
if (arr[i] >= A && arr[i] <= B)
{
bool repeat = false ;
for ( int j = i + 1; j < N; j++)
{
if (arr[i] == arr[j])
{
repeat = true ;
break ;
}
}
if (repeat)
{
if (flag)
Console.Write( " " );
Console.Write(arr[i]);
flag = true ;
}
}
}
if (!flag)
Console.Write(-1);
Console.WriteLine();
}
static void FindElements( int [] arr, int N, int Q, List<Tuple< int , int >> queries)
{
for ( int i = 0; i < Q; i++)
{
int A = queries[i].Item1;
int B = queries[i].Item2;
PrintElements(arr, N, A, B);
}
}
static void Main()
{
int [] arr = { 1, 5, 1, 2, 3, 3, 4, 0, 0 };
int N = arr.Length;
int Q = 2;
List<Tuple< int , int >> queries = new List<Tuple< int , int >>
{
Tuple.Create(1, 3),
Tuple.Create(0, 0)
};
FindElements(arr, N, Q, queries);
}
}
|
Javascript
function printElements(arr, N, A, B) {
let flag = false ;
for (let i = 0; i < N; i++) {
if (arr[i] >= A && arr[i] <= B) {
let repeat = false ;
for (let j = i + 1; j < N; j++) {
if (arr[i] == arr[j]) {
repeat = true ;
break ;
}
}
if (repeat) {
if (flag)
console.log( " " );
console.log(arr[i].toString());
flag = true ;
}
}
}
if (!flag)
console.log( "-1" );
}
function findElements(arr, N, Q, queries) {
for (let i = 0; i < Q; i++) {
let A = queries[i][0];
let B = queries[i][1];
printElements(arr, N, A, B);
console.log();
}
}
let arr = [1, 5, 1, 2, 3, 3, 4, 0, 0];
let N = arr.length;
let Q = 2;
let queries = [[1, 3], [0, 0]];
findElements(arr, N, Q, queries);
|
Time Complexity: O(Q * N2)
Auxiliary Space: O(1)
Efficient Approach: An efficient approach is based on the idea of a hash map to store the frequency of all the elements. Follow the steps mentioned below:
- Traverse the hash map, and check if the frequency of an element is greater than 1.
- If yes, then check if the element is present in the range of [A, B] or not.
- If yes, then print it else skip the element.
- Continue the above procedure till all the elements of the hash map are traversed.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
unordered_map< int , int > freq;
void storeFrequency( int arr[], int n)
{
for ( int i = 0; i < n; i++) {
freq[arr[i]]++;
}
}
void printElements( int a, int b)
{
for ( auto it = freq.begin(); it != freq.end(); it++) {
if ((it->second) > 1) {
int value = it->first;
if (value >= a && value <= b) {
cout << value << " " ;
}
}
}
}
void findElements( int arr[], int N, int Q,
vector<pair< int , int > >& queries)
{
storeFrequency(arr, N);
for ( int i = 0; i < Q; i++) {
int A = queries[i].first;
int B = queries[i].second;
printElements(A, B);
cout << endl;
}
}
int main()
{
int arr[] = { 1, 5, 1, 2, 3, 3, 4, 0, 0 };
int N = sizeof (arr) / sizeof (arr[0]);
int Q = 2;
vector<pair< int , int > > queries = { { 1, 3 }, { 0, 0 } };
findElements(arr, N, Q, queries);
return 0;
}
|
Java
import java.util.*;
class GFG{
static HashMap<Integer,Integer> freq= new HashMap<Integer,Integer> ();
static void storeFrequency( int arr[], int n)
{
for ( int i = 0 ; i < n; i++){
if (freq.containsKey(arr[i])){
freq.put(arr[i], freq.get(arr[i])+ 1 );
}
else {
freq.put(arr[i], 1 );
}
}
}
static void printElements( int a, int b)
{
for (Map.Entry<Integer,Integer> it : freq.entrySet()) {
if ((it.getValue()) > 1 ) {
int value = it.getKey();
if (value >= a && value <= b) {
System.out.print(value+ " " );
}
}
}
}
static void findElements( int arr[], int N, int Q,
int [][] queries)
{
storeFrequency(arr, N);
for ( int i = 0 ; i < Q; i++) {
int A = queries[i][ 0 ];
int B = queries[i][ 1 ];
printElements(A, B);
System.out.println();
}
}
public static void main(String[] args)
{
int arr[] = { 1 , 5 , 1 , 2 , 3 , 3 , 4 , 0 , 0 };
int N = arr.length;
int Q = 2 ;
int [][]queries = { { 1 , 3 }, { 0 , 0 } };
findElements(arr, N, Q, queries);
}
}
|
Python3
from collections import defaultdict
freq = defaultdict( int )
def storeFrequency(arr, n):
for i in range (n):
freq[arr[i]] + = 1
def printElements(a, b):
for it in freq:
if ((freq[it]) > 1 ):
value = it
if (value > = a and value < = b):
print (value, end = " " )
def findElements(arr, N, Q,
queries):
storeFrequency(arr, N)
for i in range (Q):
A = queries[i][ 0 ]
B = queries[i][ 1 ]
printElements(A, B)
print ()
if __name__ = = "__main__" :
arr = [ 1 , 5 , 1 , 2 , 3 , 3 , 4 , 0 , 0 ]
N = len (arr)
Q = 2
queries = [[ 1 , 3 ], [ 0 , 0 ]]
findElements(arr, N, Q, queries)
|
C#
using System;
using System.Collections.Generic;
public class GFG{
static Dictionary< int , int > freq= new Dictionary< int , int > ();
static void storeFrequency( int []arr, int n)
{
for ( int i = 0 ; i < n; i++){
if (freq.ContainsKey(arr[i])){
freq[arr[i]]= freq[arr[i]]+1;
}
else {
freq.Add(arr[i], 1);
}
}
}
static void printElements( int a, int b)
{
foreach (KeyValuePair< int , int > it in freq) {
if ((it.Value) > 1) {
int value = it.Key;
if (value >= a && value <= b) {
Console.Write(value+ " " );
}
}
}
}
static void findElements( int []arr, int N, int Q,
int [,] queries)
{
storeFrequency(arr, N);
for ( int i = 0; i < Q; i++) {
int A = queries[i,0];
int B = queries[i,1];
printElements(A, B);
Console.WriteLine();
}
}
public static void Main(String[] args)
{
int []arr = { 1, 5, 1, 2, 3, 3, 4, 0, 0 };
int N = arr.Length;
int Q = 2;
int [,]queries = { { 1, 3 }, { 0, 0 } };
findElements(arr, N, Q, queries);
}
}
|
Javascript
<script>
let freq = new Map();
function storeFrequency(arr,n)
{
for (let i = 0; i < n; i++) {
freq[arr[i]]++;
}
}
function printElements(a, b)
{
for (let [key, val] of freq.values()) {
if (val > 1) {
let value =key;
if (value >= a && value <= b) {
document.write(value + " " );
}
}
}
}
function findElements(arr, N, Q, queries)
{
storeFrequency(arr, N);
for (let i = 0; i < Q; i++) {
let A = queries[i][0];
let B = queries[i][1];
printElements(A, B);
document.write( "<br>" );
}
}
let arr = [ 1, 5, 1, 2, 3, 3, 4, 0, 0 ];
let N = arr.length;
let Q = 2;
let queries = [ [ 1, 3 ], [ 0, 0 ] ];
findElements(arr, N, Q, queries);
</script>
|
Time Complexity: O(Q*N + N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...