array

Optimal Task Assignment

February 4, 2024
easy
array

Problem Statement # You are given a list of tasks where each task has a duration. Your goal is to assign these tasks to workers so that all tasks are completed while minimizing the total time. Each worker must work on exactly two tasks and can only work on one task at a time. Return the optimal assignment of tasks. Example: Input: tasks = [6, 3, 2, 7, 5, 5] Output: [(2, 7), (3, 6), (5, 5)] Explanation: Pairing the tasks in this way results in the minimum total duration: 9, 9, and 10. ...

Zero Sum Subarray

January 19, 2024
medium
array

Problem # Given an array of integers nums, find the length of the longest subarray whose sum is zero. Example: Input: nums = [2, -2, 3, 0, 4, -7] Output: 4 Explanation: The longest zero-sum subarray is [3, 0, 4, -7]. Solution Approach # The solution involves using a hashmap to store the cumulative sum up to each index. If the same cumulative sum occurs again, it means that the subarray between these indices sums to zero. ...

Maximum Product of Three Numbers

January 19, 2024
medium
array

Problem # Given an integer array nums, find three numbers whose product is maximum and return the maximum product. Example: Input: nums = [-1, -2, -3, 4] Output: 24 Explanation: The maximum product is obtained by multiplying -3 * -2 * 4 = 24. Solution Approach # The solution involves finding the three largest numbers and the two smallest numbers in the array. The maximum product can be obtained either by multiplying the three largest numbers or by multiplying the two smallest numbers (which could be negative and thus make a positive product when multiplied together) with the largest number. ...

Rotate Array K Times

January 14, 2024
medium
array

Problem # Given an array nums and a non-negative integer k, rotate the array to the right by k steps. Each step involves moving the last element of the array to the front. Example: Input: nums = [1, 2, 3, 4, 5, 6, 7], k = 3 Output: [5, 6, 7, 1, 2, 3, 4] Solution Approach # The solution involves reversing parts of the array. First, reverse the entire array, then reverse the first k elements, and finally reverse the remaining n-k elements. ...

Maximum Difference in an Array

January 14, 2024
medium
array

Problem # Given an array of integers nums, find the maximum difference between any two elements nums[j] and nums[i] such that j > i. Example: Input: nums = [7, 1, 5, 4] Output: 4 Explanation: The maximum difference is between 5 and 1. Solution Approach # The solution involves iterating through the array while keeping track of the minimum element found so far and the maximum difference. Algorithm Steps # Initialize two variables: min_element to the first element and max_diff to 0. ...

Maximum sum of any subarray

December 16, 2023
medium
array

Problem # Given an array of numbers, find the maximum sum of any contiguous subarray of the array. For example, given the array [34, -50, 42, 14, -5, 86], the maximum sum would be 137, since we would take elements 42, 14, -5, and 86. Given the array [-5, -1, -8, -9], the maximum sum would be 0, since we would not take any elements. Do this in O(N) time. ...

Array Rotation and Sum

December 15, 2023
medium
array

Problem # Sure, here’s a medium-level algorithm interview question that I’ve created: Problem: “Array Rotation and Sum” You are given an array of n integers and a number, k. Your task is to write a function that rotates the array to the right by k steps, and then finds the sum of a specific subarray. Specifically, your function should implement the following steps: Rotate the array to the right by k steps. ...