ProtyayMnd50 revised this gist . Go to revision
1 file changed, 18 insertions
sumof_factors.py(file created)
@@ -0,0 +1,18 @@ | |||
1 | + | def sum_of_factors(num): | |
2 | + | # Sum of factors of num | |
3 | + | sum_factors = 0 | |
4 | + | for i in range(1, int(num**0.5) + 1): | |
5 | + | if num % i == 0: | |
6 | + | sum_factors += i | |
7 | + | if i != num // i: | |
8 | + | sum_factors += num // i | |
9 | + | return sum_factors | |
10 | + | ||
11 | + | def maxSubsetSum(arr): | |
12 | + | # Apply sum_of_factors to each element in arr | |
13 | + | return [sum_of_factors(num) for num in arr] | |
14 | + | ||
15 | + | # Example usage: | |
16 | + | arr = [12] # Example input from the image | |
17 | + | result = maxSubsetSum(arr) | |
18 | + | print(result) # Expected output: [28] |
ProtyayMnd50 revised this gist . Go to revision
1 file changed, 45 insertions
getminSum.py(file created)
@@ -0,0 +1,45 @@ | |||
1 | + | ||
2 | + | def getMinimumSum(arr): | |
3 | + | n = len(arr) | |
4 | + | if n < 3: | |
5 | + | return -1 | |
6 | + | ||
7 | + | left_min = [None] * n | |
8 | + | min_so_far = float('inf') | |
9 | + | ||
10 | + | # Populate left_min | |
11 | + | for j in range(n): | |
12 | + | if min_so_far < arr[j]: | |
13 | + | left_min[j] = min_so_far | |
14 | + | else: | |
15 | + | left_min[j] = None | |
16 | + | if arr[j] < min_so_far: | |
17 | + | min_so_far = arr[j] | |
18 | + | ||
19 | + | right_min = [None] * n | |
20 | + | min_so_far = float('inf') | |
21 | + | ||
22 | + | # Populate right_min | |
23 | + | for j in range(n-1, -1, -1): | |
24 | + | if min_so_far < arr[j]: | |
25 | + | right_min[j] = min_so_far | |
26 | + | else: | |
27 | + | right_min[j] = None | |
28 | + | if arr[j] < min_so_far: | |
29 | + | min_so_far = arr[j] | |
30 | + | ||
31 | + | min_sum = float('inf') | |
32 | + | for j in range(n): | |
33 | + | if left_min[j] is not None and right_min[j] is not None: | |
34 | + | current_sum = left_min[j] + arr[j] + right_min[j] | |
35 | + | if current_sum < min_sum: | |
36 | + | min_sum = current_sum | |
37 | + | ||
38 | + | return min_sum if min_sum != float('inf') else -1 | |
39 | + | ||
40 | + | # Example Usage: | |
41 | + | if __name__ == "__main__": | |
42 | + | arr = [3, 4, 5, 1, 2, 3, 1] | |
43 | + | result = getMinimumSum(arr) | |
44 | + | print(result) # Output: 4 | |
45 | + |