You are given an integer N. Is there a permutation of digits of integer that’s divisible by 8? A permutation of digits of integer N is defined as an integer formed by rearranging the digits of N. For example, if the number N = 123, then {123, 132, 213, 231, 312, 321} are the possible permutations.
Input Format
The first line contains an integer T i.e. number of test cases.
T lines follow, each containing the integer N.
The first line contains an integer T i.e. number of test cases.
T lines follow, each containing the integer N.
Output Format
For each test case print
For each test case print
YES
if there exists one such re-arrangement of N such that it is divisible by 8 or NO
if there isn’t.
Constraints
1 <= T <= 45
0 <= N <= 10110
1 <= T <= 45
0 <= N <= 10110
Note
Re-arrangements of 10 are {10, 01} which boils down to {10, 1}.
Re-arrangements of 10 are {10, 01} which boils down to {10, 1}.
Sample Input
2
61
75
Sample Output
YES
NO
Explanation
Test case #00: 16 is permutation of 61 which is divisible by 8.
Test case #01: None of permutation of 75, {57, 75}, are divisible by 8.
Test case #00: 16 is permutation of 61 which is divisible by 8.
Test case #01: None of permutation of 75, {57, 75}, are divisible by 8.
solution -----> we know that if last 3 digit of a number is divisible by 8 then the whole no is divisible by 8 .
code-->
import java.util.*;
class Solution{
static boolean ok(int i ,int j ,int k , String s){
if((s.charAt(i)*100 + s.charAt(j)*10 + s.charAt(k))%8==0)
return true ;
else
return false ;
}
static void check(String z){
int si = z.length();
if(si==0){
System.out.println("NO");
return ;
}
if(si==1){
if(Integer.parseInt(z)%8==0){
System.out.println("YES");
return ;
}
System.out.println("NO");
return ;
}
if(si==2){
int a = z.charAt(0)-48 ;
int b = z.charAt(1)-48 ;
if(((a*10+b)%8==0) || ((b*10+a)%8==0)){
System.out.println("YES");
return ;
}
System.out.println("NO");
return ;
}
for(int i=0 ; i < si-2 ;i++){
for(int j=i+1 ; j < si-1 ; j++){
for(int k = j+1 ; k < si ; k++ ){
if(ok(i,j,k,z) || ok(i,k,j,z) || ok(j,i,k,z) || ok(j,k,i,z) || ok(k,j,i,z) || ok(k,i,j,z) ){
System.out.println("YES");
return ;
}
}
}
}
System.out.println("NO");
}
public static void main(String ss[]){
Scanner s = new Scanner(System.in);
String a = s.nextLine();
boolean answer =false;
int t = Integer.parseInt(a);
for(int i=0;i<t;i++){
String z = s.nextLine();
check(z);
}
}
}
No comments:
Post a Comment