- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures

- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces

# Count pairs (i,j) such that (i+j) is divisible by both A and B in C++

We are given variables N, M, A and B. The goal is to find ordered pairs of positive numbers( i, j ) such that their sum is divisible by both A and B. And 1<=i<=N and 1<=j<=M.

We will traverse using two loops for i and j. If sum (i+j)%A==0 && (i+j)%B==0. Increment count.

Let’s understand with examples.

**Input**

N = 5, M = 10, A = 2, B = 3;

**Output**

Ordered pairs (i,j) where (i+j) is divisible by both A & B: 9

**Explanation**

Pairs will be (1,5) (2,4) (2,10) (3,3) (3,9) (4,2) (4,8) (5,1) (5,7). Total pairs is 9.

**Input**

N = 10, M = 10, A = 10, B = 11;

**Output**

Ordered pairs (i,j) where (i+j) is divisible by both A & B: 0

**Explanation**

No such pairs possible.

## Approach used in the below program is as follows

We take integers N, M, A, B.

Function sumDivisible(int n,int m,int a,int b) takes all variables and returns the count of ordered pairs with sum divisible by A and B.

Take the initial variable count as 0 for pairs.

Traverse using two for loop to find i and j.

Start from i=1 to i<=n and j=1 to j<=m.

Check if (i+j)%a==0 or (i+j)%b==0.

If true increment count.

At the end of all loops count will have a total number of such pairs.

Return the count as result.

## Example

#include <bits/stdc++.h> using namespace std; int sumDivisible(int n,int m,int a,int b){ int count = 0; for (int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ if((i+j)%a==0 && (i+j)%b==0) { count++; } } } return count; } int main(){ int N = 50, M = 100, A = 5, B = 10; cout <<"Ordered pairs (i,j) where (i+j) is divisible by both A & B: "<<sumDivisible(N,M,A,B); return 0; }

## Output

If we run the above code it will generate the following output −

Ordered pairs (i,j) where (i+j) is divisible by both A & B: 500

- Related Articles
- Count the number of pairs (i, j) such that either arr[i] is divisible by arr[j] or arr[j] is divisible by arr[i] in C++
- Count of unique pairs (arr[i], arr[j]) such that i < j in C++
- Count of pairs of (i, j) such that ((n % i) % j) % n is maximized in C++
- Count unordered pairs (i,j) such that product of a[i] and a[j] is power of two in C++
- Count number of pairs (i, j) such that arr[i] * arr[j] > arr[i] + arr[j] in C++
- Count pairs in an array such that LCM(arr[i], arr[j]) > min(arr[i],arr[j]) in C++
- Count the triplets such that A[i] < B[j] < C[k] in C++
- Count pairs in an array that hold i*arr[i] > j*arr[j] in C++
- Count pairs in an array such that both elements has equal set bits in C++
- Rearrange an array such that ‘arr[j]’ becomes ‘i’ if ‘arr[i]’ is ‘j’ in C++
- Program to find number of pairs (i, j) such that ith and jth elements are same in Python
- Count of pairs from 1 to a and 1 to b whose sum is divisible by N in C++
- Rearrange array such that arr[i] >= arr[j] if i is even and arr[i]
- Count pairs in array whose sum is divisible by K in C++
- Count pairs in array whose sum is divisible by 4 in C++