Here’s the problem statement: GIven an array of integers that represent the worth of stock on consecutive days, find the maximum profit that can be made by buying stock on any day and then selling it on another day in the future. You are allowed multiple buy-sell transactions. However, you cannot both buy and sellContinue reading “Scribble 14: Maximize Profit Part 2”
Tag Archives: Array
Scribble 13: Maximize Profit
This is a really interesting problem. Here’s the problem statement: Given an array of integers that represents the worth of stock on consecutive days, find the maximum profit that can be earned by buying stock on any day and then selling it on another day in the future. For example: The Array: [9,1,5,4,6,2]Output: 5 (buyContinue reading “Scribble 13: Maximize Profit”
Scribble 12: Maximum Range
Here’s the problem statement: Given an array of integers, find the end points of the longest range of values present in the array. Note the elements in the range need not necessarily be placed adjacent/in order in the input array. For example:The array: [4, 20, 5, 6, 21, 22, 7, 8, 41, 9] Output: [6]Continue reading “Scribble 12: Maximum Range”
Scribble 11: First Duplicate
Here’s the problem statement: Given an array of n integers such that every integer lies only in [1, n], find the element in the array who’s duplicate has the minimum index of all the indices that are occupied by duplicate values. For example: Array: [3 2 5 2 3]Output: 2 Approach 1: This is a naiveContinue reading “Scribble 11: First Duplicate”
Scribble 10: Move Zeros to The End
Here’s the problem statement: Given an array of integers, manipulate its elements such that all zeros in it are placed after all the non-zero elements and the order of the non-zero elements is retained. For example: The array: [1, 45, 0, 7, 0, 5, 0, 0] Output: [1, 45, 7, 5, 0, 0, 0, 0] Let’s diveContinue reading “Scribble 10: Move Zeros to The End”
Scribble 9: Is Subsequence
Here’s the problem statement: Given 2 arrays of integers, tell if the second array is a valid subsequence of the first i.e. if the second array has some/all adjacent/nonadjacent elements of the first array in the same order. For example: Array 1: [1, 12, 45, 79, 0, 5, 6, 32]Array 2: [12, 45, 79, 5, 32]Output:Continue reading “Scribble 9: Is Subsequence”
Scribble 8: Smallest Unsorted Subarray
Here’s the problem statement: Given an array of integers find the length of the smallest subarray which when sorted will make the entire array sorted in the ascending order. For Example:The array: [2, 6, 4, 8, 10, 9, 15]Output: 5 (the subarray is: [6, 4, 8, 10, 9]) Let’s dive into the solution: Loop throughContinue reading “Scribble 8: Smallest Unsorted Subarray”
Scribble 7: Product Array
Here’s the problem statement: Given an array of integers, return a new array of the same length as the given array such that the result array’s value at index i = product of elements at all indices of the input array except the element at the ith index. For example:The array: [1, 3, 5, 6,Continue reading “Scribble 7: Product Array”
Scribble 6: Trapping Rain Water
This is a really interesting problem. Here’s the problem statement: Given an array of integers that represent the heights of mud walls built next to each other by the little children at a village school. The length and width of each wall is 1 unit. Find the volume of water that gets collected in theContinue reading “Scribble 6: Trapping Rain Water”
Scribble 5: Smallest Difference
Here’s the problem statement: Given two arrays of integers, find a pair of integers – one from each array such that the length of the real number line between them is the smallest. Assume there is only one such pair. For example:Array1: 1, 11, 15, 2 Array2: 500, 23, 19, 127, 12, 4 Output: 1 (difference betweenContinue reading “Scribble 5: Smallest Difference”