diff --git a/GAcmbw/a5HLL3.py b/GAcmbw/a5HLL3.py new file mode 100644 index 00000000..2ead9d28 --- /dev/null +++ b/GAcmbw/a5HLL3.py @@ -0,0 +1,5 @@ +my_list = [10, 20, 30, 40, 50] # Example list + +# Access the third item (index 2) and print it +third_item = my_list[2] +print(third_item) diff --git a/ihQrkg/26PeRb.kt b/ihQrkg/26PeRb.kt new file mode 100644 index 00000000..e4250c1c --- /dev/null +++ b/ihQrkg/26PeRb.kt @@ -0,0 +1,43 @@ +fun maxSumSubarrayWithKEvens(arr: IntArray, k: Int): Pair { + var left = 0 + var evenCount = 0 + var maxSum = 0 + var maxLeft = 0 + var maxRight = 0 + + for (right in arr.indices) { + if (arr[right] % 2 == 0) { + evenCount++ + } + + while (evenCount > k) { + if (arr[left] % 2 == 0) { + evenCount-- + } + left++ + } + + val currentSum = arr.sliceArray(left..right).sum() + + if (currentSum > maxSum) { + maxSum = currentSum + maxLeft = left + maxRight = right + } + } + + return Pair(maxLeft, maxRight) +} + +fun main() { + val arr = intArrayOf(1, 2, 3, 4, 6, 8, 10, 12, 14) + val k = 2 + + val result = maxSumSubarrayWithKEvens(arr, k) + + println("Maximum sum subarray with at most $k even integers:") + for (i in result.first..result.second) { + print("${arr[i]} ") + } + println("\nSum: ${arr.sliceArray(result.first..result.second).sum()}") +}