0% found this document useful (0 votes)
77 views2 pages

Mitsogo Coding

The document contains five different algorithms for solving various problems. These include partitioning an array into segments, finding the smallest special subarray length, calculating a specific value based on two arrays, determining the minimum days to teach a song, and maximizing matching pairs between two arrays. Each algorithm is presented with its respective function and example usage.

Uploaded by

srivijendran92
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views2 pages

Mitsogo Coding

The document contains five different algorithms for solving various problems. These include partitioning an array into segments, finding the smallest special subarray length, calculating a specific value based on two arrays, determining the minimum days to teach a song, and maximizing matching pairs between two arrays. Each algorithm is presented with its respective function and example usage.

Uploaded by

srivijendran92
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

BEST PARTITION
input1 = list(map(int, input("Enter array elements: ").split()))
input2 = len(input1)
input3 = int(input("Enter the number of segments: "))

a = res = 0
b = sum(input1)

while a <= b:
c=0
t=0
m = (a + b) // 2
for i in input1:
t += i
if t >= m:
t=0
c += 1
if c >= input3:
res = m
a=m+1
else:
b=m-1

print(res)

2.OR SPECIAL

def smallest_special_subarray_length(arr, K):


left, current_or = 0, 0
min_length = float('inf')

for right in range(len(arr)):


current_or |= arr[right]

while current_or >= K:


min_length = min(min_length, right - left + 1)
current_or ^= arr[left]
left += 1

return min_length if min_length != float('inf') else -1

# Example usage
arr = list(map(int, input("Enter array elements: ").split()))
K = int(input("Enter the integer K: "))
print(smallest_special_subarray_length(arr, K))

3. ALLENS VALUE
def allensval(n, a, g):

r = [i for i in a if i not in g]

return min(g) - min(r)


# Input processing
n = int(input("Enter the size of array A: "))
a = list(map(int, input("Enter array A elements: ").split()))
g = list(map(int, input("Enter array G elements: ").split()))

# Function call with corrected parameters


print(allensval(n, a, g))

4. MUSIC TEACHER

def min_days_to_teach(song):
from collections import Counter

# Count frequencies of each character


freq = Counter(song)

# Find the maximum frequency


max_freq = max(freq.values())

return max_freq

# Example usage
song = input("Enter the song string: ")
print(min_days_to_teach(song))

5. CHOCOLATE ARRANGEMENT
def max_matching_pairs(A, B):
N = len(A)
B_extended = B + B
max_pairs = 0

for i in range(N):
pairs = sum(A[j] == B_extended[i + j] for j in range(N))
max_pairs = max(max_pairs, pairs)

return max_pairs

# Example usage
A = list(map(int, input("Enter array A elements: ").split()))
B = list(map(int, input("Enter array B elements: ").split()))

print(max_matching_pairs(A, B))

You might also like