# Queries for decimal values of subarrays of a binary array in C++

C++Server Side ProgrammingProgramming

#### C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

#### Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

#### Master C and Embedded C Programming- Learn as you go

Best Seller

66 Lectures 5.5 hours

In this problem, we are given a binary array bin[] and Q queries each consists of two values L and R. Our task is to create a program to solve queries for decimal values of subarrays of a binary array in C++.

Problem description − Here to solve each query, we will have to find and print the decimal number which is created by the subarray starting from L to R i.e. subarray[L...R].

Let’s take an example to understand the problem,

## Input

bin[] = {1, 1, 0, 0, 1, 0, 1, 0, 0, 0}
Q = 2
2 5
0 6

## Output

2 101

## Explanation

For query 1, the subarray formed is { 0, 0, 1, 0}. It will create the binary number 0010 whose decimal conversion is 2.

For query 2, the subarray formed is { 1, 1, 0, 0, 1, 0, 1}. It will create the binary number 1100101 whose decimal conversion is 101.

## Solution approach

A simple solution is by traversing the binary string from index L to index R, find the binary number which is formed, and then convert the given binarynumber to its decimal equivalent.

Program to show the implementation of our approach

## Example

Live Demo

#include <iostream>
#include <math.h>
using namespace std;

int CalcDecimalValue(int bin[], int L, int R) {
int decimal = 0;
int j = 0;
for(int i = R; i >= L; i--){
decimal += bin[i] * pow(2, j);
j++;
}
return decimal;
}

int main() {

int bin[] = {1, 1, 0, 0, 1, 0, 1, 0, 0, 0};
int n = sizeof(bin) / sizeof(bin[0]);
int Q = 2;
int query[Q][2] = {{2, 5},{0, 6}};
for(int i = 0; i < Q; i++){
cout<<"For query "<<(i+1)<<": The decimal value of subarray is "<<CalcDecimalValue(bin, query[i]   [0], query[i][1])<<"";
}
return 0;
}

## Output

For query 1: The decimal value of subarray is 2
For query 2: The decimal value of subarray is 101

Another approach to solving the problem is by using a pre-computed array. We will create a precomputed array that will store the decimal number created until the (n-i)th index value. And for solving queries, we will find the difference between the value at l and r.

The ith value of the array will be found using the binary to decimal conversion formula. Appling it from the right side, i.e. from n-1,

decimalArray[i] = bin[i]*2^(n-1-i) + bin[i+1]*2^(n-1-i+1) + … bin[n- 1]*2^(0).

Program to illustrate the working of our solution,

## Example

Live Demo

#include <bits/stdc++.h>
using namespace std;
int decimalArray[1000];

void createDecimalArray(int bin[], int n){
memset(decimalArray, 0, n*sizeof(int));
decimalArray[n - 1] = bin[n - 1] * pow(2, 0);
for (int i = n - 2; i >= 0; i--)
decimalArray[i] = decimalArray[i + 1] + bin[i] * (pow(2,(n - 1 - i)));
}

int CalcDecimalValue(int L, int R, int n){
if (R != n - 1)
return (decimalArray[L] - decimalArray[R + 1]) / (pow(2, (n - 1 - R)));
return decimalArray[L] / (1 << (n - 1 - R));
}

int main(){

int bin[] = {1, 1, 0, 0, 1, 0, 1, 0, 0, 0};
int n = sizeof(bin) / sizeof(bin[0]);
createDecimalArray(bin, n);
int Q = 2;
int query[Q][2] = {{2, 5},{0, 6}};
for(int i = 0; i < Q; i++){
cout<<"For query "<<(i+1)<<": The decimal value of subarray is "<<CalcDecimalValue(query[i][0],    query[i][1], n)<<"";
}
return 0;
}

## Output

For query 1: The decimal value of subarray is 2
For query 2: The decimal value of subarray is 101
Updated on 09-Sep-2020 07:48:03