L2 Questions Final
L2 Questions Final
There is a broken calculator that has the integer startValue on its display initially. In one operation, you
can:
Given two integers startValue and target, return the minimum number of operations needed to
display target on the calculator.
Example 1:
Output: 2
Explanation: Use double operation and then decrement operation {2 -> 4 -> 3}.
Example 2:
Output: 2
Example 3:
Output: 3
Explanation: Use double, decrement and double {3 -> 6 -> 5 -> 10}.
Test Cases
Startvalue=2
Target=3
Ans 2
Startvalue=5
Target=8
Ans 2
Hidden Test Case
Startvalue=3
target= 10
Ans 3
Question 2)
200. Number of Islands
Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the
number of islands.
An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically.
You may assume all four edges of the grid are all surrounded by water.
Example 1:
Input: grid = [
["1","1","1","1","0"],
["1","1","0","1","0"],
["1","1","0","0","0"],
["0","0","0","0","0"]
Output: 1
Example 2:
Input: grid = [
["1","1","0","0","0"],
["1","1","0","0","0"],
["0","0","1","0","0"],
["0","0","0","1","1"]
Output: 3
Constraints:
● m == grid.length
● n == grid[i].length
Test Cases:
grid = [
["0","0","0"],
["0","0","0"],
["0","0","0"]
Ans 0
grid = [
["1","1","1"],
["1","1","1"],
["1","1","1"]
Ans 1
grid = [
["1","0","1"],
["0","1","0"],
["1","0","1"]
Ans 5
Question 3)
1014. Best Sightseeing Pair
You are given an integer array values where values[i] represents the value of the ith sightseeing spot.
Two sightseeing spots i and j have a distance j - i between them.
The score of a pair (i < j) of sightseeing spots is values[i] + values[j] + i - j: the sum of the values of the
sightseeing spots, minus the distance between them.
Example 1:
Output: 11
Example 2:
Output: 2
Constraints:
Test cases
values = [10, 9, 8, 7, 6]
Ans 17
values = [1, 3, 5, 9, 2, 1]
Ans 12
Ans 17
Question 4)
1710 Maximum units on truck
You are assigned to put some amount of boxes onto one truck. You are given a 2D array boxTypes,
where boxTypes[i] = [numberOfBoxesi, numberOfUnitsPerBoxi]:
You are also given an integer truckSize, which is the maximum number of boxes that can be put on the
truck. You can choose any boxes to put on the truck as long as the number of boxes does not
exceed truckSize.
Return the maximum total number of units that can be put on the truck.
Example 1:
Output: 8
You can take all the boxes of the first and second types, and one box of the third type.
Example 2:
Output: 91
Constraints:
Test Cases
[[1,3],[2,2],[3,1]]
Trucksize=4
Ans 8
[[5,10],[2,5],[4,7],[3,9]]
Trucksize=10
Ans 91
[[10, 5], [7, 8], [4, 10], [2, 12], [6, 6]]
Trucksize=20
Ans 161
Question 5)
739. Daily Temperatures
Given an array of integers temperatures represents the daily temperatures, return an array answer such
that answer[i] is the number of days you have to wait after the ith day to get a warmer temperature. If
there is no future day for which this is possible, keep answer[i] == 0 instead.
Example 1:
Output: [1,1,4,2,1,1,0,0]
Example 2:
Output: [1,1,1,0]
Example 3:
Output: [1,1,0]
Constraints:
temperatures = [50,50,50,50]
Ans [0,0,0,0]
Ans [1,1,1,0,0,0,0]
Ans [1,1,1,1,0,0,0]
Question 6)
75. Sort Colors
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.
Example 1:
Output: [0,0,1,1,2,2]
Example 2:
Output: [0,1,2]
Constraints:
● n == nums.length
● nums[i] is either 0, 1, or 2.
Test Cases:
nums = [0,0,1,1,2,2]
[0,0,1,1,2,2]
nums = [2,2,2,2]
[2,2,2,2]
nums = [1,2,0,1,2,0,0,1]
[0,0,0,1,1,1,2,2]
Question 7)
1323 Maximum 69 number
You are given a positive integer num consisting only of digits 6 and 9.
Return the maximum number you can get by changing at most one digit (6 becomes 9,
and 9 becomes 6).
Example 1:
Input: num = 9669
Output: 9969
Explanation:
Changing the first digit results in 6669.
Changing the second digit results in 9969.
Changing the third digit results in 9699.
Changing the fourth digit results in 9666.
The maximum number is 9969.
Example 2:
Input: num = 9996
Output: 9999
Explanation: Changing the last digit 6 to 9 results in the maximum number.
Example 3:
Input: num = 9999
Output: 9999
Explanation: It is better not to apply any change.
Constraints:
1 <= num <= 104
num consists of only 6 and 9 digits.
Test Cases
9669
Ans 9969
9996
Ans 9999
Hidden Test Case
9999
Ans 9999
Question 8)
151. Reverse Words in a String
A word is defined as a sequence of non-space characters. The words in s will be separated by at least
one space.
Note that s may contain leading or trailing spaces or multiple spaces between two words. The returned
string should only have a single space separating the words. Do not include any extra spaces.
Example 1:
Example 2:
Explanation: Your reversed string should not contain leading or trailing spaces.
Example 3:
Explanation: You need to reduce multiple spaces between two words to a single space in the reversed
string.
Constraints:
● s contains English letters (upper-case and lower-case), digits, and spaces ' '.
Test Cases:
s = "hello"
Ans "hello"
Question 9)
605. Can Place Flowers
You have a long flowerbed in which some of the plots are planted, and some are not. However, flowers
cannot be planted in adjacent plots.
Given an integer array flowerbed containing 0's and 1's, where 0 means empty and 1 means not empty,
and an integer n, return true if n new flowers can be planted in the flowerbed without violating the no-
adjacent-flowers rule and false otherwise.
Example 1:
Output: true
Example 2:
Input: flowerbed = [1,0,0,0,1], n = 2
Output: false
Constraints:
● flowerbed[i] is 0 or 1.
Test Cases:
flowerbed = [0,0,0,0,0], n = 2
Ans true
flowerbed = [1,0,1,0,1], n = 1
Ans false
flowerbed = [0,0,1,0,0], n = 1
Ans true
Question 10)
1790. Check if One String Swap Can Make Strings Equal
You are given two strings s1 and s2 of equal length. A string swap is an operation where you choose two
indices in a string (not necessarily different) and swap the characters at these indices.
Return true if it is possible to make both strings equal by performing at most one string
swap on exactly one of the strings. Otherwise, return false.
Example 1:
Output: true
Explanation: For example, swap the first character with the last character of s2 to make "bank".
Example 2:
Output: false
Example 3:
Output: true
Explanation: The two strings are already equal, so no string swap operation is required.
Constraints:
● s1.length == s2.length
Test Cases:
s1 = "abcd"
s2 = "abcd"
Ans true
s1 = "abcd"
s2 = "badc"
Ans false
s1 = "converse"
s2 = "conevrse"
Ans true
Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit,
return 0.
Example 1:
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.
Example 2:
Output: 0
Explanation: In this case, no transactions are done and the max profit = 0.
Constraints:
Test Case
[7,1,5,3,6,4]
Ans 5
[7,6,4,3,1]
Ans 0
[3,2,6,5,0,3]
Ans 4
Example 1:
Output: [1,3,12,0,0]
Example 2:
Output: [0]
Example 2:
Output: [0]
Constraints:
Test Case
[0,1,0,3,12]
Ans [1,3,12,0,0]
[0]
Ans [0]
[4,0,5,0,6,7,0,8]
Ans [4,5,6,7,8,0,0,0]
13. 15 3Sum
Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k,
and j != k, and nums[i] + nums[j] + nums[k] == 0.
Notice that the solution set must not contain duplicate triplets.
Example 1:
Output: [[-1,-1,2],[-1,0,1]]
Explanation:
Notice that the order of the output and the order of the triplets does not matter.
Example 2:
Output: []
Example 3:
Output: [[0,0,0]]
Constraints:
[-1,0,1,2,-1,-4]
Ans [[-1,-1,2],[-1,0,1]]
[0,1,1]
Ans []
[0,0,0]
Ans [[0,0,0]]
Given a 0-indexed integer array nums, find a peak element, and return its index. If the array
contains multiple peaks, return the index to any of the peaks.
You may imagine that nums[-1] = nums[n] = -. In other words, an element is always considered to be strictly greater than a neig
Example 1:
Output: 2
Explanation: 3 is a peak element and your function should return the index number 2.
Example 2:
Output: 5
Explanation: Your function can return either index number 1 where the peak element is 2, or index
number 5 where the peak element is 6.
Constraints:
1 <= nums.length <= 1000
Test Case
[1,2,3,1]
Ans 2
[1,2,1,3,5,6,4]
ANs 5
hidden
[1,2,3,1]
Ans 2
Example 1:
Output: 1
Explanation:
Example 2:
Explanation:
The third distinct maximum does not exist, so the maximum (2) is returned instead.
Example 3:
Output: 1
Explanation:
The second distinct maximum is 2 (both 2's are counted together since they have the same value).
Constraints:
Test Cases
[3,2,1]
Ans 1
[1,2]
Ans 2
[2,2,3,1]
Ans 1
16. 134 Gas Station
There are n gas stations along a circular route, where the amount of gas at the i th station is gas[i].
You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from the i th station to its
next (i + 1)th station. You begin the journey with an empty tank at one of the gas stations.
Given two integer arrays gas and cost, return the starting gas station's index if you can travel around
the circuit once in the clockwise direction, otherwise return -1. If there exists a solution, it is
guaranteed to be unique.
Example 1:
Output: 3
Explanation:
Start at station 3 (index 3) and fill up with 4 unit of gas. Your tank = 0 + 4 = 4
Travel to station 3. The cost is 5. Your gas is just enough to travel back to station 3.
Example 2:
Output: -1
Explanation:
You can't start at station 0 or 1, as there is not enough gas to travel to the next station.
Let's start at station 2 and fill up with 4 unit of gas. Your tank = 0 + 4 = 4
You cannot travel back to station 2, as it requires 4 unit of gas but you only have 3.
Therefore, you can't travel around the circuit once no matter where you start.
Constraints:
n == gas.length == cost.length
Test Case
gas=[1,2,3,4,5]
cost=[3,4,5,1,2]
ans 3
gas= [2,3,4]
cost=[3,4,3]
Ans -1
Hidden
gas = [3, 1, 4, 5, 2]
cost = [2, 2, 3, 4, 5]
Ans -1
Example 1:
Input: citations = [3,0,6,1,5]
Output: 3
Explanation: [3,0,6,1,5] means the researcher has 5 papers in total and each
of them had received 3, 0, 6, 1, 5 citations respectively.
Since the researcher has 3 papers with at least 3 citations each and the
remaining two with no more than 3 citations each, their h-index is 3.
Example 2:
Input: citations = [1,3,1]
Output: 1
Constraints:
n == citations.length
1 <= n <= 5000
0 <= citations[i] <= 1000
Test Cases
[3,0,6,1,5]
Ans 3
[1,3,1]
Ans 1
Hidden
[10, 8, 5, 4, 3]
Ans 4
A valid additive sequence should contain at least three numbers. Except for the first two numbers,
each subsequent number in the sequence must be the sum of the preceding two.
Given a string containing only digits, return true if it is an additive number or false otherwise.
Note: Numbers in the additive sequence cannot have leading zeros, so sequence 1, 2, 03 or 1, 02,
3 is invalid.
Example 1:
Input: "112358"
Output: true
Explanation:
1 + 1 = 2, 1 + 2 = 3, 2 + 3 = 5, 3 + 5 = 8
Example 2:
Input: "199100199"
Output: true
Explanation:
Constraints:
Test Case
"112358"
Ans true
"199100199"
ANs true
Hidden
"123456789"
Ans false
You are also given two 0-indexed integer arrays energy and experience, both of length n.
You will face n opponents in order. The energy and experience of the ith opponent is denoted
by energy[i] and experience[i] respectively. When you face an opponent, you need to have
both strictly greater experience and energy to defeat them and move to the next opponent if
available.
Defeating the ith opponent increases your experience by experience[i], but decreases your energy
by energy[i].
Before starting the competition, you can train for some number of hours. After each hour of training,
you can either choose to increase your initial experience by one, or increase your initial energy by
one.
Return the minimum number of training hours required to defeat all n opponents.
Example 1:
Output: 8
Explanation: You can increase your energy to 11 after 6 hours of training, and your experience to 5
after 2 hours of training.
- You have more energy and experience than the 0th opponent so you win.
- You have more energy and experience than the 1st opponent so you win.
- You have more energy and experience than the 2nd opponent so you win.
- You have more energy and experience than the 3rd opponent so you win.
Example 2:
Output: 0
Explanation: You do not need any additional energy or experience to win the competition, so we
return 0.
Constraints:
n == energy.length == experience.length
1 <= n <= 100
Test Cases
initialEnergy=5
initialExperience=3
energy= [1,4,3,2]
experience= [2,6,3,1]
Ans 8
initialEnergy=2
initialExperience=4
energy= [1]
experience= [3]
Ans 0
initialEnergy=5
initialExperience=3
energy= [1,4,3]
experience= [2,6,8]
Ans 6
Example 1:
Output: false
Example 2:
Output: false
Example 3:
Output: true
Test Case
ransomNote=”a”
magazine=”b”
ans false
ransomNote=”aa”
magazine=”ab”
ans false
ransomNote=”aa”
magazine=”aab”
Ans true
You are given the array paths, where paths[i] = [cityAi, cityBi] means there exists a direct path going
from cityAi to cityBi. Return the destination city, that is, the city without any path outgoing to another
city.
It is guaranteed that the graph of paths forms a line without any loop, therefore, there will be exactly
one destination city.
Example 1:
Example 2:
Output: "A"
"A".
Example 3:
Output: "Z"
Constraints:
paths[i].length == 2
cityAi != cityBi
All strings consist of lowercase and uppercase English letters and the space character.
Test Cases
[["B","C"],["D","B"],["C","A"]]
Ans “A”
[["A","Z"]]
Ans “Z”
22. 74 :- Search a 2D Matrix
You are given an m x n integer matrix matrix with the following properties:
● The first integer of each row is greater than the last integer of the previous row.
Given an integer target, return true if target is in the matrix, or false otherwise.
Example 1:
Output: true
Example 2:
Constraints:
m == matrix.length
n == matrix[i].length
Test Case
[[1,3,5,7],[10,11,16,20],[23,30,34,60]]
target=3
Ans true
[[1,3,5,7],[10,11,16,20],[23,30,34,60]]
target=13
Ans False
matrix = [
[2, 4, 6, 8],
target = 25
Ans False
Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer.
3. Conversion: Read the integer by skipping leading zeros until a non-digit character is
encountered or the end of the string is reached. If no digits were read, then the result is 0.
4. Rounding: If the integer is out of the 32-bit signed integer range [-231, 231 - 1], then round the
integer to remain in the range. Specifically, integers less than -231 should be rounded to -231,
and integers greater than 231 - 1 should be rounded to 231 - 1.
Example 1:
Input: s = "42"
Output: 42
Explanation:
The underlined characters are what is read in and the caret is the current reader position.
Step 2: "42" (no characters read because there is neither a '-' nor '+')
Example 2:
Output: -42
Explanation:
Step 3: " -042" ("042" is read in, leading zeros ignored in the result)
^
Example 3:
Input: s = "1337c0d3"
Output: 1337
Explanation:
Step 2: "1337c0d3" (no characters read because there is neither a '-' nor '+')
Step 3: "1337c0d3" ("1337" is read in; reading stops because the next character is a non-digit)
Example 4:
Input: s = "0-1"
Output: 0
Explanation:
Step 2: "0-1" (no characters read because there is neither a '-' nor '+')
Step 3: "0-1" ("0" is read in; reading stops because the next character is a non-digit)
Example 5:
Output: 0
Explanation:
Constraints:
s consists of English letters (lower-case and upper-case), digits (0-9), ' ', '+', '-', and '.'.
Test Case
s="42"
Ans 42
" -042"
Ans -42
"1337c0d3"
Ans 1337
"0-1"
ANs 0
Ans 0
A phrase is a palindrome if it reads the same forward and backward, ignoring cases, spaces, and non-
alphanumeric characters.
Example 1:
Output: true
Example 2:
Output: false
Example 3:
Since an empty string reads the same forward and backward, it is a palindrome.
Constraints:
Test Case
Ans true
"race a car"
Ans False
""
Ans true
You are given a string s consisting of lowercase English letters. A duplicate removal consists of choosing
two adjacent and equal letters and removing them.
Return the final string after all such duplicate removals have been made. It can be proven that the
answer is unique.
Example 1:
Input: s = "abbaca"
Output: "ca"
Explanation:
For example, in "abbaca" we could remove "bb" since the letters are adjacent and equal, and this is the
only possible move. The result of this move is that the string is "aaca", of which only "aa" is possible, so
the final string is "ca".
Example 2:
Input: s = "azxxzy"
Output: "ay"
Constraints:
Test Case
"abbaca"
Ans “ca”
"azxxzy"
Ans “ay”
You are given an array of characters letters that is sorted in non-decreasing order, and a
character target. There are at least two different characters in letters.
Return the smallest character in letters that is lexicographically greater than target. If such a character
does not exist, return the first character in letters.
Example 1:
Output: "c"
Explanation: The smallest character that is lexicographically greater than 'a' in letters is 'c'.
Example 2:
Output: "f"
Explanation: The smallest character that is lexicographically greater than 'c' in letters is 'f'.
Example 3:
Input: letters = ["x","x","y","y"], target = "z"
Output: "x"
Explanation: There are no characters in letters that is lexicographically greater than 'z' so we return
letters[0].
Constraints:
Test Cases
["c","f","j"]
Target=”a”
Ans “c”
["c","f","j"]
Target=”c”
Ans “f”
["x","x","y","y"]
Target=”z”
Ans “x”
Given an array of strings words, return the words that can be typed using letters of the alphabet on only
one row of American keyboard like the image below.
Note that the strings are case-insensitive, both lowercased and uppercased of the same letter are
treated as if they are at the same row.
Example 1:
Output: ["Alaska","Dad"]
Explanation:
Both "a" and "A" are in the 2nd row of the American keyboard due to case insensitivity.
Example 2:
Output: []
Example 3:
Output: ["adsdf","sfd"]
Constraints:
["Hello","Alaska","Dad","Peace"]
Ans ["Alaska","Dad"]
["omk"]
Ans [ ]
["adsdf","sfd"]
ANs ["adsdf","sfd"]
Given a string paragraph and a string array of the banned words banned, return the most frequent word
that is not banned. It is guaranteed there is at least one word that is not banned, and that the answer
is unique.
The words in paragraph are case-insensitive and the answer should be returned in lowercase.
Example 1:
Input: paragraph = "Bob hit a ball, the hit BALL flew far after it was hit.", banned = ["hit"]
Output: "ball"
Explanation:
"ball" occurs twice (and no other word does), so it is the most frequent non-banned word in the
paragraph.
and that "hit" isn't the answer even though it occurs more because it is banned.
Example 2:
Output: "a"
Constraints:
Test Cases
"Bob hit a ball, the hit BALL flew far after it was hit."
Banned= ["hit"]
Ans “ball”
"a."
Banned= []
Ans “a”
You are given an array coordinates, coordinates[i] = [x, y], where [x, y] represents the coordinate of a
point. Check if these points make a straight line in the XY plane.
Example 1:
Input: coordinates = [[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]]
Output: true
Example 2:
Output: false
Constraints:
coordinates[i].length == 2
Test Case
[[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]]
ANs true
[[1,1],[2,2],[3,4],[4,5],[5,6],[7,7]]
Ans false
You are given an integer array score of size n, where score[i] is the score of the ith athlete in a
competition. All the scores are guaranteed to be unique.
The athletes are placed based on their scores, where the 1st place athlete has the highest score, the 2nd
place athlete has the 2nd highest score, and so on. The placement of each athlete determines their rank:
● For the 4th place to the nth place athlete, their rank is their placement number (i.e., the xth
place athlete's rank is "x").
Return an array answer of size n where answer[i] is the rank of the ith athlete.
Example 1:
Example 2:
Constraints:
n == score.length
Test Case
[5,4,3,2,1]
[10,3,8,9,4]
Given an integer array nums, you need to find one continuous subarray such that if you only sort this
subarray in non-decreasing order, then the whole array will be sorted in non-decreasing order.
Example 1:
Output: 5
Explanation: You need to sort [6, 4, 8, 10, 9] in ascending order to make the whole array sorted in
ascending order.
Example 2:
Output: 0
Example 3:
Input: nums = [1]
Output: 0
Constraints:
Test Case
[2,6,4,8,10,9,15]
Ans 5
[1,2,3,4]
ANs 0
[1]
ANs 0
A subsequence of a string is a new string that is formed from the original string by deleting some (can be
none) of the characters without disturbing the relative positions of the remaining characters. (i.e., "ace"
is a subsequence of "abcde" while "aec" is not).
Example 1:
Output: true
Example 2:
Output: false
Constraints:
0 <= s.length <= 100
Test Case
s="abc"
t="ahbgdc"
Ans true
s="axc"
t="ahbgdc"
Ans false
You are given two positive integers n and k. A factor of an integer n is defined as an integer i where n % i
== 0.
Consider a list of all factors of n sorted in ascending order, return the kth factor in this list or return -1 if
n has less than k factors.
Example 1:
Input: n = 12, k = 3
Output: 3
Example 2:
Input: n = 7, k = 2
Output: 7
Example 3:
Input: n = 4, k = 4
Output: -1
Explanation: Factors list is [1, 2, 4], there is only 3 factors. We should return -1.
Constraints:
Test Case
n=12
k=3
Ans 3
n=7
k=2
Ans 7
n=4
k=4
ANs -1
You are given an array of n strings strs, all of the same length.
The strings can be arranged such that there is one on each line, making a grid.
abc
bce
cae
You want to delete the columns that are not sorted lexicographically. In the above example (0-
indexed), columns 0 ('a', 'b', 'c') and 2 ('c', 'e', 'e') are sorted, while column 1 ('b', 'c', 'a') is not, so you
would delete column 1.
Output: 1
cba
daf
ghi
Columns 0 and 2 are sorted, but column 1 is not, so you only need to delete 1 column.
Example 2:
Output: 0
Column 0 is the only column and is sorted, so you will not delete any columns.
Example 3:
Output: 3
zyx
wvu
tsr
Constraints:
n == strs.length
["cba","daf","ghi"]
Ans 1
["a","b"]
Ans 0
["zyx","wvu","tsr"]
Ans 3
You are given a 0-indexed integer array arr and an integer k. The array arr is circular. In other words, the
first element of the array is the next element of the last element, and the last element of the array is the
previous element of the first element.
Return the minimum number of operations such that the sum of each subarray of length k is equal.
Example 1:
Output: 1
Example 2:
Input: arr = [2,5,5,7], k = 3
Output: 5
Explanation: we can do three operations on index 0 to make its value equal to 5 and two operations on
index 3 to make its value equal to 5.
Constraints:
Test Case
[1,4,1,3]
k=2
Ans 1
[2,5,5,7]
k=3
Ans 5
Given an integer array nums, return the largest perimeter of a triangle with a non-zero area, formed
from three of these lengths. If it is impossible to form any triangle of a non-zero area, return 0.
Example 1:
Output: 5
Explanation: You can form a triangle with three side lengths: 1, 2, and 2.
Example 2:
Output: 0
Explanation:
As we cannot use any three side lengths to form a triangle of non-zero area, we return 0.
Constraints:
Test Cases
[2,1,2]
Ans 5
[1,2,1,10]
Ans 0
Given an integer array nums and an integer k, modify the array in the following way:
You should apply this process exactly k times. You may choose the same index i multiple times.
Return the largest possible sum of the array after modifying it in this way.
Example 1:
Output: 5
Output: 6
Example 3:
Output: 13
Constraints:
Test Cases
[4,2,3]
K=1
Ans 5
[3,-1,0,2]
K=3
Ans 6
[2,-3,-1,5,-4]
K=2
Ans 13
Given an array of distinct integers candidates and a target integer target, return a list of all unique
combinations of candidates where the chosen numbers sum to target. You may return the
combinations in any order.
The same number may be chosen from candidates an unlimited number of times. Two combinations
are unique if the
frequency
The test cases are generated such that the number of unique combinations that sum up to target is less
than 150 combinations for the given input.
Example 1:
Output: [[2,2,3],[7]]
Explanation:
2 and 3 are candidates, and 2 + 2 + 3 = 7. Note that 2 can be used multiple times.
7 is a candidate, and 7 = 7.
Example 2:
Output: [[2,2,2,2],[2,3,3],[3,5]]
Example 3:
Output: []
Constraints:
Test cases:
candidates = [2, 3, 6, 7]
target = 7
Output: [[2, 2, 3], [7]]
candidates = [2, 3, 5]
target = 8
candidates = [2]
target = 1
Output: []
You are given an integer array height of length n. There are n vertical lines drawn such that the two
endpoints of the ith line are (i, 0) and (i, height[i]).
Find two lines that together with the x-axis form a container, such that the container contains the most
water.
Example 1:
Explanation: The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max
area of water (blue section) the container can contain is 49.
Example 2:
Output: 1
Constraints:
n == height.length
Test Cases:
height = [1, 8, 6, 2, 5, 4, 8, 3, 7]
49
height = [5, 5, 5, 5, 5]
20
height = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
45
Given an integer array nums, return true if there exists a triple of indices (i, j, k) such that i < j <
k and nums[i] < nums[j] < nums[k]. If no such indices exists, return false.
Example 1:
Output: true
Output: false
Example 3:
Output: true
Explanation: The triplet (3, 4, 5) is valid because nums[3] == 0 < nums[4] == 4 < nums[5] == 6.
Constraints:
Other cases:
nums = [1, 2, 3, 4, 5]
True
nums = [5, 4, 3, 2, 1]
False
True
On a 2D plane, there are n points with integer coordinates points[i] = [xi, yi]. Return the minimum
time in seconds to visit all the points in the order given by points.
You have to visit the points in the same order as they appear in the array.
You are allowed to pass through points that appear later in the order, but these do not count
as visits.
Example 1:
Output: 7
Explanation: One optimal path is [1,1] -> [2,2] -> [3,3] -> [3,4] -> [2,3] -> [1,2] -> [0,1] -> [-1,0]
Example 2:
Output: 5
Constraints:
points.length == n
points[i].length == 2
Test Cases
[[1,1],[3,4],[-1,0]]
Ans 7
[[3,2],[-2,2]]
Ans 5
Example 1:
Input: n = 16
Output: true
Example 2:
Input: n = 5
Output: false
Example 3:
Input: n = 1
Output: true
Constraints:
Test Cases
N=16
Ans true
N=5
Ans false
N=1
Ans true
Given an integer array nums, return the greatest common divisor of the smallest number and
largest number in nums.
The greatest common divisor of two numbers is the largest positive integer that evenly divides both
numbers.
Example 1:
Output: 2
Explanation:
Example 2:
Explanation:
Example 3:
Output: 3
Explanation:
Constraints:
Test Cases
[2,5,6,9,10]
Ans 2
[7,5,6,8,3]
Ans 1
[3,3]
Ans 3
Given the integer n, return any beautiful array nums of length n. There will be at least one valid
answer for the given n.
Example 1:
Input: n = 4
Output: [2,1,4,3]
Example 2:
Input: n = 5
Output: [3,1,2,5,4]
Constraints:
Test Cases
Ans [3,1,2,4]
Ans [3,5,1,2,4]
Given an array arr of integers, check if there exist two indices i and j such that:
i != j
arr[i] == 2 * arr[j]
Example 1:
Output: true
Explanation: For i = 0 and j = 2, arr[i] == 10 == 2 * 5 == 2 * arr[j]
Example 2:
Output: false
Constraints:
Test Case
[10,2,5,3]
Ans true
[3,1,7,11]
Ans false