Given an array arr[] containing Q positive integers and two numbers A and B, the task is to find the number of ordered pairs (x, y) for every number N, in array arr, such that:
Examples:
Input: arr[] = {15, 25}, A = 1, B = 2
Output: 2 1
Explanation:
For 15: There are 2 ordered pairs (x, y) = (13, 1) & (5, 5) such that 1*x + 2*y = 15
For 25: There is only one ordered pair (x, y) = (21, 2) such that 1*x + 2*y = 25
Input: arr[] = {50, 150}, A = 5, B = 10
Output: 1 0
Naive Approach: The naive approach for this problem is:
- For every query N in the array, compute the Fibonacci numbers up to N
- Check for all possible pairs, from this Fibonacci numbers, if it satisfies the given condition A*x + B*y = N.
Time complexity: O(N2)
Efficient Approach:
- Precompute all the Fibonacci numbers and store it in an array.
- Now, iterate over the Fibonacci numbers and for all the possible combinations, update the number of ordered pairs for every number in range [1, max(arr)], and store it in another array.
- Now, for every query N, the number of ordered pairs can be answered in constant time.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#define size 10001
using namespace std;
long long fib[100010];
int freq[100010];
bool isPerfectSquare( int x)
{
int s = sqrt (x);
return (s * s == x);
}
int isFibonacci( int n)
{
if (isPerfectSquare(5 * n * n + 4)
|| isPerfectSquare(5 * n * n - 4))
return 1;
return 0;
}
void compute( int a, int b)
{
for ( int i = 1; i < 100010; i++) {
fib[i] = isFibonacci(i);
}
for ( int x = 1; x < 100010; x++) {
for ( int y = 1; y < size; y++) {
if (fib[x] == 1 && fib[y] == 1
&& a * x + b * y < 100010) {
freq[a * x + b * y]++;
}
}
}
}
int main()
{
int Q = 2, A = 5, B = 10;
compute(A, B);
int arr[Q] = { 50, 150 };
for ( int i = 0; i < Q; i++) {
cout << freq[arr[i]] << " " ;
}
return 0;
}
|
Java
class GFG{
static final int size = 10001 ;
static long []fib = new long [ 100010 ];
static int []freq = new int [ 100010 ];
static boolean isPerfectSquare( int x)
{
int s = ( int ) Math.sqrt(x);
return (s * s == x);
}
static int isFibonacci( int n)
{
if (isPerfectSquare( 5 * n * n + 4 )
|| isPerfectSquare( 5 * n * n - 4 ))
return 1 ;
return 0 ;
}
static void compute( int a, int b)
{
for ( int i = 1 ; i < 100010 ; i++) {
fib[i] = isFibonacci(i);
}
for ( int x = 1 ; x < 100010 ; x++) {
for ( int y = 1 ; y < size; y++) {
if (fib[x] == 1 && fib[y] == 1
&& a * x + b * y < 100010 ) {
freq[a * x + b * y]++;
}
}
}
}
public static void main(String[] args)
{
int Q = 2 , A = 5 , B = 10 ;
compute(A, B);
int arr[] = { 50 , 150 };
for ( int i = 0 ; i < Q; i++) {
System.out.print(freq[arr[i]]+ " " );
}
}
}
|
Python 3
import math
size = 101
fib = [ 0 ] * 100010
freq = [ 0 ] * ( 100010 )
def isPerfectSquare(x):
s = int (math.sqrt(x))
return (s * s) = = x
def isFibonacci(n):
if (isPerfectSquare( 5 * n * n + 4 ) or isPerfectSquare( 5 * n * n - 4 )):
return 1 ;
return 0 ;
def compute( a, b):
for i in range ( 1 , 100010 ):
fib[i] = isFibonacci(i)
for x in range ( 1 , 100010 ):
for y in range ( 1 , size):
if (fib[x] = = 1 and fib[y] = = 1 and a * x + b * y < 100010 ):
freq[a * x + b * y] + = 1
Q = 2
A = 5
B = 10
compute(A, B);
arr = [ 50 , 150 ]
for i in range (Q):
print (freq[arr[i]], end = " " )
|
C#
using System;
class GFG{
static readonly int size = 10001;
static long []fib = new long [100010];
static int []freq = new int [100010];
static bool isPerfectSquare( int x)
{
int s = ( int ) Math.Sqrt(x);
return (s * s == x);
}
static int isFibonacci( int n)
{
if (isPerfectSquare(5 * n * n + 4)
|| isPerfectSquare(5 * n * n - 4))
return 1;
return 0;
}
static void compute( int a, int b)
{
for ( int i = 1; i < 100010; i++) {
fib[i] = isFibonacci(i);
}
for ( int x = 1; x < 100010; x++) {
for ( int y = 1; y < size; y++) {
if (fib[x] == 1 && fib[y] == 1
&& a * x + b * y < 100010) {
freq[a * x + b * y]++;
}
}
}
}
public static void Main(String[] args)
{
int Q = 2, A = 5, B = 10;
compute(A, B);
int []arr = { 50, 150 };
for ( int i = 0; i < Q; i++) {
Console.Write(freq[arr[i]]+ " " );
}
}
}
|
Javascript
<script>
var size = 10001
var fib = Array(100010).fill(0);
var freq = Array(100010).fill(0);
function isPerfectSquare(x)
{
var s = parseInt(Math.sqrt(x));
return (s * s == x);
}
function isFibonacci(n)
{
if (isPerfectSquare(5 * n * n + 4)
|| isPerfectSquare(5 * n * n - 4))
return 1;
return 0;
}
function compute(a, b)
{
for ( var i = 1; i < 100010; i++)
{
fib[i] = isFibonacci(i);
}
for ( var x = 1; x < 100010; x++)
{
for ( var y = 1; y < size; y++)
{
if (fib[x] == 1 && fib[y] == 1
&& a * x + b * y < 100010)
{
freq[a * x + b * y]++;
}
}
}
}
var Q = 2, A = 5, B = 10;
compute(A, B);
var arr = [ 50, 150 ];
for ( var i = 0; i < Q; i++)
{
document.write(freq[arr[i]] + " " );
}
</script>
|
Time Complexity: O(size + 100010)
Auxiliary Space: O(size + 100010)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
14 Jan, 2022
Like Article
Save Article