Smallest value of X satisfying the condition X % A[i] = B[i] for two given arrays
Last Updated :
17 Jul, 2021
Given two arrays A[] and B[], both consisting of N positive integers, an integer P and the elements of the array A[] are pairwise co-prime, the task is to find the smallest integer X which is at least P and X % A[i] is equal to B[i] for all i over the range of indices [0, N – 1].
Examples:
Input: A[] = {3, 4, 5}, B[] = {2, 3, 1}, P = 72
Output: 131
Explanation:
Consider the following operations for the value of X as 131.
- X % A[0] = 131 % 3 = 2 (= B[0])
- X % A[1] = 131 % 4 = 3 (= B[1])
- X % A[2] = 131 % 5 = 1 (= B[2])
Therefore, 131 is the smallest integer which is at least P( = 72).
Input: A[] = {5, 7}, B[] = {1, 3}, P = 0
Output: 31
Approach: The idea to solve the given problem is to use the Chinese Remainder Theorem. Follow the steps below to solve the given problem:
- Calculate the LCM of the array A[], which is equal to the product of all elements present in the array A[], say M, since all the elements are co-prime.
- Using Chinese Remainder Theorem, find the required smallest positive integer Y. Therefore, the value of X is given by (Y + K * M) for some integer K, that satisfies X % A[i] = B[i] for all i over the range of indices [0, N – 1].
- The value of K can be found from the equation Y + K * M >= P, which equates to K >= (P – Y)/M.
- Therefore, the required smallest possible integer X is (Y + K * M).
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int inv( int a, int m)
{
int m0 = m, t, q;
int x0 = 0, x1 = 1;
if (m == 1)
return 0;
while (a > 1)
{
q = a / m;
t = m;
m = a % m;
a = t;
t = x0;
x0 = x1 - q * x0;
x1 = t;
}
if (x1 < 0)
x1 += m0;
return x1;
}
int findMinX( int A[], int B[], int N)
{
int prod = 1;
for ( int i = 0; i < N; i++)
prod *= A[i];
int result = 0;
for ( int i = 0; i < N; i++)
{
int pp = prod / A[i];
result += B[i] * inv(pp, A[i]) * pp;
}
return result % prod;
}
int product( int a[], int n)
{
int ans = 1;
for ( int i = 0; i < n; i++)
{
ans *= a[i];
}
return ans;
}
void findSmallestInteger( int A[], int B[],
int P, int n)
{
int Y = findMinX(A, B, n);
int M = product(A,n);
int K = ceil ((( double )P - ( double )Y) /
( double )M);
int X = Y + K * M;
cout << X;
}
int main()
{
int A[] = { 3, 4, 5 };
int B[] = { 2, 3, 1 };
int n = sizeof (A) / sizeof (A[0]);
int P = 72;
findSmallestInteger(A, B, P,n);
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
public class Main {
static int inv( int a, int m)
{
int m0 = m, t, q;
int x0 = 0 , x1 = 1 ;
if (m == 1 )
return 0 ;
while (a > 1 ) {
q = a / m;
t = m;
m = a % m;
a = t;
t = x0;
x0 = x1 - q * x0;
x1 = t;
}
if (x1 < 0 )
x1 += m0;
return x1;
}
static int findMinX( int A[], int B[], int N)
{
int prod = 1 ;
for ( int i = 0 ; i < N; i++)
prod *= A[i];
int result = 0 ;
for ( int i = 0 ; i < N; i++) {
int pp = prod / A[i];
result += B[i] * inv(pp, A[i]) * pp;
}
return result % prod;
}
static int product( int a[])
{
int ans = 1 ;
for ( int i = 0 ; i < a.length; i++) {
ans *= a[i];
}
return ans;
}
public static void findSmallestInteger( int A[], int B[],
int P)
{
int Y = findMinX(A, B, A.length);
int M = product(A);
int K = ( int )Math.ceil((( double )P - ( double )Y)
/ ( double )M);
int X = Y + K * M;
System.out.println(X);
}
public static void main(String[] args)
{
int A[] = { 3 , 4 , 5 };
int B[] = { 2 , 3 , 1 };
int P = 72 ;
findSmallestInteger(A, B, P);
}
}
|
Python3
import math
def inv(a, m):
m0 = m
x0 = 0
x1 = 1
if (m = = 1 ):
return 0
while (a > 1 ):
q = a / / m
t = m
m = a % m
a = t
t = x0
x0 = x1 - q * x0
x1 = t
if (x1 < 0 ):
x1 + = m0
return x1
def findMinX(A, B, N):
prod = 1
for i in range (N):
prod * = A[i]
result = 0
for i in range (N):
pp = prod / / A[i]
result + = B[i] * inv(pp, A[i]) * pp
return result % prod
def product(a, n):
ans = 1
for i in range (n):
ans * = a[i]
return ans
def findSmallestInteger(A, B, P, n):
Y = findMinX(A, B, n)
M = product(A, n)
K = math.ceil((P - Y) / M)
X = Y + K * M
print (X)
if __name__ = = "__main__" :
A = [ 3 , 4 , 5 ]
B = [ 2 , 3 , 1 ]
n = len (A)
P = 72
findSmallestInteger(A, B, P, n)
|
C#
using System;
public class GFG
{
static int inv( int a, int m)
{
int m0 = m, t, q;
int x0 = 0, x1 = 1;
if (m == 1)
return 0;
while (a > 1) {
q = a / m;
t = m;
m = a % m;
a = t;
t = x0;
x0 = x1 - q * x0;
x1 = t;
}
if (x1 < 0)
x1 += m0;
return x1;
}
static int findMinX( int [] A, int [] B, int N)
{
int prod = 1;
for ( int i = 0; i < N; i++)
prod *= A[i];
int result = 0;
for ( int i = 0; i < N; i++) {
int pp = prod / A[i];
result += B[i] * inv(pp, A[i]) * pp;
}
return result % prod;
}
static int product( int [] a)
{
int ans = 1;
for ( int i = 0; i < a.Length; i++) {
ans *= a[i];
}
return ans;
}
public static void findSmallestInteger( int [] A, int [] B,
int P)
{
int Y = findMinX(A, B, A.Length);
int M = product(A);
int K = ( int )Math.Ceiling((( double )P - ( double )Y)
/ ( double )M);
int X = Y + K * M;
Console.WriteLine(X);
}
public static void Main( string [] args)
{
int [] A = { 3, 4, 5 };
int [] B = { 2, 3, 1 };
int P = 72;
findSmallestInteger(A, B, P);
}
}
|
Javascript
<script>
function inv(a, m)
{
var m0 = m, t, q;
var x0 = 0, x1 = 1;
if (m == 1)
return 0;
while (a > 1)
{
q = parseInt(a / m);
t = m;
m = a % m;
a = t;
t = x0;
x0 = x1 - q * x0;
x1 = t;
}
if (x1 < 0)
x1 += m0;
return x1;
}
function findMinX(A, B, N)
{
var prod = 1;
for ( var i = 0; i < N; i++)
prod *= A[i];
var result = 0;
for ( var i = 0; i < N; i++)
{
var pp = parseInt(prod / A[i]);
result += B[i] * inv(pp, A[i]) * pp;
}
return result % prod;
}
function product(a, n)
{
var ans = 1;
for ( var i = 0; i < n; i++)
{
ans *= a[i];
}
return ans;
}
function findSmallestInteger(A, B, P, n)
{
var Y = findMinX(A, B, n);
var M = product(A,n);
var K = Math.ceil((P - Y) / M);
var X = Y + K * M;
document.write( X);
}
var A = [ 3, 4, 5 ];
var B = [ 2, 3, 1 ];
var n = A.length;
var P = 72;
findSmallestInteger(A, B, P,n);
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...