C++ code to get two numbers in range x with given rules


Suppose we have a number x. We have to find two integers a and b, such that both of them will be in between 1 and x, a is divisible by b, a * b > x but a/b < x. If not possible, return -1. So, if the input is like x = 10, then the output will be 6 and 3, (other answers are also possible)

To solve this, we will follow these steps −

if x < 2, then:
   print -1
   return
print x and x

Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h>
using namespace std;
void solve(int x){
   if (x < 2){
      cout << -1;
      return;
   }
   cout << x << ", " << x;
}
int main(){
   int x = 10;
   solve(x);
}

Input

10

Output

10,10

Updated on: 15-Mar-2022

192 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements