0% found this document useful (0 votes)
9 views59 pages

L2 Questions Final

The document outlines various coding problems, including operations with a broken calculator, counting islands in a grid, maximizing sightseeing scores, and sorting colors. Each problem is accompanied by example inputs and outputs, constraints, and test cases. The problems cover a range of algorithms and data structures suitable for coding interviews.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views59 pages

L2 Questions Final

The document outlines various coding problems, including operations with a broken calculator, counting islands in a grid, maximizing sightseeing scores, and sorting colors. Each problem is accompanied by example inputs and outputs, constraints, and test cases. The problems cover a range of algorithms and data structures suitable for coding interviews.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 59

Question 1)

991 Broken Calculator

There is a broken calculator that has the integer startValue on its display initially. In one operation, you
can:

 multiply the number on display by 2, or

 subtract 1 from the number on display.

Given two integers startValue and target, return the minimum number of operations needed to
display target on the calculator.

Example 1:

Input: startValue = 2, target = 3

Output: 2

Explanation: Use double operation and then decrement operation {2 -> 4 -> 3}.

Example 2:

Input: startValue = 5, target = 8

Output: 2

Explanation: Use decrement and then double {5 -> 4 -> 8}.

Example 3:

Input: startValue = 3, target = 10

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

● 1 <= m, n <= 300

● grid[i][j] is '0' or '1'.

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

Hidden Test Case

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.

Return the maximum score of a pair of sightseeing spots.

Example 1:

Input: values = [8,1,5,2,6]

Output: 11

Explanation: i = 0, j = 2, values[i] + values[j] + i - j = 8 + 5 + 0 - 2 = 11

Example 2:

Input: values = [1,2]

Output: 2

Constraints:

● 2 <= values.length <= 5 * 104

● 1 <= values[i] <= 1000

Test cases

values = [10, 9, 8, 7, 6]

Ans 17

values = [1, 3, 5, 9, 2, 1]

Ans 12

Hidden Test Case

values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

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]:

 numberOfBoxesi is the number of boxes of type i.

 numberOfUnitsPerBoxi is the number of units in each box of the type i.

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:

Input: boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4

Output: 8

Explanation: There are:

- 1 box of the first type that contains 3 units.

- 2 boxes of the second type that contain 2 units each.

- 3 boxes of the third type that contain 1 unit each.

You can take all the boxes of the first and second types, and one box of the third type.

The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8.

Example 2:

Input: boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10

Output: 91

Constraints:

 1 <= boxTypes.length <= 1000

 1 <= numberOfBoxesi, numberOfUnitsPerBoxi <= 1000

 1 <= truckSize <= 106

Test Cases

[[1,3],[2,2],[3,1]]
Trucksize=4

Ans 8

[[5,10],[2,5],[4,7],[3,9]]

Trucksize=10

Ans 91

Hidden Test case

