Sunday, 16 March 2025

Leetcode Weekly Contest 441 Q2. Closest Equal Element Queries

Q2. Closest Equal Element Queries

Important learnings wrt 🔁Circular Array🔁  : 

 The two distances between elt at i & j  : 

  • forward_dist  = (j-i+n) % n;
  • backward_dist = (i-j+n) % n; 
How to get the two indexes `d` distance away from a given index, i ?
  • next_d_idx = ( i + d) % n ;
  • prev_d_idx = ( i - d + n) % n ;
As a special case when d=1, we can get next & prev elements as : 
  • next_idx = (i+1) % n 
  • prev_idx = (i-1+n ) % n
Alternatively, the +1 for forward should be thought as +(n-1) for backward or more generally, +d should be thought as +(n-d) when marching in backward direction.

Sunday, 9 March 2025

"Mastering Power Set Generation: A Deep Dive into Recursion” from a Java ☕ programmer’s perspective

Motivation for the notes :

  1. Leetcode#78. Subsets 
  2. G4G Power Set 

There are broadly two approaches :

  1. Bit Masking (iterative) 😷{not focus of this blog}
  2. Backtracking (recursive) ⭐
    1. Include-Exclude paradigm : here we see two variants & why we should use one over other
    2. For-loop paradigm : prefer this (reason at the end)

Continue reading my latest blog compiled at notion.so by clicking here :  "Mastering Power Set Generation: A Deep Dive into Recursion” from a Java ☕ programmer’s perspective