Check if any permutation of a large number is divisible by 8 | C ++ program to check if any permutation of a large number is divisible by 8 or not ; Function to check if any permutation of a large number is divisible by 8 ; Less than three digit number can be checked directly . ; check for the reverse of a number ; Stores the Frequency of characters in the n . ; Iterates for all three digit numbers divisible by 8 ; stores the frequency of all single digit in three - digit number ; check if the original number has the digit ; when all are checked its not possible ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool solve ( string n , int l ) { if ( l < 3 ) { if ( stoi ( n ) % 8 == 0 ) return true ; reverse ( n . begin ( ) , n . end ( ) ) ; if ( stoi ( n ) % 8 == 0 ) return true ; return false ; } int hash [ 10 ] = { 0 } ; for ( int i = 0 ; i < l ; i ++ ) hash [ n [ i ] - '0' ] ++ ; for ( int i = 104 ; i < 1000 ; i += 8 ) { int dup = i ; int freq [ 10 ] = { 0 } ; freq [ dup % 10 ] ++ ; dup = dup / 10 ; freq [ dup % 10 ] ++ ; dup = dup / 10 ; freq [ dup % 10 ] ++ ; dup = i ; if ( freq [ dup % 10 ] > hash [ dup % 10 ] ) continue ; dup = dup / 10 ; if ( freq [ dup % 10 ] > hash [ dup % 10 ] ) continue ; dup = dup / 10 ; if ( freq [ dup % 10 ] > hash [ dup % 10 ] ) continue ; return true ; } return false ; } int main ( ) { string number = "31462708" ; int l = number . length ( ) ; if ( solve ( number , l ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }