Digit Rearrangement Divisible By 8
Preview mode. Log in to edit, run, submit, and save progress.
Medium
Digit Rearrangement Divisible By 8
A security system receives numbers as strings. For each string, you may rearrange its digits in any order. Determine whether at least one rearrangement forms an integer divisible by 8. Return YES for each string where such an arrangement exists, otherwise return NO.
Examples
Example 1
Input:
3 61 123 111
Output:
YES YES NO
Explanation: 61 can be rearranged as 16, which is divisible by 8. 123 can be rearranged as 312, which is divisible by 8. No rearrangement of 111 is divisible by 8.
Approach hint
A number is divisible by 8 if its last three digits form a number divisible by 8.
Common mistake
Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.
solution.cpp