Count all prefixes of the given binary array which are divisible by x
Last Updated :
09 Jun, 2022
Given a binary array arr[] and an integer x, the task is to count all the prefixes of the given array which are divisible by x.
Note: The ith prefix from arr[0] to arr[i] is interpreted as a binary number (from most-significant-bit to least-significant-bit.)
Examples:
Input: arr[] = {0, 1, 0, 1, 1}, x = 5
Output: 2
0 = 0
01 = 1
010 = 2
0101 = 5
01011 = 11
0 and 0101 are the only prefixes divisible by 5.
Input: arr[] = {1, 0, 1, 0, 1, 1, 0}, x = 2
Output: 3
Naive Approach: Iterate from 0 to i to convert each binary prefix to decimal and check whether the number is divisible by x or not.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Function to return the count of total
// binary prefix which are divisible by x
int CntDivbyX(int arr[], int n, int x)
{
// Initialize with zero
int number = 0;
int count = 0;
for (int i = 0; i < n; i++) {
// Convert all prefixes to decimal
number = number * 2 + arr[i];
// If number is divisible by x
// then increase count
if ((number % x == 0))
count += 1;
}
return count;
}
// Driver code
int main()
{
int arr[] = { 1, 0, 1, 0, 1, 1, 0 };
int n = sizeof(arr) / sizeof(arr[0]);
int x = 2;
cout << CntDivbyX(arr, n, x);
return 0;
}
Java
// Java implementation of the approach
class GfG
{
// Function to return the count of total
// binary prefix which are divisible by x
static int CntDivbyX(int arr[], int n, int x)
{
// Initialize with zero
int number = 0;
int count = 0;
for (int i = 0; i < n; i++)
{
// Convert all prefixes to decimal
number = number * 2 + arr[i];
// If number is divisible by x
// then increase count
if ((number % x == 0))
count += 1;
}
return count;
}
// Driver Code
public static void main(String []args)
{
int arr[] = { 1, 0, 1, 0, 1, 1, 0 };
int n = arr.length;
int x = 2;
System.out.println(CntDivbyX(arr, n, x));
}
}
// This code is contributed by Rituraj Jain
Python3
# Python 3 implementation of the approach
# Function to return the count of total
# binary prefix which are divisible by x
def CntDivbyX(arr, n, x):
# Initialize with zero
number = 0
count = 0
for i in range(n):
# Convert all prefixes to decimal
number = number * 2 + arr[i]
# If number is divisible by x
# then increase count
if ((number % x == 0)):
count += 1
return count
# Driver code
if __name__ == '__main__':
arr = [1, 0, 1, 0, 1, 1, 0]
n = len(arr)
x = 2
print(CntDivbyX(arr, n, x))
# This code is contributed by
# Surendra_Gangwar
C#
// C# implementation of the approach
using System;
class GfG
{
// Function to return the count of total
// binary prefix which are divisible by x
static int CntDivbyX(int[] arr, int n, int x)
{
// Initialize with zero
int number = 0;
int count = 0;
for (int i = 0; i < n; i++)
{
// Convert all prefixes to decimal
number = number * 2 + arr[i];
// If number is divisible by x
// then increase count
if ((number % x == 0))
count += 1;
}
return count;
}
// Driver Code
public static void Main()
{
int[] arr = { 1, 0, 1, 0, 1, 1, 0 };
int n = arr.Length;
int x = 2;
Console.WriteLine(CntDivbyX(arr, n, x));
}
}
// This code is contributed by Code_Mech.
PHP
<?php
// PHP implementation of the approach
// Function to return the count of total
// binary prefix which are divisible by x
function CntDivbyX($arr, $n, $x)
{
// Initialize with zero
$number = 0;
$count = 0;
for ($i = 0; $i < $n; $i++)
{
// Convert all prefixes to decimal
$number = $number * 2 + $arr[$i];
// If number is divisible by x
// then increase count
if (($number % $x == 0))
$count += 1;
}
return $count;
}
// Driver code
$arr = array(1, 0, 1, 0, 1, 1, 0);
$n = sizeof($arr);
$x = 2;
echo CntDivbyX($arr, $n, $x);
// This code is contributed by Akanksha Rai
JavaScript
<script>
// Javascript implementation of the approach
// Function to return the count of total
// binary prefix which are divisible by x
function CntDivbyX(arr, n, x)
{
// Initialize with zero
let number = 0;
let count = 0;
for (let i = 0; i < n; i++) {
// Convert all prefixes to decimal
number = number * 2 + arr[i];
// If number is divisible by x
// then increase count
if ((number % x == 0))
count += 1;
}
return count;
}
// Driver code
let arr = [ 1, 0, 1, 0, 1, 1, 0 ];
let n = arr.length;
let x = 2;
document.write(CntDivbyX(arr, n, x));
</script>
Time Complexity: O(N) where N is the size of the array
Auxiliary Space: O(1)
Efficient Approach: As we see in the above approach we convert each binary prefix to a decimal number like 0, 01, 010, 0101.... but as the value of n(size of array) increases then the resultant number will be very large and no. will be out of range of data type, so we can make use of the modular properties.
Instead of doing number = number * 2 + arr[ i ] , we can do better as number = (number * 2 + arr[ i ] ) % x
Explanation: We start with number = 0 and repeatedly do number = number * 2 + arr[ i ] then in each iteration we'll get a new term of the above sequence.
A = {1, 0, 1, 0, 1, 1, 0}
"1" = 0*2 + 1 = 1
"10" = 1*2 + 0 = 2
"101" = 2*2 + 1 = 5
"1010" = 5*2 + 0 = 10
"10101" = 10*2 + 1 = 21
"101011" = 21*2 + 1 = 43
"1010110" = 43*2 + 0 =86
Since we are repeatedly taking the remainders of the number at each step, at each step we have, newNum = oldNum * 2 + arr[i] .By the rules of modular arithmetic (a * b + c) % m is same as ((a * b) % m + c % m) % m. So, it doesn't matter whether oldNum is the remainder or the original number, the answer would be correct.
Note: Similar article discussed here.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Function to return the count of total
// binary prefix which are divisible by x
int CntDivbyX(int arr[], int n, int x)
{
// Initialize with zero
int number = 0;
int count = 0;
for (int i = 0; i < n; i++) {
// Instead of converting all prefixes
// to decimal, take reminder with x
number = (number * 2 + arr[i]) % x;
// If number is divisible by x
// then reminder = 0
if (number == 0)
count += 1;
}
return count;
}
// Driver code
int main()
{
int arr[] = { 1, 0, 1, 0, 1, 1, 0 };
int n = sizeof(arr) / sizeof(arr[0]);
int x = 2;
cout << CntDivbyX(arr, n, x);
return 0;
}
Java
// Java implementation of the approach
class GFG {
// Function to return the count of total
// binary prefix which are divisible by x
public static int CntDivbyX(int arr[], int n, int x)
{
// Initialize with zero
int number = 0;
int count = 0;
for (int i = 0; i < n; i++) {
// Instead of converting all prefixes
// to decimal, take reminder with x
number = (number * 2 + arr[i]) % x;
// If number is divisible by x
// then reminder = 0
if (number == 0)
count += 1;
}
return count;
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 1, 0, 1, 0, 1, 1, 0 };
int n = 7;
int x = 2;
System.out.print(CntDivbyX(arr, n, x));
}
}
Python3
# Python3 implementation of the approach
# Function to return the count of total
# binary prefix which are divisible by x
def CntDivbyX(arr, n, x):
number = 0
count = 0
for i in range (0, n):
# Instead of converting all prefixes
# to decimal, take reminder with x
number = ( number * 2 + arr[i] ) % x
# If number is divisible by x
# then reminder = 0
if number == 0:
count += 1
return count
# Driver code
arr = [1, 0, 1, 0, 1, 1, 0]
n = 7
x = 2
print( CntDivbyX(arr, n, x) )
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the count of total
// binary prefix which are divisible by x
public static int CntDivbyX(int []arr, int n, int x)
{
// Initialize with zero
int number = 0;
int count = 0;
for (int i = 0; i < n; i++)
{
// Instead of converting all prefixes
// to decimal, take reminder with x
number = (number * 2 + arr[i]) % x;
// If number is divisible by x
// then reminder = 0
if (number == 0)
count += 1;
}
return count;
}
// Driver code
public static void Main()
{
int []arr = { 1, 0, 1, 0, 1, 1, 0 };
int n = 7;
int x = 2;
Console.Write(CntDivbyX(arr, n, x));
}
}
// This code is contributed by Ryuga
PHP
<?php
// PHP implementation of the approach
// Function to return the count of total
// binary prefix which are divisible by x
function CntDivbyX($arr, $n, $x)
{
// Initialize with zero
$number = 0;
$count1 = 0;
for ($i = 0; $i < $n; $i++)
{
// Instead of converting all prefixes
// to decimal, take reminder with x
$number = ($number * 2 + $arr[$i]) % $x;
// If number is divisible by x
// then reminder = 0
if ($number == 0)
$count1 += 1;
}
return $count1;
}
// Driver code
$arr = array(1, 0, 1, 0, 1, 1, 0);
$n = sizeof($arr);
$x = 2;
echo CntDivbyX($arr, $n, $x);
// This code is contributed by Akanksha Rai
?>
JavaScript
<script>
// Javascript implementation of the approach
// Function to return the count of total
// binary prefix which are divisible by x
function CntDivbyX(arr, n, x)
{
// Initialize with zero
let number = 0;
let count = 0;
for (let i = 0; i < n; i++) {
// Instead of converting all prefixes
// to decimal, take reminder with x
number = (number * 2 + arr[i]) % x;
// If number is divisible by x
// then reminder = 0
if (number == 0)
count += 1;
}
return count;
}
// Driver code
let arr = [ 1, 0, 1, 0, 1, 1, 0 ];
let n = arr.length;
let x = 2;
document.write(CntDivbyX(arr, n, x));
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Count the number of elements in an array which are divisible by k
Given an array of integers. The task is to calculate the count of a number of elements which are divisible by a given number k. Examples: Input: arr[] = { 2, 6, 7, 12, 14, 18 }, k = 3 Output: 3 Numbers which are divisible by k are { 6, 12, 18 } Input: arr[] = { 2, 6, 7, 12, 14, 18 }, k = 2 Output: 5
6 min read
Count of elements in given Array divisible by all elements in their prefix
Given an array arr[] containing N positive integers, the task is to find the total number of elements in the array that are divisible by all the elements present before them. Examples: Input: arr[] = {10, 6, 60, 120, 30, 360}Output: 3Explanation: 60, 120 and 360 are the required elements. Input: arr
12 min read
Count of pairs of Array elements which are divisible by K when concatenated
Given an array arr[] and an integer K, the task is to count the pair of indices (i, j) such that i !=j and concatenation of a[i] and a[j] is divisible by K. Example: Input: arr[] = [4, 5, 2], K = 2 Output: 4 Explanation: All possible concatenations are {45, 42, 54, 52, 24, 25}. Out of these, the num
8 min read
Count numbers in range 1 to N which are divisible by X but not by Y
Given two positive integers X and Y, the task is to count the total numbers in range 1 to N which are divisible by X but not Y. Examples: Input: x = 2, Y = 3, N = 10 Output: 4 Numbers divisible by 2 but not 3 are : 2, 4, 8, 10 Input : X = 2, Y = 4, N = 20 Output : 5 Numbers divisible by 2 but not 4
10 min read
Prefix of a given String that are divisible by K
Given a 0-indexed based string str of length n consisting of digits, and a positive integer K, the task is to find the prefixes of a String that are exactly Divisible by K. Store the prefix that is divisible by K, in a vector of String and print them. String word str consists of only digits from 0 t
5 min read
Queries to count numbers from given range which are divisible by all its digits
Given a 2D array arr[][] with each row of the form of a query { L, R }, the task is to count the numbers in the range [L, R] such that the number is divisible by all of its non-zero digit. Examples: Input: arr[][] ={ {1, 5}, {12, 14} } Output: 5 1 Explanation: Query1: All the numbers in the range [1
8 min read
Count numbers in a range that are divisible by all array elements
Given N numbers and two numbers L and R, the task is to print the count of numbers in the range [L, R] which are divisible by all the elements of the array. Examples: Input: a[] = {1, 4, 2], L = 1, R = 10 Output : 2 In range [1, 10], the numbers 4 and 8 are divisible by all the array elements. Input
8 min read
Count all Arrays within a Range and Divisible by 3
Given three integers n,l,r, the task is to determine the count of such arrays that follow the below two conditions: All the elements of the array are between l to r (inclusive) The sum of all the elements is divisible by 3.Since the answer can be very long, print the remainder when the result is div
9 min read
Count maximum concatenation of pairs from given array that are divisible by 3
Given an array arr[] of size N, the task is to count the pairs whose concatenation of elements is divisible by 3 and each array element present in at most one pair. Examples: Input: arr[] = { 5, 3, 2, 8, 7 } Output: 1 Explanation: Possible pairs whose concatenation is divisible by 3 are { 27, 72, 78
13 min read
Count of pairs in Array whose product is divisible by K
Given an array A[] and positive integer K, the task is to count the total number of pairs in the array whose product is divisible by K. Examples : Input: A[] = [1, 2, 3, 4, 5], K = 2Output: 7Explanation: The 7 pairs of indices whose corresponding products are divisible by 2 are(0, 1), (0, 3), (1, 2)
9 min read