Infosys Leet Code
Infosys Leet Code
Two Sum
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example 1:
Output: [0,1]
Example 2:
Output: [1,2]
Example 3:
Output: [0,1]
Constraints:
Follow-up: Can you come up with an algorithm that is less than O(n ) time complexity?
2
Example 1:
Input: s = "abcabcbb"
Output: 3
Example 2:
Input: s = "bbbbb"
Output: 1
Input: s = "pwwkew"
Output: 3
Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
Constraints:
Example 1:
Input: s = "babad"
Output: "bab"
Example 2:
Input: s = "cbbd"
Output: "bb"
Constraints:
7. Reverse Integer
Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-2 , 2
31 31
- 1], then
return 0.
Assume the environment does not allow you to store 64-bit integers (signed or unsigned).
Example 1:
Input: x = 123
Output: 321
Example 2:
Input: x = -123
Output: -321
Example 3:
Input: x = 120
Output: 21
Constraints:
-231
<= x <= 2
31
- 1
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:
0 <= a, b, c, d < n
a, b, c, and d are distinct.
nums[a] + nums[b] + nums[c] + nums[d] == target
Example 1:
Output: [[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]
Example 2:
Output: [[2,2,2,2]]
Constraints:
Example 1:
Input: s = "()"
Output: true
Example 2:
Input: s = "()[]{}"
Output: true
Example 3:
Input: s = "(]"
Output: false
Constraints:
Example 1:
Input: n = 3
Output: ["((()))","(()())","(())()","()(())","()()()"]
Example 2:
Input: n = 1
Output: ["()"]
Constraints:
1 <= n <= 8
Example 1:
Output: 6
Example 2:
Output: 1
Example 3:
Output: 23
Constraints:
1 <= nums.length <= 10 5
Follow up: If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
You are given an integer array nums. You are initially positioned at the array's first index, and each element in the array represents your maximum jump length at
that position.
Return true if you can reach the last index, or false otherwise.
Example 1:
Output: true
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.
Example 2:
Output: false
Explanation: You will always arrive at index 3 no matter what. Its maximum jump length is 0, which makes it impossible to reach
the last index.
Constraints:
SOLUTIONS:
JAVA
enum Index {
Index[] memo;
public boolean canJumpFromPosition(int position, int[] nums) {
if (memo[position] != Index.UNKNOWN) {
if (canJumpFromPosition(nextPosition, nums)) {
memo[position] = Index.GOOD;
return true;
memo[position] = Index.BAD;
return false;
memo[i] = Index.UNKNOWN;
memo[memo.length - 1] = Index.GOOD;
}
}
Given the two integers m and n, return the number of possible unique paths that the robot can take to reach the bottom-right corner.
The test cases are generated so that the answer will be less than or equal to 2 * 10 .
9
Example 1:
Input: m = 3, n = 7
Output: 28
Example 2:
Input: m = 3, n = 2
Output: 3
Explanation: From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
Constraints:
Example 1:
Input: matrix = [[1,1,1],[1,0,1],[1,1,1]]
Output: [[1,0,1],[0,0,0],[1,0,1]]
Example 2:
Output: [[0,0,0,0],[0,4,5,0],[0,3,1,0]]
Constraints:
m == matrix.length
n == matrix[0].length
1 <= m, n <= 200
-231
<= matrix[i][j] <= 2 31
- 1
Follow up:
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
1 <= n <= 300
nums[i] is either 0, 1, or 2.
Follow up: Could you come up with a one-pass algorithm using only constant extra space?
You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n, representing
the number of elements in nums1 and nums2 respectively.
Merge nums1 and nums2 into a single array sorted in non-decreasing order.
The final sorted array should not be returned by the function, but instead be stored inside the array nums1. To accommodate
this, nums1 has a length of m + n, where the first m elements denote the elements that should be merged, and the
last n elements are set to 0 and should be ignored. nums2 has a length of n.
Example 1:
Output: [1,2,2,3,5,6]
The result of the merge is [1,2,2,3,5,6] with the underlined elements coming from nums1.
Example 2:
Output: [1]
Example 3:
Output: [1]
Note that because m = 0, there are no elements in nums1. The 0 is only there to ensure the merge result
can fit in nums1.
Constraints:
nums1.length == m + n
nums2.length == n
0 <= m, n <= 200
1 <= m + n <= 200
-109 <= nums1[i], nums2[j] <= 109
Follow up: Can you come up with an algorithm that runs in O(m + n) time?
Solutions:
class Solution {
nums1[i + m] = nums2[i];
Arrays.sort(nums1);
Python3
class Solution:
def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
"""
"""
for i in range(n):
nums1[i + m] = nums2[i]
nums1.sort()
In Pascal's triangle, each number is the sum of the two numbers directly above it as shown:
Example 1:
Input: numRows = 5
Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
Example 2:
Input: numRows = 1
Output: [[1]]
Constraints:
1 <= numRows <= 30
You are given an array prices where prices[i] is the price of a given stock on the ith 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:
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:
Solutions:
Java
int maxprofit = 0;
maxprofit = profit;
return maxprofit;
Python3
class Solution:
max_profit = 0
max_profit = profit
return max_profit
Example 1:
Output: true
Example 2:
Output: false
Explanation: "raceacar" is not a palindrome.
Example 3:
Output: true
Since an empty string reads the same forward and backward, it is a palindrome.
Constraints:
The test cases are generated so that the answer will fit in a 32-bit integer.
Example 1:
Output: 6
Example 2:
Output: 0
Constraints:
Table: Employee
+-------------+------+
| id | int |
| salary | int |
+-------------+------+
id is the primary key (column with unique values) for this table.
Each row of this table contains information about the salary of an employee.
Write a solution to find the second highest salary from the Employee table. If there is no second highest salary, return null (return None in Pandas).
Example 1:
Input:
Employee table:
+----+--------+
| id | salary |
+----+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
+----+--------+
Output:
+---------------------+
| SecondHighestSalary |
+---------------------+
| 200 |
+---------------------+
Example 2:
Input:
Employee table:
+----+--------+
| id | salary |
+----+--------+
| 1 | 100 |
+----+--------+
Output:
+---------------------+
| SecondHighestSalary |
+---------------------+
| null |
+---------------------+
Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the
police.
Example 1:
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
Example 2:
Output: 12
Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1).
Constraints:
Note that you must do this in-place without making a copy of the array.
Example 1:
Output: [1,3,12,0,0]
Example 2:
Output: [0]
Constraints:
-231
<= nums[i] <= 2
31
- 1
Follow up: Could you minimize the total number of operations done?
Example 1:
Output: 4
Example 2:
Output: 4
Example 3:
Output: 1
Constraints:
Follow up: Can you come up with an algorithm that runs in O(n log(n)) time complexity?
Example 1:
Input: nums = [5,2,6,1]
Output: [2,1,1,0]
Explanation:
Example 2:
Output: [0]
Example 3:
Output: [0,0]
Constraints:
One envelope can fit into another if and only if both the width and height of one envelope are greater than the other envelope's width and height.
Return the maximum number of envelopes you can Russian doll (i.e., put one inside the other).
Example 1:
Output: 3
Explanation: The maximum number of envelopes you can Russian doll is 3 ([2,3] => [5,4] => [6,7]).
Example 2:
Output: 1
Constraints:
1 <= envelopes.length <= 10 5
envelopes[i].length == 2
1 <= w , h <= 10
i i
5
Example 1:
Input: s = "leetcode"
Output: 0
Example 2:
Input: s = "loveleetcode"
Output: 2
Example 3:
Input: s = "aabb"
Output: -1
Constraints:
Example 1:
Output: true
Example 2:
Output: false
Constraints:
Example 1:
Output: 2
Example 2:
Output: 10
Constraints:
Example 1:
Output: 2
Example 2:
Output: 2
Constraints:
Example 1:
Output: 4
Example 2:
Output: -1
Constraints:
Example 1:
Input: s = "()"
Output: 1
Example 2:
Input: s = "(())"
Output: 2
Example 3:
Input: s = "()()"
Output: 2
Constraints:
2 <= s.length <= 50
s consists of only '(' and ')'.
s is a balanced parentheses string.
There are gas stations along the way. The gas stations are represented as an array stations where stations[i] = [position , fuel ] indicates that
i i
the i gas station is position miles east of the starting position and has fuel liters of gas.
th
i i
The car starts with an infinite tank of gas, which initially has startFuel liters of fuel in it. It uses one liter of gas per one mile that it drives. When the car reaches a
gas station, it may stop and refuel, transferring all the gas from the station into the car.
Return the minimum number of refueling stops the car must make in order to reach its destination. If it cannot reach the destination, return -1.
Note that if the car reaches a gas station with 0 fuel left, the car can still refuel there. If the car reaches the destination with 0 fuel left, it is still considered to have
arrived.
Example 1:
Output: 0
Example 2:
Output: -1
Explanation: We can not reach the target (or even the first gas station).
Example 3:
Output: 2
We drive to position 10, expending 10 liters of fuel. We refuel from 0 liters to 60 liters of gas.
and refuel from 10 liters to 50 liters of gas. We then drive to and reach the target.
Constraints:
arr.length >= 3
There exists some i with 0 < i < arr.length - 1 such that:
o arr[0] < arr[1] < ... < arr[i - 1] < arr[i]
o arr[i] > arr[i + 1] > ... > arr[arr.length - 1]
Example 1:
Output: false
Example 2:
Output: false
Example 3:
Output: true
Constraints:
F(0) = 0, F(1) = 1
Example 1:
Input: n = 2
Output: 1
Example 2:
Input: n = 3
Output: 2
Example 3:
Input: n = 4
Output: 3
Constraints:
0 <= n <= 30
A good array is an array where the number of different integers in that array is exactly k.
Example 1:
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2]
Example 2:
Input: nums = [1,2,1,3,4], k = 3
Output: 3
Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
Constraints:
Return the number of pairs of songs for which their total duration in seconds is divisible by 60. Formally, we want the number of indices i, j such that i <
j with (time[i] + time[j]) % 60 == 0.
Example 1:
Output: 3
Example 2:
Output: 3
Explanation: All three pairs have a total duration of 120, which is divisible by 60.
Constraints:
A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8.
A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days.
Output: 3
Example 2:
Output: 0
Constraints:
Example 1:
Output: [0,1,2,4,8,3,5,6,7]
Example 2:
Output: [1,2,4,8,16,32,64,128,256,512,1024]
Explantion: All integers have 1 bit in the binary representation, you should just sort them in ascending order.
Constraints:
Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at
that index i times.
Do nothing.
Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the
shift operations i times.
Return true if it's possible to convert s into t in no more than k moves, otherwise return false.
Example 1:
Output: true
Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'.
Example 2:
Output: false
Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move.
However, there is no way to shift the other characters in the remaining moves to obtain t from s.
Example 3:
Output: true
Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get
'b'.
Constraints:
you can choose some of your n coins such that their values sum up to x.
Return the maximum number of consecutive integer values that you can make with your coins starting from and including 0.
Note that you may have multiple coins of the same value.
Example 1:
Output: 2
- 0: take []
- 1: take [1]
Example 2:
Output: 8
- 0: take []
- 1: take [1]
- 2: take [1,1]
- 3: take [1,1,1]
- 4: take [4]
- 5: take [4,1]
- 6: take [4,1,1]
- 7: take [4,1,1,1]
Example 3:
Output: 20
Constraints:
coins.length == n
1 <= n <= 4 * 10 4
There are n stones arranged in a row. On each player's turn, while the number of stones is more than one, they will do the following:
1. Choose an integer x > 1, and remove the leftmost x stones from the row.
2. Add the sum of the removed stones' values to the player's score.
3. Place a new stone, whose value is equal to that sum, on the left side of the row.
The game stops when only one stone is left in the row.
The score difference between Alice and Bob is (Alice's score - Bob's score). Alice's goal is to maximize the score difference, and Bob's goal is
the minimize the score difference.
Given an integer array stones of length n where stones[i] represents the value of the i stone from the left, return the score difference between Alice and
th
Example 1:
Output: 5
Explanation:
- Alice removes the first 4 stones, adds (-1) + 2 + (-3) + 4 = 2 to her score, and places a stone of
- Bob removes the first 2 stones, adds 2 + (-5) = -3 to his score, and places a stone of value -3 on
Example 2:
Output: 13
Explanation:
- Alice removes all stones, adds 7 + (-6) + 5 + 10 + 5 + (-2) + (-6) = 13 to her score, and places a
Example 3:
Output: -22
Explanation:
- Alice can only make one move, which is to remove both stones. She adds (-10) + (-12) = -22 to her
score and places a stone of value -22 on the left. stones = [-22].
Constraints:
n == stones.length
2 <= n <= 10 5
You may choose to mutate a single substring of num. To mutate a substring, replace each digit num[i] with the digit it maps to in change (i.e.
replace num[i] with change[num[i]]).
Return a string representing the largest possible integer after mutating (or choosing not to) a single substring of num.
Example 1:
Output: "832"
- 1 maps to change[1] = 8.
Example 2:
Output: "934"
- 0 maps to change[0] = 9.
- 2 maps to change[2] = 3.
- 1 maps to change[1] = 4.
Example 3:
Output: "5"
Explanation: "5" is already the largest number that can be created, so return it.
Constraints:
Example 1:
Output: 2
The absolute difference between the sums of the arrays is abs((3 + 9) - (7 + 3)) = 2.
Example 2:
Output: 72
The absolute difference between the sums of the arrays is abs((-36) - (36)) = 72.
Example 3:
Output: 0
The absolute difference between the sums of the arrays is abs((2 + 4 + -9) - (-1 + 0 + -2)) = 0.
Constraints:
1 <= n <= 15
nums.length == 2 * n
-10 <= nums[i] <= 10
7 7
2233. Maximum Product After K Increments
You are given an array of non-negative integers nums and an integer k. In one operation, you may choose any element from nums and increment it by 1.
Return the maximum product of nums after at most k operations. Since the answer may be very large, return it modulo 10 + 7. Note that you should maximize
9
Example 1:
Output: 20
Note that there may be other ways to increment nums to have the maximum product.
Example 2:
Output: 216
Explanation: Increment the second number 1 time and increment the fourth number 1 time.
Note that there may be other ways to increment nums to have the maximum product.
Constraints:
A 0-indexed integer array arr of length n is considered ideal if the following conditions hold:
Return the number of distinct ideal arrays of length n. Since the answer may be very large, return it modulo 10 + 7.
9
Example 1:
Input: n = 2, maxValue = 5
Output: 10
Explanation: The following are the possible ideal arrays:
- Arrays starting with the value 1 (5 arrays): [1,1], [1,2], [1,3], [1,4], [1,5]
Example 2:
Input: n = 5, maxValue = 3
Output: 11
- With 2 nd
distinct value 2 (4 arrays): [1,1,1,1,2], [1,1,1,2,2], [1,1,2,2,2], [1,2,2,2,2]
- With 2 nd
distinct value 3 (4 arrays): [1,1,1,1,3], [1,1,1,3,3], [1,1,3,3,3], [1,3,3,3,3]
Constraints:
2 <= n <= 10 4
A segment is a contiguous sequence of positive integers in nums. A segment sum is the sum of every element in a segment.
Return an integer array answer, of length n, where answer[i] is the maximum segment sum after applying the i removal.
th
Note: The same index will not be removed more than once.
Example 1:
Output: [14,7,2,2,0]
Query 2: Remove the 3rd element, nums becomes [0,2,5,0,1] and the maximum segment sum is 7 for segment [2,5].
Query 3: Remove the 2nd element, nums becomes [0,2,0,0,1] and the maximum segment sum is 2 for segment [2].
Query 4: Remove the 4th element, nums becomes [0,2,0,0,0] and the maximum segment sum is 2 for segment [2].
Query 5: Remove the 1st element, nums becomes [0,0,0,0,0] and the maximum segment sum is 0, since there are no segments.
Example 2:
Output: [16,5,3,0]
Query 1: Remove the 3rd element, nums becomes [3,2,11,0] and the maximum segment sum is 16 for segment [3,2,11].
Query 2: Remove the 2nd element, nums becomes [3,2,0,0] and the maximum segment sum is 5 for segment [3,2].
Query 3: Remove the 1st element, nums becomes [3,0,0,0] and the maximum segment sum is 3 for segment [3].
Query 4: Remove the 0th element, nums becomes [0,0,0,0] and the maximum segment sum is 0, since there are no segments.
Constraints:
n == nums.length == removeQueries.length
1 <= n <= 10 5
For each index i, names[i] and heights[i] denote the name and height of the i person. th
Example 1:
Output: ["Mary","Emma","John"]
Example 2:
Output: ["Bob","Alice","Bob"]
Explanation: The first Bob is the tallest, followed by Alice and the second Bob.
Constraints:
n == names.length == heights.length
1 <= n <= 10 3
An integer is considered beautiful if the sum of its digits is less than or equal to target.
Return the minimum non-negative integer x such that n + x is beautiful. The input will be generated such that it is always possible to make n beautiful.
Example 1:
Output: 4
Explanation: Initially n is 16 and its digit sum is 1 + 6 = 7. After adding 4, n becomes 20 and digit sum becomes 2 + 0 = 2. It
can be shown that we can not make n beautiful with adding non-negative integer less than 4.
Example 2:
Output: 33
Explanation: Initially n is 467 and its digit sum is 4 + 6 + 7 = 17. After adding 33, n becomes 500 and digit sum becomes 5 + 0 +
0 = 5. It can be shown that we can not make n beautiful with adding non-negative integer less than 33.
Example 3:
Input: n = 1, target = 1
Output: 0
Explanation: Initially n is 1 and its digit sum is 1, which is already smaller than or equal to target.
Constraints:
1 <= n <= 10 12
integer array factory where factory[j] = [position , limit ] indicates that position is the position of the j factory and that the j factory can
j j j
th th
All the robots are initially broken; they keep moving in one direction. The direction could be the negative or the positive direction of the X-axis. When a robot
reaches a factory that did not reach its limit, the factory repairs the robot, and it stops moving.
At any moment, you can set the initial direction of moving for some robot. Your target is to minimize the total distance traveled by all the robots.
Return the minimum total distance traveled by all the robots. The test cases are generated such that all the robots can be repaired.
Note that
Example 1:
Output: 4
- The first robot at position 0 moves in the positive direction. It will be repaired at the first factory.
- The second robot at position 4 moves in the negative direction. It will be repaired at the first factory.
- The third robot at position 6 will be repaired at the second factory. It does not need to move.
The total distance is |2 - 0| + |2 - 4| + |6 - 6| = 4. It can be shown that we cannot achieve a better total distance than 4.
Example 2:
Output: 2
- The first robot at position 1 moves in the positive direction. It will be repaired at the second factory.
- The second robot at position -1 moves in the negative direction. It will be repaired at the first factory.
The total distance is |2 - 1| + |(-2) - (-1)| = 2. It can be shown that we cannot achieve a better total distance than 2.
Constraints:
The input will be generated such that it is always possible to repair every robot.
For example, if n = 7, then the node with the label 3 has the node with the label floor(3 / 2) = 1 as its parent, and the node with the
label 7 has the node with the label floor(7 / 2) = 3 as its parent.
You are also given an integer array queries. Initially, every node has a value 0 on it. For each query queries[i], you should flip all values in the subtree of the
node with the label queries[i].
Return the total number of nodes with the value 1 after processing all the queries.
Note that:
Flipping the value of a node means that the node with the value 0 becomes 1 and vice versa.
floor(x) is equivalent to rounding x down to the nearest integer.
Example 1:
Output: 3
Explanation: The diagram above shows the tree structure and its status after performing the queries. The blue node represents the
value 0, and the red node represents the value 1.
After processing the queries, there are three red nodes (nodes with value 1): 1, 3, and 5.
Example 2:
Output: 1
Explanation: The diagram above shows the tree structure and its status after performing the queries. The blue node represents the
value 0, and the red node represents the value 1.
After processing the queries, there are one red node (node with value 1): 2.
Constraints:
1 <= n <= 10 5
A subset of nums is beautiful if it does not contain two integers with an absolute difference equal to k.
A subset of nums is an array that can be obtained by deleting some (possibly none) elements from nums. Two subsets are different if and only if the chosen indices
to delete are different.
Example 1:
Output: 4
Explanation: The beautiful subsets of the array nums are: [2], [4], [6], [2, 6].
It can be proved that there are only 4 beautiful subsets in the array [2,4,6].
Example 2:
Output: 1
It can be proved that there is only 1 beautiful subset in the array [1].
Constraints:
You are also given an integer array banned containing some positions from the array. For the ith position in banned, arr[banned[i]] = 0, and banned[i] !=
p.
You can perform multiple operations on arr. In an operation, you can choose a subarray with size k and reverse the subarray. However, the 1 in arr should
never go to any of the positions in banned. In other words, after each operation arr[banned[i]] remains 0.
Return an array ans where for each i from [0, n - 1], ans[i] is the minimum number of reverse operations needed to bring the 1 to position i in arr, or -1 if it
is impossible.
Output: [0,-1,-1,1]
Explanation: In this case k = 4 so there is only one possible reverse operation we can perform, which is reversing the whole
array. Initially, 1 is placed at position 0 so the amount of operations we need for position 0 is 0. We can never place a 1 on the
banned positions, so the answer for positions 1 and 2 is -1. Finally, with one reverse operation we can bring the 1 to index 3, so
the answer for position 3 is 1.
Example 2:
Output: [0,-1,-1,-1,-1]
Explanation: In this case the 1 is initially at position 0, so the answer for that position is 0. We can perform reverse
operations of size 3. The 1 is currently located at position 0, so we need to reverse the subarray [0, 2] for it to leave that
position, but reversing that subarray makes position 2 have a 1, which shouldn't happen. So, we can't move the 1 from position 0,
making the result for all the other positions -1.
Example 3:
Output: [-1,-1,0,-1]
Explanation: In this case we can only perform reverse operations of size 1. So the 1 never changes its position.
Constraints:
1 <= n <= 10 5
0 <= p <= n - 1
0 <= banned.length <= n - 1
0 <= banned[i] <= n - 1
1 <= k <= n
banned[i] != p
all values in banned are unique