Problem Β· Array

Nums That Are Divisible by N πŸ‹

Learn this problem
● EasyTwo Sigma logoTwo SigmaINTERNOA

Problem 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[]) β†’ long

Examples

Example 1

N = 3arr = [1,2,3,4,5]return = 4

There are four valid pairs:

  • arr[0] + arr[1] = 1 + 2 = 3
  • arr[0] + arr[4] = 1 + 5 = 6
  • arr[1] + arr[3] = 2 + 4 = 6
  • arr[3] + arr[4] = 4 + 5 = 9

Each sum is divisible by 3, so the function returns 4.

Constraints

  • 1 <= arr.length <= 10^5
  • 1 <= arr[i] <= 10^9
  • 1 <= N <= 10^9

More Two Sigma problems

drafts saved locally
public long sumBeingAbletoBeDivisibleByN(int N, int[] arr) {
    // write your code here
}
N3
arr[1,2,3,4,5]
expected4
checking account