

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count all prefixes of the given binary array which are divisible by x in C++
In this tutorial, we will be discussing a program to find the number of prefixes of the binary array which are divisible by x.
For this we will be provided with binary array and a value x. Our task is to find the number of elements whose prefixes are divisible by given value x.
Example
#include <bits/stdc++.h> using namespace std; //counting the elements with prefixes //divisible by x int count_divx(int arr[], int n, int x){ int number = 0; int count = 0; for (int i = 0; i < n; i++) { number = number * 2 + arr[i]; //increasing count if ((number % x == 0)) count += 1; } return count; } int main(){ int arr[] = { 1, 0, 1, 0, 1, 1, 0 }; int n = sizeof(arr) / sizeof(arr[0]); int x = 2; cout << count_divx(arr, n, x); return 0; }
Output
3
- Related Questions & Answers
- Count the number of elements in an array which are divisible by k in C++
- Count numbers which are divisible by all the numbers from 2 to 10 in C++
- Count all 0s which are blocked by 1s in binary matrix in C++
- Count numbers in range 1 to N which are divisible by X but not by Y in C++
- Count numbers in a range that are divisible by all array elements in C++
- Sum of first N natural numbers which are divisible by X or Y
- Find elements of an array which are divisible by N using STL in C++
- Product of all the elements in an array divisible by a given number K in C++
- Count all prefixes in given string with greatest frequency using Python
- Maximize the number of sum pairs which are divisible by K in C++
- Count n digit numbers divisible by given number in C++
- Count numbers in range that are divisible by all of its non-zero digits in C++
- Program to count number of trailing zeros of minimum number x which is divisible by all values from 1 to k in Python
- C++ program to rearrange all elements of array which are multiples of x in increasing order
- Count rotations divisible by 8 in C++
Advertisements