-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from artemis-i-guess/main
added solution of Q29 of Leetcode in cpp
- Loading branch information
Showing
1 changed file
with
30 additions
and
0 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
leetCode Solutions/Q29_divideTwoIntegers/Q29_divideTwoIntegers.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// using logs -> formula : a/b = e ln(a) / e ln(b) = e( ln(a) – ln(b) ). | ||
// adding 0.0000000001 to compensate for errors | ||
|
||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
|
||
long long divide(long long dividend, long long divisor) { | ||
|
||
// edge cases | ||
if (dividend == 0 || divisor == 0) return 0; | ||
if(dividend==INT_MIN && divisor==-1) return INT_MAX; | ||
if(dividend==INT_MIN && divisor==1) return INT_MIN; | ||
|
||
// finding out sign of answer | ||
bool sign = (dividend>=0) == (divisor>=0) ? true : false; | ||
|
||
//getting absolute value | ||
long long div = abs(dividend); | ||
long long divsr = abs(divisor); | ||
|
||
// using the formula | ||
long long ans = exp(log(div) - log(divsr)+ 0.0000000001); | ||
|
||
return sign ? ans : -ans; | ||
} | ||
}; |