Create a programming task related to common algorithms and data…
- User
Assistant: User: Create a programming task related to common algorithms and data structures. The task should follow this format:
{ "problem_description": "You are given an array of integers. Write a function to find the maximum sum of a subarray within the array.", "input": "An array of integers.", "output": "The maximum sum of a subarray within the input array.", "examples": [ { "input": "[-2, 1, -3, 4, -1, 2, 1, -5, 4]", "output": "6", "explanation": "The subarray [4, -1, 2, 1] has the maximum sum of 6." }, { "input": "[1, 2, 3, 4, -10, 5, 6]", "output": "15", "explanation": "The subarray [1, 2, 3, 4, -10, 5, 6] has the maximum sum of 15." }, { "input": "[5, 4, -1, 7, 8]", "output": "23", "explanation": "The subarray [5, 4, -1, 7, 8] has the maximum sum of 23." }, { "input": "[-1, -2, -3, -4]", "output": "-1", "explanation": "The subarray [-1] has the maximum sum of -1." } ], "function_signature": "def max_subarray_sum(arr):", "solution": { "code": " max_sum = float('-inf') current_sum = 0 for num in arr: current_sum += num if current_sum > max_sum: max_sum = current_sum if current_sum < 0: current_sum = 0 return max_sum" } }
Assistant:
- Assistant
{ "problem_description": "You are tasked with implementing a binary search algorithm. Write a function that efficiently searches for an integer target in a sorted array and returns its index or -1 if the target is not present.", "input": "A sorted array of integers and an integer target.", "output": "An integer representing the index of the target in the array, or -1 if the target is not found.", "examples": [ { "input": "[1, 2, 3, 4, 5, 6, 7, 8, 9], 5", "output": "4", "explanation": "The target 5 is at index 4 in the array." }, { "input": "[10, 20, 30, 40, 50], 30", "output": "2", "explanation": "The target 30 is at index 2." }, { "input": "[1, 3, 5, 7, 9, 11], 8", "output": "-1", "explanation": "The target 8 is not in the array, so the function returns -1." }, { "input": "[-5, -3, 0, 1, 4, 6, 10], 0", "output": "2", "explanation": "The target 0 is at index 2 in the array." } ], "function_signature": "def binary_search(arr, target):", "solution": { "code": " left, right = 0, len(arr) - 1\n while left <= right:\n mid = (left + right) // 2\n if arr[mid] == target:\n return mid\n elif arr[mid] < target:\n left = mid + 1\n else:\n right = mid - 1\n return -1" } }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.