Python Program to check whether it is possible to make a divisible by 3 number using all digits in an array

In this article, we will learn how to check whether it's possible to make a number divisible by 3 using all digits from an array of integers.

Problem Statement

Given an array of integers, we need to determine if we can form a number using all the digits from these integers such that the resulting number would be divisible by 3.

Mathematical Concept

This solution is based on a fundamental property from number theory: a number is divisible by 3 if and only if the sum of its digits is divisible by 3. This means we don't need to form the actual number?we just need to check if the sum of all digits is divisible by 3.

Algorithm

The approach works as follows ?

  1. Extract all digits from each number in the array
  2. Calculate the sum of all digits
  3. Check if this sum is divisible by 3

Implementation

def isPossibleToMakeDivisible(arr, n):
    digit_sum = 0
    
    # Extract all digits and calculate their sum
    for i in range(n):
        num = arr[i]
        while num > 0:
            digit_sum += num % 10  # Get last digit
            num = num // 10        # Remove last digit
    
    # Check if sum is divisible by 3
    return digit_sum % 3 == 0

# Test case 1
arr1 = [33, 40, 90]
n1 = 3
if isPossibleToMakeDivisible(arr1, n1):
    print("Test 1 - Yes: Can form a number divisible by 3")
else:
    print("Test 1 - No: Cannot form a number divisible by 3")

# Test case 2
arr2 = [12, 34, 56]
n2 = 3
if isPossibleToMakeDivisible(arr2, n2):
    print("Test 2 - Yes: Can form a number divisible by 3")
else:
    print("Test 2 - No: Cannot form a number divisible by 3")
Test 1 - No: Cannot form a number divisible by 3
Test 2 - Yes: Can form a number divisible by 3

How It Works

Let's trace through the first example ?

  • Array: [33, 40, 90]
  • Digits: 3, 3, 4, 0, 9, 0
  • Sum: 3 + 3 + 4 + 0 + 9 + 0 = 19
  • 19 % 3 = 1 (not divisible by 3)
  • Result: No

For the second example ?

  • Array: [12, 34, 56]
  • Digits: 1, 2, 3, 4, 5, 6
  • Sum: 1 + 2 + 3 + 4 + 5 + 6 = 21
  • 21 % 3 = 0 (divisible by 3)
  • Result: Yes

Optimized Version

We can simplify the function using modular arithmetic ?

def isPossibleToMakeDivisible(arr):
    total_remainder = 0
    
    for num in arr:
        # Add remainder of current number when divided by 3
        total_remainder = (total_remainder + num) % 3
    
    return total_remainder == 0

# Test with the same examples
print("Array [33, 40, 90]:", "Yes" if isPossibleToMakeDivisible([33, 40, 90]) else "No")
print("Array [12, 34, 56]:", "Yes" if isPossibleToMakeDivisible([12, 34, 56]) else "No")
Array [33, 40, 90]: No
Array [12, 34, 56]: Yes

Key Points

  • Divisibility Rule: A number is divisible by 3 if the sum of its digits is divisible by 3
  • Efficiency: We don't need to form the actual number, just check the sum
  • Modular Arithmetic: Using modulo operation prevents integer overflow for large sums

Conclusion

This approach efficiently determines if digits from an array can form a number divisible by 3 using the mathematical property that divisibility by 3 depends only on the sum of digits. The time complexity is O(n) where n is the number of elements in the array.

Updated on: 2026-03-25T06:34:30+05:30

354 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements