- 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 number and we have to remove more digit from the number. So that the new number formed after removal is divisible by 6.

Let’s take an example to learn the concept better −

Input : 1324 Output : 4

**Explanation** − on removing the 4th number we will get 132 which is divisible by 6.

Here, we are given a number and we have to return the position from where the number is removed to make it divisible by 6.

For solving this problem, we will try to create a logic that solves the problem. For this, we will use our knowledge that if a number is divisible by 2 and 3 then it is divisible by 6.

After removing the digit from the number the new number formed will be checked for divisibility of 6 i.e. divisibility of 2 and 3 both.

Based on the number we can find if the number created by removing a digit will be divisible by 6 or not. There arise two conditions if we see the last digit of the number.

When the last digit is odd the only way possible is to remove the last digit. And the new number will not be divisible by 6 only if the digit next to the last digit is even. Otherwise, the solution is not possible.

If the last digit is even we have to find the remainder when the number is divided by 3. and based on this number we can check which digit can be removed.

On dividing the number by 3 three cases −

**Remainder is 1** − if the remainder of the division is 1 then any of the digits 1, 4, 7 can be removed from the array. If multiple digits are available to be removed then we will remove the digit in such a way that the number formed after removing is the greatest.

**Remainder is 2** − if the remainder of the division is 2 then any of the digits 2, 5, 8 can be removed from the array. If multiple digits are available to be removed then we will remove the digit in such a way that number formed after removing is the greatest.

**Remainder is 3** − if the remainder of the division is 1 then any of the digits 3, 6, 9 can be removed from the array. If multiple digits are available to be removed then we will remove the digit in such a way that number formed after removing is the greatest.

Based on these let's solve a few problems and find the desired output −

In this case, the only digit that can be removed is 1 from the last position and the number formed will be 3432134 which is divisible by 6. So, we will return the position of 1 removed i.e. 8

In this case, the only digit that can be removed is 1 from the last position and the number formed will be 341224. Which is not divisible by 6. So, we will return -1.

In this case, we need to divide the number by 3 and find the remainder which is equal to 2. So, in the case of 2 as the remainder, we can remove 2, 5, 8 from the number. So, 8 from position 1 and 5 can be removed to make the number divisible by 2. We will remove 8 from the 5th position because if we remove it from 1st position a smaller number will be returned. The new number formed will be 809760 which is divisible by 6. So, we will return 5.

Based on this logic lets create a program to solve the problem −

#include <bits/stdc++.h> using namespace std; void isDivisibleBy6(string num){ int n = num.length(); int a[n]; int sum = 0; for (int i = 0; i < n; i++) { a[i] = num[i] - '0'; sum += a[i]; } if (a[n - 1] % 2){ if ( (a[n - 2] % 2 != 0) || (sum - a[n - 1]) % 3 != 0) { cout << "-1" << endl; } else { cout << n << endl; } } else { int re = sum % 3; int del = -1; int flag = 0; for (int i = 0; i < n - 1; i++) { if ((a[i]) % 3 == re) { if (a[i + 1] > a[i]) { del = i; flag = 1; break; } else { del = i; } } } if (flag == 0) { if (a[n - 2] % 2 == 0 and re == a[n - 1] % 3) del = n - 1; } if (del == -1) cout << -1 << endl; else { cout << del + 1 << endl; } } } int main(){ string number = "343224152"; isDivisibleBy6(number); return 0; }

5

- Related Questions & Answers
- Number of digits to be removed to make a number divisible by 3 in C++
- Largest K digit number divisible by X in C++
- Minimum number of elements to be removed to make XOR maximum using C++.
- Count n digit numbers divisible by given number in C++
- C++ Program for Largest K digit number divisible by X?
- C++ Program for Smallest K digit number divisible by X?
- C++ Programming for Smallest K digit number divisible by X?
- Largest N digit number divisible by given three numbers in C++
- How to generate 6-digit random number in MySQL?
- C++ Program for the Largest K digit number divisible by X?
- Minimum number of elements that should be removed to make the array good using C++.
- Is the digit divisible by the previous digit of the number in JavaScript
- Find permutation of n which is divisible by 3 but not divisible by 6 in C++
- Minimum removals in a number to be divisible by 10 power raised to K in C++
- Python Program for Smallest K digit number divisible by X

Advertisements