
- 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 a distinct pair (x, y) in given range such that x divides y in C++
Here we will see one interesting problem, we will find a pair (x, y), where x and y are in range so l <= x, y <= r, the pair will have one property, the value of x divides y. If there are multiple pairs available, then choose only one.
We can solve this problem in O(1) time, if we get the value of lower limit l and 2l. We know that the smallest value of y/x can be 2, and if some greater value is present in the range then 2 will be in range. And if we increase x, it will also increase 2x, so l and 2l will be the minimum pair to fall in given range.
Example
#include<iostream> using namespace std; void getPair(int l, int r) { int x = l; int y = 2 * l; cout << "(" << x << ", " << y << ")" << endl; } int main() { int l = 3, r = 6; getPair(l, r); }
Output
(3, 6)
- Related Articles
- Find number of pairs (x, y) in an array such that x^y > y^x in C++
- Count Distinct Non-Negative Integer Pairs (x, y) that Satisfy the Inequality x*x + y*y < n in C++
- Count of pairs (x, y) in an array such that x < y in C++
- Count Distinct Non-Negative Integer Pairs (x, y) that Satisfy the Inequality x*x +\ny*y < n in C++
- Find maximum among x^(y^2) or y^(x^2) where x and y are given in C++
- If \( a \) and \( b \) are distinct positive primes such that \( \sqrt[3]{a^{6} b^{-4}}=a^{x} b^{2 y} \), find \( x \) and \( y \).
- Find the smallest number X such that X! contains at least Y trailing zeros in C++
- Find larger of x^y and y^x in C++
- Find smallest values of x and y such that ax – by = 0 in C++
- Find x, y, z that satisfy 2/n = 1/x + 1/y + 1/z in C++
- Solve the following pair of equations by reducing them to a pair of linear equations$ 6 x+3 y=6 x y $$2 x+4 y=5 x y $
- Find the value of $p$ such that the pair of equations \( 3 x-y-5=0 \) and \( 6 x-2 y-p=0 \), has no solution.
- A pair of linear equations which has a unique solution \( x=2, y=-3 \) is(A) \( x+y=-1 \)\( 2 x-3 y=-5 \)(B) \( 2 x+5 y=-11 \)\( 4 x+10 y=-22 \)(C) \( 2 x-y=1 \)\( 3 x+2 y=0 \)(D) \( x-4 y-14=0 \)\( 5 x-y-13=0 \)
- Factorize the algebraic expression $a(x-y)+2b(y-x)+c(x-y)^2$.
- Find a relation between x and y such that point$ (x, y)$ is equidistant from the point (7, 1) and (3, 5)

Advertisements