
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Queries to check whether a given digit is present in the given Range in C++
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
#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
#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
- Related Articles
- Queries to update a given index and find gcd in range in C++
- Queries to update a given index and find gcd in range in C++ Program
- Check whether the given string is a valid identifier in Python
- C++ program to find number in given range where each digit is distinct
- Java program to check if a Substring is present in a given String
- Python Program to check if a substring is present in a given string.
- C# program to check if a Substring is present in a Given String
- Program to Find Out the Occurrence of a Digit from a Given Range in Python
- Find the Initial Array from given array after range sum queries in C++
- Java Program to Check Whether the Given String is Pangram
- Golang Program to Check Whether the Given String is Pangram
- Count numbers with unit digit k in given range in C++
- Check whether the value given in the bracket is a solution to the given equation $n+5=19 $( n =1).
- Queries for counts of array elements with values in given range in C++
- Check whether a given number is Polydivisible or Not
