Given a large number, check if a subsequence of digits is divisible by 8

Given a number of at most 100 digits. We have to check if it is possible, after removing certain digits, to obtain a number of at least one digit which is divisible by 8. We are forbidden to rearrange the digits.

Examples :

Input : 1787075866
Output : Yes
There exist more one or more subsequences
divisible by 8. Example subsequences are
176, 16 and 8.

Input : 6673177113
Output : No 
No subsequence is divisible by 8.

Input : 3144
Output : Yes
The subsequence 344 is divisible by 8.

Property of the divisibility by eight : number can be divided by eight if and only if its last three digits form a number that can be divided by eight. Thus, it is enough to test only numbers that can be obtained from the original one by crossing out and that contain at most three digits i.e we check all one digits, two digits and three digit number combinations.

Method 1 (Brute Force):


We apply the brute force approach. We permute all possible single digit, double digit and triple digit combinations using iterative ladder. If we encounter a single digit number divisible by 8 or a double digit number combination divisible by 8 or a triple digit number combination divisible by 8, then that will be the solution to our problem.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to check if a subsequence of digits
// is divisible by 8.
#include <bits/stdc++.h>
using namespace std;
  
// Function to calculate any permutation divisible
// by 8. If such permutation exists, the function
// will return that permutation else it will return -1
bool isSubSeqDivisible(string str)
{
    // Generating all possible permutations and checking
    // if any such permutation is divisible by 8
    for (int i = 0; i < l; i++) {
        for (int j = i; j < l; j++) {
            for (int k = j; k < l; k++) {
                if (arr[i] % 8 == 0)
                    return true;
  
                else if ((arr[i] * 10 + arr[j]) % 8 == 0 && i != j)
                    return true;
  
                else if ((arr[i] * 100 + arr[j] * 10 + arr[k]) % 8 == 0 && i != j && j != k && i != k)
                    return true;
            }
        }
    }
    return false;
}
  
// Driver function
int main()
{
    string str = "3144";
    if (isSubSeqDivisible(str))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to check if a subsequence
// of digits is divisible by 8.
import java.io.*;
  
class GFG {
  
    // Function to calculate any permutation
    // divisible by 8. If such permutation
    // exists, the function will return
    // that permutation else it will return -1
    static boolean isSubSeqDivisible(String str)
    {
  
        int i, j, k, l = str.length();
        int arr[] = new int[l];
  
        // Generating all possible permutations
        // and checking if any such
        // permutation is divisible by 8
        for (i = 0; i < l; i++) {
            for (j = i; j < l; j++) {
                for (k = j; k < l; k++) {
                    if (arr[i] % 8 == 0)
                        return true;
  
                    else if ((arr[i] * 10 + arr[j]) % 8 == 0 && i != j)
                        return true;
  
                    else if ((arr[i] * 100 + arr[j] * 10 + arr[k]) % 8 == 0
                             && i != j && j != k && i != k)
                        return true;
                }
            }
        }
        return false;
    }
  
    // Driver function
    public static void main(String args[])
    {
  
        String str = "3144";
        if (isSubSeqDivisible(str))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}
  
// This code is contributed by Nikita Tiwari.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program to
# check if a subsequence of digits
# is divisible by 8.
  
# Function to calculate any
# permutation divisible
# by 8. If such permutation
# exists, the function
# will return that permutation
# else it will return -1
def isSubSeqDivisible(st) :
  
    l = len(st)
    arr = [0] * l
  
    # Generating all possible
    # permutations and checking
    # if any such permutation
    # is divisible by 8
    for i in range(0, l) :
        for j in range(i, l) :
            for k in range(j, l) :
                if (arr[i] % 8 == 0) :
                    return True
   
                elif ((arr[i]*10 + arr[j])% 8 == 0 and i != j) :
                    return True
   
                elif ((arr[i] * 100 + arr[j] * 10 + arr[k]) % 8 == 0 and i != j and j != k and i != k) :
                    return True
             
    return False
   
# Driver function
  
st = "3144"
if (isSubSeqDivisible(st)) :
    print("Yes")
else :
    print("No")
  
# This code is contributed
# by Nikita Tiwari.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to check if a subsequence
// of digits is divisible by 8.
using System;
  
class GFG {
  
    // Function to calculate any permutation
    // divisible by 8. If such permutation
    // exists, the function will return
    // that permutation else it will return -1
    static bool isSubSeqDivisible(string str)
    {
        int i, j, k, l = str.Length;
        int[] arr = new int[l];
  
        // Generating all possible permutations
        // and checking if any such
        // permutation is divisible by 8
        for (i = 0; i < l; i++) {
            for (j = i; j < l; j++) {
                for (k = j; k < l; k++) {
                    if (arr[i] % 8 == 0)
                        return true;
  
                    else if ((arr[i] * 10 + arr[j])
                                     % 8
                                 == 0
                             && i != j)
                        return true;
  
                    else if ((arr[i] * 100 + arr[j] * 10 + arr[k]) % 8 == 0
                             && i != j && j != k
                             && i != k)
                        return true;
                }
            }
        }
  
        return false;
    }
  
    // Driver function
    public static void Main()
    {
        string str = "3144";
  
        if (isSubSeqDivisible(str))
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
    }
}
  
// This code is contributed by vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to check if 
// a subsequence of digits
// is divisible by 8.
  
// Function to calculate any 
// permutation divisible
// by 8. If such permutation
// exists, the function
// will return that permutation 
// else it will return -1
  
function isSubSeqDivisible($str)
{
    // Generating all possible 
    // permutations and checking
    // if any such permutation 
    // is divisible by 8
    $l = strlen($str);
    for ( $i = 0; $i < $l ; $i++) 
    {
        for ($j = $i; $j <$l; $j++) 
        {
            for ($k = $j; $k < $l; $k++) 
            {
                if ($arr[$i] % 8 == 0)
                    return true;
  
                else if (($arr[$i] * 10 + 
                          $arr[$j]) % 8 == 0 && 
                          $i != $j)
                    return true;
  
                else if (($arr[$i] * 100 + $arr[$j] *
                          10 + $arr[$k]) % 8 == 0 && 
                          $i != $j && $j != $k && 
                          $i != $k)
                    return true;
            }
        }
    }
    return false;
}
  
// Driver Code
$str = "3144";
if (isSubSeqDivisible($str))
echo "Yes";
else
echo "No";
  
// This code is contributed by aj_36
?>

chevron_right