Count of pairs of (i, j) such that ((n % i) % j) % n is maximized in C++


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)

Approach used in the below program is as follows

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.

Efficient Approach

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.

Example (naive approach)

 Live Demo

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

Output

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

Example (Efficient Approach)

 Live Demo

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

Output

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

Updated on: 03-Dec-2020

71 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements