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 given an array of integers. Write a function to find the length of the longest increasing subsequence within the array.", "input": "An array of integers.", "output": "The length of the longest increasing subsequence within the input array.", "examples": [ { "input": "[10, 9, 2, 5, 3, 7, 101, 18]", "output": "4", "explanation": "The longest increasing subsequence is [2, 3, 7, 101], which has a length of 4." }, { "input": "[0, 1, 0, 3, 2, 3]", "output": "4", "explanation": "The longest increasing subsequence is [0, 1, 2, 3], which has a length of 4." }, { "input": "[7, 7, 7, 7, 7, 7, 7]", "output": "1", "explanation": "All elements are the same, so the longest increasing subsequence has a length of 1." }, { "input": "[4, 10, 4, 3, 8, 9]", "output": "3", "explanation": "The longest increasing subsequence is [4, 8, 9], which has a length of 3." } ], "function_signature": "def longest_increasing_subsequence(arr):", "solution": { "code": " if not arr:\n return 0\n dp = [1] * len(arr)\n for i in range(1, len(arr)):\n for j in range(i):\n if arr[i] > arr[j]:\n dp[i] = max(dp[i], dp[j] + 1)\n return max(dp)" } }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.