Find if it is possible to get a ratio from given ranges of costs and quantities

Given the range of cost from lowCost to upCost and range of quantity from lowQuant to upQuant, find if it is possible to get a given ration ratio r where r = \frac{cost}{quantity} , and lowCost <= cost <= upCost and lowQuant <= quantity <= upQuant.

Examples :

Input : lowCost = 1, upCost = 10, 
        lowQuant = 2, upQuant = 8
        r = 3
Output : Yes
Explanation:
cost / quantity = 6 / 2 = 3
where cost is in [1, 10] and quantity
is in [2, 8]

Input : lowCost = 14, upCost = 30, 
        lowQuant = 5, upQuant = 12
        r = 9
Output : No



Approach: From the given formula, following equation can be easily deduced: cost = quantity*r.
From this equation, logic can be easily deduced. Check the product of every value of quantity with r and if any value of the product lies between lowCost and upCost, then answer is Yes otherwise it is No.

Below is the implementation of above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find if it is
// possible to get the ratio r
#include <bits/stdc++.h>
using namespace std;
  
// Returns true if it is 
// possible to get ratio r 
// from given cost and 
// quantity ranges.
bool isRatioPossible(int lowCost, int upCost,
                     int lowQuant, int upQuant, 
                     int r)
{
    for (int i = lowQuant; i <= upQuant; i++) 
    {
  
        // Calculating cost corresponding
        // to value of i
        int ans = i * r;
        if (lowCost <= ans && ans <= upCost)
            return true;
    }
  
    return false;
}
  
// Driver Code
int main()
{
    int lowCost = 14, upCost = 30, 
        lowQuant = 5, upQuant = 12, 
        r = 9;
  
    if (isRatioPossible(lowCost, upCost, 
                        lowQuant, upQuant, r))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find if it is
// possible to get the ratio r
import java.io.*;
  
class Ratio 
{
  
    // Returns true if it is 
    // possible to get ratio r 
    // from given cost and 
    // quantity ranges.
    static boolean isRatioPossible(int lowCost, int upCost,
                                   int lowQuant, int upQuant, 
                                   int r)
    {
        for (int i = lowQuant; i <= upQuant; i++) 
        {
  
            // Calculating cost corresponding
            // to value of i
            int ans = i * r;
            if (lowCost <= ans && ans <= upCost)
                return true;
        }
  
        return false;
    }
  
    // Driver Code
    public static void main(String args[])
    {
        int lowCost = 14, upCost = 30
            lowQuant = 5, upQuant = 12, r = 9;
  
        if (isRatioPossible(lowCost, upCost, 
                            lowQuant, upQuant, r))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program to find if it 
# is possible to get the ratio r
  
# Returns true if it is 
# possible to get ratio r 
# from given cost and 
# quantity ranges.
def isRatioPossible(lowCost, upCost, 
                    lowQuant, upQuant, r) :
      
    for i in range(lowQuant, upQuant + 1) :
          
        # Calculating cost corresponding
        # to value of i
        ans = i * r
          
        if (lowCost <= ans and ans <= upCost) :
            return True
              
    return False
  
      
# Driver Code
lowCost = 14; upCost = 30
lowQuant = 5; upQuant = 12; r = 9
  
if (isRatioPossible(lowCost, upCost,
                    lowQuant,upQuant, r)) :
    print( "Yes" )
else :
    print( "No" )
      
# This code is contributed 
# by Nikita Tiwari.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find if it is
// possible to get the ratio r
using System;
  
class Ratio 
{
  
    // Returns true if it is 
    // possible to get ratio r 
    // from given cost and 
    // quantity ranges.
    static bool isRatioPossible(int lowCost, int upCost,
                                int lowQuant, int upQuant, 
                                int r)
    {
        for (int i = lowQuant; i <= upQuant; i++) 
        {
  
            // Calculating cost corresponding
            // to value of i
            int ans = i * r;
            if (lowCost <= ans && ans <= upCost)
                return true;
        }
  
        return false;
    }
  
    // Driver Code
    public static void Main()
    {
        int lowCost = 14, upCost = 30, 
            lowQuant = 5, upQuant = 12, r = 9;
  
        if (isRatioPossible(lowCost, upCost, 
                            lowQuant, upQuant, r))
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
    }
}
  
// This code is contributed by vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
//PHP program to find if it is
// possible to get the ratio r
  
// Returns true if it is 
// possible to get ratio r 
// from given cost and 
// quantity ranges.
function isRatioPossible($lowCost, $upCost,
                         $lowQuant, $upQuant,$r)
{
    for ($i = $lowQuant; $i <= $upQuant; $i++) 
    {
  
        // Calculating cost corresponding
        // to value of i
        $ans = $i * $r;
        if ($lowCost <= $ans && $ans <= $upCost)
            return true;
    }
  
    return false;
}
  
// Driver Code
$lowCost = 14; $upCost = 30; 
$lowQuant = 5; $upQuant = 12; $r = 9;
  
if (isRatioPossible($lowCost, $upCost
                    $lowQuant, $upQuant, $r))
    echo "Yes";
else
    echo "No";
  
# This code is contributed by ajit
?>

chevron_right



Output :

No


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : jit_t



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.