Pre-requisites: Segment Tree
Given an array of digits arr. Given a number of range [L, R] and a digit X with each range. The task is to check for each given range [L, R] whether the digit X is present within that range in the array arr.
Input : arr = [1, 3, 3, 9, 8, 7] l1=0, r1=3, x=2 // Range 1 l1=2, r1=5, x=3 // Range 2 Output : NO YES For Range 1: The digit 2 is not present within range [0, 3] in the array. For Range 2: The digit 3 is present within the range [2, 5] at index 2 in the given array.
Naive Approach: A naive approach is to traverse through each of the given range of the digits in the array and check whether the digit is present or not.
Time Complexity: O(N) for each query.
Better Approach: A better approach is to use Segment Tree. Since there are only 10 digits possible from (0-9), so each node of the segment tree will contain all the digits within the range of that node. We will use Set Data Structure at every node to store the digits. Set is a special data structure which removes redundant elements and store them in ascending order. We have used set data structure since it will be easier to merge 2 child nodes to get the parent node in the segment tree. We will insert all the digits present in the children nodes in the parent set and it will automatically remove the redundant digits. Hence at every set(node) there will be at max 10 elements (0-9 all the digits).
Also there are inbuilt count function which returns the count of the element present in the set which will be helpful in the query function to check whether a digit is present at the node or not. If the count will be greater than 0 that means the element is present in the set we will return true else return false.
Below is the implementation of the above approach:
Time Complexity: O(N) once for building the segment tree, then O(logN) for each query.
- Array range queries over range queries
- Queries to check whether all the elements in the given range occurs even number of times
- Queries for the count of even digit sum elements in the given range using Segment Tree.
- Queries for count of even digit sum elements in given range using MO's Algorithm
- Queries to count integers in a range [L, R] such that their digit sum is prime and divisible by K
- Count numbers divisible by K in a range with Fibonacci digit sum for Q queries
- Range Queries to find the Element having Maximum Digit Sum
- Queries for elements having values within the range A to B in the given index range using Segment Tree
- Range Queries to count elements lying in a given Range : MO's Algorithm
- Count of Numbers in Range where first digit is equal to last digit of the number
- Count numbers in a range with digit sum divisible by K having first and last digit different
- Find all numbers between range L to R such that sum of digit and sum of square of digit is prime
- Binary Indexed Tree : Range Update and Range Queries
- Queries to check whether all the elements can be made positive by flipping signs exactly K times
- Check whether a node is leaf node or not for multiple queries
- Queries to check whether bitwise AND of a subarray is even or odd
- Check whether XOR of all numbers in a given range is even or odd
- Queries to check if any non-repeating element exists within range [L, R] of an Array
- Find the XOR of the elements in the given range [L, R] with the value K for a given set of queries
- Generate a number such that the frequency of each digit is digit times the frequency in given number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.