
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Print digit’s position to be removed to make a number divisible by 6 in C++
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.
Approach
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
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.
When the last digit is even
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 −
When the last digit is odd
1. 34241341
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
2. 3214241
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.
When the last digit is even
1. 8097860
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.
Example
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; }
Output
5
- Related Articles
- Number of digits to be removed to make a number divisible by 3 in C++
- Minimum number of elements to be removed to make XOR maximum using C++.
- Find the greatest 3 digit number which is divisible by the number 4 ,5 and 6.
- Largest K digit number divisible by X in C++
- Find how many two-digit numbers are divisible by 6.
- Python Program to Print all Numbers in a Range Divisible by a Given Number
- Golang Program to Print all Numbers in a Range Divisible by a Given Number
- Find the greatest 3 digit number that is exactly divisible by 6 15 and 21
- Is the digit divisible by the previous digit of the number in JavaScript
- Count n digit numbers divisible by given number in C++
- Find the largest 4 digit number divisible by 16.
- A number is divisible by $12$. By what other numbers will that number be divisible?
- 18 is divisible by both 2 and 3 . It is also divisible by \( 2 \times 3=6 \). Similarly, a number is divisible by both 4 and 6. Can we say that the number must also be divisible by \( 4 \times 6=24 \) ? If not, give an example to justify your answer.
- Largest N digit number divisible by given three numbers in C++
- Find the value of * by replacing it with the smallest digit to make 52*8 divisible by 3.
