Skip to content

Instantly share code, notes, and snippets.

View rishi93's full-sized avatar

Rajarishi Devarajan rishi93

View GitHub Profile
@rishi93
rishi93 / test1.py
Last active March 24, 2016 06:11
Ambiguous Permutations - PERMUT2
def ifAmbiguous(arr,length):
for i in range(0,length):
num = i + 1
pos = arr[i] - 1
if arr[pos] != num:
return False
return True
while True:
@rishi93
rishi93 / test1.py
Last active September 5, 2024 19:47
Bytelandian Gold Coins - COINS
#Using Recursion - You will run out of time for large values of n
def exchange_recursion(n):
if n < 12:
return n
else:
return exchange_recursion(n//2) + exchange_recursion(n//3) + exchange_recursion(n//4)
#Using caching - This saves a lot of time
arr = {}
@rishi93
rishi93 / test1.py
Created March 16, 2016 04:40
How caching destroys the barriers of speed
# fib1() takes a lot of time. It will never be able to calculate 50th term of fibonacci series
def fib1(n):
if n <= 1:
return n
else:
return fib1(n-1) + fib1(n-2)
arr = {}
# fib() uses caching. Saves a lot of time. It can calculate fib(50) in very quick time
@rishi93
rishi93 / pi.py
Created March 17, 2016 05:03
Digits of Pi
# Calculating Pi, William Shank's method using Machin's formula
def arctan(x):
result = 0
for i in range(1,11):
t = 2*i - 1
if (i % 2 == 0):
result = result - (x**t)/t
else:
result = result + (x**t)/t
@rishi93
rishi93 / test1.py
Created March 17, 2016 06:32
Primes in Interval (Million)
from math import sqrt
#Sieve Of Eratosthenes - Start
last = int(sqrt(2147483647)) + 1
limit = int(sqrt(last)) + 1
arr = [True for i in range(0,last+1)]
i = 2
all_primes = []
@rishi93
rishi93 / test1.cpp
Last active August 20, 2016 11:11
Complete the sequence - CMPLS
#include <iostream>
#include <algorithm>
using namespace std;
bool ifStop(int arr[], int limit){
int first = arr[0];
for(int i=1; i<limit; i++){
if(arr[i] != first){
return false;
@rishi93
rishi93 / test1.py
Last active March 24, 2016 06:09
Number of n-permutations with exactly k inversions - PERMUT1
# Generating the n permutations with k inversions table - Start
# Also called the Mahonian numbers
arr = [[0 for _ in range(0,98+1)] for _ in range(1,12+1)]
for i in range(1,12+1):
arr[i-1][0] = 1
for i in range(1,12+1):
arr[i-1][1] = i-1
for i in range(3,12+1):
for j in range(2,98+1):
for k in range(0,i):
@rishi93
rishi93 / test1.py
Last active March 24, 2016 15:23
Add Reversed - ADDREV
#Approach 1
def reverse_string(string):
return string[::-1] #Slicing Begin to end in steps of -1
t = int(input())
for testcase in range(0,t):
a,b = input().split()
result = str( int(reverse_string(a)) + int(reverse_string(b)) )
print( int(reverse_string(result)) )
#End
@rishi93
rishi93 / test1.py
Created March 25, 2016 07:42
Longest Increasing Subsequence
def lis(arr,n):
dp = [1 for x in range(0,n)]
for i in range(1,n):
for j in range(0,i):
if dp[j] >= dp[i] and arr[j] < arr[i]:
dp[i]+=1
max_value = max(dp)
result = []
for i in range(n-1,-1,-1):
if dp[i] == max_value:
@rishi93
rishi93 / test1.py
Last active April 3, 2016 14:01
Balanced Brackets using Segment Tree - BRCKTS
from math import ceil,log
class node:
def __init__(self):
self.needLeft = 0
self.needRight = 0
def construct(arr,index,start,end):
global tree
if start == end: