Minimum Operations to Make Array Equal

You have an array arr of length n where arr[i] = (2 * i) + 1 for all valid values of i (i.e. 0 <= i < n). In one operation, you can select two indices x and y where 0 <= x, y < n and subtract 1 from arr[x] and add 1 to arr[y] (i.e. perform arr[x] -=1 and arr[y] += 1). The goal is to make all the elements of the array equal. It is guaranteed that all the elements of the …

Minimum Operations to Make Array Equal Read More »

Nth Catalan Number

Hi Geeks, in this article we going to see about what are Catalan numbers, how to find nth Catalan number, by using Catalan number how we can find the Unique Binary Search trees. Catalan Number Formation By using Catalan Numbers approach, here we will solve one problem from Leetcode – Unique Binary Search Trees. Given …

Nth Catalan Number Read More »

Global and Local Inversions

We have some permutation A of [0, 1, …, N – 1], where N is the length of A. The number of (global) inversions is the number of i < j with 0 <= i < j < N and A[i] > A[j]. The number of local inversions is the number of i with 0 <= i < N and A[i] > A[i+1]. Return true if and only if the number of global …

Global and Local Inversions Read More »

Fizz Buzz

Write a program that outputs the string representation of numbers from 1 to n. But for multiples of three, it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”. Example: n = 15, Return: [ “1”, “2”, “Fizz”, …

Fizz Buzz Read More »

LRU Cache – Clean Code

Design a data structure that follows the constraints of a Least Recently Used (LRU) cache. Implement the LRUCache class: LRUCache(int capacity) Initialize the LRU cache with positive size capacity. int get(int key) Return the value of the key if the key exists, otherwise return -1. void put(int key, int value) Update the value of the key if the key exists. Otherwise, add the key-value pair to the cache. If the number of keys …

LRU Cache – Clean Code Read More »

Decode String

Hi geeks, in this article, we going to solve one interesting problem in leetcode, that is Decode string. It tests our analytical and problem solving skills. Given an encoded string, return its decoded string. The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note that k is guaranteed to be a positive integer. …

Decode String Read More »

Trapping Rain Water

Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. Example: Input: [0,1,0,2,1,0,1,3,2,1,2,1] Output: 6 Solution

Maximal Rectangle

Given a 2D binary matrix filled with 0’s and 1’s, find the largest rectangle containing only 1’s and return its area. Example: Input: [ [“1″,”0″,”1″,”0″,”0”], [“1″,”0″,”1″,”1″,”1”], [“1″,”1″,”1″,”1″,”1”], [“1″,”0″,”0″,”1″,”0”] ] Output: 6 Solution C# Program

Container With Most Water

Given n non-negative integers a1, a2, …, an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. Note: You may not slant the container and n is at least 2. Example: …

Container With Most Water Read More »