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 count indices pairs for which elements sum is power of 2 in Python
Given a list of numbers, we need to find the number of index pairs (i, j) where i < j such that nums[i] + nums[j] equals a power of 2 (2^k for some k ? 0).
Problem Understanding
For the input nums = [1, 2, 6, 3, 5], we have three valid pairs:
(6, 2): sum is 8 = 2^3
(5, 3): sum is 8 = 2^3
(1, 3): sum is 4 = 2^2
Algorithm Approach
We use a frequency counter to track elements we've seen. For each element x, we check if there exists a previously seen element y such that x + y equals a power of 2:
Initialize result counter and frequency map
-
For each element x in the array:
Check all powers of 2 from 2^0 to 2^31
For each power 2^j, check if (2^j - x) exists in our frequency map
Add the frequency count to our result
Update frequency map with current element
Implementation
from collections import Counter
def solve(nums):
res, c = 0, Counter()
for x in nums:
for j in range(32): # Check powers of 2 from 2^0 to 2^31
res += c[(1 << j) - x] # (1 << j) is 2^j
c[x] += 1
return res
# Test with the given example
nums = [1, 2, 6, 3, 5]
result = solve(nums)
print(f"Number of valid pairs: {result}")
Number of valid pairs: 3
How It Works
The algorithm uses bit shifting (1 << j) to generate powers of 2 efficiently. For each element, it checks if there's a complement that would sum to any power of 2. Since we process elements left to right and only look for previously seen elements, we ensure i < j condition is satisfied.
Step-by-Step Execution
from collections import Counter
def solve_with_debug(nums):
res, c = 0, Counter()
print("Processing elements:")
for i, x in enumerate(nums):
print(f"\nElement {x} at index {i}:")
pairs_found = 0
for j in range(32):
power_of_2 = 1 << j
complement = power_of_2 - x
if complement in c and c[complement] > 0:
pairs_found += c[complement]
print(f" Found {c[complement]} pair(s) with sum {power_of_2} (complement: {complement})")
res += pairs_found
c[x] += 1
print(f" Total pairs so far: {res}")
return res
nums = [1, 2, 6, 3, 5]
result = solve_with_debug(nums)
Processing elements: Element 1 at index 0: Total pairs so far: 0 Element 2 at index 1: Total pairs so far: 0 Element 6 at index 2: Found 1 pair(s) with sum 8 (complement: 2) Total pairs so far: 1 Element 3 at index 3: Found 1 pair(s) with sum 4 (complement: 1) Total pairs so far: 2 Element 5 at index 4: Found 1 pair(s) with sum 8 (complement: 3) Total pairs so far: 3
Time and Space Complexity
Time Complexity: O(n × 32) = O(n), where n is the length of the array
Space Complexity: O(n) for the frequency counter
Conclusion
This solution efficiently finds pairs whose sum is a power of 2 by using a frequency counter and checking all possible powers of 2 up to 2^31. The key insight is processing elements sequentially while maintaining a count of previously seen elements.
