Given an array of N integers and 3 integers D, A and B. The task is to find the number of array elements that we can convert D into by performing the following operations on D:
- Add A (+A)
- Subtract A (-A)
- Add B (+B)
- Subtract B (-B)
Note: It is allowed to perform any number of operations of any type.
Examples:
Input : arr = {1, 2, 3}, D = 6, A = 3, B = 2
Output : 3
Explanation:
We can derive 1 from D by performing (6 - 3(A) - 2(B))
We can derive 2 from D by performing (6 - 2(A) - 2(A))
We can derive 3 from D by performing (6 - 3(A))
Thus, All array elements can be derived from D.
Input : arr = {1, 2, 3}, D = 7, A = 4, B = 2
Output : 2
Explanation:
We can derive 1 from D by performing (7 - 4(A) - 2(B))
We can derive 3 from D by performing (7 - 4(A))
Thus, we can derive {1, 3}
Lets say the we want to check if the element ai can be derived from D:
Suppose we perform:
- The operation of type1(i.e Add A) P times.
- The operation of type 2(i.e Subtract A) Q times.
- The operation of type 3(i.e Add B) R times.
- The operation of type 4(i.e Subtract B) S times.
Let the value we get after performing these operations be X, then,
-> X = P*A – Q*A + R*B – S*B
-> X = (P – Q) * A + (R – S) * B
Suppose we successfully derive Ai from D, i.e X = |Ai – D|,
-> |Ai – D| = (P – Q) * A + (R – S) * B
Let (P – Q) = some constant say, U
and similarly let (R – S) be a constant, V
-> |Ai – D| = U * A + V * B
This is in the form of the Linear Diophantine Equation and the solution exists only when |Ai – D| is divisible by gcd(A, B).
Thus now we can simply iterate over the array and count all such Ai for which |Ai – D| is divisible by gcd(a, b).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int gcd( int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
int findPossibleDerivables( int arr[], int n, int D,
int A, int B)
{
int gcdAB = gcd(A, B);
int counter = 0;
for ( int i = 0; i < n; i++) {
if (( abs (arr[i] - D) % gcdAB) == 0) {
counter++;
}
}
return counter;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 7, 13 };
int n = sizeof (arr) / sizeof (arr[0]);
int D = 5, A = 4, B = 2;
cout << findPossibleDerivables(arr, n, D, A, B) << "\n" ;
int a[] = { 1, 2, 3 };
n = sizeof (a) / sizeof (a[0]);
D = 6, A = 3, B = 2;
cout << findPossibleDerivables(a, n, D, A, B) << "\n" ;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int gcd( int a, int b)
{
if (a == 0 )
return b;
return gcd(b % a, a);
}
static int findPossibleDerivables( int arr[], int n, int D,
int A, int B)
{
int gcdAB = gcd(A, B);
int counter = 0 ;
for ( int i = 0 ; i < n; i++) {
if ((Math.abs(arr[i] - D) % gcdAB) == 0 ) {
counter++;
}
}
return counter;
}
public static void main (String[] args) {
int arr[] = { 1 , 2 , 3 , 4 , 7 , 13 };
int n = arr.length;
int D = 5 , A = 4 , B = 2 ;
System.out.println( findPossibleDerivables(arr, n, D, A, B));
int a[] = { 1 , 2 , 3 };
n = a.length;
D = 6 ;
A = 3 ;
B = 2 ;
System.out.println( findPossibleDerivables(a, n, D, A, B));
}
}
|
Python3
def gcd(a, b) :
if (a = = 0 ) :
return b
return gcd(b % a, a);
def findPossibleDerivables(arr, n, D, A, B) :
gcdAB = gcd(A, B)
counter = 0
for i in range (n) :
if (( abs (arr[i] - D) % gcdAB) = = 0 ) :
counter + = 1
return counter
if __name__ = = "__main__" :
arr = [ 1 , 2 , 3 , 4 , 7 , 13 ]
n = len (arr)
D, A, B = 5 , 4 , 2
print (findPossibleDerivables(arr, n, D, A, B))
a = [ 1 , 2 , 3 ]
n = len (a)
D, A, B = 6 , 3 , 2
print (findPossibleDerivables(a, n, D, A, B))
|
C#
using System;
public class GFG {
static int gcd( int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static int findPossibleDerivables( int []arr, int n, int D,
int A, int B)
{
int gcdAB = gcd(A, B);
int counter = 0;
for ( int i = 0; i < n; i++) {
if ((Math.Abs(arr[i] - D) % gcdAB) == 0) {
counter++;
}
}
return counter;
}
public static void Main () {
int []arr = { 1, 2, 3, 4, 7, 13 };
int n = arr.Length;
int D = 5, A = 4, B = 2;
Console.WriteLine( findPossibleDerivables(arr, n, D, A, B));
int []a = { 1, 2, 3 };
n = a.Length;
D = 6;
A = 3;
B = 2;
Console.WriteLine( findPossibleDerivables(a, n, D, A, B));
}
}
|
PHP
<?php
function gcd( $a , $b )
{
if ( $a == 0)
return $b ;
return gcd( $b % $a , $a );
}
function findPossibleDerivables( $arr , $n ,
$D , $A , $B )
{
$gcdAB = gcd( $A , $B );
$counter = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if (( abs ( $arr [ $i ] - $D ) % $gcdAB ) == 0)
{
$counter ++;
}
}
return $counter ;
}
$arr = array ( 1, 2, 3, 4, 7, 13 );
$n = sizeof( $arr );
$D = 5;
$A = 4;
$B = 2;
echo findPossibleDerivables( $arr , $n ,
$D , $A , $B ), "\n" ;
$a = array ( 1, 2, 3 );
$n = sizeof( $a );
$D = 6;
$A = 3;
$B = 2;
echo findPossibleDerivables( $arr , $n ,
$D , $A , $B ), "\n" ;
?>
|
Javascript
<script>
function gcd(a , b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
function findPossibleDerivables(arr , n , D , A , B) {
var gcdAB = gcd(A, B);
var counter = 0;
for (i = 0; i < n; i++)
{
if ((Math.abs(arr[i] - D) % gcdAB) == 0) {
counter++;
}
}
return counter;
}
var arr = [ 1, 2, 3, 4, 7, 13 ];
var n = arr.length;
var D = 5, A = 4, B = 2;
document.write(findPossibleDerivables(arr, n, D, A, B)+ "<br/>" );
var a = [ 1, 2, 3 ];
n = a.length;
D = 6;
A = 3;
B = 2;
document.write(findPossibleDerivables(a, n, D, A, B));
</script>
|
Complexity Analysis:
- Time Complexity: O(log(max(A, B) + N), where N is the number of array elements and A & B represents the value of given integers.
- Auxiliary Space: O(log(max(A, B)) due to recursive stack space .