{"componentChunkName":"component---src-templates-blog-template-js","path":"/module-10-9-7","result":{"data":{"site":{"siteMetadata":{"title":"SITESH"}},"markdownRemark":{"html":"<p>MOD: 10^9 + 7 (let's say we call this MOD here)</p>\n<p>Context:</p>\n<p>Those who have done some competitive coding challenges or Leetcode or Hackerrank problems, you might have come across this special number to be used before returning your final answer.<br>\nMore specifically, the result in most of the cases go beyond the allowable ranges for a datatype and it overflows and introduces lot of bugs in the code.</p>\n<p>You can get the required answer by taking a modulo of your result by MOD.</p>\n<p><strong>Let's take an example of Java</strong></p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">public int multiplier(int a, int b) {\n  int c = a * b;\n  return c;\n}</code></pre></div>\n<p>In this case, we have a simple multiplier which multiplies two int numbers and returns a <strong>int.</strong></p>\n<p>If we put a = 40001, b = 40001, then both of them are well within the range of int <strong>(-2147483648 to 2147483647</strong>.)</p>\n<p>But as soon as we multiply, c overflows and gets a negative number.</p>\n<p>So, will the modulo help in this case ? Let's try it out.</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">public int multiplier(int a, int b) {\n  int c = (a % MOD * b % MOD );\n  return c % MOD;\n}</code></pre></div>\n<p>Did you see what I did. </p>\n<p>Yes you are correct, I took the modulo with the individual number and then I took with the result.</p>\n<p><strong>Problems ?</strong></p>\n<p>When we multiplied both the numbers, c has already overflown and the above code basically takes a modulo on the -ve number which we never wanted.</p>\n<p><strong>Solution ?</strong> </p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">public int multiplier(int a, int b) {\n  long c = a * b;\n  long res = c % MOD;\n  return (int) res;\n}</code></pre></div>\n<p>This will help you solve the problem.</p>\n<p>Thanks for reading, I hope you don't spend entire 2 hours not knowing WTH my solution is not working.</p>\n<p>Happy Coding !</p>","frontmatter":{"date":"December 13, 2021","path":"/module-10-9-7","title":"The 10^9 + 7 that everyone faces.","thumbnail":null,"metaDescription":null}}},"pageContext":{}}}