We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Divisible Sum Pairs
Divisible Sum Pairs
Sort by
recency
|
2475 Discussions
|
Please Login in order to post a comment
For Python3 Platform
I wrote the code from scratch just to get more practice and time complexity of my code is O(n+k) and not O(n^2) which is way more efficient
def divisibleSumPairs(n, k, ar): count = 0
Input
n, k = map(int, input().split()) ar = list(map(int, input().split()))
Output
print(divisibleSumPairs(n, k, ar))
Mi solución en Python 3:
//Java solution` public static int divisibleSumPairs(int n, int k, List ar) { int pairs = 0; // include constraints if (k > 0 && 2 <= n && n <= 100) { for (int i = 0; i < n; i++) { for (int j = 1+i; j < n; j++) { int sum = ar.get(i) + ar.get(j); if (sum % k == 0) { pairs++; } } } }
`
//Java solution` public static int divisibleSumPairs(int n, int k, List ar) { int pairs = 0; // include constraints if (k > 0 && 2 <= n && n <= 100) { for (int i = 0; i < n; i++) { for (int j = 1+i; j < n; j++) { int sum = ar.get(i) + ar.get(j); if (sum % k == 0) { pairs++; } } } }
`