You are given a number. Your task is to check if there exists a permutation of the digits of this number which is divisible by 4.
Examples:
Input: 003
Output: true
Explanation: For 003, we have a permutation 300 which is divisible by 4.Input: 123456
Output: true
Explanation: For 123456, we have 123564 which is a permutation of 123456 and is divisible by 4.
Table of Content
[Naive Approach] Permutation Generation - O(n! × n) Time and O(n) Space
Generate all permutations of the string using recursion. For each permutation, check divisibility by 4 by examining last two digits (or last digit for single-digit numbers). Return true if any permutation forms a number divisible by 4.
- Use recursion to generate all permutations by swapping characters
- At base case, check if current permutation forms number divisible by 4
- For single digit, check if digit % 4 == 0
- For multi-digit, check if last two digits form number divisible by 4
#include <bits/stdc++.h>
using namespace std;
bool found = false;
void solve(string &s, int index)
{
if (found)
return;
int n = s.size();
if (index == n)
{
// Handle one-digit numbers.
if (n == 1)
{
if ((s[0] - '0') % 4 == 0)
found = true;
return;
}
// Only check the last two digits.
int lastTwo = (s[n - 2] - '0') * 10 + (s[n - 1] - '0');
if (lastTwo % 4 == 0)
found = true;
return;
}
for (int i = index; i < n; i++)
{
swap(s[index], s[i]);
solve(s, index + 1);
swap(s[index], s[i]);
}
}
int divisibleByFour(string s)
{
solve(s, 0);
return found;
}
int main()
{
string s = "4317";
if (divisibleByFour(s))
{
cout << "true";
}
else
{
cout << "false";
}
return 0;
}
import java.util.*;
class GfG {
static boolean found = false;
static void solve(char[] s, int index) {
if (found)
return;
int n = s.length;
if (index == n) {
// Handle one-digit numbers.
if (n == 1) {
if ((s[0] - '0') % 4 == 0)
found = true;
return;
}
// Only check the last two digits.
int lastTwo = (s[n - 2] - '0') * 10 + (s[n - 1] - '0');
if (lastTwo % 4 == 0)
found = true;
return;
}
for (int i = index; i < n; i++) {
char temp = s[index];
s[index] = s[i];
s[i] = temp;
solve(s, index + 1);
temp = s[index];
s[index] = s[i];
s[i] = temp;
}
}
static boolean divisibleByFour(String s) {
found = false;
solve(s.toCharArray(), 0);
return found;
}
public static void main(String[] args) {
String s = "4317";
if (divisibleByFour(s)) {
System.out.println("true");
} else {
System.out.println("false");
}
}
}
found = False
def solve(s, index):
global found
if found:
return
n = len(s)
if index == n:
# Handle one-digit numbers.
if n == 1:
if int(s[0]) % 4 == 0:
found = True
return
# Only check the last two digits.
last_two = int(s[-2:])
if last_two % 4 == 0:
found = True
return
for i in range(index, n):
# Swap
s_list = list(s)
s_list[index], s_list[i] = s_list[i], s_list[index]
s = ''.join(s_list)
solve(s, index + 1)
# Backtrack
s_list = list(s)
s_list[index], s_list[i] = s_list[i], s_list[index]
s = ''.join(s_list)
def divisibleByFour(s):
global found
found = False
solve(s, 0)
return found
if __name__ == "__main__":
s = "4317"
if divisibleByFour(s):
print("true")
else:
print("false")
using System;
class GfG {
static bool found = false;
static void solve(char[] s, int index) {
if (found)
return;
int n = s.Length;
if (index == n) {
// Handle one-digit numbers.
if (n == 1) {
if ((s[0] - '0') % 4 == 0)
found = true;
return;
}
// Only check the last two digits.
int lastTwo = (s[n - 2] - '0') * 10 + (s[n - 1] - '0');
if (lastTwo % 4 == 0)
found = true;
return;
}
for (int i = index; i < n; i++) {
char temp = s[index];
s[index] = s[i];
s[i] = temp;
solve(s, index + 1);
temp = s[index];
s[index] = s[i];
s[i] = temp;
}
}
static bool divisibleByFour(string s) {
found = false;
solve(s.ToCharArray(), 0);
return found;
}
static void Main(string[] args) {
string s = "4317";
if (divisibleByFour(s)) {
Console.WriteLine("true");
} else {
Console.WriteLine("false");
}
}
}
let found = false;
function solve(s, index) {
if (found)
return;
const n = s.length;
if (index === n) {
// Handle one-digit numbers.
if (n === 1) {
if (parseInt(s[0]) % 4 === 0)
found = true;
return;
}
// Only check the last two digits.
const lastTwo = parseInt(s.substring(n - 2));
if (lastTwo % 4 === 0)
found = true;
return;
}
for (let i = index; i < n; i++) {
// Swap
let sArr = s.split('');
[sArr[index], sArr[i]] = [sArr[i], sArr[index]];
let newS = sArr.join('');
solve(newS, index + 1);
// Backtrack (swap back)
sArr = newS.split('');
[sArr[index], sArr[i]] = [sArr[i], sArr[index]];
newS = sArr.join('');
s = newS;
}
}
function divisibleByFour(s) {
found = false;
solve(s, 0);
return found;
}
const s = "4317";
if (divisibleByFour(s)) {
console.log("true");
} else {
console.log("false");
}
Output
false
[Expected Approach] Last Two Digits Check - O(n²) Time and O(1) Space
A number is divisible by 4 if its last two digits form a number divisible by 4. Try every ordered pair of digits from the string as potential last two digits. If any pair is divisible by 4, a valid permutation exists.
- If n == 1, check if single digit is divisible by 4
- For each i from 0 to n-1
- For each j from i+1 to n-1
- Form number using s[i] as tens digit and s[j] as units digit
- If num % 4 == 0, return true
#include <bits/stdc++.h>
using namespace std;
bool divisibleByFour(string s)
{
int n = s.size();
// If the number has only one digit.
if (n == 1)
{
// Checking if the digit is divisible by 4.
int num = (s[0] - '0');
if (num % 4 == 0)
return true;
else
return false;
}
// Iterating over all possible pairs
// of digits in the number.
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
// Checking if the last two numbers are divisible by 4.
int num1 = (s[i] - '0') * 10 + (s[j] - '0');
int num2 = (s[j] - '0') * 10 + (s[i] - '0');
if (num1 % 4 == 0 or num2 % 4 == 0)
return true;
}
}
return false;
}
int main()
{
string s = "4317";
if (divisibleByFour(s))
{
cout << "true";
}
else
{
cout << "false";
}
return 0;
}
import java.util.Scanner;
public class Main {
public static boolean divisibleByFour(String s) {
int n = s.length();
// If the number has only one digit.
if (n == 1)
{
// Checking if the digit is divisible by 4.
int num = (s.charAt(0) - '0');
if (num % 4 == 0)
return true;
else
return false;
}
// Iterating over all possible pairs
// of digits in the number.
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
// Checking if the last two numbers are divisible by 4.
int num1 = (s.charAt(i) - '0') * 10 + (s.charAt(j) - '0');
int num2 = (s.charAt(j) - '0') * 10 + (s.charAt(i) - '0');
if (num1 % 4 == 0 || num2 % 4 == 0)
return true;
}
}
return false;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String s = "4317";
if (divisibleByFour(s))
{
System.out.println("true");
}
else
{
System.out.println("false");
}
}
}
def divisibleByFour(s):
n = len(s)
# If the number has only one digit.
if n == 1:
# Checking if the digit is divisible by 4.
num = int(s[0])
if num % 4 == 0:
return True
else:
return False
# Iterating over all possible pairs
# of digits in the number.
for i in range(n):
for j in range(i + 1, n):
# Checking if the last two numbers are divisible by 4.
num1 = int(s[i]) * 10 + int(s[j])
num2 = int(s[j]) * 10 + int(s[i])
if num1 % 4 == 0 or num2 % 4 == 0:
return True
return False
s = "4317"
if divisibleByFour(s):
print('true')
else:
print('false')
using System;
public class Program
{
public static bool divisibleByFour(string s)
{
int n = s.Length;
// If the number has only one digit.
if (n == 1)
{
// Checking if the digit is divisible by 4.
int num = (s[0] - '0');
if (num % 4 == 0)
return true;
else
return false;
}
// Iterating over all possible pairs
// of digits in the number.
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
// Checking if the last two numbers are divisible by 4.
int num1 = (s[i] - '0') * 10 + (s[j] - '0');
int num2 = (s[j] - '0') * 10 + (s[i] - '0');
if (num1 % 4 == 0 || num2 % 4 == 0)
return true;
}
}
return false;
}
public static void Main()
{
string s = "4317";
if (divisibleByFour(s))
{
Console.WriteLine("true");
}
else
{
Console.WriteLine("false");
}
}
}
function divisibleByFour(s) {
let n = s.length;
// If the number has only one digit.
if (n == 1) {
// Checking if the digit is divisible by 4.
let num = (s.charCodeAt(0) - '0'.charCodeAt(0));
if (num % 4 == 0)
return true;
else
return false;
}
// Iterating over all possible pairs
// of digits in the number.
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
// Checking if the last two numbers are divisible by 4.
let num1 = (s.charCodeAt(i) - '0'.charCodeAt(0)) * 10 + (s.charCodeAt(j) - '0'.charCodeAt(0));
let num2 = (s.charCodeAt(j) - '0'.charCodeAt(0)) * 10 + (s.charCodeAt(i) - '0'.charCodeAt(0));
if (num1 % 4 == 0 || num2 % 4 == 0)
return true;
}
}
return false;
}
let s = "4317";
if (divisibleByFour(s)) {
console.log('true');
} else {
console.log('false');
}
Output
false