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
Check if array sum can be made K by three operations on it in Python
Given an array of numbers and a target value K, we need to check if we can transform the array to have sum K by applying exactly one of three operations to each element:
- Make the number negative
- Add its index (1-based) to the number
- Subtract its index from the number
For example, with nums = [1,2,3,7] and k = 8, we can subtract indices from elements 2 and 3 to get [1, 0, 0, 7] with sum 8.
Approach Using Dynamic Programming
We use memoization to explore all possible combinations of operations. For each element, we try all three operations and check if any path leads to the target sum ?
def can_make_sum_k(nums, k):
memo = {}
def solve(i, current_sum):
# Base case: processed all elements
if i == len(nums):
return current_sum == k
# Check memoization
if (i, current_sum) in memo:
return memo[(i, current_sum)]
# Try all three operations on nums[i]
original_val = nums[i]
index = i + 1 # 1-based indexing
# Operation 1: Make negative
op1 = solve(i + 1, current_sum - 2 * original_val)
# Operation 2: Add index
op2 = solve(i + 1, current_sum + index)
# Operation 3: Subtract index
op3 = solve(i + 1, current_sum - index)
# Store result
memo[(i, current_sum)] = op1 or op2 or op3
return memo[(i, current_sum)]
# Start with original sum
original_sum = sum(nums)
return solve(0, original_sum)
# Test the function
nums = [1, 2, 3, 7]
k = 8
result = can_make_sum_k(nums, k)
print(f"Can make sum {k}: {result}")
Can make sum 8: True
How It Works
The algorithm explores all 3^n possible combinations where n is the array length. For each element at index i:
-
Operation 1: Changes
nums[i]to-nums[i], affecting sum by-2 * nums[i] -
Operation 2: Changes
nums[i]tonums[i] + (i+1), affecting sum by+(i+1) -
Operation 3: Changes
nums[i]tonums[i] - (i+1), affecting sum by-(i+1)
Step-by-Step Example
For nums = [1,2,3,7] and k = 8 with original sum = 13 ?
def trace_solution(nums, k):
def solve_with_trace(i, current_sum, operations):
if i == len(nums):
if current_sum == k:
print(f"Solution found: {operations}")
return True
return False
original_val = nums[i]
index = i + 1
# Try making negative
if solve_with_trace(i + 1, current_sum - 2 * original_val,
operations + [f"Make nums[{i}] negative"]):
return True
# Try adding index
if solve_with_trace(i + 1, current_sum + index,
operations + [f"Add index {index} to nums[{i}]"]):
return True
# Try subtracting index
if solve_with_trace(i + 1, current_sum - index,
operations + [f"Subtract index {index} from nums[{i}]"]):
return True
return False
original_sum = sum(nums)
solve_with_trace(0, original_sum, [])
nums = [1, 2, 3, 7]
k = 8
trace_solution(nums, k)
Solution found: ['Add index 1 to nums[0]', 'Subtract index 2 from nums[1]', 'Subtract index 3 from nums[2]', 'Add index 4 to nums[3]']
Conclusion
This dynamic programming approach efficiently checks if an array can be transformed to have sum K using the three allowed operations. The memoization prevents redundant calculations, making it practical for moderate-sized arrays.
