- 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 a number num as input. The goal is to find the number of pairs of form (i,j) such that ((num%i)%j)%num is maximized and i and j both are in range [1,num].

Let us understand with examples

**Input** − num=4

**Output** − Count of pairs of (i, j) such that ((n % i) % j) % n is maximized are − 3

**Explanation** − Pairs will be: (3,2), (3,3), (3,4)

**Input** − num=6

**Output** − Count of pairs of (i, j) such that ((n % i) % j) % n is maximized are − 4

**Explanation** − Pairs will be: (4,3, (4,4), (4,5), (4,6)

We will solve this problem using two approaches. In the naive approach, maximum remainder value can be obtained if we have num as its half. Set temp=num/2 +1. Set maximum remainder as total=num%temp. Traverse for i,j from 0 to num and find i,j such that ((num % i) % j) % num == total.

Take input num as integer.

Function maximized_pair(int num) takes num and returns the count of pairs of (i, j) such that ((n % i) % j) % n is maximized.

Take the initial count as 0.

Reduce num to half for maximum remainder. Set temp = ((num / 2) + 1).

Calculate maximum remainder as total = num % temp;

For pairs (i,j). Traverse using two for loops for i and j in the range [1,num].

If value ((num % i) % j) % num is equal to total, increment count.

At the end of both for loops return count as result.

Get **maximum remainder as total = num % temp** where **temp is num/2+1**. Now for pairs (i,j) we have to choose i as num to get the maximum remainder. We can choose j from the range total to num in order to make total as maximum. So the count of pairs will be **num-total**.

**In case num=2**, return 4 as pairs will be (1,1), (1,2), (2,1), (2,2) and will not be calculated using num-total.

Take input num as integer.

Function maximized_pair(int num) takes num and returns the count of pairs of (i, j) such that ((n % i) % j) % n is maximized.

Take the initial count as 0.

If num==2, return 4.

Else reduce num to half for maximum remainder. Set temp = ((num / 2) + 1).

Calculate maximum remainder as total = num % temp;

Set count=num-total

At the end return count as result.

#include<bits/stdc++.h> using namespace std; int maximized_pair(int num){ int count = 0; int temp = ((num / 2) + 1); int total = num % temp; for (int i = 1; i <= num; i++){ for (int j = 1; j <= num; j++){ int check = ((num % i) % j) % num; if (check == total){ count++; } } } return count; } int main(){ int num = 10; cout<<"Count of pairs of (i, j) such that ((n % i) % j) % n is maximized are: "<<maximized_pair(num); }

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

Count of pairs of (i, j) such that ((n % i) % j) % n is maximized are: 6

#include<bits/stdc++.h> using namespace std; int maximized_pair(int num){ int count = 0; if (num == 2){ return 4; } else{ int temp = ((num / 2) + 1); int total = num % temp; count = num - total; } return count; } int main(){ int num = 10; cout<<"Count of pairs of (i, j) such that ((n % i) % j) % n is maximized are: "<<maximized_pair(num); }

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

Count of pairs of (i, j) such that ((n % i) % j) % n is maximized are: 6

- Related Questions & Answers
- 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 pairs (i,j) such that (i+j) is divisible by both A and B in C++
- Count unordered pairs (i,j) such that product of a[i] and a[j] is power of two in C++
- 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 frequency of k in a matrix of size n where matrix(i, j) = i+j 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++
- Program to find a pair (i, j) where nums[i] + nums[j] + (i -j) is maximized in Python?
- Maximum difference of indices (i, j) such that A[i][j] = 0 in the given matrix 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 value of |arr[i] – arr[j] - + |i – j| in C++
- Count the triplets such that A[i] < B[j] < C[k] in C++
- Rearrange an array such that ‘arr[j]’ becomes ‘i’ if ‘arr[i]’ is ‘j’ in C++
- Maximize arr[j] – arr[i] + arr[l] – arr[k], such that i < j < k < l in C++

Advertisements