File tree Expand file tree Collapse file tree 2 files changed +21
-2
lines changed
src/interview-questions/recursion Expand file tree Collapse file tree 2 files changed +21
-2
lines changed Original file line number Diff line number Diff line change @@ -650,14 +650,17 @@ abstract data type (ADT) - ADT is defined as a user point of view of a data type
650650
651651![ x to the power n using optimized recursion with multiple subproblems] ( https://i.imgur.com/Ym3qjhj.png )
652652
653+ - [ Question] ( https://codepen.io/roopkt/pen/XWMxvKg?editors=0010 )
654+ - [ Answer] ( https://codepen.io/roopkt/pen/oNZaKLy?editors=0010 )
655+
653656#### Calculate Modular Exponentiation using recursion
654657
655658Modular Exponentiation is the remainder dividing up on ` Pow(x,n) ` by ` M ` .
656659
657660![ Modular Exponentiation is the remainder dividing up on ` Pow(x,n) ` by ` M ` ] ( https://i.imgur.com/Y3GkhRT.png )
658661
659- - [ Question] ( https://codepen.io/roopkt/pen/XWMxvKg ?editors=0010 )
660- - [ Answer] ( https://codepen.io/roopkt/pen/oNZaKLy ?editors=0010 )
662+ - [ Question] ( https://codepen.io/roopkt/pen/GRWYVEa ?editors=0010 )
663+ - [ Answer] ( https://codepen.io/roopkt/pen/gOmBVWJ ?editors=0010 )
661664
662665## References
663666
Original file line number Diff line number Diff line change 1+ /**
2+ Time complexity O(logN)
3+
4+ */
5+
6+ function mod ( x , n , m ) {
7+ if ( n === 0 ) return 1 ;
8+ if ( n % 2 === 0 ) {
9+ const y = mod ( x , n / 2 , m ) ;
10+ return ( y * y ) % m ;
11+ } else {
12+ const p = ( x % m ) * mod ( x , n - 1 , m ) ;
13+
14+ return p % m ;
15+ }
16+ }
You can’t perform that action at this time.
0 commit comments