Queries to find the count of integers in a range that contain the given pattern

Given a binary pattern patt and Q queries where each query consists of a range [L, R], for every query the task is to find the count of integers from the given range such that they contain the given pattern in their binary representation.

Examples:

Input: q[][] = {{2, 10}}, patt = “101”
Output:
2
5(101) and 10(1010) are the only valid integers.



Input: q[][] = {{122, 150}, {18, 1000}}, patt = “1111”
Output:
7
227

Approach:

  • Create an array res[] where res[i] will store the count of valid integers from the range [0, i].
  • Starting from 0, find the binary representation of all the integer and check whether the given pattern occurs in it.
  • Update the res[] array based on the values found in the previous step.
  • Now every query can be answered in O(1) as res[R] – res[L – 1].

Below is the implementation of the above approach:

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
import java.util.*;
class GFG {
  
    // Function to return the pre-calculate array
    // such that arr[i] stores the count of
    // valid numbers in the range [0, i]
    static int[] preCalculate(int max, String pattern)
    {
        int arr[] = new int[max + 1];
  
        // If 0 is a valid number
        if (pattern == "0")
            arr[0] = 1;
        else
            arr[0] = 0;
  
        // For every element i
        for (int i = 1; i <= max; i++) {
  
            // If i is avalid number
            if (Integer.toBinaryString(i).contains(pattern)) {
                arr[i] = 1 + arr[i - 1];
            }
            else {
                arr[i] = arr[i - 1];
            }
        }
        return arr;
    }
  
    // Function to perform the queries
    static void performQueries(int queries[][],
                               int q, String pattern)
    {
  
        // Maximum value for the end of any range
        int max = Integer.MIN_VALUE;
        for (int i = 0; i < q; i++)
            max = Math.max(max, queries[i][1]);
  
        // res[i] stores the count of valid
        // integers from the range [0, i]
        int res[] = preCalculate(max, pattern);
  
        for (int i = 0; i < q; i++) {
            int l = queries[i][0];
            int r = queries[i][1];
  
            if (l == 0)
                System.out.println(res[r]);
            else
                System.out.println(res[r] - res[l - 1]);
        }
    }
  
    // Driver code
    public static void main(String args[])
    {
        int queries[][] = { { 2, 10 }, { 8, 120 } };
        int q = queries.length;
        String pattern = "101";
  
        performQueries(queries, q, pattern);
    }
}

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System; 
using System.Numerics;
  
class GFG
{
  
    //integer to binary string 
    public static string toBinaryString(int x) 
    {
        char[] bits = new char[32];
        int i = 0;
      
        while (x != 0) 
        {
            bits[i++] = (x & 1) == 1 ? '1' : '0';
            x >>= 1;
        }
      
        Array.Reverse(bits, 0, i);
        return new string(bits);
    }
      
    // Function to return the pre-calculate array
    // such that arr[i] stores the count of
    // valid numbers in the range [0, i]
    static int[] preCalculate(int max, string pattern)
    {
        int []arr = new int[max + 1];
  
        // If 0 is a valid number
        if (pattern == "0")
            arr[0] = 1;
        else
            arr[0] = 0;
  
        // For every element i
        for (int i = 1; i <= max; i++) 
        {
            // If i is avalid number
            if (toBinaryString(i).Contains(pattern))
            {
                arr[i] = 1 + arr[i - 1];
            }
            else 
            {
                arr[i] = arr[i - 1];
            }
        }
        return arr;
    }
  
    // Function to perform the queries
    static void performQueries(int [,]queries,
                               int q, string pattern)
    {
  
        // Maximum value for the end of any range
        int max = int.MinValue;
        for (int i = 0; i < q; i++)
            max = Math.Max(max, queries[i, 1]);
  
        // res[i] stores the count of valid
        // integers from the range [0, i]
        int []res = preCalculate(max, pattern);
  
        for (int i = 0; i < q; i++) 
        {
            int l = queries[i, 0];
            int r = queries[i, 1];
  
            if (l == 0)
                Console.WriteLine(res[r]);
            else
                Console.WriteLine(res[r] - res[l - 1]);
        }
    }
  
    // Driver code
    public static void Main(string []args)
    {
        int [,]queries = { { 2, 10 }, { 8, 120 } };
        int q = queries.GetLength(0) ;
        string pattern = "101";
  
        performQueries(queries, q, pattern);
    }
}
  
// This code is contributed by Arnab Kundu

chevron_right


Output:

2
59


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.



Improved By : andrew1234