Print digit’s position to be removed to make a number divisible by 6 in C++

C++Server Side ProgrammingProgramming

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 −

 Live Demo

#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
raja
Published on 03-Jan-2020 07:30:46
Advertisements