def max_cost_split(s):
n = len(s)
max_cost = 0
for i in range(1, n):
a = s[:i]
b = s[i:]
cost_a = len(set(a))
cost_b = len(set(b))
max_cost = max(max_cost, cost_a + cost_b)
return n - max_cost
Infosys max cost split code in python
n = len(s)
max_cost = 0
for i in range(1, n):
a = s[:i]
b = s[i:]
cost_a = len(set(a))
cost_b = len(set(b))
max_cost = max(max_cost, cost_a + cost_b)
return n - max_cost
Infosys max cost split code in python
👍1
class TreeNode:
def init(self, value=0, left=None, right=None):
self.value = value
self.left = left
self.right = right
def count_nodes(node, counts):
if node is None:
return
if node.value in counts:
counts[node.value] += 1
else:
counts[node.value] = 1
count_nodes(node.left, counts)
count_nodes(node.right, counts)
def find_double_roots(root):
counts = {}
count_nodes(root, counts)
double_roots = [value for value, count in counts.items() if count > 1]
return double_roots
def main():
# Example tree:
# 1
# / \
# 2 3
# / \
# 2 4
root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(3)
root.left.left = TreeNode(2)
root.left.right = TreeNode(4)
result = find_double_roots(root)
print("Nodes with double roots:", result)
if name == "main":
main()
def init(self, value=0, left=None, right=None):
self.value = value
self.left = left
self.right = right
def count_nodes(node, counts):
if node is None:
return
if node.value in counts:
counts[node.value] += 1
else:
counts[node.value] = 1
count_nodes(node.left, counts)
count_nodes(node.right, counts)
def find_double_roots(root):
counts = {}
count_nodes(root, counts)
double_roots = [value for value, count in counts.items() if count > 1]
return double_roots
def main():
# Example tree:
# 1
# / \
# 2 3
# / \
# 2 4
root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(3)
root.left.left = TreeNode(2)
root.left.right = TreeNode(4)
result = find_double_roots(root)
print("Nodes with double roots:", result)
if name == "main":
main()
👍3❤1
def longest_equal_subarray():
n = int(input())
A = [int(input()) for _ in range(n)]
A = [-1 if x == 0 else 1 for x in A]
prefix_sum_map = {}
prefix_sum = 0
max_length = 0
for i in range(n):
prefix_sum += A[i]
if prefix_sum == 0:
max_length = i + 1
if prefix_sum in prefix_sum_map:
max_length = max(max_length, i - prefix_sum_map[prefix_sum])
else:
prefix_sum_map[prefix_sum] = i
return max_length
print(longest_equal_subarray())
Infosys Longest Subarray code
n = int(input())
A = [int(input()) for _ in range(n)]
A = [-1 if x == 0 else 1 for x in A]
prefix_sum_map = {}
prefix_sum = 0
max_length = 0
for i in range(n):
prefix_sum += A[i]
if prefix_sum == 0:
max_length = i + 1
if prefix_sum in prefix_sum_map:
max_length = max(max_length, i - prefix_sum_map[prefix_sum])
else:
prefix_sum_map[prefix_sum] = i
return max_length
print(longest_equal_subarray())
Infosys Longest Subarray code
👍4
import sys
def get_ans(N, K, A):
def main():
N = int(sys.stdin.readline().strip())
K = int(sys.stdin.readline().strip())
A = []
for _ in range(N):
A.append(int(sys.stdin.readline().strip()))
result = get_ans(N, K, A)
print(result)
if name == "main":
main()
Subset with LCM code in python
def get_ans(N, K, A):
def main():
N = int(sys.stdin.readline().strip())
K = int(sys.stdin.readline().strip())
A = []
for _ in range(N):
A.append(int(sys.stdin.readline().strip()))
result = get_ans(N, K, A)
print(result)
if name == "main":
main()
Subset with LCM code in python
👎4👍2