

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find minimum positive integer x such that a(x^2) + b(x) + c >= k in C++
Suppose we have four integers a, b, c and k. We have to find the minimum positive value x, such that the following equation satisfies −
𝑎𝑥2+𝑏𝑥+𝑐 ≥𝑘
If a = 3, b = 4, c = 5 and k = 6, then output will be 1
To solve this, we will use the bisection approach. The lower limit will be 0 since x has to be a minimum positive integer.
Example
#include<iostream> using namespace std; int getMinX(int a, int b, int c, int k) { int x = INT8_MAX; if (k <= c) return 0; int right = k - c; int left = 0; while (left <= right) { int mid = (left + right) / 2; int val = (a * mid * mid) + (b * mid); if (val > (k - c)) { x = min(x, mid); right = mid - 1; } else if (val < (k - c)) left = mid + 1; else return mid; } return x; } int main() { int a = 3, b = 2, c = 4, k = 15; cout << "Minimum value of x is: " << getMinX(a, b, c, k); }
Output −
Minimum value of x is: 2
- Related Questions & Answers
- Find minimum x such that (x % k) * (x / k) == n in C++
- Minimum positive integer value possible of X for given A and B in X = P*A + Q*B in C++
- Find maximum value of x such that n! % (k^x) = 0 in C++
- Count of all possible values of X such that A % X = B in C++
- Find number of pairs (x, y) in an array such that x^y > y^x in C++
- Find a distinct pair (x, y) in given range such that x divides y in C++
- Sum of the Series 1 + x/1 + x^2/2 + x^3/3 + .. + x^n/n in C++
- Program to find sum of 1 + x/2! + x^2/3! +…+x^n/(n+1)! in C++
- Find maximum among x^(y^2) or y^(x^2) where x and y are given in C++
- Find the smallest number X such that X! contains at least Y trailing zeros in C++
- Find x, y, z that satisfy 2/n = 1/x + 1/y + 1/z in C++
- Count of pairs (x, y) in an array such that x < y in C++
- Find a number x such that sum of x and its digits is equal to given n in C++
- Find a number x such that sum of x and its digits is equal to given n using C++.
- Count Distinct Non-Negative Integer Pairs (x, y) that Satisfy the Inequality x*x + y*y < n in C++
Advertisements