Queries to check whether a given digit is present in the given Range in C++

C++Server Side ProgrammingProgramming

In this problem, we have given an array arr[] and some queries each consisting of three values, L and R, and val. Our task is to create a program to solve Queries to check whether a given digit is present in the given Range in C++.

Problem Description−

To solve each query, we need to check if the given element val is present in the given Range between L and R or not.

Let’s take an example to understand the problem,

Input:arr[] = {4, 8, 1, 7, 2, 9, 3, 5, 1}

Q = 3

query = {{1, 4, 3}, {0, 2, 1}, {4, 7, 2 }}

Output: Not Present

Present

Present

Explanation

Query 1: range is [1, 4], subarray = {8, 1, 7, 2}. 3 is not present in this subarray.

Query 2: range is [0, 2], subarray = {4, 8, 1}. 1 is present in this subarray.

Query 1: range is [4, 7], subarray = {2, 9, 3, 5, 1}. 2 is present in this subarray.

Solution Approach

A simple way to solve the problem is by traversing the subarray and checking whether the given element is present in the range.

Example

 Live Demo

#include <iostream>
using namespace std;
bool isElementPresent(int arr[], int L, int R, int val){
   for(int i = L; i <= R; i++ ){
      if(arr[i] == val){
         return true;
      }
   }
   return false;
}
int main(){
   int arr[] = {4, 8, 1, 7, 2, 9, 3, 5, 1};
   int Q = 3;
   int query[Q][3] = {{1, 4, 3}, {0, 2, 1}, {4, 7, 2 }};
   for(int i = 0; i < Q; i++){
      cout<<"For Query "<<(i+1);
      if(isElementPresent(arr, query[i][0], query[i][1], query[i][2]))
         cout<<": The given digit "<<query[i][2]<<" is present in the given range\n";
      else
         cout<<": The given digit "<<query[i][2]<<" is not present in the given range\n";
   }
   return 0;
}

Output

For Query 1: The given digit 3 is not present in the given range
For Query 2: The given digit 1 is present in the given range
For Query 3: The given digit 2 is present in the given range

This is approach uses a loop, hence has a time complexity of the order O(Q * N). Where Q is the number of queries.

A better solution approach could be using a segment Tree to store all possible digits (0 - 9). To avoid duplicate elements in the node we will be using the set data structure (it has a property to eliminate duplicate elements). This will help us reduce the total number of elements in each node to a maximum of 10.

Then, for the query, we will check if the element is present or not in the given range.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
set<int> SegTree[36];
void buildSegmentTree(int* arr, int index, int start, int end) {
   if (start == end) {
      SegTree[index].insert(arr[start]);
      return;
   }
   int middleEle = (start + end) >> 1;
   buildSegmentTree(arr, 2 * index, start, middleEle);
   buildSegmentTree(arr, 2 * index + 1, middleEle + 1, end);
   for (auto it : SegTree[2 * index])
      SegTree[index].insert(it);
   for (auto it : SegTree[2 * index + 1])
      SegTree[index].insert(it);
}
bool isElementPresent(int index, int start, int end, int L, int R, int val){
   if (L <= start && end <= R) {
      if (SegTree[index].count(val) != 0) {
         return true;
      }
      else
         return false;
      }
   if (R < start || end < L) {
      return false;
   }
   int middleEle = (start + end) >> 1;
   bool isPresentInLeftSubArray = isElementPresent((2 * index), start,middleEle, L, R, val);
   bool isPresentInRightSubArray = isElementPresent((2 * index + 1),(middleEle + 1), end, L, R, val);
   return isPresentInLeftSubArray or isPresentInRightSubArray;
}
int main(){
   int arr[] = {4, 8, 1, 7, 2, 9, 3, 5, 1};
   int n = sizeof(arr)/sizeof(arr[0]);
   int Q = 3;
   int query[Q][3] = {{1, 4, 3}, {0, 2, 1}, {4, 7, 2 }};
   buildSegmentTree(arr, 1, 0, (n - 1));
   for(int i = 0; i < Q; i++){
      cout<<"For Query "<<(i+1);
      if(isElementPresent(1, 0, (n - 1), query[i][0], query[i][1], query[i][2]))
         cout<<": The given digit "<<query[i][2]<<" is present in the given
         range\n";
      else
         cout<<": The given digit "<<query[i][2]<<" is not present in the given range\n";
   }
   return 0;
}

Output

For Query 1: The given digit 3 is not present in the given range
For Query 2: The given digit 1 is present in the given range
For Query 3: The given digit 2 is present in the given range
raja
Updated on 09-Oct-2020 09:09:14

Advertisements