{"id":1364,"date":"2023-09-24T12:54:05","date_gmt":"2023-09-24T12:54:05","guid":{"rendered":"https:\/\/spacican.com\/notes\/?p=1364"},"modified":"2024-06-29T15:47:53","modified_gmt":"2024-06-29T15:47:53","slug":"dsa-math-notes","status":"publish","type":"post","link":"https:\/\/spacican.com\/notes\/dsa-math-notes\/","title":{"rendered":"DSA Math Notes"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Faulhaber&#8217;s formula<\/h4>\n\n\n\n<p>\\[ 1+2+3+\u2026+n\\ \\ \\ =\\ \\ \\ \\frac{n\\left(n+1\\right)}{2} \\]<\/p>\n\n\n\n<p>\\[ 1^2+2^2+3^2+\u2026+n^2\\ \\ \\ =\\ \\ \\ \\frac{n\\left(n+1\\right)\\left(2n+1\\right)}{6} \\]<\/p>\n\n\n\n<p>\\[ 1^3+2^3+3^3+\u2026+n^3\\ \\ \\ =\\ \\ \\ \\left[\\frac{n\\left(n+1\\right)}{2}\\right]^2 \\]<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Sum of AP, GP and Harmonic series<\/h4>\n\n\n\n<p>For an AP series, \\[ a_1+a_2+a_3+\u2026..+b\\ \\ =\\ \\ \\frac{1}{2}\\left(a+b\\right) \\]<\/p>\n\n\n\n<p>For a GP series, \\[ a+ak+ak^2+ak^3+\u2026..+b\\ \\ =\\ \\ \\frac{bk-a}{k-1} \\]<\/p>\n\n\n\n<p>For a Harmonic Series \\[ \\sum_{k=1}^n\\frac{1}{k} \\], the sum is always less than or equal to \\( 1+\\log_2n \\)<\/p>\n\n\n\n<p>\\[ \\Rightarrow 1+\\frac{1}{2}+\\frac{1}{3}+\\frac{1}{4}+\\frac{1}{5}+\\frac{1}{6}+\\frac{1}{7}+\\frac{1}{8}+\u2026. \\]<\/p>\n\n\n\n<p>\\[ \\le1+\\underline{\\frac{1}{2}+\\frac{1}{2}}+\\underline{\\frac{1}{4}+\\frac{1}{4}+\\frac{1}{4}+\\frac{1}{4}}+\\underline{\\frac{1}{8}+\u2026\u2026..} \\]<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Sieve of Eratosthenes (Linear Sieve) Algorithm<\/h4>\n\n\n\n<p>Used to find all prime numbers from 1 to N with the help of a boolean array.<\/p>\n\n\n\n<p class=\"accordion-button tertiary collapsed accordion-title\">\\( O\\left(n\\log\\log n\\right) \\)solution in Java<\/p><div class=\"accordion-panel collapsed\">\n<div class=\"wp-block-create-block-indent indenter\">\n<p>Following is an <mark>in-efficient<\/mark> \\( O\\left(n^2\\right) \\) way to prepare the array:<\/p>\n\n\n\n<pre class=\"java\" style=\"font-family:monospace;background:white\"><span style=\"color: #000066; font-weight: bold;\">int<\/span> maxN <span style=\"color: #339933;\">=<\/span> <span style=\"color: #cc66cc;\">100<\/span><span style=\"color: #339933;\">;<\/span>\n<span style=\"color: #000066; font-weight: bold;\">boolean<\/span> isPrime<span style=\"color: #009900;\">&#91;<\/span><span style=\"color: #009900;\">&#93;<\/span> <span style=\"color: #339933;\">=<\/span> <span style=\"color: #000000; font-weight: bold;\">new<\/span> <span style=\"color: #000066; font-weight: bold;\">boolean<\/span><span style=\"color: #009900;\">&#91;<\/span>maxN <span style=\"color: #339933;\">+<\/span> <span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #009900;\">&#93;<\/span><span style=\"color: #339933;\">;<\/span>\nisPrime<span style=\"color: #009900;\">&#91;<\/span><span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #009900;\">&#93;<\/span> <span style=\"color: #339933;\">=<\/span> <span style=\"color: #000066; font-weight: bold;\">false<\/span><span style=\"color: #339933;\">;<\/span>\n<span style=\"color: #000000; font-weight: bold;\">for<\/span> <span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #000066; font-weight: bold;\">int<\/span> i <span style=\"color: #339933;\">=<\/span> <span style=\"color: #cc66cc;\">2<\/span><span style=\"color: #339933;\">;<\/span> i <span style=\"color: #339933;\">&lt;=<\/span> maxN<span style=\"color: #339933;\">;<\/span> i<span style=\"color: #339933;\">++<\/span><span style=\"color: #009900;\">&#41;<\/span>\n    <span style=\"color: #000000; font-weight: bold;\">for<\/span> <span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #000066; font-weight: bold;\">int<\/span> j <span style=\"color: #339933;\">=<\/span> i <span style=\"color: #339933;\">*<\/span> <span style=\"color: #cc66cc;\">2<\/span><span style=\"color: #339933;\">;<\/span> j <span style=\"color: #339933;\">&lt;=<\/span> maxN<span style=\"color: #339933;\">;<\/span> j <span style=\"color: #339933;\">+=<\/span> i<span style=\"color: #009900;\">&#41;<\/span> <span style=\"color: #009900;\">&#123;<\/span>\n        isPrime<span style=\"color: #009900;\">&#91;<\/span>j<span style=\"color: #009900;\">&#93;<\/span> <span style=\"color: #339933;\">=<\/span> <span style=\"color: #000066; font-weight: bold;\">false<\/span><span style=\"color: #339933;\">;<\/span><\/pre>\n\n\n\n<p>Following is an <mark>optimised<\/mark> \\( O\\left(n\\log\\left(\\log n\\right)\\right) \\) version of above approach:<\/p>\n\n\n\n<pre class=\"java\" style=\"font-family:monospace;background:white\"><span style=\"color: #000066; font-weight: bold;\">int<\/span> maxN <span style=\"color: #339933;\">=<\/span> <span style=\"color: #cc66cc;\">100<\/span><span style=\"color: #339933;\">;<\/span>\n<span style=\"color: #000066; font-weight: bold;\">boolean<\/span> isPrime<span style=\"color: #009900;\">&#91;<\/span><span style=\"color: #009900;\">&#93;<\/span> <span style=\"color: #339933;\">=<\/span> <span style=\"color: #000000; font-weight: bold;\">new<\/span> <span style=\"color: #000066; font-weight: bold;\">boolean<\/span><span style=\"color: #009900;\">&#91;<\/span>maxN <span style=\"color: #339933;\">+<\/span> <span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #009900;\">&#93;<\/span><span style=\"color: #339933;\">;<\/span>\nisPrime<span style=\"color: #009900;\">&#91;<\/span><span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #009900;\">&#93;<\/span> <span style=\"color: #339933;\">=<\/span> <span style=\"color: #000066; font-weight: bold;\">false<\/span><span style=\"color: #339933;\">;<\/span>\n<span style=\"color: #000000; font-weight: bold;\">for<\/span> <span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #000066; font-weight: bold;\">int<\/span> i <span style=\"color: #339933;\">=<\/span> <span style=\"color: #cc66cc;\">2<\/span><span style=\"color: #339933;\">;<\/span> i <span style=\"color: #339933;\">*<\/span> i <span style=\"color: #339933;\">&lt;=<\/span> maxN<span style=\"color: #339933;\">;<\/span> i<span style=\"color: #339933;\">++<\/span><span style=\"color: #009900;\">&#41;<\/span>\n    <span style=\"color: #000000; font-weight: bold;\">if<\/span> <span style=\"color: #009900;\">&#40;<\/span>isPrime<span style=\"color: #009900;\">&#91;<\/span>i<span style=\"color: #009900;\">&#93;<\/span><span style=\"color: #009900;\">&#41;<\/span>\n        <span style=\"color: #000000; font-weight: bold;\">for<\/span> <span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #000066; font-weight: bold;\">int<\/span> j <span style=\"color: #339933;\">=<\/span> i <span style=\"color: #339933;\">*<\/span> i<span style=\"color: #339933;\">;<\/span> j <span style=\"color: #339933;\">&lt;=<\/span> maxN<span style=\"color: #339933;\">;<\/span> j <span style=\"color: #339933;\">+=<\/span> i<span style=\"color: #009900;\">&#41;<\/span> <span style=\"color: #009900;\">&#123;<\/span>\n            isPrime<span style=\"color: #009900;\">&#91;<\/span>j<span style=\"color: #009900;\">&#93;<\/span> <span style=\"color: #339933;\">=<\/span> <span style=\"color: #000066; font-weight: bold;\">false<\/span><span style=\"color: #339933;\">;<\/span><\/pre>\n<\/div>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\">GCD (Greatest Common Devisor)<\/h3>\n\n\n\n<p>GCD of two numbers \\( a \\) and \\( b \\) is the greatest integer number which is a divisor of both \\( a \\) and \\( b \\). As an example, GCD of 6 and 10 is 2 because 2 is the greatest integer which can equally devide both 6 and 10. and GCD of 6 and 12 is 6 because 6 can equally devide both 6 as well as 12.<\/p>\n\n\n\n<p>GCD is also called HCF (Highest common factor) or GCM (Greatest common measure)<\/p>\n\n\n\n<div class=\"wp-block-create-block-indent indenter\">\n<h4 class=\"wp-block-heading\">Properties of GCD<\/h4>\n\n\n\n<p><\/p>\n\n\n\n<ul>\n<li>\\( \\gcd\\left(a,b\\right)=\\gcd\\left(b,a\\right) \\) i.e. GCD is a cumutative function.<\/li>\n\n\n\n<li>\\( \\gcd\\left(a,\\gcd\\left(b,c\\right)\\right)=\\gcd\\left(\\gcd\\left(a,b\\right),c\\right) \\). i.e. GCD is assosiative function. Therefore, the GCD of multiple values can be simply denoted as \\( \\gcd\\left(a,b,c,\u2026\\right) \\).<\/li>\n\n\n\n<li>\\( \\gcd\\left(a,0\\right)=a \\). Because any number \\( a \\) can equally devide both \\( a \\) as well as \\( 0 \\)<\/li>\n\n\n\n<li>If \\( c \\) is any constant integer, then \\( \\gcd\\left(a,c\\times a+r\\right)=\\gcd\\left(a,r\\right) \\)<\/li>\n\n\n\n<li><mark><strong>Euclidean algorithm for GCD<\/strong>: <\/mark> Euclidean algorithm is based on the previous property ( \\( \\gcd\\left(a,c\\times a+r\\right)=\\gcd\\left(a,r\\right) \\)). According to Euclidean algo, if \\( a\\ge b \\) and \\( r \\) is remainder of \\[ \\frac{a}{b} \\] then <span class=\"border-curly-black\">\\( \\gcd\\left(a,b\\right)=\\gcd\\left(r,b\\right) \\)<\/span>. Euclidean algo can be used to efficiently calculate GCD&#8217;s of two integers using recursive function calls.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-create-block-indent indenter\">\n<p class=\"accordion-button tertiary collapsed accordion-title\">Euclidean algorithm implementation<\/p><div class=\"accordion-panel collapsed\">\n<pre class=\"java\" style=\"font-family:monospace;\">    <span style=\"color: #000000; font-weight: bold;\">public<\/span> <span style=\"color: #000066; font-weight: bold;\">int<\/span> gcd<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #000066; font-weight: bold;\">int<\/span> a, <span style=\"color: #000066; font-weight: bold;\">int<\/span> b<span style=\"color: #009900;\">&#41;<\/span> <span style=\"color: #009900;\">&#123;<\/span>\n        <span style=\"color: #000000; font-weight: bold;\">if<\/span> <span style=\"color: #009900;\">&#40;<\/span>b <span style=\"color: #339933;\">==<\/span> <span style=\"color: #cc66cc;\">0<\/span><span style=\"color: #009900;\">&#41;<\/span> <span style=\"color: #000000; font-weight: bold;\">return<\/span> a<span style=\"color: #339933;\">;<\/span>\n        <span style=\"color: #666666; font-style: italic;\">\/\/notice that the position of a % b, b are swapped, this is to ensure<\/span>\n        <span style=\"color: #666666; font-style: italic;\">\/\/that the right argument is always smaller than left argument<\/span>\n        <span style=\"color: #666666; font-style: italic;\">\/\/if not swapped, seperate condition checks have to be implemented which<\/span>\n        <span style=\"color: #666666; font-style: italic;\">\/\/will check the larger and smaller numbers to calculate the remainder<\/span>\n        <span style=\"color: #000000; font-weight: bold;\">return<\/span> gcd<span style=\"color: #009900;\">&#40;<\/span>b, a <span style=\"color: #339933;\">%<\/span> b<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n    <span style=\"color: #009900;\">&#125;<\/span><\/pre>\n<\/div>\n<\/div>\n\n\n\n<ul>\n<li>If \\( g \\) is gcd of \\( a \\) and \\( b \\) then \\( g \\) is also a divisor of \\( a+b \\). In other words, \\( \\gcd\\left(a,b\\right)=\\gcd\\left(a,b,a+b\\right) \\).<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-create-block-indent indenter\">\n<p class=\"accordion-button tertiary collapsed accordion-title\">Proof<\/p><div class=\"accordion-panel collapsed\">\n<p>Let, \\( g \\) is GCD of \\( a \\) and \\( b \\)<\/p>\n\n\n\n<p>\\( \\Rightarrow a=g\\cdot k_1 \\) and \\( b=g.k_2 \\)<\/p>\n\n\n\n<p>\\( \\Rightarrow\\left(a+b\\right)=g\\left(k_1+k_2\\right) \\)<\/p>\n\n\n\n<p>Which prooves that \\( g \\) is divisor of \\( \\left(a+b\\right) \\)<\/p>\n<\/div>\n<\/div>\n\n\n\n<ul>\n<li>\\( \\gcd\\left(a,b\\right)=\\gcd\\left(a,a+b\\right) \\). Similarly, \\( \\gcd\\left(a,b,c,\u2026\\right)=\\gcd\\left(a,a+b,a+b+c,\u2026\\right) \\)<\/li>\n\n\n\n<li>\\( \\gcd\\left(a,b\\right)=\\gcd\\left(-a,b\\right)=\\gcd\\left(a,-b\\right)=\\gcd\\left(-a,-b\\right) \\)<\/li>\n\n\n\n<li>\\( \\gcd\\left(\u2026,a_i,\u2026.,a_j\u2026\\right)=\\gcd\\left(\u2026,a_i,\u2026.,a_j+n\\cdot a_i\u2026\\right) \\).<\/li>\n\n\n\n<li>If \\( a_1\\text{ and }a_2 \\) are co-prime, then \\( \\gcd\\left(an,b\\right)=\\gcd\\left(n,b\\right) \\)<\/li>\n\n\n\n<li>If \\( \\gcd\\left(a,b\\right)=d \\), \\( \\Rightarrow a=P\\cdot d \\) and \\( \\Rightarrow b=Q\\cdot d \\) where \\( P \\) and \\( Q \\) are some constants, then \\( P \\) and \\( Q \\) are co-prime.\n<ul>\n<li>\\( \\gcd\\left(P,Q\\right)=1 \\)<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h5 class=\"wp-block-heading\">From wikipedia<\/h5>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<ul>\n<li>If&nbsp;<em>a<\/em>&nbsp;divides the product&nbsp;<em>b<\/em>\u22c5<em>c<\/em>, and&nbsp;gcd(<em>a<\/em>,&nbsp;<em>b<\/em>) =&nbsp;<em>d<\/em>, then&nbsp;<em>a<\/em>\/<em>d<\/em>&nbsp;divides&nbsp;<em>c<\/em>.<\/li>\n\n\n\n<li>If&nbsp;<em>m<\/em>&nbsp;is a positive integer, then&nbsp;gcd(<em>m<\/em>\u22c5<em>a<\/em>,&nbsp;<em>m<\/em>\u22c5<em>b<\/em>) =&nbsp;<em>m<\/em>\u22c5gcd(<em>a<\/em>,&nbsp;<em>b<\/em>).<\/li>\n\n\n\n<li>If&nbsp;<em>m<\/em>&nbsp;is a positive common divisor of&nbsp;<em>a<\/em>&nbsp;and&nbsp;<em>b<\/em>, then&nbsp;gcd(<em>a<\/em>\/<em>m<\/em>,&nbsp;<em>b<\/em>\/<em>m<\/em>) = gcd(<em>a<\/em>,&nbsp;<em>b<\/em>)\/<em>m<\/em>. This can further be deduced into following:\n<ol class=\"has-small-font-size\">\n<li>\\[ \\gcd\\left(\\frac{a_1}{\\gcd\\left(a_1,\u2026,a_i\\right)},\u2026,\\frac{a_i}{\\gcd\\left(a_1,\u2026,a_i\\right)}\\right)=\\frac{\\gcd\\left(a_1,\u2026,a_i\\right)}{\\gcd\\left(a_1,\u2026,a_i\\right)}=1 \\]<\/li>\n\n\n\n<li>\\[ \\gcd\\left(a_1,\u2026,a_i\\right)=m\\cdot\\gcd\\left(\\frac{a_1}{m},\u2026,\\frac{a_i}{m}\\right) \\]<\/li>\n<\/ol>\n<\/li>\n\n\n\n<li>If&nbsp;<em>m<\/em>&nbsp;is any integer, then&nbsp;gcd(<em>a<\/em>&nbsp;+&nbsp;<em>m<\/em>\u22c5<em>b<\/em>,&nbsp;<em>b<\/em>) = gcd(<em>a<\/em>,&nbsp;<em>b<\/em>)<\/li>\n\n\n\n<li>If&nbsp;<em>a<\/em><sub>1<\/sub>&nbsp;and&nbsp;<em>a<\/em><sub>2<\/sub>&nbsp;are relatively prime, then&nbsp;gcd(<em>a<\/em><sub>1<\/sub>\u22c5<em>a<\/em><sub>2<\/sub>,&nbsp;<em>b<\/em>) = gcd(<em>a<\/em><sub>1<\/sub>,&nbsp;<em>b<\/em>)\u22c5gcd(<em>a<\/em><sub>2<\/sub>,&nbsp;<em>b<\/em>)<\/li>\n\n\n\n<li>gcd(<em>n<\/em><sup><em>a<\/em><\/sup>&nbsp;\u2212 1,&nbsp;<em>n<\/em><sup><em>b<\/em><\/sup>&nbsp;\u2212 1) =&nbsp;<em>n<\/em><sup>gcd(<em>a<\/em>,<em>b<\/em>)<\/sup>&nbsp;\u2212 1<\/li>\n<\/ul>\n<cite>https:\/\/en.wikipedia.org\/wiki\/Greatest_common_divisor#Properties<\/cite><\/blockquote>\n<\/div>\n\n\n\n<div class=\"wp-block-create-block-extra-margin-top extra-margin-top\"><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">Extended Euclidean algorithm<\/h4>\n\n\n\n<div class=\"wp-block-create-block-indent indenter\">\n<p>In addition to calculating just the GCD using euclidean algo as explained above in properties of GCD, the <span style=\"text-decoration: underline;\">Extended<\/span> Euclidean algorithm also helps in finding the coefficients of B\u00e9zout&#8217;s identity \\( x \\)  and \\( y \\). i.e., if \\( \\gcd\\left(a,b\\right)=d \\), then the extended euclidean helps in finding coefficients \\( x \\) and \\( y \\), such that \\( xa+yb=d \\).<\/p>\n\n\n\n<p>The above equation is called B\u00e9zout&#8217;s identity. This equation is particularly usefull when \\( a \\) and \\( b \\) are co-primes, in that case, \\( xa+yb=1 \\) which implies that \\( x \\) is modular multiplicative inverse of \\( a \\) and \\( y \\) is modular multiplicative inverse of \\( b \\).  Modular multiplicative inverse, for instance, has application in cryptography. <\/p>\n\n\n\n<ul>\n<li><a href=\"https:\/\/www.youtube.com\/watch?v=6KmhCKxFWOs\">Nice video<\/a> explaining the Extended Euclidean algorithm for calculating cofficients of B\u00e9zout&#8217;s identity.<\/li>\n<\/ul>\n\n\n\n<p class=\"accordion-button tertiary collapsed accordion-title\">Implementation of Extended Euclidean Algorithm<\/p><div class=\"accordion-panel collapsed\">\n<p><\/p>\n\n\n\n<p>As we know that based on euclidean algorithm \\( \\gcd\\left(a,b\\right)=\\gcd\\left(b,a\\%b\\right) \\).<\/p>\n\n\n\n<p>Because of this recursive behaviour, the coefficients \\( x \\) and \\( y \\) of integers \\( a \\) and \\( b \\) ( such that \\( ax+by=\\gcd\\left(a,b\\right) \\) ) can be obtained with the help of the coefficients \\( x_0 \\) and \\( y_0 \\) of integers \\( b \\) and \\( a\\%b \\) (such that \\( bx_0+\\left(a\\%b\\right)y_0=\\gcd\\left(b,a\\%b\\right) \\) )<\/p>\n\n\n\n<p>Derivation of relation between \\( x,y\\text{ and }x_0,y_0 \\) (as explained on <a href=\"https:\/\/www.geeksforgeeks.org\/euclidean-algorithms-basic-and-extended\/\">GFG<\/a>):<\/p>\n\n\n\n<p>\\[ \\begin{aligned} &amp;\\gcd\\left(a,b\\right)=\\gcd\\left(b,a\\%b\\right) \\\\ \\text{but, }&amp;\\gcd\\left(a,b\\right)=x\\cdot a+y\\cdot b \\\\ \\text{similarly, }&amp;\\gcd\\left(b,a\\%b\\right)=x_0\\cdot b+y_0\\cdot a\\%b \\end{aligned} \\]<\/p>\n\n\n\n<p>based on above 3 equations,<\/p>\n\n\n\n<p>\\[ \\begin{aligned} x\\cdot a+y\\cdot b &amp;=x_0\\cdot b+y_0\\cdot a\\%b \\\\ x\\cdot a+y\\cdot b &amp;=x_0\\cdot b+y_0\\cdot (a-\\frac{a}{b}*b) \\ \\ \\ \\ \\ -\\text{ ( note that here }\\frac{a}{b}\\text{ represents integer devision of } a \\text{ by } b \\text{)}\\\\x\\cdot a+y\\cdot b &amp;=x_0\\cdot b+y_0\\cdot a-y_0\\frac{a}{b}b \\\\ &amp;=y_0\\cdot a+\\left(x_0-y_0\\frac{a}{b}\\right)\\cdot b \\end{aligned} \\]<\/p>\n\n\n\n<p>Comparing LHS with RHS, we get<\/p>\n\n\n\n<p><span class=\"border-curly-red\">\\[ x=y_0\\text{ and }y=x_0-y_0\\cdot\\frac{a}{b} \\]<\/span><\/p>\n\n\n\n<div class=\"wp-block-create-block-extra-margin-top extra-margin-top\"><\/div>\n\n\n\n<h5 class=\"wp-block-heading\">Implementation based on above relation between \\( x,y,x_0\\text{ and }y_0 \\)<\/h5>\n\n\n\n<pre class=\"java\" style=\"font-family:monospace;\"><span style=\"color: #000000; font-weight: bold;\">class<\/span> Solution<span style=\"color: #009900;\">&#123;<\/span>\n    <span style=\"color: #000000; font-weight: bold;\">private<\/span> <span style=\"color: #000066; font-weight: bold;\">int<\/span> x, y, g<span style=\"color: #339933;\">;<\/span>\n&nbsp;\n    <span style=\"color: #000000; font-weight: bold;\">private<\/span> <span style=\"color: #000066; font-weight: bold;\">void<\/span> gcd<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #000066; font-weight: bold;\">int<\/span> a, <span style=\"color: #000066; font-weight: bold;\">int<\/span> b<span style=\"color: #009900;\">&#41;<\/span> <span style=\"color: #009900;\">&#123;<\/span>\n&nbsp;\n        <span style=\"color: #666666; font-style: italic;\">\/\/keep in mind the default value of x and y for base condition<\/span>\n        <span style=\"color: #000000; font-weight: bold;\">if<\/span> <span style=\"color: #009900;\">&#40;<\/span>b <span style=\"color: #339933;\">==<\/span> <span style=\"color: #cc66cc;\">0<\/span><span style=\"color: #009900;\">&#41;<\/span> <span style=\"color: #009900;\">&#123;<\/span>\n            x <span style=\"color: #339933;\">=<\/span> <span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #339933;\">;<\/span>\n            y <span style=\"color: #339933;\">=<\/span> <span style=\"color: #cc66cc;\">0<\/span><span style=\"color: #339933;\">;<\/span>\n            g <span style=\"color: #339933;\">=<\/span> a<span style=\"color: #339933;\">;<\/span>\n            <span style=\"color: #000000; font-weight: bold;\">return<\/span><span style=\"color: #339933;\">;<\/span>\n        <span style=\"color: #009900;\">&#125;<\/span>\n&nbsp;\n        <span style=\"color: #666666; font-style: italic;\">\/\/notice how x and y become x0 and y0 due to the recursive call<\/span>\n        gcd<span style=\"color: #009900;\">&#40;<\/span>b, a <span style=\"color: #339933;\">%<\/span> b<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n        <span style=\"color: #000066; font-weight: bold;\">int<\/span> x0 <span style=\"color: #339933;\">=<\/span> x, y0 <span style=\"color: #339933;\">=<\/span> y<span style=\"color: #339933;\">;<\/span>\n&nbsp;\n        <span style=\"color: #666666; font-style: italic;\">\/\/following is performed based on the derived relations between x, y, x0 and y0<\/span>\n        x <span style=\"color: #339933;\">=<\/span> y0<span style=\"color: #339933;\">;<\/span>\n        y <span style=\"color: #339933;\">=<\/span> x0 <span style=\"color: #339933;\">-<\/span> y0 <span style=\"color: #339933;\">*<\/span> <span style=\"color: #009900;\">&#40;<\/span>a<span style=\"color: #339933;\">\/<\/span>b<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n&nbsp;\n    <span style=\"color: #009900;\">&#125;<\/span>\n&nbsp;\n    <span style=\"color: #000000; font-weight: bold;\">public<\/span> <span style=\"color: #000066; font-weight: bold;\">void<\/span> printGCD<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #000066; font-weight: bold;\">int<\/span> a,<span style=\"color: #000066; font-weight: bold;\">int<\/span> b<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #009900;\">&#123;<\/span>\n        x <span style=\"color: #339933;\">=<\/span> <span style=\"color: #cc66cc;\">0<\/span><span style=\"color: #339933;\">;<\/span> y <span style=\"color: #339933;\">=<\/span> <span style=\"color: #cc66cc;\">0<\/span><span style=\"color: #339933;\">;<\/span> g <span style=\"color: #339933;\">=<\/span> <span style=\"color: #cc66cc;\">0<\/span><span style=\"color: #339933;\">;<\/span>\n&nbsp;\n        gcd<span style=\"color: #009900;\">&#40;<\/span>a, b<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n&nbsp;\n        <span style=\"color: #003399;\">System<\/span>.<span style=\"color: #006633;\">out<\/span>.<span style=\"color: #006633;\">println<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #0000ff;\">&quot;GCD of &quot;<\/span><span style=\"color: #339933;\">+<\/span>a<span style=\"color: #339933;\">+<\/span><span style=\"color: #0000ff;\">&quot; and &quot;<\/span><span style=\"color: #339933;\">+<\/span>b<span style=\"color: #339933;\">+<\/span><span style=\"color: #0000ff;\">&quot; : &quot;<\/span> <span style=\"color: #339933;\">+<\/span> g<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n        <span style=\"color: #003399;\">System<\/span>.<span style=\"color: #006633;\">out<\/span>.<span style=\"color: #006633;\">println<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #0000ff;\">&quot;bezout's identity (a*x + b*y = GCD) : &quot;<\/span><span style=\"color: #339933;\">+<\/span>a<span style=\"color: #339933;\">+<\/span><span style=\"color: #0000ff;\">&quot;*&quot;<\/span><span style=\"color: #339933;\">+<\/span>x<span style=\"color: #339933;\">+<\/span><span style=\"color: #0000ff;\">&quot; + &quot;<\/span><span style=\"color: #339933;\">+<\/span>b<span style=\"color: #339933;\">+<\/span><span style=\"color: #0000ff;\">&quot;*&quot;<\/span><span style=\"color: #339933;\">+<\/span>y<span style=\"color: #339933;\">+<\/span><span style=\"color: #0000ff;\">&quot; = &quot;<\/span><span style=\"color: #339933;\">+<\/span>g<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n    <span style=\"color: #009900;\">&#125;<\/span>\n<span style=\"color: #009900;\">&#125;<\/span>\n&nbsp;\n<span style=\"color: #666666; font-style: italic;\">\/\/Driver class<\/span>\n<span style=\"color: #000000; font-weight: bold;\">class<\/span> Test <span style=\"color: #009900;\">&#123;<\/span>\n    <span style=\"color: #000000; font-weight: bold;\">public<\/span> <span style=\"color: #000000; font-weight: bold;\">static<\/span> <span style=\"color: #000066; font-weight: bold;\">void<\/span> main<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #003399;\">String<\/span> args<span style=\"color: #009900;\">&#91;<\/span><span style=\"color: #009900;\">&#93;<\/span><span style=\"color: #009900;\">&#41;<\/span> <span style=\"color: #009900;\">&#123;<\/span>\n        <span style=\"color: #000000; font-weight: bold;\">new<\/span> Solution<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span>.<span style=\"color: #006633;\">printGCD<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #cc66cc;\">12<\/span>, <span style=\"color: #cc66cc;\">42<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n    <span style=\"color: #009900;\">&#125;<\/span>\n<span style=\"color: #009900;\">&#125;<\/span><\/pre>\n<\/div>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\">LCM (Least common Multiple)<\/h3>\n\n\n\n<p>LCM of two numbers \\( a \\) and \\( b \\) is the smallest number which is divisible by both \\( a \\) as well as \\( b \\). As an example, LCM of 6 and 10 is 30 because 30 is divisible by both 6 as well as 10. Similarly, LCM of 6 and 12 is 12 because 12 is devisible by both 12 as well as 6. <mark>LCM and GCD are closely related. By definition, LCM is opposite of GCD<\/mark><\/p>\n\n\n\n<p>For two numbers \\( a \\) and \\( b \\),<\/p>\n\n\n\n<p>\\( \\operatorname{lcm}\\left(a,b\\right)\\cdot\\gcd\\left(a,b\\right)=\\left|ab\\right| \\) \\( \\Rightarrow \\) <span class=\"border-curly-black\">\\[ \\operatorname{lcm}\\left(a,b\\right)=\\frac{\\left|ab\\right|}{\\gcd\\left(a,b\\right)} \\]<\/span><\/p>\n\n\n\n<p>It is important to note that \\[ \\operatorname{lcm}\\left(a,b,c\\right)\\ne\\frac{\\left|abc\\right|}{\\gcd\\left(a,b,c\\right)} \\]<\/p>\n\n\n\n<p><span class=\"border-curly-black\">\\[ \\operatorname{lcm}\\left(a,b,c\\right)=\\frac{abc}{\\gcd\\left(ab,bc,ca\\right)} \\]<\/span><\/p>\n\n\n\n<p>Similarly, \\[ \\operatorname{lcm}\\left(a,b,c,d\\right)=\\frac{abcd}{\\gcd\\left(abc,bcd,cda,dab\\right)} \\]<\/p>\n\n\n\n<p>Also, following relations between GCD and LCM holds true:<\/p>\n\n\n\n<ul>\n<li>\\( \\gcd\\left(a,\\operatorname{lcm}\\left(b,c,d\u2026\\right)\\right)=\\operatorname{lcm}\\left(\\gcd\\left(a,b\\right),\\gcd\\left(a,c\\right),\\gcd\\left(a,d\\right),\u2026\\right) \\)<\/li>\n\n\n\n<li>\\( \\operatorname{lcm}\\left(a,\\gcd\\left(b,c,d\u2026\\right)\\right)=\\gcd\\left(\\operatorname{lcm}\\left(a,b\\right),\\operatorname{lcm}\\left(a,c\\right),\\operatorname{lcm}\\left(a,d\\right),\u2026\\right) \\)<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">Properties of LCM<\/h4>\n\n\n\n<p>Properties are mostly similar to (but opposite of) GCD. By replacing \\[ \\gcd\\left(a,b\\right)\\text{ with }\\frac{\\left|a\\cdot b\\right|}{\\operatorname{lcm}\\left(a,b\\right)} \\] in the properties of GCD, following properties of LCM can be obtained.<\/p>\n\n\n\n<div class=\"wp-block-create-block-indent indenter\">\n<ul>\n<li>If \\( a \\) and \\( b \\) both equally devide \\( n \\), i.e. \\( a\\mid n\\text{ and }b\\mid n \\), then \\( \\operatorname{lcm}\\left(a,b\\right) \\) also equally devides \\( n \\), i.e. \\( \\operatorname{lcm}\\left(a,b\\right)\\mid n \\)<\/li>\n\n\n\n<li>\\( \\operatorname{lcm}\\left(an,bn\\right)=n\\operatorname{lcm}\\left(a,b\\right) \\)<\/li>\n\n\n\n<li>If \\( a \\) and \\( b \\) are co-prime, then \\[ \\operatorname{lcm}\\left(ab,n\\right)=\\frac{\\operatorname{lcm}\\left(a,n\\right)\\cdot\\operatorname{lcm}\\left(b,n\\right)}{m} \\]<\/li>\n<\/ul>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Modular Arithmatic<\/h3>\n\n\n\n<div class=\"wp-block-create-block-indent indenter\">\n<h4 class=\"wp-block-heading\">Modulo vs Remainder<\/h4>\n\n\n\n<p>Modulo and Remainder are same when performed on positive operands, but can produce different result when operands are negative. <mark><u class=\"underline\">in Java and C++, the % operator always produces the remainder of given operands<\/u><\/mark>. The result of % operator can be different when Modulo of operands is expected.<\/p>\n\n\n\n<p><strong><mark>Remainder<\/mark><\/strong>: The part that remains after integer devision of two operands.<\/p>\n\n\n\n<div class=\"wp-block-create-block-indent indenter\">\n<p class=\"accordion-button tertiary collapsed accordion-title\">Example of remainder of different operands<\/p><div class=\"accordion-panel collapsed\">\n<ul>\n<li><em>7 % 3 = <strong><mark>1<\/mark><\/strong><\/em> because \\[ \\frac{7}{3}=2 \\] which gives <em>7 = 3 * 2 + <strong>1<\/strong><\/em><\/li>\n\n\n\n<li><em>7 % -3 = <strong><mark>1<\/mark><\/strong><\/em> because \\[ \\frac{7}{-3}=-2 \\] which gives <em>7 = -3 * -2 + <strong>1<\/strong><\/em><\/li>\n\n\n\n<li><em>-7 % 3 = <strong><mark>-1<\/mark><\/strong><\/em> because \\[ \\frac{-7}{3}=-2 \\] which gives <em>-7 = 3 * -2 <strong>-1<\/strong><\/em><\/li>\n\n\n\n<li><em>-7 % -3 = <strong><mark>-1<\/mark><\/strong><\/em> because \\[ \\frac{-7}{-3}=2 \\] which gives <em>-7 = -3 * 2 <strong>-1<\/strong><\/em><\/li>\n<\/ul>\n<\/div>\n\n\n\n<p>In short, if the <mark>first operand<\/mark> of % operator (remainder operator) is negative, the result will be  \\( -1\\times \\) <em>remainder of same operands when first operand was positive<\/em>. The sign of second operand doesn&#8217;t have effect on the result<\/p>\n<\/div>\n\n\n\n<p><strong><mark>Modulo<\/mark><\/strong> (Euclidean division): Modulo is based on the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Euclidean_division#Division_theorem\">division theorem<\/a> according to which, if \\( a \\) and \\( b \\) are two integers, than there exist two integers \\( q \\) and \\( r \\), such that<\/p>\n\n\n\n<div class=\"wp-block-create-block-indent indenter\">\n<p>\\( a=q\\cdot b+r \\)<\/p>\n\n\n\n<p>Where <mark>\\( 0\\le r\\le\\left|b\\right| \\)<\/mark><\/p>\n\n\n\n<p>Therefore, modulo of two operands is always positive regardless of the sign of any of the operand.<\/p>\n\n\n\n<p class=\"accordion-button tertiary collapsed accordion-title\">Example of modulo of different operands<\/p><div class=\"accordion-panel collapsed\">\n<ul>\n<li><em>7 mod 3 = <strong><mark>1<\/mark><\/strong> <\/em>because<em> 7 = 3 * 2 + <strong>1<\/strong><\/em><\/li>\n\n\n\n<li><em>7 mod -3 = <strong><mark>1<\/mark><\/strong> <\/em>because <em>7 = -3 * -2 + <strong>1<\/strong><\/em><\/li>\n\n\n\n<li><em>-7 mod 3 = <strong><mark>2<\/mark><\/strong> <\/em>because <em>-7 = 3 * -3 + <strong>2<\/strong><\/em><\/li>\n\n\n\n<li><em>-7 mod -3 = <strong><mark>2<\/mark><\/strong> <\/em>because <em>-7 = -3 * 3 + <strong>2<\/strong><\/em><\/li>\n<\/ul>\n<\/div>\n\n\n\n<p>See <a href=\"https:\/\/leetcode.com\/problems\/subarray-sums-divisible-by-k\/description\/?envType=daily-question&amp;envId=2024-06-09\">this problem<\/a> which is based on modulo (not remainder).<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Implimentation of Modulo in Java<\/h5>\n\n\n\n<p>Modulo can be simply derived with the help of remainder of two operands. If the remainder is positive, the modulo remains same as remainder, however if remainder is negative, the modulo is simply \\( \\left|\\text{second operand}\\right|+\\text{remainder} \\)<\/p>\n\n\n\n<pre class=\"java\" style=\"font-family:monospace;\"><span style=\"color: #000066; font-weight: bold;\">int<\/span> aMODb<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #000066; font-weight: bold;\">int<\/span> a, <span style=\"color: #000066; font-weight: bold;\">int<\/span> b<span style=\"color: #009900;\">&#41;<\/span> <span style=\"color: #009900;\">&#123;<\/span>\n    <span style=\"color: #000066; font-weight: bold;\">int<\/span> mod <span style=\"color: #339933;\">=<\/span> a <span style=\"color: #339933;\">%<\/span> b<span style=\"color: #339933;\">;<\/span>\n    <span style=\"color: #000000; font-weight: bold;\">if<\/span> <span style=\"color: #009900;\">&#40;<\/span>mod <span style=\"color: #339933;\">&lt;<\/span> <span style=\"color: #cc66cc;\">0<\/span><span style=\"color: #009900;\">&#41;<\/span>\n        mod <span style=\"color: #339933;\">=<\/span> <span style=\"color: #003399;\">Math<\/span>.<span style=\"color: #006633;\">abs<\/span><span style=\"color: #009900;\">&#40;<\/span>b<span style=\"color: #009900;\">&#41;<\/span> <span style=\"color: #339933;\">+<\/span> mod<span style=\"color: #339933;\">;<\/span>\n    <span style=\"color: #000000; font-weight: bold;\">return<\/span> mod<span style=\"color: #339933;\">;<\/span>\n<span style=\"color: #009900;\">&#125;<\/span><\/pre>\n<\/div>\n\n\n\n<h4 class=\"wp-block-heading\">Congruence<\/h4>\n\n\n\n<ul>\n<li>If <em>a % m = b<\/em>, then it can be said that &#8220;<em>a<\/em> is congruent to <em>b<\/em> with respect to modulo <em>m<\/em>&#8221; denoted \\( a\\equiv b\\left(\\text{mod}\\ m\\right) \\)<\/li>\n\n\n\n<li>In other words, two integers <em>a<\/em> and <em>b<\/em> are said to be congruent module <em>m<\/em> if <em>m<\/em> is a divisor of their difference.<br>\\( \\Rightarrow \\) If \\( a\\equiv b\\left(\\text{mod}\\ m\\right) \\) then \\( a-b=k\\cdot m \\) (where \\( k \\) is some constant)<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">Modular multiplicative inverse<\/h4>\n\n\n\n<p>\\( x \\) is a modular multiplicative inverse of integer \\( a \\) (with respect to modulo \\( m \\)) if \\( ax\\ \\text{mod}\\ m=1 \\) or \\( ax\\equiv1\\left(\\text{mod}\\ m\\right) \\). Furthermore, modular multiplicative inverse \\( x \\) will exist <strong>only if<\/strong> \\( a \\) and \\( m \\) are co-prime, i.e. GCD of \\( a \\) and \\( m \\) is 1<\/p>\n\n\n\n<p>Modular multiplicative inverse can be obtained:<\/p>\n\n\n\n<ul>\n<li>using <a href=\"https:\/\/en.wikipedia.org\/wiki\/Extended_Euclidean_algorithm#:~:text=The%20extended%20Euclidean%20algorithm%20is%20particularly%20useful%20when%20a%20and%20b%20are%20coprime.%20With%20that%20provision%2C%20x%20is%20the%20modular%20multiplicative%20inverse%20of%20a%20modulo%20b%2C%20and%20y%20is%20the%20modular%20multiplicative%20inverse%20of%20b%20modulo%20a\" data-type=\"link\" data-id=\"https:\/\/en.wikipedia.org\/wiki\/Extended_Euclidean_algorithm#:~:text=The%20extended%20Euclidean%20algorithm%20is%20particularly%20useful%20when%20a%20and%20b%20are%20coprime.%20With%20that%20provision%2C%20x%20is%20the%20modular%20multiplicative%20inverse%20of%20a%20modulo%20b%2C%20and%20y%20is%20the%20modular%20multiplicative%20inverse%20of%20b%20modulo%20a\">Extended euclidean algorithm<\/a>: Extended euclidean algorithm can be used to find the coefficient \\( x \\) and \\( y \\) in equation \\( xa+yb=\\gcd\\left(a,b\\right) \\). If <em>a<\/em>&nbsp;and&nbsp;<em>b<\/em>&nbsp;are coprime, that is, if&nbsp;\\( xa+yb=1 \\), then <em>x<\/em>&nbsp;is the modular multiplicative inverse&nbsp;of&nbsp;<em>a<\/em> modulo <em>b<\/em>, and&nbsp;<em>y<\/em>&nbsp;is the modular multiplicative inverse of&nbsp;<em>b<\/em>&nbsp;modulo&nbsp;<em>a<\/em>.\n<ul>\n<li>Note: while using extended euclidean algorithm to find Modular multiplicative inverse (MMI), the MMI can sometimes come out to be negative. Simply add \\( m \\) to it to make it positive<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>using Fermat&#8217;s little theorm (if \\( m \\) is prime)<\/li>\n<\/ul>\n\n\n\n<h5 class=\"wp-block-heading\">Fermat&#8217;s little theorm<\/h5>\n\n\n\n<p>According to Fermat&#8217;s little theorm, if \\( p \\) is a prime number, then for any integer \\( a \\), \\( a^p\\equiv a\\left(\\ \\text{mod}\\ p\\right) \\) (or \\( a^p\\ mod\\ p=a\\ mod\\ p \\))<\/p>\n\n\n\n<p>Deviding both sides in above equation by \\( a \\), \\[ \\frac{a^p}{a}\\equiv\\frac{a}{a}\\left(\\text{mod}\\ \\frac{p}{\\gcd\\left(a,p\\right)}\\right) \\]<\/p>\n\n\n\n<p>If \\( a \\) and \\( p \\) are coprime to each other, we get <span class=\"border-curly-black\">\\[ a^{p-1}\\equiv1\\left(\\text{mod}\\ p\\right) \\]<\/span><\/p>\n\n\n\n<p>Multiply above equation by \\[ \\frac{a}{a} \\], we get \\[ a\\times a^{p-2}\\equiv1\\left(\\text{mod}\\ p\\right) \\]<\/p>\n\n\n\n<p>Above equation implies that, for any integer \\( a \\), <mark>\\[ a^{p-2} \\] will be it&#8217;s inverse with respect to modulo \\( p \\)<\/mark> if \\( p \\) is a prime number and both \\( a \\) and \\( p \\) are co-prime to each other.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Properties of Modulos<\/h4>\n\n\n\n<ul>\n<li>Distributive: \\( \\left(a+b\\right)\\ \\text{mod}\\ m\\ =\\ \\left(a\\ \\text{mod}\\ m\\ +b\\ \\text{mod}\\ m\\right)\\text{mod}\\ m \\)<\/li>\n\n\n\n<li>\\( \\left(a\\times b\\right)\\ \\text{mod}\\ m\\ =\\ \\left(a\\ \\text{mod}\\ m\\ \\times b\\ \\text{mod}\\ m\\right)\\text{mod}\\ m \\)<\/li>\n\n\n\n<li>However, same is <strong>not always true<\/strong> when it comes to division, i.e: \\[ \\left(\\frac{a}{b}\\right)\\ \\text{mod}\\ m\\ \\ne\\left(\\frac{a\\ \\text{mod}\\ m}{b\\ \\text{mod}\\ m}\\right) \\]<\/li>\n\n\n\n<li>But, If \\( a\\equiv b\\left(\\ \\text{mod}\\ m\\ \\right) \\), then \\[ \\frac{a}{d}\\equiv\\frac{b}{d}\\left(\\ \\text{mod}\\ \\frac{m}{\\gcd\\left(m,d\\right)}\\right) \\]<\/li>\n\n\n\n<li>\\( \\Rightarrow \\) If \\( a\\equiv b\\left(\\text{mod}\\ m\\right) \\) and if an integer \\( d \\) is coprime with \\( m \\) ,then \\[ \\frac{a}{d}\\equiv\\frac{b}{d}\\left(\\text{mod}\\ m\\right) \\]<\/li>\n<\/ul>\n\n\n\n<p>How to find \\[ \\left(\\frac{a}{b}\\right)\\ \\text{mod}\\ m \\] without directly dividing <em>a<\/em> by <em>b<\/em> (in case <em>a<\/em> and <em>b<\/em> are unknowingly huge)?<\/p>\n\n\n\n<div class=\"wp-block-create-block-indent indenter\">\n<ul>\n<li>\\[ \\left(\\frac{a}{b}\\right)\\ \\text{mod}\\ m \\] is equivalant to \\[ \\left(a\\times b^{-1}\\right)\\ \\text{mode}\\ m \\] where \\[ b^{-1} \\] is modular multiplicative inverse of \\( b \\)<\/li>\n<\/ul>\n\n\n\n<p>Example: Find \\[ \\left(\\frac{888888888}{98765432}\\right)\\text{mod}\\ 1000000007 \\] without directly divinding 888888888 by 98765432<\/p>\n\n\n\n<p>Solution: Since 98765432 and 1000000007 are coprime to each other,  Find modulo multiplicative inverse of 98765432 with respect to modulo 1000000007, that is \\[ 98765432^{\\left(1000000007-2\\right)}\\text{mod}\\ 1000000007 \\]. Using a custom <code>modPower()<\/code> function to solve above equation, 609375003 is obtained as inverse of 98765432 with respect to modulo 1000000007. therefore:<\/p>\n\n\n\n<p>\\[ \\therefore\\frac{888888888}{98765432}\\text{mod}\\ 1000000007=888888888\\times609375003\\ \\text{mod}\\ 1000000007=9 \\]<\/p>\n\n\n\n<p>The result \\( 9 \\) is indeed correct and can be confirmed by dividing 888888888 by 98765432 directly<\/p>\n<\/div>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Bit Manipulation<\/h2>\n\n\n\n<div class=\"wp-block-create-block-indent indenter\">\n<h3 class=\"wp-block-heading\">XOR operator properties<\/h3>\n\n\n\n<div class=\"wp-block-create-block-indent indenter\">\n<ul>\n<li>\\( a \\oplus a = 0 \\)<\/li>\n\n\n\n<li>\\( a \\oplus b = b \\oplus a \\)<\/li>\n\n\n\n<li>\\( (a \\oplus b) \\oplus c = a \\oplus (b\\oplus c) \\)<\/li>\n\n\n\n<li>If <mark>\\( a \\oplus b = c \\)<\/mark> then <mark>\\( a \\oplus c = b \\) <\/mark>and <mark>\\( b \\oplus c = a \\)<\/mark> (<a href=\"https:\/\/leetcode.com\/problems\/count-pairs-of-points-with-distance-k\/description\/\">Tricky problem based on this<\/a>)<\/li>\n\n\n\n<li><strong>Distribution of XOR over OR<\/strong> : \\( a\\oplus\\left(b\\mid c\\right)=\\left(a\\oplus b\\right)\\mid\\left(a\\oplus c\\right) \\)<\/li>\n\n\n\n<li><strong>Distribution of XOR over AND<\/strong> : \\( \\left(x\\&amp;a\\right)\\oplus\\left(y\\&amp;a\\right)=\\left(x\\oplus y\\right)\\&amp;a \\)<\/li>\n\n\n\n<li>For an array \\( A \\) of length \\( n \\),\n<ul>\n<li>if \\[ A_0\\ \\oplus A_1\\oplus A_2\\oplus\u2026\u2026.\\oplus A_{n-1}\\ \\ =0 \\], then for each \\( i\\in\\left[0,n\\right] \\),<\/li>\n\n\n\n<li>\\[ A_0\\oplus A_1\\oplus\u2026..\\oplus A_i\\ \\ \\ =\\ \\ A_{i+1}\\oplus A_{i+2}\\oplus\u2026\u2026\\oplus A_{n-1} \\]<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Problems<\/h3>\n\n\n\n<ul>\n<li>Count number of times i<sub>th<\/sub> bit is set in integers when counting from integer 0 to n<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-create-block-indent indenter\">\n<p class=\"accordion-button tertiary collapsed accordion-title\">Solution<\/p><div class=\"accordion-panel collapsed\">\n<ul>\n<li>Observe the 1st, 2nd bits when counting from 0 to 7<br>000<br>001<br>010<br>011<br>100<br>101<br>110<br>111<\/li>\n\n\n\n<li>The 1st bit (right most) is set after every one unset bit, whereas the two 2nd bit is set after every two unset bits. Similarly for the 3rd bit, four bits are set after every four unset bits. This pattern can be used to easily calculated number times bit at any position is set when counting from 0 to n. <\/li>\n\n\n\n<li>See problem <a href=\"https:\/\/leetcode.com\/problems\/find-products-of-elements-of-big-array\/description\/\">Find Products of Elements of Big Array<\/a> which is based on above observation<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Combinatorics<\/h3>\n\n\n\n<p>Crazy combinatorics <a href=\"https:\/\/leetcode.com\/problems\/poor-pigs\/\">problem<\/a> with good <a href=\"https:\/\/leetcode.com\/problems\/poor-pigs\/solutions\/94273\/solution-with-detailed-explanation\/comments\/98666\">explanation <\/a>of bucket arrangement.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Matrix<\/h3>\n\n\n\n<p>TODO<\/p>\n\n\n\n<div class=\"wp-block-create-block-indent indenter\">\n<h4 class=\"wp-block-heading\">Matrix Exponentiation<\/h4>\n\n\n\n<p>Leetcode problem: <a href=\"https:\/\/leetcode.com\/problems\/count-vowels-permutation\/description\/\" data-type=\"link\" data-id=\"https:\/\/leetcode.com\/problems\/count-vowels-permutation\/description\/\">1220. Count Vowels Permutation<\/a> with O(logN) solution using Matrix Exponentiation.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Power of adjacency Matrix<\/h4>\n\n\n\n<p><a href=\"https:\/\/math.stackexchange.com\/questions\/1890620\/finding-path-lengths-by-the-power-of-adjacency-matrix-of-an-undirected-graph\">https:\/\/math.stackexchange.com\/questions\/1890620\/finding-path-lengths-by-the-power-of-adjacency-matrix-of-an-undirected-graph<\/a><\/p>\n\n\n\n<p>Leetcode problem : <a href=\"https:\/\/leetcode.com\/problems\/knight-dialer\/description\/?envType=daily-question&amp;envId=2023-11-27\" data-type=\"link\" data-id=\"https:\/\/leetcode.com\/problems\/knight-dialer\/description\/?envType=daily-question&amp;envId=2023-11-27\">Knight Dialer<\/a> with \\( O\\left(\\log n\\right) \\) <a href=\"https:\/\/leetcode.com\/problems\/knight-dialer\/solutions\/189252\/o-logn\/?envType=daily-question&amp;envId=2023-11-27\">solution<\/a> using power of adjacency matrix<\/p>\n<\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Faulhaber&#8217;s formula \\[ 1+2+3+\u2026+n\\ \\ \\ =\\ \\ \\ \\frac{n\\left(n+1\\right)}{2} \\] \\[ 1^2+2^2+3^2+\u2026+n^2\\ \\ \\ =\\ \\ \\ \\frac{n\\left(n+1\\right)\\left(2n+1\\right)}{6} \\] \\[ 1^3+2^3+3^3+\u2026+n^3\\ \\ \\ =\\ \\ \\ \\left[\\frac{n\\left(n+1\\right)}{2}\\right]^2 \\] Sum of AP, GP and Harmonic series For an AP series, \\[ a_1+a_2+a_3+\u2026..+b\\ \\ =\\ \\ \\frac{1}{2}\\left(a+b\\right) \\] For a GP series, \\[ a+ak+ak^2+ak^3+\u2026..+b\\ \\ =\\ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1737,"comment_status":"open","ping_status":"closed","sticky":false,"template":"single_compact.php","format":"standard","meta":{"footnotes":""},"categories":[2,4,6],"tags":[],"_links":{"self":[{"href":"https:\/\/spacican.com\/notes\/wp-json\/wp\/v2\/posts\/1364"}],"collection":[{"href":"https:\/\/spacican.com\/notes\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/spacican.com\/notes\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/spacican.com\/notes\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/spacican.com\/notes\/wp-json\/wp\/v2\/comments?post=1364"}],"version-history":[{"count":142,"href":"https:\/\/spacican.com\/notes\/wp-json\/wp\/v2\/posts\/1364\/revisions"}],"predecessor-version":[{"id":1712,"href":"https:\/\/spacican.com\/notes\/wp-json\/wp\/v2\/posts\/1364\/revisions\/1712"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/spacican.com\/notes\/wp-json\/wp\/v2\/media\/1737"}],"wp:attachment":[{"href":"https:\/\/spacican.com\/notes\/wp-json\/wp\/v2\/media?parent=1364"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/spacican.com\/notes\/wp-json\/wp\/v2\/categories?post=1364"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/spacican.com\/notes\/wp-json\/wp\/v2\/tags?post=1364"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}