Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 7311

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 60k
Author
Asked: November 28, 20242024-11-28T02:38:08+00:00 2024-11-28T02:38:08+00:00

LeetCode Question 29

  • 60k

29. Divide Two Integers

Given two integers dividend and divisor, divide two integers without using multiplication, division, and mod operator.

Let's solve it 🙂

var divide = function(dividend, divisor) { }; 
Enter fullscreen mode Exit fullscreen mode

Function divide has two parameters dividend and divisor.

First of all, Let's check some weird scenarios:

  1. Question has a note which says 32-bit signed integer range: [−2*31, 2*31 − 1] so this indicates the max and min(on Negative side) number which we need to process
  2. What if the dividend and divisor both have negative value
  3. What if the dividend or divisor has negative value

Let's code it 😉

 const signedIntegerValue = 2**31  var divide = function(dividend, divisor) {     const retIsNegative = Math.sign(divisor) !== Math.sign(dividend);     dividend = Math.abs(dividend)     divisor = Math.abs(divisor)      let result = 0      // Logic here below...      return retIsNegative ? -result : result }; 
Enter fullscreen mode Exit fullscreen mode

Define the 32 bit signed integer range as constant at above

Now inside function calculate weather we have one of the value as negative or not.

If both signs are not same then, return value is negative.

Next lines, Makes both of them to the absolute value as it will be easy to handle the sign later.

And predefined result value 0 and added result value as the return value for the function.

_Logic _

    while (divisor <= dividend) {         let value = divisor         let multiple = 1         while (value + value <= dividend) {             value += value             multiple += multiple         }         dividend = dividend - value         result += multiple     }      if (result > (signedIntegerValue - 1)) {         return retIsNegative ? -signedIntegerValue : signedIntegerValue - 1     } 
Enter fullscreen mode Exit fullscreen mode

Consider dividend value is 10 and divisor value is 3.
Running a while loop till the divisor is less than dividend.

Assigning divisor value to value variable

Initially, considering multiple value is 1.

Running a nest while loop when (*value + value *) which is 2 * divisor value is less than dividend – This loop runs for the logarithmic purpose to remove the values from dividend

so now the value = value + value which is (3 + 3) = 6

and multiple value is now 2

Now, nested while loop check the condition

so new value of the value = value + value which is (6 + 3) = 9 and 9 < divisor 10

New value of value is 9 and multiple stands as 3

Now, nested while loop check the condition, where 12 <= 10 which is false.

Hence nested loop is over and next line after the loop starts executing.

Hence new value of dividend is 10 – 9 = 1

return value is result(0) + multiple(3) = 3

Checking the return value with the signed 32 integer value

and returning according to the signatures for the max and min signed 32 integer value.

If the return is not greater than signed integer value then it returns as the signed value suggests.

Full Code as follows:

const signedIntegerValue = 2 ** 31 var divide = function (dividend, divisor) {     const retIsNegative = Math.sign(divisor) !== Math.sign(dividend);     dividend = Math.abs(dividend)     divisor = Math.abs(divisor)      let result = 0     while (divisor <= dividend) {         let value = divisor         let multiple = 1         while (value + value <= dividend) {             value += value             multiple += multiple         }         dividend = dividend - value         result += multiple     }      if (result > (signedIntegerValue - 1)) {         return retIsNegative ? -signedIntegerValue : signedIntegerValue - 1     }     return retIsNegative ? -result : result }; 
Enter fullscreen mode Exit fullscreen mode

Hope this helps. Happy Coding!

iterativejavascriptleetcodewebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.