
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find minimum x such that (x % k) * (x / k) == n in C++
Given two positive integers n and k, and we have to find the positive integer x, such that (x % k)*(x / k) is same as n. So if the n and k are 4 and 6 respectively, then the output will be 10. So (10 % 6) * (10 / 6) = 4.
As we know that the value of x % k will be in range [1 to k – 1] (0 is not included) Here we will find possible integer in the range that divides n and hence the given equation becomes: x = (n * k) / (x % k) + (x % k)
Example
#include<iostream> using namespace std; int minValue(int x, int y){ return (x > y)?y:x; } int getX(int n, int k) { int x = INT_MAX; for (int rem = k - 1; rem > 0; rem--) { if (n % rem == 0) x = minValue(x, rem + (n / rem) * k); } return x; } int main() { int n = 4, k = 6; cout << "The minimum value of x: " << getX(n, k); }
Output
The minimum value of x: 10
- Related Articles
- Find minimum positive integer x such that a(x^2) + b(x) + c >= k in C++
- Find maximum value of x such that n! % (k^x) = 0 in C++
- Polynomial \( f(x)=x^{2}-5 x+k \) has zeroes \( \alpha \) and \( \beta \) such that \( \alpha-\beta=1 . \) Find the value of \( 4 k \).
- Find the value of \( k \), if \( x-1 \) is a factor of \( p(x) \) in each of the following cases:(i) \( p(x)=x^{2}+x+k \)(ii) \( p(x)=2 x^{2}+k x+\sqrt{2} \)(iii) \( p(x)=k x^{2}-\sqrt{2} x+1 \)(iv) \( p(x)=k x^{2}-3 x+k \)
- Find \( k \) so that \( x^{2}+2 x+k \) is a factor of \( 2 x^{4}+x^{3}-14 x^{2}+5 x+6 \). Also find all the zeroes of the two polynomials.
- Find \( \mathrm{k} \) so that \( x^{2}+2 x+k \) is a factor of \( 2 x^{4}+x^{3}-14 x^{2}+5 x+6 \). Also, find all the zeroes of the two polynomials.
- Find the value of k such that the polynomial $x^{2}-(k+6)x+2(2k-1)$ has sum of its zeros equal to half of their product.
- Find number of pairs (x, y) in an array such that x^y > y^x in C++
- Place k elements such that minimum distance is maximized in C++
- Find smallest number K such that K % p = 0 and q % K = 0 in C++
- Find minimum radius such that atleast k point lie inside the circle in C++
- Find a number x such that sum of x and its digits is equal to given n in C++
- Find x such that:$\frac{-1}{5}=\frac{8}{x}$.
- For which value(s) of \( k \) will the pair of equations\( k x+3 y=k-3 \)\( 12 x+k y=k \)have no solution?
- Largest K digit number divisible by X in C++

Advertisements