- Related Questions & Answers
- Possible to make a divisible by 3 number using all digits in an array in C++
- Maximum number of pieces in N cuts in C++
- Find an array element such that all elements are divisible by it using c++
- Minimum Cuts can be made in the Chessboard such that it is not divided into 2 parts in C++
- Check if a large number is divisible by 3 or not in C++
- Find the number of ways to divide number into four parts such that a = c and b = d in C++
- Maximum possible sum of a window in an array such that elements of same window in other array are unique in c++
- Minimum Cuts can be made in the Chessboard such that it is not divided into 2 parts in Python
- Find permutation of n which is divisible by 3 but not divisible by 6 in C++
- Maximum sum such that no two elements are adjacent in C++
- C/C++ Program to check whether it is possible to make a divisible by 3 number using all digits in an array?
- Count the number of pairs (i, j) such that either arr[i] is divisible by arr[j] or arr[j] is divisible by arr[i] in C++
- Maximum subsequence sum such that no three are consecutive
- Find triplet such that number of nodes connecting these triplets is maximum in C++
- Check if a large number is divisible by 3 or not in java

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

In this problem, we are given a large integer value (with up to 10^{5} digits). Our task is to print the total number of cuts required such that maximum parts are divisible by 3.

Let’s take an example to understand the problem

**Input** − 9216

**Output** − 3

**Explanation** − the number is divided as 9|21|6.

To solve this problem, we will have to start for the least significant bit of the number i.e. the last digit of the number. For here, we will find the smallest number divisible by three. And then update count based on it.

**Example** − if arr[i] makes a 3 divisible number, then we will increase the count and move to the next digit of the number. If arr[i] is not divisible by 3, then we will combine it with the next digit and check for divisibility of 3.

**Divisibility of 3** − A number is divisible by 3 if the number of digits of the number is divisible by three.

Program to show the implementation of our solution

#include <bits/stdc++.h> using namespace std; int countMaximum3DivisibleNumbers(string number){ int n = number.length(); vector<int> remIndex(3, -1); remIndex[0] = 0; vector<int> counter(n + 1); int r = 0; for (int i = 1; i <= n; i++) { r = (r + number[i-1] - '0') % 3; counter[i] = counter[i-1]; if (remIndex[r] != -1) counter[i] = max(counter[i], counter[remIndex[r]] + 1); remIndex[r] = i+1; } return counter[n]; } int main() { string number = "216873491"; cout<<"The number of 3 divisible number created by cutting "<<number<<" are : " <<countMaximum3DivisibleNumbers(number); return 0; }

The number of 3 divisible number created by cutting 216873491 are : 5

Advertisements