Max Sub Array:: Using Namespace Int Int Int Int Int Int Int Int
Max Sub Array:: Using Namespace Int Int Int Int Int Int Int Int
#include<iostream>
#include<algorithm>
using namespace std;
sum = 0;
int rsum=-999;
for(int i=mid+1; i<=high; i++){
sum+=a[i];
if(sum>rsum){
rsum=sum;
}
else{
break;
}
}
return (lsum+rsum);
}
int mss(int a[], int low, int high) {
int mid=0, leftsum=0, rightsum=0, crosssum=0;
if (high==low){
return a[low];
}
else{
mid=(high+low)/2;
leftsum = mss(a, low, mid);
rightsum = mss(a, mid+1, high);
crosssum = css(a, low, mid, high);
}
int ret = max({leftsum, rightsum, crosssum});
return ret;
}
int main(){
int n=0;
cout<<"Enter number of elements:";
cin>>n;
int a[n];
cout<<"Enter elements:";
for(int i=0; i<n; i++){
cin>>a[i];
}
int sum=mss(a, 0, n-1);
cout<<sum;
return 0;
}
Karatsuba Algorithm:
x = int(input())
y = int(input())
def karatsuba(x,y):
if x<10 or y<10:
return x*y
else:
n = max(len(str(x)),len(str(y)))
half=n//2
a = x//(10**(half))
b = x%(10**(half))
c = y//(10**(half))
d = y%(10**(half))
ac = karatsuba(a,c)
bd = karatsuba(b,d)
ad_plus_bc = karatsuba(a+b,c+d)-ac-bd
return ac*(10**(2*(half))) + (ad_plus_bc*(10**half)) + bd
print(karatsuba(x,y))
Fractional Knapsack:
#include <iostream>
#include <vector>
using namespace std;
LCS:
#include <bits/stdc++.h>
using namespace std;
KMP:
#include <iostream>
#include <vector>
#include <string>
f1 = [0] * n
f2 = [0] * n
a = [[7, 9, 3, 4, 8, 4],
[8, 5, 6, 4, 5, 7]]
t = [[2, 3, 1, 3, 4],
[2, 1, 2, 2, 1]]
e = [2, 3]
x = [3, 2]
print("Fastest time:", assembly_line_scheduling(a, t, e, x))
0-1 Knapsack:
return dp[n][capacity]
weights = [2, 3, 4, 5]
values = [3, 4, 5, 6]
capacity = 5
print("Maximum value:", knapsack(weights, values, capacity))
Huffman Coding:
class HuffmanNode:
def __init__(self, char, freq):
self.char = char
self.freq = freq
self.left = None
self.right = None
def __lt__(self, other):
return self.freq < other.freq
def build_huffman_tree(text):
freq = defaultdict(int)
for char in text:
freq[char] += 1
pq = []
for char, f in freq.items():
node = HuffmanNode(char, f)
heappush(pq, node)
while len(pq) > 1:
left = heappop(pq)
right = heappop(pq)
merge_node = HuffmanNode(None, left.freq + right.freq)
merge_node.left = left
merge_node.right = right
heappush(pq, merge_node)
return pq[0]
def encode_huffman(root, prefix="", code={}):
if root is not None:
if root.char is not None:
code[root.char] = prefix
encode_huffman(root.left, prefix + "0", code)
encode_huffman(root.right, prefix + "1", code)
return code
def huffman_encoding(text):
if not text:
return None, None
root = build_huffman_tree(text)
code = encode_huffman(root)
encoded_text = ''.join(code[char] for char in text)
return encoded_text, root
def huffman_decoding(encoded_text, root):
decoded_text = ""
current_node = root
for bit in encoded_text:
if bit == '0':
current_node = current_node.left
else:
current_node = current_node.right
if current_node.char is not None:
decoded_text += current_node.char
current_node = root
return decoded_text
text = "this is an example for huffman encoding"
encoded_text, tree = huffman_encoding(text)
print("Encoded text:", encoded_text)
decoded_text = huffman_decoding(encoded_text, tree)
print("Decoded text:", decoded_text)
Rabin Karp:
def hash_string(s):
h = 0
for char in s:
h = (h * prime + ord(char)) % modulus
return h
pattern_hash = hash_string(pattern)
text_hash = hash_string(text[:m])
return -1
pattern = "abc"
text = "ababcabcabababd"
result = rabin_karp(pattern, text)
if result != -1:
print("Pattern found at index:", result)
else:
print("Pattern not found in the text.")