Friday, January 27, 2023
HomeSoftware DevelopmentDiscover the quantity N, the place (N+X) divisible by Y and (N-Y)...

Discover the quantity N, the place (N+X) divisible by Y and (N-Y) divisible by X


Enhance Article

Save Article

Like Article

Enhance Article

Save Article

Given two numbers X and Y. the duty is to search out the quantity N (N ≤ 10^18) which satisfies these two situations:

  • (N + X) is divisible by Y
  • (N – Y) is divisible by X

Examples:

Enter: X = 18, Y = 42
Output:780

Enter: X = 100, Y = 200
Output: 500

Strategy: The issue could be solved primarily based on the next concept:

We will implement the easy math idea of the quantity system.

  • N + X = Y    …………(i)
  • N – Y = X     …………(ii)

Normalizes the equation we are able to get N could also be equal to (X*Y – X + Y). Additionally, it’s satisfying these two situations. So, the reply is (X * Y – X + Y).

Beneath is the implementation of the above method:

C++

#embody <iostream>

utilizing namespace std;

#outline int lengthy lengthy int

  

int FindN(int x, int y)

{

  

    

    int N = (x * y) - x + y;

  

    

    return N;

}

  

signed fundamental()

{

  

    int X = 18, Y = 42;

  

    

    cout << FindN(X, Y);

    return 0;

}

Time Complexity: O(1)
Auxiliary House: O(1)



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments