Open In App

Count of pairs in range [P, Q] with numbers as multiple of R and their product lie in range [P*Q/4, P*Q]

Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function 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]
void findPairs(int p, int q, int r)
{
    // Store multiple of r
    // in range of [P, Q]
    vector<int> v;
 
    // Iterate in the range [p, q]
    for (int i = p; i <= q; i++) {
        if (i % r == 0) {
            v.push_back(i);
        }
    }
 
    // Vector to store pair of answer
    vector<pair<int, int> > ans;
 
    // Iterate through the vector v
    for (int i = 0; i < v.size(); i++) {
 
        // Iterate in the range [i+1, v.size()-1]
        for (int j = i + 1; j < v.size(); j++) {
 
            // If pair follow this condition
            // insert the pair in vector ans
            if (v[i] * v[j] >= p * q / 4
                && v[i] * v[j] <= p * q) {
                ans.push_back({ v[i], v[j] });
            }
        }
    }
 
    // If no pair satisfy the conditions, print -1
    if (ans.size() == 0) {
        cout << -1 << endl;
    }
    else {
 
        // Print the pairs
        // which satisfy the given condition
        for (int i = 0; i < ans.size(); i++) {
 
            cout << ans[i].first << " "
                 << ans[i].second << endl;
        }
    }
}
 
// Driver Code
int main()
{
 
    // Given Input
    int p = 14, q = 30, r = 5;
 
    // Function Call
    findPairs(p, q, r);
    return 0;
}


Java




//Java program for above approach
import java.awt.*;
import java.util.*;
class GFG{
    static class pair< T, V>{
        T first;
        V second;
    }
 
    // Function 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]
    static void findPairs(int p, int q, int r)
    {
       
        // Store multiple of r
        // in range of [P, Q]
        ArrayList<Integer> v = new ArrayList<>();
 
        // Iterate in the range [p, q]
        for (int i = p; i <= q; i++) {
            if (i % r == 0) {
                v.add(i);
            }
        }
 
        // Vector to store pair of answer
        ArrayList<pair<Integer, Integer> > ans = new ArrayList<>();
 
        // Iterate through the vector v
        for (int i = 0; i < v.size(); i++) {
 
            // Iterate in the range [i+1, v.size()-1]
            for (int j = i + 1; j < v.size(); j++) {
 
                // If pair follow this condition
                // insert the pair in vector ans
                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 no pair satisfy the conditions, print -1
        if (ans.size() == 0) {
            System.out.println(-1);
        }
        else {
 
            // Print the pairs
            // which satisfy the given condition
            for (int i = 0; i < ans.size(); i++) {
                System.out.println(ans.get(i).first +
                        " " + ans.get(i).second);
            }
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
       
        // Given Input
        int p = 14, q = 30, r = 5;
 
        // Function Call
        findPairs(p, q, r);
    }
}
 
// This code is contributed by hritikrommie.


Python3




# Python3 program for the above approach
 
# Function 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]
def findPairs(p, q, r):
     
    # Store multiple of r
    # in range of [P, Q]
    v = []
 
    # Iterate in the range [p, q]
    for i in range(p, q + 1):
        if (i % r == 0):
            v.append(i)
 
    # Vector to store pair of answer
    ans = []
  
    # Iterate through the vector v
    for i in range(len(v)):
         
        # Iterate in the range [i+1, v.size()-1]
        for j in range(i + 1, len(v)):
             
            # If pair follow this condition
            # insert the pair in vector ans
            if (v[i] * v[j] >= p * q // 4 and
                v[i] * v[j] <= p * q):
                ans.append([v[i], v[j]])
 
    # If no pair satisfy the conditions, pr-1
    if (len(ans) == 0):
        print (-1)
    else:
         
        # Print the pairs
        # which satisfy the given condition
        for i in range(len(ans)):
            print(ans[i][0], ans[i][1])
 
# Driver Code
if __name__ == '__main__':
 
    # Given Input
    p = 14
    q = 30
    r = 5
 
    # Function Call
    findPairs(p, q, r)
     
# This code is contributed by mohit kumar 29


C#




// C# program for the above approach
 
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function 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]
static void findPairs(int p, int q, int r)
{
    // Store multiple of r
    // in range of [P, Q]
    List<int> v = new List<int>();
 
    // Iterate in the range [p, q]
    for (int i = p; i <= q; i++) {
        if (i % r == 0) {
            v.Add(i);
        }
    }
 
    // Vector to store pair of answer
    List<List<int>> ans = new List<List<int>>();
 
    // Iterate through the vector v
    for(int i = 0; i < v.Count; i++) {
 
        // Iterate in the range [i+1, v.size()-1]
        for (int j = i + 1; j < v.Count; j++) {
 
            // If pair follow this condition
            // insert the pair in vector ans
            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 no pair satisfy the conditions, print -1
    if (ans.Count == 0) {
        Console.Write(-1);
    }
    else {
          
         foreach (List<int> subList in ans)
        {
            foreach (int item in subList)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine();
        }
        // Print the pairs
        // which satisfy the given condition
    }
}
 
// Driver Code
public static void Main()
{
 
    // Given Input
    int p = 14, q = 30, r = 5;
 
    // Function Call
    findPairs(p, q, r);
 
}
 
}
 
// This code is contributed by ipg2016107.


Javascript




<script>
// Javascript program for the above approach
 
 
// Function 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]
function findPairs(p, q, r) {
    // Store multiple of r
    // in range of [P, Q]
    let v = [];
 
    // Iterate in the range [p, q]
    for (let i = p; i <= q; i++) {
        if (i % r == 0) {
            v.push(i);
        }
    }
 
    // Vector to store pair of answer
    let ans = [];
 
    // Iterate through the vector v
    for (let i = 0; i < v.length; i++) {
 
        // Iterate in the range [i+1, v.size()-1]
        for (let j = i + 1; j < v.length; j++) {
 
            // If pair follow this condition
            // insert the pair in vector ans
            if (v[i] * v[j] >= p * q / 4
                && v[i] * v[j] <= p * q) {
                ans.push([v[i], v[j]]);
            }
        }
    }
 
    // If no pair satisfy the conditions, print -1
    if (ans.length == 0) {
        document.write(-1 + "<br>");
    }
    else {
 
        // Print the pairs
        // which satisfy the given condition
        for (let i = 0; i < ans.length; i++) {
 
            document.write(ans[i][0] + " "
                + ans[i][1] + "<br>");
        }
    }
}
 
// Driver Code
 
// Given Input
let p = 14, q = 30, r = 5;
 
// Function Call
findPairs(p, q, r);
 
// This code is contributed by _saurabh_jaiswal.
</script>


Output: 

15 20
15 25

 

Time Complexity: O(N2), where N is Q – P + 1.
Auxiliary Space: O(N)



Last Updated : 02 Dec, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads