Python Program to find out how many cubes are cut

Suppose we have several cubes of dimensions a, b, and c, and using them we create a new box of dimension a×b×c. The values a, b, and c are pairwise co-prime, meaning gcd(a,b) = gcd(b,c) = gcd(a,c) = 1. We need to cut the box into two pieces with a single slice and determine how many individual cubes are cut into two pieces.

The cut is made through a plane that passes through specific vertices, creating a diagonal slice through the 3D box structure.

Problem Statement

Given an array containing multiple sets of three dimensions, we need to calculate how many cubes are cut for each set. The formula to calculate the number of cut cubes is:

(a × b + a × c + b × c - 1) ÷ 2

Algorithm

To solve this problem, we follow these steps ?

  • Create an empty output list
  • For each dimension set in the input array:
    • Extract values a, b, and c
    • Calculate (a × b + a × c + b × c - 1) ÷ 2
    • Apply modulo 1000000007 to handle large numbers
    • Add the result to the output list
  • Return the output list

Example

Let us see the implementation to get a better understanding ?

def solve(n, input_array):
    output = []
    for i in range(n):
        a, b, c = input_array[i][0], input_array[i][1], input_array[i][2]
        val = ((a * b + a * c + b * c - 1) // 2) % 1000000007
        output.append(val)
    return output

# Test with example data
n = 3
dimensions = [[1, 2, 3], [4, 2, 5], [6, 8, 2]]
result = solve(n, dimensions)
print(f"Input: {dimensions}")
print(f"Output: {result}")
Input: [[1, 2, 3], [4, 2, 5], [6, 8, 2]]
Output: [5, 18, 37]

How It Works

For each dimension set, the formula calculates:

  • [1, 2, 3]: (1×2 + 1×3 + 2×3 - 1) ÷ 2 = (2 + 3 + 6 - 1) ÷ 2 = 10 ÷ 2 = 5
  • [4, 2, 5]: (4×2 + 4×5 + 2×5 - 1) ÷ 2 = (8 + 20 + 10 - 1) ÷ 2 = 37 ÷ 2 = 18
  • [6, 8, 2]: (6×8 + 6×2 + 8×2 - 1) ÷ 2 = (48 + 12 + 16 - 1) ÷ 2 = 75 ÷ 2 = 37

Conclusion

This algorithm efficiently calculates the number of cubes that get cut when slicing a 3D box diagonally. The formula (a×b + a×c + b×c - 1) ÷ 2 provides the exact count of individual cubes affected by the diagonal cut through the box structure.

Updated on: 2026-03-25T20:28:35+05:30

294 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements