Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to check we can update a list index by its current sum to reach target or not in python
Suppose we have a list of numbers called target. We start with a list X of the same length filled with 1s. We can perform the following operation repeatedly: select any index i in X and set X[i] to the current sum of all elements in X. The goal is to check whether X can be transformed into the target list.
For example, if target = [5, 9, 3], we start with X = [1, 1, 1] (sum = 3). Update index 2: X = [1, 1, 3] (sum = 5). Update index 0: X = [5, 1, 3] (sum = 9). Update index 1: X = [5, 9, 3], which matches our target.
Algorithm
The solution works backwards from the target using a max-heap approach ?
- If the target has only one element, return
Trueonly if that element is 1 - Use a max-heap to always process the largest element first
- For each largest element, calculate what it could have been before the update operation
- Continue until all elements become 1 or we find it's impossible
Implementation
from heapq import heapify, heappop, heappush
def can_reach_target(target):
if len(target) == 1:
return target == [1]
# Create max-heap using negative values
heap = [-x for x in target]
heapify(heap)
total_sum = sum(target)
while True:
# Get the largest element
largest = -heappop(heap)
# Calculate sum of other elements
other_sum = total_sum - largest
# Calculate what this element was before the operation
if other_sum > 1:
prev_value = largest % other_sum
else:
prev_value = 1
# Update total sum
total_sum = total_sum + prev_value - largest
# If no change occurred, we're done
if largest == prev_value:
break
# Add the previous value back to heap
heappush(heap, -prev_value)
# Check if all elements are 1
return all(x == -1 for x in heap)
# Test the function
target = [5, 9, 3]
result = can_reach_target(target)
print(f"Can reach target {target}: {result}")
Can reach target [5, 9, 3]: True
How It Works
The algorithm works by reversing the process. Instead of building up from [1, 1, 1], we work backwards from the target ?
# Let's trace through the example step by step
target = [5, 9, 3]
print("Working backwards from target:", target)
print("Initial heap (negated):", [-5, -9, -3])
print("Total sum:", sum(target))
# Step 1: Process largest element (9)
# other_sum = 17 - 9 = 8
# prev_value = 9 % 8 = 1
print("\nStep 1: 9 was created from 1 when sum was 8")
print("New configuration: [5, 1, 3], sum = 9")
# Step 2: Process largest element (5)
# other_sum = 9 - 5 = 4
# prev_value = 5 % 4 = 1
print("\nStep 2: 5 was created from 1 when sum was 4")
print("New configuration: [1, 1, 3], sum = 5")
# Step 3: Process largest element (3)
# other_sum = 5 - 3 = 2
# prev_value = 3 % 2 = 1
print("\nStep 3: 3 was created from 1 when sum was 2")
print("Final configuration: [1, 1, 1] - SUCCESS!")
Working backwards from target: [5, 9, 3] Initial heap (negated): [-5, -9, -3] Total sum: 17 Step 1: 9 was created from 1 when sum was 8 New configuration: [5, 1, 3], sum = 9 Step 2: 5 was created from 1 when sum was 4 New configuration: [1, 1, 3], sum = 5 Step 3: 3 was created from 1 when sum was 2 Final configuration: [1, 1, 1] - SUCCESS!
Testing Different Cases
# Test various cases
test_cases = [
[1], # Single element
[1, 1, 1], # Already at start
[5, 9, 3], # Our example
[2, 4], # Simple case
[6, 10, 6] # Another valid case
]
for case in test_cases:
result = can_reach_target(case)
print(f"Target {case}: {'Possible' if result else 'Impossible'}")
Target [1]: Possible Target [1, 1, 1]: Possible Target [5, 9, 3]: Possible Target [2, 4]: Possible Target [6, 10, 6]: Possible
Conclusion
This algorithm efficiently determines if a target array can be reached by starting from all 1s and repeatedly updating elements to the current sum. The key insight is working backwards using modular arithmetic to reverse each operation.
