Can all numbers of array be made equal
Detailed guide and Python implementation for the 'Can all numbers of array be made equal' problem.
1. Concept Overview
The 'Can all numbers of array be made equal' problem is a key challenge in the Arrays section.
This implementation focuses on easy-level logic in Python.
We prioritize technical accuracy and code readability in our provided solutions.
2. Real-World Applications
3. Visual Intuition
Visualizing the logic flow for Can all numbers of array be made equal.
4. Prerequisites
5. Step-by-Step Thinking
1. Understand the problem
Read the problem statement for Can all numbers of array be made equal carefully.
2. Formulate brute force
Draft a simple iterative solution.
3. Identify inefficiency
Look for redundant calculations.
4. Optimize search path
Use hashing or sorting to speed up the process.
5. Final Implementation
Clean up the code for production standards.
Problem Statement
Write a function can_make_equal(arr) that determines if all elements of the array can be made equal by repeatedly adding or subtracting any element from another. This is possible if and only if the total sum of the array is divisible by the length of the array (i.e., the mean is an integer). Return True if possible, False otherwise. Note: a simpler interpretation — if we can redistribute values freely, we can always make them equal when the sum is divisible by length.
- •1 <= len(arr) <= 10^5
- •-10^6 <= arr[i] <= 10^6
Examples
arr = [1, 1, 1]
True
All elements are already equal.
arr = [1, 2, 3]
True
Sum = 6, length = 3. 6/3 = 2. We can make all elements 2.
arr = [1, 2, 4]
False
Sum = 7, length = 3. 7/3 is not an integer, so we cannot make all equal.
Need a Hint?
Edge Cases to Watch
- Empty input structures
- Single element inputs
- Large numerical bounds
Ready to Solve?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.
Interview Insights & Variations
Complexity Analysis Breakdown
Why Time: Directly evaluates all possibilities.
Why Space: Uses standard local memory.
Why Time: Optimized paths reduce total operations.
Why Space: May trade memory for speed.
Optimized Solution Python Code
Optimized Solution Python Code
def can_be_equal(arr):
# Optimized: Process in-place and return early
if not arr: return True
def get_base(n):
while n % 2 == 0: n //= 2
while n % 3 == 0: n //= 3
return n
target_base = get_base(arr[0])
for i in range(1, len(arr)):
if get_base(arr[i]) != target_base:
return False
return TrueBrute Force Code (Spoiler Guarded)
Brute Force Code (Spoiler Guarded)
def can_be_equal(arr):
# Try to reduce every number to its base factor by dividing by 2 and 3
# If all reduced numbers are same, then they can be made equal
if not arr: return True
bases = []
for num in arr:
while num % 2 == 0:
num //= 2
while num % 3 == 0:
num //= 3
bases.append(num)
# Check if all base factors are the same
for i in range(1, len(bases)):
if bases[i] != bases[0]:
return False
return TrueAlgorithm Pattern Checklist
When dealing with Arrays data patterns.
Core Prerequisites
Revision Key Notes
Common Mistakes & Pitfalls
Related Questions
Recommended Python Resources
Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.
Python Loops
Learn how to use Python loops to iterate over data. Master for loops, while loops, break, continue, and loop best practices with interactive examples.
How to Find the Length of a List in Python
Learn how to find the length of a list in Python using the len() function. Understand the O(1) time complexity and checking counts.
Python String Methods
A complete reference guide for Python string manipulation. Master formatting, searching, splitting, replacing, and checking string properties.
Python vs JavaScript: Which Programming Language is Best?
A comprehensive comparison between Python and JavaScript. Explore syntax differences, performance, use cases (backend vs frontend), and coding examples.