Problem Β· Array
Nums That Are Divisible by N π
Learn this problemProblem statement
Given an integer N and an integer array arr, count the ways to choose two different indices i and j such that i < j and arr[i] + arr[j] is divisible by N.
Return the number of valid index pairs as a long.
Function
sumBeingAbletoBeDivisibleByN(N: int, arr: int[]) β longExamples
Example 1
N = 3arr = [1,2,3,4,5]return = 4There are four valid pairs:
arr[0] + arr[1] = 1 + 2 = 3arr[0] + arr[4] = 1 + 5 = 6arr[1] + arr[3] = 2 + 4 = 6arr[3] + arr[4] = 4 + 5 = 9
Each sum is divisible by 3, so the function returns 4.
Constraints
1 <= arr.length <= 10^51 <= arr[i] <= 10^91 <= N <= 10^9