Skip to content

Instantly share code, notes, and snippets.

View pratik7368patil's full-sized avatar

Pratik Patil pratik7368patil

  • Maharashtra, India
View GitHub Profile
@pratik7368patil
pratik7368patil / stock_span.java
Created July 21, 2020 15:23
Stock Span Problem Solution
import java.util.*;
class ValueWithCount {
int value;
int index;
ValueWithCount(int value, int index) {
this.value = value;
this.index = index;
}
@pratik7368patil
pratik7368patil / nim_game.py
Created July 16, 2020 18:04
Nim Game (Game of Love)
N1, N2 = [int(v) for v in input().split()][:2]
if N1 == N2 or abs(N1-N2) == 1:
print("p2")
else:
print("p1")
# More Explaination
# ----------------------------------
# 2 2 --- two piles p1 takes a first turn
@pratik7368patil
pratik7368patil / 4sum.py
Created July 16, 2020 11:52
Four sum problem solution in Python (4sum)
def findFourElements(arr, N, K):
arr.sort()
for i in range(N - 3):
if i > 0 and arr[i] == arr[i-1]:
continue
for j in range(i+1, N - 2):
if j > i+1 and arr[j] == arr[j - 1]:
continue
l = j+1
r = N - 1
@pratik7368patil
pratik7368patil / 4sum.cpp
Created July 16, 2020 11:46
Four sum problem solution (4sum)
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int getSum(int arr[],int N, int K) {
for(int i=0; i < N; i++){
if(i > 0 && arr[i] == arr[i+1]) {
continue;
}
for(int j=i+1; j<N; j++) {
@pratik7368patil
pratik7368patil / Main.java
Last active July 16, 2020 17:34
Four sum solution in java
import java.util.*;
class Pair {
int first;
int second;
public Pair(int first, int second) {
this.first = first;
this.second = second;
}
@pratik7368patil
pratik7368patil / last_kth_LL.java
Created July 3, 2020 07:21
Search Last Kth node in Linked List
class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}
class Main {
@pratik7368patil
pratik7368patil / swap_it.cpp
Created July 1, 2020 08:54
Solution for swap it problem in C++
#include <iostream>
using namespace std;
int main() {
int N;
cin >> N; // Number if values
int value;
int count = 0 ;
for(int i = 0; i < N; i++){
@pratik7368patil
pratik7368patil / reverse_words.py
Created May 30, 2020 15:10
Reverse words in sentence
# This problem can be solved using split() and join()
def reverse_words(sen):
words = sen.split(' ') # split sentence into words
# then reverse a list of words and join them as a sentence
string = ' '.join(reversed(words))
print(string)
sen = "python is love"
@pratik7368patil
pratik7368patil / find_substring.py
Created May 30, 2020 14:41
Find the length of the longest substring without repeating character
# program to find lenght of longest substring without repeating characters.
def findLongestSubstring(string):
l = len(string)
st = 0 # starting point of current substring.
maxlen = 0 # maximum length of substring
start = 0 # starting index of maximum
d = dict() # dictionary to store last occurrence
@pratik7368patil
pratik7368patil / check_roration.py
Created May 30, 2020 11:23
Check Rotation in string
# this is a easy problem you know how rotation works.
# this problem can be sloved using index()
def check_rotation(string1, string2):
if len(string1) != len(string2): # check for base case
return "No"
con = string1*2 # concatenate string with itself
if con.count(string2) > 0: # check for string2 in concatenated string
return "Yes"