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 find number of pairs from N natural numbers whose sum values are divisible by k in Python
Suppose we have a number n and another value k. We need to find the total number of pairs from the first N natural numbers whose sum is divisible by k.
Given an array A with first N natural numbers [1, 2, 3, ..., n], we have to find pairs A[i] and A[j] where i < j and (A[i] + A[j]) % k == 0.
Example
If n = 10 and k = 4, the output will be 10 because there are 10 pairs whose sum is divisible by 4:
[(1,3), (1,7), (2,6), (2,10), (3,5), (3,9), (4,8), (5,7), (6,10), (7,9)]
Algorithm
To solve this problem efficiently, we follow these steps:
- Calculate m := floor of (n / k) and r := n mod k
- Create a frequency map to count numbers by their remainder when divided by k
- For each remainder i, find its complement (k - i) mod k
- Count valid pairs and handle special cases where remainder equals its complement
Implementation
def solve(n, k):
# Calculate base frequency for each remainder
m = n // k
r = n % k
# Initialize frequency map
b = {}
for i in range(k):
b[i] = m
# Add extra elements for remainders 1 to r
for i in range(m*k+1, n+1):
j = i % k
b[j] = b[j] + 1
# Count pairs
c = 0
for i in range(k):
i1 = i
i2 = (k - i) % k
if i1 == i2:
# Same remainder case (like remainder 0 with k=4, or remainder 2 with k=4)
c = c + b[i] * (b[i] - 1)
else:
# Different remainder case
c = c + b[i1] * b[i2]
return c // 2
# Test the function
n = 10
k = 4
result = solve(n, k)
print(f"Number of pairs from first {n} natural numbers divisible by {k}: {result}")
Number of pairs from first 10 natural numbers divisible by 4: 10
How It Works
The algorithm works by grouping numbers based on their remainder when divided by k:
- Remainder 0: Numbers like 4, 8 (for k=4)
- Remainder 1: Numbers like 1, 5, 9
- Remainder 2: Numbers like 2, 6, 10
- Remainder 3: Numbers like 3, 7
For two numbers to have a sum divisible by k, their remainders must sum to k (or 0). For example, remainder 1 pairs with remainder 3, and remainder 2 pairs with remainder 2.
Test with Different Values
def solve(n, k):
m = n // k
r = n % k
b = {}
for i in range(k):
b[i] = m
for i in range(m*k+1, n+1):
j = i % k
b[j] = b[j] + 1
c = 0
for i in range(k):
i1 = i
i2 = (k - i) % k
if i1 == i2:
c = c + b[i] * (b[i] - 1)
else:
c = c + b[i1] * b[i2]
return c // 2
# Test with different values
test_cases = [(10, 4), (6, 3), (8, 2)]
for n, k in test_cases:
result = solve(n, k)
print(f"n={n}, k={k}: {result} pairs")
n=10, k=4: 10 pairs n=6, k=3: 6 pairs n=8, k=2: 12 pairs
Conclusion
This algorithm efficiently finds pairs of natural numbers whose sum is divisible by k using remainder-based grouping. The time complexity is O(k) and space complexity is O(k), making it much faster than a brute force O(n²) approach.