[[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:

Input: temperatures = [73,74,75,71,69,72,76,73]

Output: [1,1,4,2,1,1,0,0]

Example 2:

Input: temperatures = [30,40,50,60]

Output: [1,1,1,0]

Example 3:

Input: temperatures = [30,60,90]

Output: [1,1,0]

Constraints:

● 1 <= temperatures.length <= 105

● 30 <= temperatures[i] <= 100


Test Cases:

temperatures = [50,50,50,50]

Ans [0,0,0,0]

temperatures = [30, 40, 50, 60, 50, 40, 30]

Ans [1,1,1,0,0,0,0]

Hidden Test Case

temperatures = [30, 35, 40, 45, 50, 40, 30]

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:

Input: nums = [2,0,2,1,1,0]

Output: [0,0,1,1,2,2]

Example 2:

Input: nums = [2,0,1]

Output: [0,1,2]

Constraints:

● n == nums.length

● 1 <= n <= 300

● 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]

Hidden Test Case

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

Given an input string s, reverse the order of the words.

A word is defined as a sequence of non-space characters. The words in s will be separated by at least
one space.

Return a string of the words in reverse order concatenated by a single 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:

Input: s = "the sky is blue"

Output: "blue is sky the"

Example 2:

Input: s = " hello world "

Output: "world hello"

Explanation: Your reversed string should not contain leading or trailing spaces.

Example 3:

Input: s = "a good example"


Output: "example good a"

Explanation: You need to reduce multiple spaces between two words to a single space in the reversed
string.

Constraints:

● 1 <= s.length <= 104

● s contains English letters (upper-case and lower-case), digits, and spaces ' '.

● There is at least one word in s.

Test Cases:

s = "hello"

Ans "hello"

s = " this is a test "

Ans "test a is this"

Hidden Test Case

s = "one two three four five"

Ans "five four three two one"

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:

Input: flowerbed = [1,0,0,0,1], n = 1

Output: true

Example 2:
Input: flowerbed = [1,0,0,0,1], n = 2

Output: false

Constraints:

● 1 <= flowerbed.length <= 2 * 104

● flowerbed[i] is 0 or 1.

● There are no two adjacent flowers in flowerbed.

● 0 <= n <= flowerbed.length

Test Cases:

flowerbed = [0,0,0,0,0], n = 2

Ans true

flowerbed = [1,0,1,0,1], n = 1

Ans false

Hidden Test Case

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:

Input: s1 = "bank", s2 = "kanb"

Output: true

Explanation: For example, swap the first character with the last character of s2 to make "bank".
Example 2:

Input: s1 = "attack", s2 = "defend"

Output: false

Explanation: It is impossible to make them equal with one string swap.

Example 3:

Input: s1 = "kelb", s2 = "kelb"

Output: true

Explanation: The two strings are already equal, so no string swap operation is required.

Constraints:

● 1 <= s1.length, s2.length <= 100

● s1.length == s2.length

● s1 and s2 consist of only lowercase English letters.

Test Cases:

s1 = "abcd"

s2 = "abcd"

Ans true

s1 = "abcd"

s2 = "badc"

Ans false

Hidden Test Case

s1 = "converse"

s2 = "conevrse"

Ans true

11. 121 best-time-to-buy-and-sell-stock


You are given an array prices where prices[i] is the price of a given stock on the i th day.
You want to maximize your profit by choosing a single day to buy one stock and choosing a
different day in the future to sell that stock.

Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit,
return 0.

Example 1:

Input: prices = [7,1,5,3,6,4]

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:

Input: prices = [7,6,4,3,1]

Output: 0

Explanation: In this case, no transactions are done and the max profit = 0.

Constraints:

 1 <= prices.length <= 105

 0 <= prices[i] <= 104

Test Case
[7,1,5,3,6,4]

Ans 5

[7,6,4,3,1]

Ans 0

Hidden Test Case

[3,2,6,5,0,3]

Ans 4

12. 283 Move Zeroes


Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the
non-zero elements.
Note that you must do this in-place without making a copy of the array.

Example 1:

Input: nums = [0,1,0,3,12]

Output: [1,3,12,0,0]

Example 2:

Input: nums = [0]

Output: [0]

Example 2:

Input: nums = [0]

Output: [0]

Constraints:

 1 <= nums.length <= 104

 -231 <= nums[i] <= 231 - 1

Test Case

[0,1,0,3,12]

Ans [1,3,12,0,0]

[0]

Ans [0]

Hidden Test Case

[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:

Input: nums = [-1,0,1,2,-1,-4]

Output: [[-1,-1,2],[-1,0,1]]

Explanation:

nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.

nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.

nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.

The distinct triplets are [-1,0,1] and [-1,-1,2].

Notice that the order of the output and the order of the triplets does not matter.

Example 2:

Input: nums = [0,1,1]

Output: []

Explanation: The only possible triplet does not sum up to 0.

Example 3:

Input: nums = [0,0,0]

Output: [[0,0,0]]

Explanation: The only possible triplet sums up to 0.

Constraints:

 3 <= nums.length <= 3000

 -105 <= nums[i] <= 105


Test Case

[-1,0,1,2,-1,-4]

Ans [[-1,-1,2],[-1,0,1]]

[0,1,1]

Ans []

Hidden Test Case

[0,0,0]

Ans [[0,0,0]]

14. 162 Find Peak Element


A peak element is an element that is strictly greater than its neighbors.

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

You must write an algorithm that runs in O(log n) time.

Example 1:

Input: nums = [1,2,3,1]

Output: 2

Explanation: 3 is a peak element and your function should return the index number 2.

Example 2:

Input: nums = [1,2,1,3,5,6,4]

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

 -231 <= nums[i] <= 231 - 1

 nums[i] != nums[i + 1] for all valid i.

Test Case

[1,2,3,1]

Ans 2

[1,2,1,3,5,6,4]

ANs 5

hidden

[1,2,3,1]

Ans 2

15. 414 Third Maximum Number


Given an integer array nums, return the third distinct maximum number in this array. If the third
maximum does not exist, return the maximum number.

Example 1:

Input: nums = [3,2,1]

Output: 1

Explanation:

The first distinct maximum is 3.

The second distinct maximum is 2.

The third distinct maximum is 1.

Example 2:

Input: nums = [1,2]


Output: 2

Explanation:

The first distinct maximum is 2.

The second distinct maximum is 1.

The third distinct maximum does not exist, so the maximum (2) is returned instead.

Example 3:

Input: nums = [2,2,3,1]

Output: 1

Explanation:

The first distinct maximum is 3.

The second distinct maximum is 2 (both 2's are counted together since they have the same value).

The third distinct maximum is 1.

Constraints:

 1 <= nums.length <= 104

 -231 <= nums[i] <= 231 - 1

Test Cases

[3,2,1]

Ans 1

[1,2]

Ans 2

Hidden Test Case

[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:

Input: gas = [1,2,3,4,5], cost = [3,4,5,1,2]

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 4. Your tank = 4 - 1 + 5 = 8

Travel to station 0. Your tank = 8 - 2 + 1 = 7

Travel to station 1. Your tank = 7 - 3 + 2 = 6

Travel to station 2. Your tank = 6 - 4 + 3 = 5

Travel to station 3. The cost is 5. Your gas is just enough to travel back to station 3.

Therefore, return 3 as the starting index.

Example 2:

Input: gas = [2,3,4], cost = [3,4,3]

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

Travel to station 0. Your tank = 4 - 3 + 2 = 3

Travel to station 1. Your tank = 3 - 3 + 3 = 3

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

 1 <= n <= 105

 0 <= gas[i], cost[i] <= 10

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

17. 274 H-index


Given an array of integers citations where citations[i] is the number of citations
a researcher received for their ith paper, return the researcher's h-index.
According to the definition of h-index on Wikipedia: The h-index is defined as
the maximum value of h such that the given researcher has published at
least h papers that have each been cited at least h times.

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

18. 306 Additive Number


An additive number is a string whose digits can form an additive sequence.

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:

The digits can form an additive sequence: 1, 1, 2, 3, 5, 8.

1 + 1 = 2, 1 + 2 = 3, 2 + 3 = 5, 3 + 5 = 8

Example 2:

Input: "199100199"

Output: true

Explanation:

The additive sequence is: 1, 99, 100, 199.

1 + 99 = 100, 99 + 100 = 199

Constraints:

 1 <= num.length <= 35

 num consists only of digits.

Test Case

"112358"

Ans true

"199100199"

ANs true

Hidden

"123456789"

Ans false

19. 2383. Minimum Hours of Training to Win a Competition


You are entering a competition, and are given
two positive integers initialEnergy and initialExperience denoting your initial energy and initial
experience respectively.

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:

Input: initialEnergy = 5, initialExperience = 3, energy = [1,4,3,2], experience = [2,6,3,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 face the opponents in the following order:

- You have more energy and experience than the 0th opponent so you win.

Your energy becomes 11 - 1 = 10, and your experience becomes 5 + 2 = 7.

- You have more energy and experience than the 1st opponent so you win.

Your energy becomes 10 - 4 = 6, and your experience becomes 7 + 6 = 13.

- You have more energy and experience than the 2nd opponent so you win.

Your energy becomes 6 - 3 = 3, and your experience becomes 13 + 3 = 16.

- You have more energy and experience than the 3rd opponent so you win.

Your energy becomes 3 - 2 = 1, and your experience becomes 16 + 1 = 17.

You did a total of 6 + 2 = 8 hours of training before the competition, so we return 8.

It can be proven that no smaller answer exists.

Example 2:

Input: initialEnergy = 2, initialExperience = 4, energy = [1], experience = [3]

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

 1 <= initialEnergy, initialExperience, energy[i], experience[i] <= 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

Hidden Test Case

initialEnergy=5

initialExperience=3

energy= [1,4,3]

experience= [2,6,8]

Ans 6

20. 383 Ransom Note


Given two strings ransomNote and magazine, return true if ransomNote can be constructed by using
the letters from magazine and false otherwise.

Each letter in magazine can only be used once in ransomNote.

Example 1:

Input: ransomNote = "a", magazine = "b"

Output: false
Example 2:

Input: ransomNote = "aa", magazine = "ab"

Output: false

Example 3:

Input: ransomNote = "aa", magazine = "aab"

Output: true

Test Case

ransomNote=”a”

magazine=”b”

ans false

ransomNote=”aa”

magazine=”ab”

ans false

Hidden Test Case

ransomNote=”aa”

magazine=”aab”

Ans true

21.1436 Destination City

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:

Input: paths = [["London","New York"],["New York","Lima"],["Lima","Sao Paulo"]]

Output: "Sao Paulo"


Explanation: Starting at "London" city you will reach "Sao Paulo" city which is the destination city. Your
trip consist of: "London" -> "New York" -> "Lima" -> "Sao Paulo".

Example 2:

Input: paths = [["B","C"],["D","B"],["C","A"]]

Output: "A"

Explanation: All possible trips are:

"D" -> "B" -> "C" -> "A".

"B" -> "C" -> "A".

"C" -> "A".

"A".

Clearly the destination city is "A".

Example 3:

Input: paths = [["A","Z"]]

Output: "Z"

Constraints:

 1 <= paths.length <= 100

 paths[i].length == 2

 1 <= cityAi.length, cityBi.length <= 10

 cityAi != cityBi

 All strings consist of lowercase and uppercase English letters and the space character.

Test Cases

[["London","New York"],["New York","Lima"],["Lima","Sao Paulo"]]

Ans "Sao Paulo"

[["B","C"],["D","B"],["C","A"]]

Ans “A”

Hidden Test Case

[["A","Z"]]

Ans “Z”
22. 74 :- Search a 2D Matrix

You are given an m x n integer matrix matrix with the following properties:

● Each row is sorted in non-decreasing order.

● 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:

Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3

Output: true

Example 2:

Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13


Output: false

Constraints:

 m == matrix.length

 n == matrix[i].length

 1 <= m, n <= 100

 -104 <= matrix[i][j], target <= 104

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

Hidden Test Case

matrix = [

[2, 4, 6, 8],

[9, 12, 14, 18],

[21, 24, 27, 30],

[33, 36, 39, 42]

target = 25

Ans False

23. 8 :- String to Integer (atoi)

Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer.

The algorithm for myAtoi(string s) is as follows:

1. Whitespace: Ignore any leading whitespace (" ").


2. Signedness: Determine the sign by checking if the next character is '-' or '+', assuming positivity
if neither present.

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.

Return the integer as the final result.

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 1: "42" (no characters read because there is no leading whitespace)

Step 2: "42" (no characters read because there is neither a '-' nor '+')

Step 3: "42" ("42" is read in)

Example 2:

Input: s = " -042"

Output: -42

Explanation:

Step 1: " -042" (leading whitespace is read and ignored)

Step 2: " -042" ('-' is read, so the result should be negative)

Step 3: " -042" ("042" is read in, leading zeros ignored in the result)

^
Example 3:

Input: s = "1337c0d3"

Output: 1337

Explanation:

Step 1: "1337c0d3" (no characters read because there is no leading whitespace)

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 1: "0-1" (no characters read because there is no leading whitespace)

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:

Input: s = "words and 987"

Output: 0

Explanation:

Reading stops at the first non-digit character 'w'.

Constraints:

 0 <= s.length <= 200

 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

Hidden Test Case

"words and 987"

Ans 0

24. 125 :- Valid Palindrome

A phrase is a palindrome if it reads the same forward and backward, ignoring cases, spaces, and non-
alphanumeric characters.

Given a string s, return true if it is a palindrome, otherwise return false.

Example 1:

Input: s = "A man, a plan, a canal: Panama"

Output: true

Explanation: "amanaplanacanalpanama" is a palindrome.

Example 2:

Input: s = "race a car"

Output: false

Explanation: "raceacar" is not a palindrome.

Example 3:

Input: s = " "


Output: true

Explanation: s is an empty string "" after removing non-alphanumeric characters.

Since an empty string reads the same forward and backward, it is a palindrome.

Constraints:

 1 <= s.length <= 2 * 105

 s consists only of printable ASCII characters.

Test Case

"A man, a plan, a canal: Panama"

Ans true

"race a car"

Ans False

Hidden Test Case

""

Ans true

25. 1047 Remove All Adjacent Duplicates In String

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.

We repeatedly make duplicate removals on s until we no longer can.

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:

 1 <= s.length <= 105

 s consists of lowercase English letters.

Test Case

"abbaca"

Ans “ca”

"azxxzy"

Ans “ay”

26. 744. Find Smallest Letter Greater Than Target

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:

Input: letters = ["c","f","j"], target = "a"

Output: "c"

Explanation: The smallest character that is lexicographically greater than 'a' in letters is 'c'.

Example 2:

Input: letters = ["c","f","j"], target = "c"

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:

 2 <= letters.length <= 104

 letters[i] is a lowercase English letter.

 letters is sorted in non-decreasing order.

 letters contains at least two different characters.

 target is a lowercase English letter.

Test Cases

["c","f","j"]

Target=”a”

Ans “c”

["c","f","j"]

Target=”c”

Ans “f”

Hidden Test Case

["x","x","y","y"]

Target=”z”

Ans “x”

27. 500 Keyboard Row

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.

In the American keyboard:


● the first row consists of the characters "qwertyuiop",

● the second row consists of the characters "asdfghjkl", and

● the third row consists of the characters "zxcvbnm".

Example 1:

Input: words = ["Hello","Alaska","Dad","Peace"]

Output: ["Alaska","Dad"]

Explanation:

Both "a" and "A" are in the 2nd row of the American keyboard due to case insensitivity.

Example 2:

Input: words = ["omk"]

Output: []

Example 3:

Input: words = ["adsdf","sfd"]

Output: ["adsdf","sfd"]

Constraints:

 1 <= words.length <= 20

 1 <= words[i].length <= 100

 words[i] consists of English letters (both lowercase and uppercase).


Test Case

["Hello","Alaska","Dad","Peace"]

Ans ["Alaska","Dad"]

["omk"]

Ans [ ]

Hidden Test Case

["adsdf","sfd"]

ANs ["adsdf","sfd"]

28. 819. Most Common Word

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:

"hit" occurs 3 times, but it is a banned word.

"ball" occurs twice (and no other word does), so it is the most frequent non-banned word in the
paragraph.

Note that words in the paragraph are not case sensitive,

that punctuation is ignored (even if adjacent to words, such as "ball,"),

and that "hit" isn't the answer even though it occurs more because it is banned.

Example 2:

Input: paragraph = "a.", banned = []

Output: "a"

Constraints:

 1 <= paragraph.length <= 1000


 paragraph consists of English letters, space ' ', or one of the symbols: "!?',;.".

 0 <= banned.length <= 100

 1 <= banned[i].length <= 10

 banned[i] consists of only lowercase English letters.

Test Cases

"Bob hit a ball, the hit BALL flew far after it was hit."

Banned= ["hit"]

Ans “ball”

"a."

Banned= []

Ans “a”

29. 1232 Check if it is a Straight line

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:

Input: coordinates = [[1,1],[2,2],[3,4],[4,5],[5,6],[7,7]]

Output: false
Constraints:

 2 <= coordinates.length <= 1000

 coordinates[i].length == 2

 -10^4 <= coordinates[i][0], coordinates[i][1] <= 10^4

 coordinates contains no duplicate point.

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

30. 506 Relative Ranks

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:

● The 1st place athlete's rank is "Gold Medal".

● The 2nd place athlete's rank is "Silver Medal".

● The 3rd place athlete's rank is "Bronze Medal".

● 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:

Input: score = [5,4,3,2,1]

Output: ["Gold Medal","Silver Medal","Bronze Medal","4","5"]

Explanation: The placements are [1st, 2nd, 3rd, 4th, 5th].

Example 2:

Input: score = [10,3,8,9,4]


Output: ["Gold Medal","5","Bronze Medal","Silver Medal","4"]

Explanation: The placements are [1st, 5th, 3rd, 2nd, 4th].

Constraints:

 n == score.length

 1 <= n <= 104

 0 <= score[i] <= 106

 All the values in score are unique.

Test Case

[5,4,3,2,1]

Ans ["Gold Medal","Silver Medal","Bronze Medal","4","5"]

[10,3,8,9,4]

Ans ["Gold Medal","5","Bronze Medal","Silver Medal","4"]

31. 581 :- Shortest Unsorted Continuous Subarray

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.

Return the shortest such subarray and output its length.

Example 1:

Input: nums = [2,6,4,8,10,9,15]

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:

Input: nums = [1,2,3,4]

Output: 0

Example 3:
Input: nums = [1]

Output: 0

Constraints:

 1 <= nums.length <= 104

 -105 <= nums[i] <= 105

Test Case

[2,6,4,8,10,9,15]

Ans 5

[1,2,3,4]

ANs 0

Hidden Test Case

[1]

ANs 0

32. 392 Is Subsequence

Given two strings s and t, return true if s is a subsequence of t, or false otherwise.

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:

Input: s = "abc", t = "ahbgdc"

Output: true

Example 2:

Input: s = "axc", t = "ahbgdc"

Output: false

Constraints:
 0 <= s.length <= 100

 0 <= t.length <= 104

 s and t consist only of lowercase English letters.

Test Case

s="abc"

t="ahbgdc"

Ans true

s="axc"

t="ahbgdc"

Ans false

33. 1492 The kth factor of n

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

Explanation: Factors list is [1, 2, 3, 4, 6, 12], the 3rd factor is 3.

Example 2:

Input: n = 7, k = 2

Output: 7

Explanation: Factors list is [1, 7], the 2nd factor is 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:

 1 <= k <= n <= 1000

Test Case

n=12

k=3

Ans 3

n=7

k=2

Ans 7

Hidden Test Case

n=4

k=4

ANs -1

34. 944. Delete Columns to Make Sorted

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.

 For example, strs = ["abc", "bce", "cae"] can be arranged as follows:

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.

Return the number of columns that you will delete.


Example 1:

Input: strs = ["cba","daf","ghi"]

Output: 1

Explanation: The grid looks as follows:

cba

daf

ghi

Columns 0 and 2 are sorted, but column 1 is not, so you only need to delete 1 column.

Example 2:

Input: strs = ["a","b"]

Output: 0

Explanation: The grid looks as follows:

Column 0 is the only column and is sorted, so you will not delete any columns.

Example 3:

Input: strs = ["zyx","wvu","tsr"]

Output: 3

Explanation: The grid looks as follows:

zyx

wvu

tsr

All 3 columns are not sorted, so you will delete all 3.

Constraints:

 n == strs.length

 1 <= n <= 100

 1 <= strs[i].length <= 1000

 strs[i] consists of lowercase English letters.


Test Cases

["cba","daf","ghi"]

Ans 1

["a","b"]

Ans 0

Hidden Test Case

["zyx","wvu","tsr"]

Ans 3

35. 2607 :- Make K-Subarray Sums Equal

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.

You can do the following operation any number of times:

● Pick any element from arr and increase or decrease it by 1.

Return the minimum number of operations such that the sum of each subarray of length k is equal.

A subarray is a contiguous part of the array.

Example 1:

Input: arr = [1,4,1,3], k = 2

Output: 1

Explanation: we can do one operation on index 1 to make its value equal to 3.

The array after the operation is [1,3,1,3]

- Subarray starts at index 0 is [1, 3], and its sum is 4

- Subarray starts at index 1 is [3, 1], and its sum is 4

- Subarray starts at index 2 is [1, 3], and its sum is 4

- Subarray starts at index 3 is [3, 1], and its sum is 4

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.

The array after the operations is [5,5,5,5]

- Subarray starts at index 0 is [5, 5, 5], and its sum is 15

- Subarray starts at index 1 is [5, 5, 5], and its sum is 15

- Subarray starts at index 2 is [5, 5, 5], and its sum is 15

- Subarray starts at index 3 is [5, 5, 5], and its sum is 15

Constraints:

 1 <= k <= arr.length <= 105

 1 <= arr[i] <= 109

Test Case

[1,4,1,3]

k=2

Ans 1

[2,5,5,7]

k=3

Ans 5

36. 976. Largest Perimeter Triangle

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:

Input: nums = [2,1,2]

Output: 5
Explanation: You can form a triangle with three side lengths: 1, 2, and 2.

Example 2:

Input: nums = [1,2,1,10]

Output: 0

Explanation:

You cannot use the side lengths 1, 1, and 2 to form a triangle.

You cannot use the side lengths 1, 1, and 10 to form a triangle.

You cannot use the side lengths 1, 2, and 10 to form a triangle.

As we cannot use any three side lengths to form a triangle of non-zero area, we return 0.

Constraints:

 3 <= nums.length <= 104

 1 <= nums[i] <= 106

Test Cases

[2,1,2]

Ans 5

[1,2,1,10]

Ans 0

37. 1005. Maximize Sum Of Array After K Negations

Given an integer array nums and an integer k, modify the array in the following way:

 choose an index i and replace nums[i] with -nums[i].

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:

Input: nums = [4,2,3], k = 1

Output: 5

Explanation: Choose index 1 and nums becomes [4,-2,3].


Example 2:

Input: nums = [3,-1,0,2], k = 3

Output: 6

Explanation: Choose indices (1, 2, 2) and nums becomes [3,1,0,2].

Example 3:

Input: nums = [2,-3,-1,5,-4], k = 2

Output: 13

Explanation: Choose indices (1, 4) and nums becomes [2,3,-1,5,4].

Constraints:

 1 <= nums.length <= 104

 -100 <= nums[i] <= 100

 1 <= k <= 104

Test Cases

[4,2,3]

K=1

Ans 5

[3,-1,0,2]

K=3

Ans 6

Hidden Test Case

[2,-3,-1,5,-4]

K=2

Ans 13

38. 39. Combination Sum

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

of at least one of the chosen numbers is different.

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:

Input: candidates = [2,3,6,7], target = 7

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.

These are the only two combinations.

Example 2:

Input: candidates = [2,3,5], target = 8

Output: [[2,2,2,2],[2,3,3],[3,5]]

Example 3:

Input: candidates = [2], target = 1

Output: []

Constraints:

 1 <= candidates.length <= 30

 2 <= candidates[i] <= 40

 All elements of candidates are distinct.

 1 <= target <= 40

Test cases:

candidates = [2, 3, 6, 7]

target = 7
Output: [[2, 2, 3], [7]]

candidates = [2, 3, 5]

target = 8

Output: [[2, 2, 2, 2], [2, 3, 3], [3, 5]]

Hidden Test Case

candidates = [2]

target = 1

Output: []

39. Container With Most Water

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.

Return the maximum amount of water a container can store.

Notice that you may not slant the container.

Example 1:

Input: height = [1,8,6,2,5,4,8,3,7]


Output: 49

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:

Input: height = [1,1]

Output: 1

Constraints:

 n == height.length

 2 <= n <= 105

 0 <= height[i] <= 104

Test Cases:

height = [1, 8, 6, 2, 5, 4, 8, 3, 7]

49

height = [5, 5, 5, 5, 5]

20

Hidden Test Case

height = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

45

40. 334. Increasing Triplet Subsequence

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:

Input: nums = [1,2,3,4,5]

Output: true

Explanation: Any triplet where i < j < k is valid.


Example 2:

Input: nums = [5,4,3,2,1]

Output: false

Explanation: No triplet exists.

Example 3:

Input: nums = [2,1,5,0,4,6]

Output: true

Explanation: The triplet (3, 4, 5) is valid because nums[3] == 0 < nums[4] == 4 < nums[5] == 6.

Constraints:

 1 <= nums.length <= 5 * 105

 -231 <= nums[i] <= 231 – 1

Other cases:

nums = [1, 2, 3, 4, 5]

True

nums = [5, 4, 3, 2, 1]

False

Hidden Test Case

nums = [20, 100, 10, 12, 5, 13, 15]

True

41. 1266. Minimum Time Visiting All Points

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 can move according to these rules:

 In 1 second, you can either:

 move vertically by one unit,

 move horizontally by one unit, or


 move diagonally sqrt(2) units (in other words, move one unit vertically then one unit
horizontally in 1 second).

 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:

Input: points = [[1,1],[3,4],[-1,0]]

Output: 7

Explanation: One optimal path is [1,1] -> [2,2] -> [3,3] -> [3,4] -> [2,3] -> [1,2] -> [0,1] -> [-1,0]

Time from [1,1] to [3,4] = 3 seconds


Time from [3,4] to [-1,0] = 4 seconds

Total time = 7 seconds

Example 2:

Input: points = [[3,2],[-2,2]]

Output: 5

Constraints:

 points.length == n

 1 <= n <= 100

 points[i].length == 2

 -1000 <= points[i][0], points[i][1] <= 1000

Test Cases

[[1,1],[3,4],[-1,0]]

Ans 7

[[3,2],[-2,2]]

Ans 5

42. 342. Power of Four

Given an integer n, return true if it is a power of four. Otherwise, return false.

An integer n is a power of four, if there exists an integer x such that n == 4x.

Example 1:

Input: n = 16

Output: true

Example 2:

Input: n = 5

Output: false

Example 3:

Input: n = 1
Output: true

Constraints:

 -231 <= n <= 231 - 1

Test Cases

N=16

Ans true

N=5

Ans false

Hidden Test Case

N=1

Ans true

43. 1979. Find Greatest Common Divisor of Array

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:

Input: nums = [2,5,6,9,10]

Output: 2

Explanation:

The smallest number in nums is 2.

The largest number in nums is 10.

The greatest common divisor of 2 and 10 is 2.

Example 2:

Input: nums = [7,5,6,8,3]


Output: 1

Explanation:

The smallest number in nums is 3.

The largest number in nums is 8.

The greatest common divisor of 3 and 8 is 1.

Example 3:

Input: nums = [3,3]

Output: 3

Explanation:

The smallest number in nums is 3.

The largest number in nums is 3.

The greatest common divisor of 3 and 3 is 3.

Constraints:

 2 <= nums.length <= 1000

 1 <= nums[i] <= 1000

Test Cases

[2,5,6,9,10]

Ans 2

[7,5,6,8,3]

Ans 1

Hidden test case

[3,3]

Ans 3

44. 932. Beautiful Array

An array nums of length n is beautiful if:

 nums is a permutation of the integers in the range [1, n].


 For every 0 <= i < j < n, there is no index k with i < k < j where 2 * nums[k] == nums[i] +
nums[j].

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:

 1 <= n <= 1000

Test Cases

Ans [3,1,2,4]

Ans [3,5,1,2,4]

45. 1346. Check If N and Its Double Exist

Given an array arr of integers, check if there exist two indices i and j such that:

 i != j

 0 <= i, j < arr.length

 arr[i] == 2 * arr[j]

Example 1:

Input: arr = [10,2,5,3]

Output: true
Explanation: For i = 0 and j = 2, arr[i] == 10 == 2 * 5 == 2 * arr[j]

Example 2:

Input: arr = [3,1,7,11]

Output: false

Explanation: There is no i and j that satisfy the conditions.

Constraints:

 2 <= arr.length <= 500

 -103 <= arr[i] <= 103

Test Case

[10,2,5,3]

Ans true

[3,1,7,11]

Ans false

You might also like