- 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

Suppose we have an array A of 0s and 1s, consider N[i] is the i-th subarray from index A[0] to A[i] interpreted as a binary number. We have to find a list of boolean answers, where answer[i] is true if and only if N[i] is divisible by 5.

So, if the input is like [0,1,1,1,1,1], then the output will be [true,false,false,false,true,false]

To solve this, we will follow these steps −

- length := size of A
- ans:= make an array of size length, and fill with false
- number:= a binary value by concatenating each element from A
- for i in range 0 to length, do
- if number mod 5 is same as 0, then
- ans[length-i-1] := True

- number:= number / 2

- if number mod 5 is same as 0, then
- return ans

Let us see the following implementation to get better understanding −

class Solution: def prefixesDivBy5(self, A): length=len(A) ans=[False]*length number=int("".join(map(str,A)),2) for i in range(length): if number%5==0: ans[length-i-1]=True number=number>>1 return ans ob = Solution() print(ob.prefixesDivBy5([0,1,1,1,1,1]))

[0,1,1,1,1,1]

[True, False, False, False, True, False]

- Related Questions & Answers
- Check if a large number is divisible by 5 or not in C++
- Program to print all the numbers divisible by 3 and 5 in C++
- N digit numbers divisible by 5 formed from the M digits in C++
- Python program to print all the numbers divisible by 3 and 5 for a given number
- Smallest Integer Divisible by K in Python
- Check if a large number is divisible by 2, 3 and 5 or not in C++
- K Prefix in Python
- Program to make sum divisible by P in Python
- C# program to print all the numbers divisible by 3 and 5 for a given number
- Sum of the numbers up to N that are divisible by 2 or 5 in c programming
- Python - Prefix sum list
- Longest Common Prefix in Python
- Count all prefixes of the given binary array which are divisible by x in C++
- Next multiple of 5 and binary concatenation in JavaScript
- Largest number with the given set of N digits that is divisible by 2, 3 and 5 in C++

Advertisements