You are viewing a single comment's thread. Return to all comments →
Yes, this is exactly the algorithm I came up with too. Wrote mine in Swift:
import Foundation let readSplitter: () -> Array<Int> = { readLine()!.components(separatedBy: " ").map{Int($0)!}} let nk = readSplitter() let k = nk[1] let values = readSplitter() var buckets = [Int](repeating: 0, count: k) var pairCount = 0 for value in values { let modded_value = value % k let complement = (k - modded_value) % k pairCount += buckets[complement] buckets[modded_value] += 1 } print(pairCount)
Divisible Sum Pairs
You are viewing a single comment's thread. Return to all comments →
Yes, this is exactly the algorithm I came up with too. Wrote mine in Swift: