## Best Time to Buy and Sell Stock LeetCode Solution

Problem Statement The Best Time to Buy and Sell Stock LeetCode Solution – “Best Time to Buy and Sell Stock” states that You are given an array of prices where prices[i] is the price of a given stock on an ith day. You want to maximize your profit by choosing …

## Search in Rotated Sorted Array Leetcode Solution

Consider a sorted array but one index was picked and the array was rotated at that point. Now, once the array has been rotated you are required to find a particular target element and return its index. In case, the element is not present, return -1. The problem is generally …

## Text Justification LeetCode Solution

We will discuss Text Justification LeetCode Solution today Problem Statement The problem “Text Justification” states that you are given a list s[ ] of type string of size n and an integer size. Justify the text such that each line of text consists of size number of characters. You can …

## Smallest Element Repeated Exactly K Times

We are given an array A[] on size n. We have to find the smallest element that is repeated exactly k times in the array. Example Input A[]= {1, 2 ,2 ,5 ,5 ,2 ,5} K=3 Output Smallest element with frequency K is: 2 Approach 1: Brute force Main idea …

## Pair of Positive Negative Values in an Array

In pair of positive negative values in an array problem we have given an array A of distinct integers, print all the pairs having positive value and negative value of a number that exists in the array. We need to print pairs in order of their occurrences. A pair whose …

## Insert Delete GetRandom

In Insert Delete GetRandom problem we need to design a data structure that supports all following operations in average O(1) time. insert(val): Inserts an item val to the set if not already present. remove(val): Removes an item val from the set if present. getRandom: Returns a random element from the current set …

## Sorting a Queue without Extra Space

In sorting a queue without extra space problem we have given a queue, sort it using standard queue operations without extra space. Examples Input queue = 10 -> 7 -> 2 -> 8 -> 6 Output queue = 2 -> 6 -> 7 -> 8 -> 10 Input queue = …

## Search an Element in Sorted Rotated Array

In search in sorted rotated array problem we have given a sorted and rotated array and an element, check if the given element is present in the array or not. Examples Input nums[] = {2, 5, 6, 0, 0, 1, 2} target = 0 Output true Input nums[] = {2, …

## Search in Sorted Rotated Array

An element search in sorted rotated array can be found using binary search in O(logn) time. The objective of this post is to find a given element in a sorted rotated array in O(logn) time. Some example of a sorted rotated array is given. Example Input : arr[] = {7,8,9,10,1,2,3,5,6}; …

## Find Median from data Stream

In Find Median from the data Stream problem, we have given that integers are being read from a data stream. Find the median of all the elements read so far starting from the first integer till the last integer. Example Input 1: stream[ ] = {3,10,5,20,7,6} Output : 3 6.5 …