Count of pairs of (i, j) such that ((n % i) % j) % n is maximized
Last Updated :
11 Aug, 2022
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++
#include<bits/stdc++.h>
using namespace std;
int countPairs( int n)
{
int num = ((n / 2) + 1);
int max = n % num;
int count = 0;
for ( int i = 1; i <= n; i++)
{
for ( int j = 1; j <= n; j++)
{
int val = ((n % i) % j) % n;
if (val == max)
count++;
}
}
return count;
}
int main()
{
int n = 5;
cout << (countPairs(n));
}
|
Java
class GFG {
public static int countPairs( int n)
{
int num = ((n / 2 ) + 1 );
int max = n % num;
int count = 0 ;
for ( int i = 1 ; i <= n; i++) {
for ( int j = 1 ; j <= n; j++) {
int val = ((n % i) % j) % n;
if (val == max)
count++;
}
}
return count;
}
public static void main(String[] args)
{
int n = 5 ;
System.out.println(countPairs(n));
}
}
|
Python3
def countPairs(n):
num = ((n / / 2 ) + 1 )
Max = n % num
count = 0
for i in range ( 1 , n + 1 ):
for j in range ( 1 , n + 1 ):
val = ((n % i) % j) % n
if (val = = Max ):
count + = 1
return count
n = 5
print (countPairs(n))
|
C#
using System;
class GFG
{
static int countPairs( int n)
{
int num = ((n / 2) + 1) ;
int max = n % num;
int count = 0;
for ( int i = 1; i <= n; i++)
{
for ( int j = 1; j <= n; j++)
{
int val = ((n % i) % j) % n;
if (val == max)
count++;
}
}
return count;
}
public static void Main()
{
int n = 5;
Console.WriteLine(countPairs(n));
}
}
|
PHP
<?php
function countPairs( $n )
{
$num = (( $n / 2) + 1);
$max = $n % $num ;
$count = 0;
for ( $i = 1; $i <= $n ; $i ++)
{
for ( $j = 1; $j <= $n ; $j ++)
{
$val = (( $n % $i ) % $j ) % $n ;
if ( $val == $max )
$count ++;
}
}
return $count ;
}
$n = 5;
echo (countPairs( $n ));
?>
|
Javascript
<script>
function countPairs(n)
{
let num = (parseInt(n / 2, 10) + 1) ;
let max = n % num;
let count = 0;
for (let i = 1; i <= n; i++)
{
for (let j = 1; j <= n; j++)
{
let val = ((n % i) % j) % n;
if (val == max)
count++;
}
}
return count;
}
let n = 5;
document.write(countPairs(n));
</script>
|
Time Complexity: O(n2) since two nested loops are used the time taken by the algorithm to complete all operation is quadratic.
Auxiliary Space: O(1) since no extra array is used so the space taken by the algorithm is constant
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++
#include<bits/stdc++.h>
using namespace std;
int countPairs( int n)
{
if (n == 2)
return 4;
int num = ((n / 2) + 1);
int max = n % num;
int count = n - max;
return count;
}
int main()
{
int n = 5;
cout << countPairs(n);
}
|
Java
class GFG {
public static int countPairs( int n)
{
if (n == 2 )
return 4 ;
int num = ((n / 2 ) + 1 );
int max = n % num;
int count = n - max;
return count;
}
public static void main(String[] args)
{
int n = 5 ;
System.out.println(countPairs(n));
}
}
|
Python3
def countPairs(n):
if (n = = 2 ):
return 4
num = ((n / / 2 ) + 1 );
max = n % num;
count = n - max ;
return count
if __name__ = = "__main__" :
n = 5 ;
print (countPairs(n));
|
C#
using System;
class GFG
{
static int countPairs( int n)
{
if (n == 2)
return 4;
int num = ((n / 2) + 1);
int max = n % num;
int count = n - max;
return count;
}
static public void Main ()
{
int n = 5;
Console.WriteLine(countPairs(n));
}
}
|
PHP
<?php
function countPairs( $n )
{
if ( $n == 2)
return 4;
$num = ((int)( $n / 2) + 1);
$max = $n % $num ;
$count = ( $n - $max );
return $count ;
}
$n = 5;
echo (countPairs( $n ));
?>
|
Javascript
<script>
function countPairs(n)
{
if (n == 2)
return 4;
let num = (parseInt(n / 2, 10) + 1);
let max = n % num;
let count = n - max;
return count;
}
let n = 5;
document.write(countPairs(n));
</script>
|
Time Complexity: O(1) since no loop is used the algorithm takes up constant time to perform the operations
Auxiliary Space: O(1) since no extra array is used so the space taken by the algorithm is constant
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...