Check if any permutation of a large number is divisible by 8
Last Updated :
05 Sep, 2022
Given a large number N and the task is to check if any permutation of a large number is divisible by 8.
Examples:
Input: N = 31462708
Output: Yes
Many of permutation of number N like
34678120, 34278160 are divisible by 8.
Input: 75
Output: No
A naive approach is to generate all permutations of the number N and check if(N % 8 == 0) and return true if any of the permutations is divisible by 8.
An efficient approach is to use the fact that if the last three digits of a number are divisible by 8, then the number is also divisible by 8. Below are the required steps:
- Use a hash-table to count the occurrences of all digits in the given number.
- Traverse for all three-digit numbers which are divisible by 8.
- Check if the digits in the three-digit number are in the hash-table.
- If yes a number can be formed by permutation where the last three digits combine to form the three-digit number.
- If none of the three-digit numbers can be formed, return false.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
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;
}
|
Java
import java.util.*;
class GFG
{
public static boolean solve(String n, int l)
{
if (l < 3 )
{
if (Integer.parseInt(n) % 8 == 0 )
return true ;
n = new String(( new StringBuilder()).append(n).reverse());
if (Integer.parseInt(n) % 8 == 0 )
return true ;
return false ;
}
int []hash = new int [ 10 ];
for ( int i = 0 ; i < l; i++)
hash[n.charAt(i) - '0' ]++;
for ( int i = 104 ; i < 1000 ; i += 8 )
{
int dup = i;
int []freq = new int [ 10 ];
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 ;
}
public static void main(String[] args)
{
String number = "31462708" ;
int l = number.length();
if (solve(number, l))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def solve(n, l):
if l < 3 :
if int (n) % 8 = = 0 :
return True
n = n[:: - 1 ]
if int (n) % 8 = = 0 :
return True
return False
hash = 10 * [ 0 ]
for i in range ( 0 , l):
hash [ int (n[i]) - 0 ] + = 1 ;
for i in range ( 104 , 1000 , 8 ):
dup = i
freq = 10 * [ 0 ]
freq[ int (dup % 10 )] + = 1 ;
dup = dup / 10
freq[ int (dup % 10 )] + = 1 ;
dup = dup / 10
freq[ int (dup % 10 )] + = 1 ;
dup = i
if (freq[ int (dup % 10 )] >
hash [ int (dup % 10 )]):
continue ;
dup = dup / 10 ;
if (freq[ int (dup % 10 )] >
hash [ int (dup % 10 )]):
continue ;
dup = dup / 10
if (freq[ int (dup % 10 )] >
hash [ int (dup % 10 )]):
continue ;
return True
return False
if __name__ = = "__main__" :
number = "31462708"
l = len (number)
if solve(number, l):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public static bool solve(String n, int l)
{
if (l < 3)
{
if ( int .Parse(n) % 8 == 0)
return true ;
n = reverse(n);
if ( int .Parse(n) % 8 == 0)
return true ;
return false ;
}
int []hash = new int [10];
for ( int i = 0; i < l; i++)
hash[n[i] - '0' ]++;
for ( int i = 104; i < 1000; i += 8)
{
int dup = i;
int []freq = new int [10];
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 ;
}
static String reverse(String input)
{
char [] a = input.ToCharArray();
int l, r = 0;
r = a.Length - 1;
for (l = 0; l < r; l++, r--)
{
char temp = a[l];
a[l] = a[r];
a[r] = temp;
}
return String.Join( "" ,a);
}
public static void Main(String[] args)
{
String number = "31462708" ;
int l = number.Length;
if (solve(number, l))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
PHP
<?php
error_reporting (0);
function solve( $n , $l )
{
if ( $l < 3)
{
if ( intval ( $n ) % 8 == 0)
return true;
strrev ( $n );
if ( intval ( $n ) % 8 == 0)
return true;
return false;
}
$hash [10] = array (0);
for ( $i = 0; $i < $l ; $i ++)
$hash [ $n [ $i ] - '0' ]++;
for ( $i = 104;
$i < 1000; $i += 8)
{
$dup = $i ;
$freq [10] = array (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;
}
$number = "31462708" ;
$l = strlen ( $number );
if (solve( $number , $l ))
echo "Yes" ;
else
echo "No" ;
|
Javascript
<script>
function solve(n, l) {
if (l < 3) {
if (parseInt(n) % 8 === 0) return true ;
n = reverse(n);
if (parseInt(n) % 8 === 0) return true ;
return false ;
}
var hash = new Array(10).fill(0);
for ( var i = 0; i < l; i++) hash[parseInt(n[i]) - 0]++;
for ( var i = 104; i < 1000; i += 8) {
var dup = i;
var freq = new Array(10).fill(0);
freq[parseInt(dup % 10)]++;
dup = dup / 10;
freq[parseInt(dup % 10)]++;
dup = dup / 10;
freq[parseInt(dup % 10)]++;
dup = i;
if (freq[parseInt(dup % 10)] > hash[parseInt(dup % 10)])
continue ;
dup = dup / 10;
if (freq[parseInt(dup % 10)] > hash[parseInt(dup % 10)])
continue ;
dup = dup / 10;
if (freq[parseInt(dup % 10)] > hash[parseInt(dup % 10)])
continue ;
return true ;
}
return false ;
}
function reverse(input) {
var a = input.split( "" );
var l,
r = 0;
r = a.length - 1;
for (l = 0; l < r; l++, r--) {
var temp = a[l];
a[l] = a[r];
a[r] = temp;
}
return a.join( "" );
}
var number = "31462708" ;
var l = number.length;
if (solve(number, l))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(L), where L is the number of digits in the number.
Auxiliary space: O(1) as it is using constant space
Share your thoughts in the comments
Please Login to comment...