Skip to content

Instantly share code, notes, and snippets.

View jatinsharrma's full-sized avatar

Jatin Sharma jatinsharrma

  • India
View GitHub Profile
@jatinsharrma
jatinsharrma / youngestAncestor.py
Created May 10, 2020 16:52
Program that returns youngest common ancestor to given two descendants
# //////////////////////////////////////////////////
# ------------Youngest Common Ancestor--------------
# /////////////////////////////////////////////////
'''
Question: You are given a BST and 2 descendants.
Find the yongest common ancestor to the 2 given descendants node.
Example :
4
/ \
2 5
@jatinsharrma
jatinsharrma / triplets.py
Created May 9, 2020 20:22
Special triplets
# ///////////////////////////////////////
# ------------Find the Triplets---------
# //////////////////////////////////////
'''
Question : You are given an array of integer values and a interger value (suppose x).
You have to return number of triplets found in the given array such that
2nd element = 1st element + x
3rd element = 2nd element + x
@jatinsharrma
jatinsharrma / sizeOfRuns_1.py
Created May 9, 2020 19:40
Find the Size of all clusters of 1
# //////////////////////////////////////////////////////
# ---------- Find Size of clusters of 1 ---------------
# //////////////////////////////////////////////////////
'''
Question : You are given a 2D array consisting of 0 and 1s. Find the size of all clusters of 1s.
Conditions: You have to consider all neighbors
other than digonal neighbors
example:
@jatinsharrma
jatinsharrma / NextGreater.py
Created May 6, 2020 20:13
Find the next word
# ///////////////////////////////////////////////
# -----------------Next word---------------------
#///////////////////////////////////////////////
'''
Question : You are given a word, you have to find the least greatest
word to the given word. If you can not find return None
Example : given : 'acdb'
output : 'adbc'
@jatinsharrma
jatinsharrma / deleteKthFromLast.py
Created May 3, 2020 20:32
Delete n-th element from the last : Singly linked list
# ////////////////////////////////////////////////////
# --------Deleting n-th element from last-------------
# ////////////////////////////////////////////////////
'''
Logic :
Suppose we have a linked list 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 0
And we were ask to delete 6th element from last, which is '5'
# /////////////////////////////////////////////////
# --------------- Poer Set -----------------------
# ////////////////////////////////////////////////
'''
Logic :
Suppose we are given 'abc'
Now take an temp array
@jatinsharrma
jatinsharrma / All_permutation.py
Created April 30, 2020 11:09
Permutation Function
#///////////////////////////////////////////////////////////
#-------------------- Permutation -------------------------
#/////////////////////////////////////////////////////////
'''
For logic : https://gist.github.com/jatinsharrma/88345fea62cb088dfab149d81ded49b5
'''
def permutation(given):
if type(given) == type(2):
@jatinsharrma
jatinsharrma / permutation.py
Created April 30, 2020 10:34
Finding Permutation
#/////////////////////////////////////////////////////
#---------------Permutations-------------------------
#///////////////////////////////////////////////////
'''
1st attempt
Logic :
This is a recursive solution
This method will only work with natural numbers
@jatinsharrma
jatinsharrma / searching.py
Created April 28, 2020 20:50
Searching Algorithums : Linear Search | Binary Search | Interpolcation Search | exponential Search
#------------------------------------------------
#------------Searching Algorithms----------------
#------------------------------------------------
'''
Searching Algorithm:
1. Linear Search : Iterative and Recursive
2. Binary Search : Iterative and Recursive
3. Interpretation Search : Iterative
@jatinsharrma
jatinsharrma / noOfWaysCoins.py
Created April 27, 2020 21:52
No Of Ways To Make Change : Recursive and Dynamic Approch
#---------------------------------------------
#----------No Of Ways To Make Change----------
#---------------------------------------------
'''
Recursive solution
Logic:
Suppose we are given with coins as [$1,$2,$3] and we have to make $5.