Given 3 positive integers P, Q, and R, the task is to find the number of pairs such that both the elements are in the range [P, Q] and the numbers should be multiple of R, and the product of numbers should lie in the range [P × Q / 4, P × Q]. If no such pair exists, print -1.
Examples:
Input: P = 14, Q = 30, R = 5
Output:15 20
15 25
Explanation:
Multiple of R between P & Q are {15, 20, 25, 30}.
P × Q = 420 and P × Q / 4 = 105
So the pairs which satisfies the above conditions are 15, 20 and 15, 25.
Input: P = 10, Q = 20, R = 7
Output: 7 14
Approach: To solve this problem first, find the minimum and maximum range up to the pairs that can exist and then find the pairs which satisfy the above conditions. Follow the steps below to solve the problem:
- Initialize vector say, v to store all the number that is in the range [P, Q] and is a multiple of R and a vector of pairs say, ans to store the pairs that follow the above-mentioned conditions.
- Iterate in the range [P, Q] using the variable i and check if i is divisible by R, then insert i in the vector v.
- Iterate in the range [0, v.size()-1] using the variable i and perform the following steps:
- Iterate in the range [i+1, v.size()-1] using the variable j and check if v[j] * v[i] <= P * Q and v[j] * v[i] >= P * Q/4 then insert the pair in ans.
- If ans.size() is equal to 0 then print -1.
- Otherwise, print the pairs in ans.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findPairs( int p, int q, int r)
{
vector< int > v;
for ( int i = p; i <= q; i++) {
if (i % r == 0) {
v.push_back(i);
}
}
vector<pair< int , int > > ans;
for ( int i = 0; i < v.size(); i++) {
for ( int j = i + 1; j < v.size(); j++) {
if (v[i] * v[j] >= p * q / 4
&& v[i] * v[j] <= p * q) {
ans.push_back({ v[i], v[j] });
}
}
}
if (ans.size() == 0) {
cout << -1 << endl;
}
else {
for ( int i = 0; i < ans.size(); i++) {
cout << ans[i].first << " "
<< ans[i].second << endl;
}
}
}
int main()
{
int p = 14, q = 30, r = 5;
findPairs(p, q, r);
return 0;
}
|
Java
import java.awt.*;
import java.util.*;
class GFG{
static class pair< T, V>{
T first;
V second;
}
static void findPairs( int p, int q, int r)
{
ArrayList<Integer> v = new ArrayList<>();
for ( int i = p; i <= q; i++) {
if (i % r == 0 ) {
v.add(i);
}
}
ArrayList<pair<Integer, Integer> > ans = new ArrayList<>();
for ( int i = 0 ; i < v.size(); i++) {
for ( int j = i + 1 ; j < v.size(); j++) {
if (v.get(i) * v.get(j) >= p * q / 4
&& v.get(i) * v.get(j) <= p * q) {
pair<Integer,Integer> x = new pair<>();
x.first = v.get(i);
x.second = v.get(j);
ans.add(x);
}
}
}
if (ans.size() == 0 ) {
System.out.println(- 1 );
}
else {
for ( int i = 0 ; i < ans.size(); i++) {
System.out.println(ans.get(i).first +
" " + ans.get(i).second);
}
}
}
public static void main(String[] args)
{
int p = 14 , q = 30 , r = 5 ;
findPairs(p, q, r);
}
}
|
Python3
def findPairs(p, q, r):
v = []
for i in range (p, q + 1 ):
if (i % r = = 0 ):
v.append(i)
ans = []
for i in range ( len (v)):
for j in range (i + 1 , len (v)):
if (v[i] * v[j] > = p * q / / 4 and
v[i] * v[j] < = p * q):
ans.append([v[i], v[j]])
if ( len (ans) = = 0 ):
print ( - 1 )
else :
for i in range ( len (ans)):
print (ans[i][ 0 ], ans[i][ 1 ])
if __name__ = = '__main__' :
p = 14
q = 30
r = 5
findPairs(p, q, r)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void findPairs( int p, int q, int r)
{
List< int > v = new List< int >();
for ( int i = p; i <= q; i++) {
if (i % r == 0) {
v.Add(i);
}
}
List<List< int >> ans = new List<List< int >>();
for ( int i = 0; i < v.Count; i++) {
for ( int j = i + 1; j < v.Count; j++) {
if (v[i] * v[j] >= p * q / 4
&& v[i] * v[j] <= p * q) {
List< int > temp = new List< int >();
temp.Add(v[i]);
temp.Add(v[j]);
ans.Add(temp);
}
}
}
if (ans.Count == 0) {
Console.Write(-1);
}
else {
foreach (List< int > subList in ans)
{
foreach ( int item in subList)
{
Console.Write(item + " " );
}
Console.WriteLine();
}
}
}
public static void Main()
{
int p = 14, q = 30, r = 5;
findPairs(p, q, r);
}
}
|
Javascript
<script>
function findPairs(p, q, r) {
let v = [];
for (let i = p; i <= q; i++) {
if (i % r == 0) {
v.push(i);
}
}
let ans = [];
for (let i = 0; i < v.length; i++) {
for (let j = i + 1; j < v.length; j++) {
if (v[i] * v[j] >= p * q / 4
&& v[i] * v[j] <= p * q) {
ans.push([v[i], v[j]]);
}
}
}
if (ans.length == 0) {
document.write(-1 + "<br>" );
}
else {
for (let i = 0; i < ans.length; i++) {
document.write(ans[i][0] + " "
+ ans[i][1] + "<br>" );
}
}
}
let p = 14, q = 30, r = 5;
findPairs(p, q, r);
</script>
|
Time Complexity: O(N2), where N is Q – P + 1.
Auxiliary Space: O(N)