Maximum Product of Three Numbers Problem

Problem Given an integer array nums, find three numbers whose product is maximum and return the maximum product. Examples Example 1: Input: nums = [1,2,3] Output: 6 Example 2: Input: nums = [1,2,3,4] Output: 24 ...

Find fixed point in sorted array

Problem Given a sorted array of distinct integers, write a function to find the magic index or fixed point in the array, else return -1. Definition Magic index or a Fixed point in an array is an index i in the array A such that A[i] = i. Examples Example 1: Input: nums = [-6, 0, 2, 40] Output: 2 ...

Check Currency Arbitrage with Bellman Ford

Problem Suppose you are given a table of currency exchange rates, represented as a 2D array. Determine whether there is a possible arbitrage: that is, whether there is some sequence of trades you can make, starting with some amount A of any currency, so that you can end up with some amount greater than A of that currency. There are no transaction costs and you can trade fractional quantities. ...

Reservoir Sampling Explained

Problem Given a stream of elements too large to store in memory, pick a random element from the stream with uniform probability. Solution This is a classic problem known as Reservoir Sampling. The goal is to randomly select an element from a large stream of data with uniform probability, even when the entire stream cannot be stored in memory. Introduction to Problem Reservoir sampling, often referred to as Algorithm R as described by Jeffrey Vitter in Random Sampling with a Reservoir, is a widely used technique in data processing. It allows for the random selection of k samples from a set S containing n items, where n is very large or unknown. Each of the chosen k items forms a “reservoir,” ensuring that every item is selected with an equal probability of 1/n. ...

Minimum Remove to Make Valid Parentheses

Problem Given a string s of '(' , ')' and lowercase English characters. Your task is to remove the minimum number of parentheses ( '(' or ')', in any positions ) so that the resulting parentheses string is valid and return any valid string. Formally, a parentheses string is valid if and only if: It is the empty string, contains only lowercase characters, or It can be written as AB (A concatenated with B), where A and B are valid strings, or It can be written as (A), where A is a valid string. Examples Example 1: ...

Run-length Encoding

Problem Run-length Encoded is a fast and simple method of encoding strings. The basic idea is to represent repeated successive characters as a single count and character. Examples Example 1: Input: str = AAAABBBCCDAA Output: 4A3B2C1D2A Input Format public class RLECodec { public String encode(String str) { } public String decode(String data) { } } ...

Maximum Intervals Overlap Count

Problem Find maximum intervals overlap. OR There is number of elephants time span given, here time span means, year of birth to year of death. You have to calculate the period where maximum number of elephants are alive. OR Consider a big party where a log register for guest’s entry and exit times is maintained. Find the time at which there are maximum guests in the party. Note that entries in register are not in any order. ...

Longest Substring with K Distinct Characters

Problem Given a string, find the longest substring that contains only k unique or distinct characters. Examples Example 1: Input: S = "aabacbebebe", K = 3 Output: 7 Explanation: "cbebebe" is the longest substring with 3 distinct characters. Example 2: Input: S = "aaaa", K = 2 Output: -1 Explanation: There's no substring with 2 distinct characters. ...

Swap odd and even bits in an integer

Problem Write a program to swap odd and even bits in an integer with as few instructions as possible (e.g., bit 0 and bit 1 are swapped, bit 2 and bit 3 are swapped, etc). Input : An integer x Output : An integer y, which odd and even bit swapped (with 0th bit being least significant bit) Examples Example 1: Input: x = 10 = 1010 Output: 5 = 0101 (0th bit and 1st bit have been swapped,also 2nd and 3rd bit have been swapped) ...

This site uses cookies to improve your experience on our website. By using and continuing to navigate this website, you accept this. Privacy Policy