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
Find all good indices in the given Array in Pytho
Given an array of numbers, we need to find all indices where removing that element results in a good array. A good array is one where at least one element equals the sum of all other elements in the array.
The problem uses 1-based indexing, meaning the first element is at index 1, not 0.
Understanding the Problem
For the array [10, 4, 6, 2]:
- Removing index 1 (element 10): [4, 6, 2] ? 6 = 4 + 2 ?
- Removing index 2 (element 4): [10, 6, 2] ? 10 ? 6 + 2 ?
- Removing index 3 (element 6): [10, 4, 2] ? 10 ? 4 + 2 ?
- Removing index 4 (element 2): [10, 4, 6] ? 10 = 4 + 6 ?
So the answer is [1, 4].
Algorithm Approach
The key insight is that after removing element A[i], if the remaining array has sum S, then for it to be good, there must exist an element x such that x = (S - x), which means x = S/2.
Steps:
- Calculate total sum of the array
- For each element, find the sum after removing it
- Check if half of that sum exists in the remaining elements
- Handle the case where the target element appears multiple times
Implementation
from collections import defaultdict
def find_good_indices(arr):
n = len(arr)
total_sum = sum(arr)
frequency = defaultdict(int)
# Count frequency of each element
for num in arr:
frequency[num] += 1
good_indices = []
for i in range(n):
# Sum after removing arr[i]
remaining_sum = total_sum - arr[i]
# For good array, we need an element equal to half of remaining sum
if remaining_sum % 2 == 0:
target = remaining_sum // 2
if target in frequency:
# If target is the removed element, check if it appears more than once
# If target is different from removed element, it's valid
if (arr[i] == target and frequency[target] > 1) or (arr[i] != target):
good_indices.append(i + 1) # 1-based indexing
return good_indices
# Test with the example
arr = [10, 4, 6, 2]
result = find_good_indices(arr)
print("Good indices:", result)
Good indices: [1, 4]
Step-by-Step Execution
For array [10, 4, 6, 2]:
arr = [10, 4, 6, 2]
total_sum = 22
frequency = {10: 1, 4: 1, 6: 1, 2: 1}
# Index 0 (1-based: 1): Remove 10
remaining_sum = 22 - 10 = 12
target = 12 // 2 = 6
# 6 exists and 10 != 6, so index 1 is good
# Index 1 (1-based: 2): Remove 4
remaining_sum = 22 - 4 = 18
target = 18 // 2 = 9
# 9 doesn't exist, so index 2 is not good
# Index 2 (1-based: 3): Remove 6
remaining_sum = 22 - 6 = 16
target = 16 // 2 = 8
# 8 doesn't exist, so index 3 is not good
# Index 3 (1-based: 4): Remove 2
remaining_sum = 22 - 2 = 20
target = 20 // 2 = 10
# 10 exists and 2 != 10, so index 4 is good
print("Result: [1, 4]")
Result: [1, 4]
Time and Space Complexity
- Time Complexity: O(n) where n is the array length
- Space Complexity: O(n) for storing element frequencies
Conclusion
To find good indices, we check if removing each element creates an array where half the remaining sum exists as an element. The algorithm efficiently uses frequency counting to handle duplicate elements and runs in linear time.
