Find minimum positive integer x such that a(x^2) + b(x) + c >= k

Given four integers a, b, c and k. The task is to find the minimum positive value of x such that ax2 + bx + c ≥ k.

Examples:

Input: a = 3, b = 4, c = 5, k = 6
Output: 1
For x = 0, a * 0 + b * 0 + c = 5 < 6
For x = 1, a * 1 + b * 1 + c = 3 + 4 + 5 = 12 > 6

Input: a = 2, b = 7, c = 6, k = 3
Output: 0



Approach: The idea is to use binary search. The lower limit for our search will be 0 since x has to be minimum positive integer.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the minimum positive
// integer satisfying the given equation
int MinimumX(int a, int b, int c, int k)
{
    int x = INT_MAX;
  
    if (k <= c)
        return 0;
  
    int h = k - c;
    int l = 0;
  
    // Binary search to find the value of x
    while (l <= h) {
        int m = (l + h) / 2;
        if ((a * m * m) + (b * m) > (k - c)) {
            x = min(x, m);
            h = m - 1;
        }
        else if ((a * m * m) + (b * m) < (k - c))
            l = m + 1;
        else
            return m;
    }
  
    // Return the answer
    return x;
}
  
// Driver code
int main()
{
    int a = 3, b = 2, c = 4, k = 15;
    cout << MinimumX(a, b, c, k);
  
    return 0;
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
  
# Function to return the minimum positive
# integer satisfying the given equation
def MinimumX(a, b, c, k):
  
    x = 10**9
  
    if (k <= c):
        return 0
  
    h = k - c
    l = 0
  
    # Binary search to find the value of x
    while (l <= h):
        m = (l + h) // 2
        if ((a * m * m) + (b * m) > (k - c)):
            x = min(x, m)
            h = m - 1
  
        elif ((a * m * m) + (b * m) < (k - c)):
            l = m + 1
        else:
            return m
  
    # Return the answer
    return x
  
# Driver code
a, b, c, k = 3, 2, 4, 15
print(MinimumX(a, b, c, k))
  
# This code is contributed by mohit kumar

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of the approach 
  
// Function to return the minimum positive 
// integer satisfying the given equation 
function MinimumX($a, $b, $c, $k
    $x = PHP_INT_MAX; 
  
    if ($k <= $c
        return 0; 
  
    $h = $k - $c
    $l = 0; 
  
    // Binary search to find the value of x 
    while ($l <= $h
    
        $m = floor(($l + $h) / 2); 
        if (($a * $m * $m) + 
            ($b * $m) > ($k - $c))
        
            $x = min($x, $m); 
            $h = $m - 1; 
        
        else if (($a * $m * $m) + 
                 ($b * $m) < ($k - $c)) 
            $l = $m + 1; 
        else
            return $m
    
  
    // Return the answer 
    return $x
  
// Driver code 
$a = 3; $b = 2; $c = 4; $k = 15; 
  
echo MinimumX($a, $b, $c, $k);
  
// This code is contributed by Ryuga
?>

chevron_right


Output:

2


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 : mohit kumar 29, Ryuga