Skip to content

Commit

Permalink
Merge pull request #21 from artemis-i-guess/main
Browse files Browse the repository at this point in the history
added solution of Q29 of Leetcode in cpp
  • Loading branch information
DugarRishab authored Oct 2, 2022
2 parents c0353da + e50d880 commit 2a9fa1b
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions leetCode Solutions/Q29_divideTwoIntegers/Q29_divideTwoIntegers.cpp
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;
}
};

0 comments on commit 2a9fa1b

Please sign in to comment.