Substring with Largest Variance Problem

Problem The variance of a string is defined as the largest difference between the number of occurrences of any 2 characters present in the string. Note the two characters may or may not be the same. Given a string s consisting of lowercase English letters only, return the largest variance possible among all substrings of s. A substring is a contiguous sequence of characters within a string. ...

Single Number 2 - All elements except one occur thrice

Problem Given an integer array nums where every element appears three times except for one, which appears exactly once. Find the single element and return it. You must implement a solution with a linear runtime complexity and use only constant extra space. Examples Example 1: Input : nums = [1, 2, 4, 3, 3, 2, 2, 3, 1, 1] Output : 4 ...

Kth Largest Element in an Array

Problem Given an integer array nums and an integer k, return the kth largest element in the array. Note that it is the kth largest element in the sorted order, not the kth distinct element. Example 1: Input: nums = [3,2,1,5,6,4], k = 2 Output: 5 Example 2: Input: nums = [3,2,3,1,2,4,5,5,6], k = 4 Output: 4 ...

Divide Two Integers Problem

Problem Given two integers dividend and divisor, divide two integers without using multiplication, division, and mod operator. The integer division should truncate toward zero, which means losing its fractional part. For example, 8.345 would be truncated to 8, and -2.7335 would be truncated to -2. Return the quotient after dividing dividend by divisor. Note: Assume we are dealing with an environment that could only store integers within the 32-bit signed integer range: [−2^31, 2^31 − 1]. For this problem, if the quotient is strictly greater than 2^31 - 1, then return 2^31 - 1, and if the quotient is strictly less than -2^31, then return -2^31. ...

Minimum Absolute Difference in BST Problem

Minimum Absolute Difference in BST Problem Problem Given the root of a Binary Search Tree (BST), return the minimum absolute difference between the values of any two different nodes in the tree. OR Given the root of a Binary Search Tree (BST), return the minimum difference between the values of any two different nodes in the tree. Examples Example 1: graph TB; 4 --- 2 & 6; 2 --- 1 & 3; ...

Sort a Linked List

Problem Given the head of a linked list, return the list after sorting it in ascending order. Examples Example 1: Input: head = [4,2,1,3] Output: [1,2,3,4] Example 2: Input: head = [-1,5,3,4,0] Output: [-1,0,3,4,5] Solution Method 1 - Insertion Sort Insertion Sort on List ...

Dutch National Flag DNF problem

Problem Given an array nums with n objects colored red, white, or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white, and blue. We will use the integers 0, 1, and 2 to represent the color red, white, and blue, respectively. You must solve this problem without using the library’s sort function. By the way this is Dutch National Flag - 🇳🇱. ...

Longest Increasing Subsequence LIS Problem

Problem Given an unsorted array of integers, find the length of longest increasing subsequence. Examples Example 1: Input: A = [10,9,2,5,3,7,101,18] Output: 4 Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4. ...

Sliding Window Maximum Problem

Problem You are given an array of integers nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position. Return the max sliding window. Sometimes k is also denoted as w. Example Example ...

Perfect number Problem

Problem Given an integer n, return true if n is a perfect number, otherwise return false. Definition A perfect number is a natural number that is equal to the sum of its positive divisors, excluding the number itself. A divisor of an integer x is an integer that can divide x evenly. The first perfect numbers are 6, 28, 496, 8128 and all of them are in form $$ 2^{n-1}.(2^n - 1)$$ ...

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