Month: April 2021

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 »

Design Circular Queue

Design your implementation of the circular queue. The circular queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position to make a circle. It is also called “Ring Buffer”. One of the benefits of …

Design Circular Queue Read More »

Longest Valid Parentheses

Given a string containing just the characters ‘(‘ and ‘)’, find the length of the longest valid (well-formed) parentheses substring. Example 1: Input: s = “(()” Output: 2 Explanation: The longest valid parentheses substring is “()”. Example 2: Input: s = “)()())” Output: 4 Explanation: The longest valid parentheses substring is “()()”. Example 3: Input: s = “” …

Longest Valid Parentheses Read More »