- 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

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

- Related Questions & Answers
- Maximum number of pieces in N cuts in C++
- Possible to make a divisible by 3 number using all digits in an array in C++
- Number of substrings divisible by 8 and not by 3 in C++
- C++ Partition a Number into Two Divisible Parts
- Minimum Cuts can be made in the Chessboard such that it is not divided into 2 parts in C++
- Find an array element such that all elements are divisible by it using c++
- Number of digits to be removed to make a number divisible by 3 in C++
- Divide number into two parts divisible by given numbers in C++ Program
- Number of Groups of Sizes Two Or Three Divisible By 3 in C++
- Find the number of ways to divide number into four parts such that a = c and b = d in C++
- Minimum Cuts can be made in the Chessboard such that it is not divided into 2 parts in Python
- Check if a large number is divisible by 3 or not in C++
- Maximum possible sum of a window in an array such that elements of same window in other array are unique in c++
- Maximum sum such that no two elements are adjacent in C++
- Largest number with the given set of N digits that is divisible by 2, 3 and 5 in C++

Advertisements