Skip to content

Instantly share code, notes, and snippets.

View kunal768's full-sized avatar
🎯
Focusing

Kunaaaaaaaaaaal kunal768

🎯
Focusing
View GitHub Profile
@kunal768
kunal768 / bsearch.py
Created April 12, 2024 22:38
Binary Search - New Way
class Solution:
def search(self, nums: List[int], target: int) -> int:
n = len(nums)
lo, hi = 0, n - 1
while hi - lo > 1 :
mid = (lo + hi)//2
if nums[mid] < target :
lo = mid + 1
else:
hi = mid
@kunal768
kunal768 / solution.py
Created April 11, 2024 09:45
Sub palindrome queries - HackerEarth
# Question Link - https://www.hackerearth.com/practice/algorithms/string-algorithm/string-searching/practice-problems/algorithm/palindrome-queries-eefd5c23/
'''
Problem
You are given a string
that contains
lowercase alphabets. There are
queries of the form
. Your task is to determine if every character in the range
can be arranged in such a manner that a palindromic substring for that range can be formed. If it is possible to create the palindrome substring in the provided interval, then print Possible. Otherwise, print Impossible.
@kunal768
kunal768 / kadane.py
Created August 10, 2022 05:56
Three ways to use Kadane's Algorithm - Maximum Sum Subarray
# Space Optimized Iterative
class Solution:
##Complete this function
#Function to find the sum of contiguous subarray with maximum sum.
def maxSubArraySum(self,A,N):
##Your code here
currmax, maxsofar = A[0], A[0]
for i in range(1, N):
currmax = max(A[i], A[i] + currmax)
@kunal768
kunal768 / reverse-LL.py
Created August 1, 2022 11:17
Quick code to reverse LL
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
rev = None
while head :
rev, rev.next, head = head, rev, head.next
return rev
@kunal768
kunal768 / main.go
Last active November 7, 2020 16:17
Go program to print sum of squares of positive integers from given spaced integers without using a 'for' statement
package main
import (
"fmt"
"os"
"strings"
"strconv"
"bufio"
)
class Solution:
# @param A : tuple of list of integers
# @return a list of integers
def spiralOrder(self, A):
n,m = len(A),len(A[0])
l,r,t,d = 0,m-1,0,n-1
ans = []
direction = 0
while l<= r and t <= d :
if direction == 0 :
class Solution:
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
if not intervals :
return []
ans = []
intervals.sort(key = lambda x :x[0])
for interval in intervals :
if not ans or interval[0] > ans[-1][1] :
ans.append(interval)
else:
def dfs(graph , node, parent,visited):
visited[node] = True
for adj in graph[node] :
if visited[adj]:
if adj != parent :
return True
else :
if dfs(graph,adj,node,visited) == True :
return True
class Solution:
def kthSmallest(self, root: TreeNode, k: int) -> int:
arr = []
def inorder(root,arr):
if root.left :
inorder(root.left,arr)
if root :
arr.append(root.val)
if root.right :
inorder(root.right,arr)