- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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.

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.

#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; }

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 Questions & Answers
- 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 pairs of (i, j) such that ((n % i) % j) % n is maximized in C++
- Count number of pairs (i, j) such that arr[i] * arr[j] > arr[i] + arr[j] in C++
- Count of unique pairs (arr[i], arr[j]) such that i < j in C++
- Count unordered pairs (i,j) such that product of a[i] and a[j] is power of two in C++
- Count pairs in an array such that LCM(arr[i], arr[j]) > min(arr[i],arr[j]) in C++
- Count pairs in an array that hold i*arr[i] > j*arr[j] in C++
- Count the triplets such that A[i] < B[j] < C[k] in C++
- Rearrange array such that arr[i] >= arr[j] if i is even and arr[i]<=arr[j] if i is odd and j < i in C++
- Maximum difference of indices (i, j) such that A[i][j] = 0 in the given matrix in C++
- Program to find a pair (i, j) where nums[i] + nums[j] + (i -j) is maximized in Python?
- Minimize (max(A[i], B[j], C[k]) – min(A[i], B[j], C[k])) of three different sorted arrays in C++
- Minimize (max(A[i], B[j], C[k]) – min(A[i], B[j], C[k])) of three different sorted arrays in Python
- Count frequency of k in a matrix of size n where matrix(i, j) = i+j in C++
- Rearrange an array such that ‘arr[j]’ becomes ‘i’ if ‘arr[i]’ is ‘j’ in C++

Advertisements