Find numbers that divide X and Y to produce the same remainder
Last Updated :
20 Dec, 2022
Given two integers X and Y, the task is to find and print the numbers that divide X and Y to produce the same remainder.
Examples:
Input: X = 1, Y = 5
Output: 1, 2, 4
Explanation:
Let the number be M. It can be any value in the range [1, 5]:
If M = 1, 1 % 1 = 0 and 5 % 1 = 0
If M = 2, 1 % 2 = 1 and 5 % 2 = 1
If M = 3, 1 % 3 = 1 and 5 % 3 = 2
If M = 4, 1 % 4 = 1 and 5 % 4 = 1
If M = 5, 1 % 5 = 1 and 5 % 5 = 0
Therefore, the possible M values are 1, 2, 4
Input: X = 8, Y = 10
Output: 1, 2
Naive Approach: The naive approach for this problem is to check the modulo value for all the possible values of M in the range [1, max(X, Y)] and print the value of M if the condition satisfies.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
void printModulus( int X, int Y)
{
int n = max(X, Y);
for ( int i = 1; i <= n; i++) {
if (X % i == Y % i)
cout << i << " " ;
}
}
int main()
{
int X, Y;
X = 10;
Y = 20;
printModulus(X, Y);
return 0;
}
|
Java
import java.util.*;
import java.io.*;
class GFG{
static void printModulus( int X, int Y)
{
int n = Math.max(X, Y);
for ( int i = 1 ; i <= n; i++) {
if (X % i == Y % i)
System.out.print(i + " " );
}
}
public static void main(String[] args)
{
int X, Y;
X = 10 ;
Y = 20 ;
printModulus(X, Y);
}
}
|
Python3
def printModulus( X, Y):
n = max (X, Y)
for i in range ( 1 , n + 1 ):
if (X % i = = Y % i):
print (i,end = " " )
X = 10
Y = 20
printModulus(X, Y)
|
C#
using System;
class GFG{
static void printModulus( int X, int Y)
{
int n = Math.Max(X, Y);
for ( int i = 1; i <= n; i++) {
if (X % i == Y % i)
Console.Write(i + " " );
}
}
public static void Main()
{
int X, Y;
X = 10;
Y = 20;
printModulus(X, Y);
}
}
|
Javascript
<script>
function printModulus(X, Y)
{
var n = Math.max(X, Y);
for ( var i = 1; i <= n; i++) {
if (X % i == Y % i)
document.write(i+ " " );
}
}
X = 10;
Y = 20;
printModulus(X, Y);
</script>
|
Time Complexity: O(max(X, Y))
Auxiliary Space: O(1)
Efficient Approach: Let’s assume that Y is greater than X by a difference of D.
- Then Y can be expressed as
Y = X + D
and
Y % M = (X + D) % M
= (X % M) + (D % M)
- Now, the condition becomes whether X % M and X % M + D % M are equal or not.
- Here, since X % M is common on both the sides, the value of M is true if for some M, D % M = 0.
- Therefore, the required values of M will be the factors of D.
Below is the implementation of the above approach:
CPP
#include <iostream>
using namespace std;
void printModulus( int X, int Y)
{
int d = abs (X - Y);
int i = 1;
while (i * i <= d) {
if (d % i == 0) {
cout << i << " " ;
if (d / i != i)
cout << d / i << " " ;
}
i++;
}
}
int main()
{
int X = 10;
int Y = 26;
printModulus(X, Y);
return 0;
}
|
Java
import java.util.*;
import java.io.*;
class GFG{
static void printModulus( int X, int Y)
{
int d = Math.abs(X - Y);
int i = 1 ;
while (i * i <= d) {
if (d % i == 0 ) {
System.out.print(i+ " " );
if (d / i != i)
System.out.print(d / i+ " " );
}
i++;
}
}
public static void main(String[] args)
{
int X = 10 ;
int Y = 26 ;
printModulus(X, Y);
}
}
|
Python3
def printModulus(X, Y):
d = abs (X - Y);
i = 1 ;
while (i * i < = d):
if (d % i = = 0 ):
print (i, end = "");
if (d / / i ! = i):
print (d / / i, end = " " );
i + = 1 ;
if __name__ = = '__main__' :
X = 10 ;
Y = 26 ;
printModulus(X, Y);
|
C#
using System;
public class GFG{
static void printModulus( int X, int Y)
{
int d = Math.Abs(X - Y);
int i = 1;
while (i * i <= d) {
if (d % i == 0) {
Console.Write(i+ " " );
if (d / i != i)
Console.Write(d / i+ " " );
}
i++;
}
}
public static void Main(String[] args)
{
int X = 10;
int Y = 26;
printModulus(X, Y);
}
}
|
Javascript
<script>
function printModulus(X, Y)
{
var d = Math.abs(X - Y);
var i = 1;
while (i * i <= d) {
if (d % i == 0) {
document.write(i + " " );
if (d / i != i)
document.write(parseInt(d / i) + " " );
}
i++;
}
}
var X = 10;
var Y = 26;
printModulus(X, Y);
</script>
|
Time Complexity Analysis O(sqrt(D)), where D is the difference between the values X and Y.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...