The goal is to explore all possible combinations that could lead to the maximum product of three integers. This includes:
- The three largest positive numbers.
- The two most negative numbers multiplied by the largest positive number.
The algorithm avoids sorting for performance and instead uses manual tracking of max/min values.
- Loop through the list once.
- Track the top 3 max numbers (
max1
,max2
,max3
). - Track the bottom 2 min numbers (
min1
,min2
). - Compare the product of:
max1 * max2 * max3
min1 * min2 * max1
- Return the greater value.
- A recursive version applies the same logic but passes updated variables with each call.
File Name | Description |
---|---|
Code.py |
Main iterative version of the algorithm. Efficient and avoids sorting. |
Recursive_Code.py |
Alternative recursive approach to calculate the result. |
Main.py |
Pseudocode-style breakdown of the logic used in the recursive algorithm. |
Pseudocode.md |
Logical flow documentation of the solution, useful for reports/presentation. |
arr = [-10, -3, 5, 6, -20]
print(max_product_of_three(arr))
# Output: 1200 → (-10 * -20 * 6)
- 🧮 Handles both negative and positive numbers.
- 🚀 Runs in linear time:
O(n)
- 📦 No external libraries needed.
- 🔀 Includes both iterative and recursive implementations.
- Install Python 3.x on your system.
- Clone or download the project files.
- Place all files in a single directory.
- Run the main version using:
python Code.py
Or test the recursive version:
python Recursive_Code.py
Project Leader: Amr Yasser Badr
Team Members:
- Youssef Mahmoud Younes
- Sama Saeed El-Fishawy