This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 = {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |