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”
Category Archives: Uncategorized
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 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 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”