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

Given an integer n, the task is to count the number of pairs (i, j) such that ((n % i) % j) % n is maximized where 1 ≤ i, j ≤ n

Examples:

Input: n = 5
Output: 3
(3, 3), (3, 4) and (3, 5) are the only valid pairs.

Input: n = 55
Output: 28



Naive Approach: To obtain the maximum remainder value, n has to be divided by (n / 2) + 1. Store max = n % ((n / 2) + 1), now check for all possible values of i and j. If ((n % i) % j) % n = max then update count = count + 1. Print the count in the end.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP implementation of the approach
#include<bits/stdc++.h>
using namespace std;
  
// Function to return the count of required pairs
int countPairs(int n)
{
    // Number which will give the max value
    // for ((n % i) % j) % n
    int num = ((n / 2) + 1);
      
    // To store the maximum possible value of
    // ((n % i) % j) % n
    int max = n % num;
  
    // To store the count of possible pairs
    int count = 0;
  
    // Check all possible pairs
    for (int i = 1; i <= n; i++) 
    {
        for (int j = 1; j <= n; j++)
        {
  
            // Calculating the value of ((n % i) % j) % n
            int val = ((n % i) % j) % n;
  
            // If value is equal to maximum
            if (val == max)
                count++;
        }
    }
  
    // Return the number of possible pairs
    return count;
}
  
// Driver code
int main()
{
    int n = 5;
    cout << (countPairs(n));
}
  
// This code is contributed by 
// Surendra_Gangwar

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
class GFG {
  
    // Function to return the count of required pairs
    public static int countPairs(int n)
    {
        // Number which will give the max value
        // for ((n % i) % j) % n
        int num = ((n / 2) + 1);
  
        // To store the maximum possible value of
        // ((n % i) % j) % n
        int max = n % num;
  
        // To store the count of possible pairs
        int count = 0;
  
        // Check all possible pairs
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
  
                // Calculating the value of ((n % i) % j) % n
                int val = ((n % i) % j) % n;
  
                // If value is equal to maximum
                if (val == max)
                    count++;
            }
        }
  
        // Return the number of possible pairs
        return count;
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int n = 5;
        System.out.println(countPairs(n));
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
  
