Skip to content

Instantly share code, notes, and snippets.

View kushaagr's full-sized avatar
🏹
Aiming high

Kushagra Mehrotra kushaagr

🏹
Aiming high
  • Bangalore, India
  • 03:44 (UTC +05:30)
  • LinkedIn in/kushaagr
View GitHub Profile
@kushaagr
kushaagr / can_reach.py
Last active August 10, 2025 22:11
[Can two numbers become same] Determine if it is possible to make S equal to T #numbertheory
## Select an element x in S, and remove one occurrence of x in S.
## Then, either insert x+k into S, or insert |x−k| into S.
## Determine if it is possible to make S equal to T.
def can_reach(a, b, k):
"""
k = 5
2 9 6
8 4 11
Can you reach 8 from 2 by repeatedly adding/subtracting k?
Yes: 2-5 = -3; -3 - 5 = -8; abs(-8) = 8
@kushaagr
kushaagr / connected-components.py
Last active March 23, 2025 16:13
[Connected components] Count connected components using DFS
def recursivelyMark(nodeID, nodes):
(connections, visited) = nodes[nodeID]
if visited:
return
nodes[nodeID][1] = True
for connectedNodeID in connections:
recursivelyMark(connectedNodeID, nodes)
def main():
nodes = [[[1], False], [[0], False], [[3], False], [[2], False], [[], False], [[], False]]
@kushaagr
kushaagr / chdir-to-current-file.py
Last active March 23, 2025 16:14
[Change PWD to main] Set pwd in python environment to current file's location
# Get the absolute path of the directory containing main.py
script_dir = os.path.dirname(os.path.abspath(__file__))
# Change the current working directory to script_dir
os.chdir(script_dir)
print("Current Working Directory:", os.getcwd())