# Function to return the count of 
# required pairs
def countPairs(n):
  
    # Number which will give the Max 
    # value for ((n % i) % j) % n
    num = ((n // 2) + 1)
      
    # To store the Maximum possible value 
    # of ((n % i) % j) % n
    Max = n % num
  
    # To store the count of possible pairs
    count = 0
  
    # Check all possible pairs
    for i in range(1, n + 1):
      
        for j in range(1, n + 1):
  
            # Calculating the value of
            # ((n % i) % j) % n
            val = ((n % i) % j) % n
  
            # If value is equal to Maximum
            if (val == Max):
                count += 1
          
    # Return the number of possible pairs
    return count
  
# Driver code
n = 5
print(countPairs(n))
  
# This code is contributed
# by Mohit Kumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the above approach 
using System;
  
class GFG
  
// Function to return the count of required pairs 
static int countPairs(int n) 
    // Number which will give the max 
    // value for ((n % i) % j) % n 
    int num = ((n / 2) + 1) ;
  
    // To store the maximum possible value 
    // of ((n % i) % j) % n 
    int max = n % num; 
  
    // To store the count of possible pairs 
    int count = 0; 
  
    // Check all possible pairs 
    for (int i = 1; i <= n; i++) 
    
        for (int j = 1; j <= n; j++) 
        
  
            // Calculating the value of 
            // ((n % i) % j) % n 
            int val = ((n % i) % j) % n; 
  
            // If value is equal to maximum 
            if (val == max) 
                count++; 
        
    
  
    // Return the number of possible pairs 
    return count; 
  
// Driver code 
public static void Main() 
    int n = 5; 
    Console.WriteLine(countPairs(n)); 
}
  
// This code is contributed by Ryuga
  

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of the 
// approach Function to return 
// the count of required pairs
  
function countPairs($n)
{
    // Number which will give the max 
    // value for ((n % i) % j) % n
    $num = (($n / 2) + 1);
      
    // To store the maximum possible 
    // value of ((n % i) % j) % n
    $max = $n % $num;
  
    // To store the count of possible pairs
    $count = 0;
  
    // Check all possible pairs
    for ($i = 1; $i <= $n; $i++) 
    {
        for ($j = 1; $j <= $n; $j++)
        {
  
            // Calculating the value 
            // of ((n % i) % j) % n
            $val = (($n % $i) % $j) % $n;
  
            // If value is equal to maximum
            if ($val == $max)
                $count++;
        }
    }
  
    // Return the number of possible pairs
    return $count;
}
  
// Driver code
    $n = 5;
    echo (countPairs($n));
  
// This code is contributed by ajit..
?>

chevron_right


Output:

3

Time Complexity: O(n2)

Efficient Approach: Get the maximum value for remainder i.e. max = n % num where num = ((n / 2) + 1). Now i has to be chosen as num in order to obtain the maximum value and j can be chosen as any value from the range [max, n] because we don’t need to reduce the maximum value calculated and choosing j > max will not affect the previous value obtained. So the total pairs will be n – max.
This approach will not work for n = 2. This is because, for n = 2, maximum remainder will be 0 and n – max will give 2 but we know that the answer is 4. All possible pairs in this case are (1, 1), (1, 2), (2, 1) and (2, 2).

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include<bits/stdc++.h>
using namespace std;
  
// Function to return the count of
// required pairs
int countPairs(int n)
{
  
    // Special case
    if (n == 2)
        return 4;
  
    // Number which will give the max value
    // for ((n % i) % j) % n
    int num = ((n / 2) + 1);
  
    // To store the maximum possible value 
    // of ((n % i) % j) % n
    int max = n % num;
  
    // Count of possible pairs
    int count = n - max;
  
    return count;
}
  
// Driver code
int main()
{
    int n = 5;
    cout << countPairs(n);
}
  
// This code is contributed by Code_Mech.

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
class GFG {
  
    // Function to return the count of required pairs
    public static int countPairs(int n)
    {
  
        // Special case
        if (n == 2)
            return 4;
  
        // Number which will give the max value
        // for ((n % i) % j) % n
        int num = ((n / 2) + 1);
  
        // To store the maximum possible value of
        // ((n % i) % j) % n
        int max = n % num;
  
        // Count of possible pairs
        int count = n - max;
  
        return count;
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int n = 5;
        System.out.println(countPairs(n));
    }
}

chevron_right


Python3

# Python3 implementation of the approach

# Function to return the count of required pairs
def countPairs(n):

# Special case
if (n == 2):
return 4

# Number which will give the max value
# for ((n % i) % j) % n
num = ((n // 2) + 1);

# To store the maximum possible value
# of ((n % i) % j) % n
max = n % num;

# Count of possible pairs
count = n – max;

return count

# Driver code
if __name__ ==”__main__” :

n = 5;
print(countPairs(n));

# This code is contributed by Code_Mech

C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of above approach
using System;
  
class GFG
{
      
    // Function to return the count of required pairs
    static int countPairs(int n)
    {
  
        // Special case
        if (n == 2)
            return 4;
  
        // Number which will give the max value
        // for ((n % i) % j) % n
        int num = ((n / 2) + 1);
  
        // To store the maximum possible value of
        // ((n % i) % j) % n
        int max = n % num;
  
        // Count of possible pairs
        int count = n - max;
  
        return count;
    }
  
    // Driver code
    static public void Main ()
    {
            int n = 5;
        Console.WriteLine(countPairs(n));
    }
}
  
// This code is contributed by Tushil.. 

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of the approach
  
// Function to return the count
// of required pairs
function countPairs($n)
{
  
    // Special case
    if ($n == 2)
        return 4;
  
    // Number which will give the max 
    // value. for ((n % i) % j) % n
    $num = ((int)($n / 2) + 1);
  
    // To store the maximum possible 
    // value of((n % i) % j) % n
    $max = $n % $num;
  
    // Count of possible pairs
    $count = ($n - $max);
  
    return $count;
}
  
// Driver code
$n = 5;
echo (countPairs($n));
  
// This code is contributed by ajit
?>

chevron_right


Output:

3

Time Complexity: O(1)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.