ACC Coding5
ACC Coding5
Question-1
Examples:
Given an integer array nums, find the subarray with the largest sum, and return its sum.
Example 1:
Input: nums = [-2,1,-3,4,-1,2,1,-5,4]
Output: 6
Explanation: The subarray [4,-1,2,1] has the largest sum 6.
Example 2:
Input: nums = [1]
Output: 1
Explanation: The subarray [1] has the largest sum 1.
Example 3:
Input: nums = [5,4,-1,7,8]
Output: 23
Explanation: The subarray [5,4,-1,7,8] has the largest sum 23.
Constraints:
1 <= nums.length <= 105
-104 <= nums[i] <= 104
Question-3
Given and , what's the minimum number of packages that Luke must drop
to supply all of his bases?
Question-3
Given and , what's the minimum number of packages that Luke must drop to supply
all of his bases?
Example
n=2
M=3
Packages can be dropped at the corner between cells (0, 0), (0, 1), (1, 0) and (1, 1) to
supply bases. Another package can be dropped at a border between (0, 2) and (1,
2). This supplies all bases using packages.
Function Description
Complete the gameWithCells function in the editor below.
gameWithCells has the following parameters:
•int n: the number of rows in the game
•int m: the number of columns in the game
Returns
•int: the minimum number of packages required
Question-3
Input Format
Two space-separated integers describing the respective values of n and m.
• Constraints
0< n, m<=1000
Output Format
Print a single integer denoting the minimum number of supply packages Luke must drop.
Sample Input 0
22
Sample Output 0
1
Explanation 0
Luke has four bases in a 2×2 grid. If he drops a single package where the walls of all four bases intersect, then
those four cells can access the package:
Because he managed to supply all four bases with a single supply drop, we print as our answer
Question-4
Write a program to calculate and return the sum of absolute difference between the adjacent number in an array
of positive integers from the position entered by the user.
Note : You are expected to write code in the findTotalSum function only which receive three positional arguments:
1st : number of elements in the array
2nd : array
3rd : position from where the sum is to be calculated
Example Input :
Input
input 1 : 7 (no. of test cases)
input 2 : 11 22 12 24 13 26 14 (size of the array)
input 3 : 5 (array elements)
Output
25
Explanation
The first parameter 7 is the size of the array. Next is an array of integers and input 5 is the position from where you
have to calculate the Total Sum. The output is 25 as per calculation below.
| 26-13 | = 13
| 14-26 | = 12
Total Sum = 13 + 12 = 25
Question-5
Rohan and his team are participating in the Treasure Hunt event of college in
which in each step they have to solve one problem to get a clue about the
Treasure location. Rohan’s team has performed very well and reached the final
step where they have to provide a code of a problem to get a final clue about
treasure .
Given a string s, they need to find the longest palindromic subsequence’s
length in s.
A subsequence is a sequence that can be derived from another sequence by
deleting some or no elements without changing the order of the remaining
elements.
The string contains only lowercase letters.
Write a program to help Rohan’s team that takes in input as String x and
returns the length of the longest palindromic subsequence of x.
Question-5
Input Specification:
input1: string input
Output Specification:
Return the length of the longest palindromic subsequence
Example 1:
Input: s = “bbbab”
Output: 4
Explanation: One possible longest palindromic subsequence is “bbbb”.
Example 2:
Input: s = “cbbd”
Output: 2
Explanation: One possible longest palindromic subsequence is “bb”.
Question-6
A chef has n orders lined up. The waiting time penalty before an order starts getting processed is k. And for each order, a
corresponding processing time t is given (in minutes) along with its order id. The earning per order is c times the time taken to
process the order. Find out the profits earned for each order from 1 to n and print them in order of the input. The order is free if
the profits are negative for that order i.e. the profits are max(0, profit).
The following rules are used for processing the orders:
Pick the order which arrives first. The chef can process only one order at a time.
Among the orders already arrived, pick the one with the least processing time.
Input Format:
First the number of test cases T is given. Then T test cases follow.
Each test case consists of n (the number of orders),c (the cost per minute it takes to process any order, which is the same for all
orders), and k (the waiting time penalty per minute, the chef faces once an order has arrived and is there in the “waiting to be
processed queue”).
Then in the following n lines, an order with its arrival time and the processing time is given.
The order id of the first of these n lines is 1, the order id of the 2nd order is 2, and so on till the nth line has the order id n.
NOTE: The input may or may not be sorted by the arrival time i.e. order 3 may arrive earlier than order 2 and so on.
Output Format: For each test case, print in a single line the profits earned for orders 1 to n, respectively.
Question-6
Constraints:
1<=T<=10 ; 1<=n<=10^5 ; 1<=order arrival time, order processing time <= 10^9 ; 1<=k<=100 ; 1<=c<=10^4 ; Time limit: 1 second per
test case
Example :
Input:
1
3 10 16
12
23
31
Explanation:
The number of test cases T = 1. A number of orders n = 3. Processing cost per order per minute i.e. c = 10. Penalty cost for an order
already arrived but in the waiting queue per minute of waiting i.e. k = 1. Then the list of orders follows. Order 1 arrives at time
t=1. the chef does nothing from t=0 to t=1.
Since he has to pick up the order which arrived first his profit earned for order 1 would be processing time*c — k*waiting time.
Since the order was processed immediately as it came, the waiting time would be 0 for order 1. Hence, profit of order 1 = 2*10 —
16*0= 20. Now the time t=1+2=3. Therefore both orders 2 and 3 have arrived and are in the waiting queue.
But the chef chooses order 3 to be processed first since it has less processing time. Therefore profit for order 3 is 1*10-16*0=10, as
order 3 was processed the moment it came. Now the time t=3+1=4 and the last order 2 is taken up which has been waiting for 4-
2=2 minutes. Therefore the profit earned for order 2 = 3*10 — 16*2= 30 — 32 = -2. Therefore the order is free or profit = 0.
Output: 20 0 10
Question-7
Example 1:
Input: source = "abcd", target = "acbe", original = ["a","b","c","c","e","d"], changed = ["b","c","b","e","b","e"], cost = [2,5,5,1,2,20]
Output: 28
Explanation: To convert the string "abcd" to string "acbe":
- Change value at index 1 from 'b' to 'c' at a cost of 5.
- Change value at index 2 from 'c' to 'e' at a cost of 1.
- Change value at index 2 from 'e' to 'b' at a cost of 2.
- Change value at index 3 from 'd' to 'e' at a cost of 20.
The total cost incurred is 5 + 1 + 2 + 20 = 28.
It can be shown that this is the minimum possible cost.
Question-7
Example 2:
Input: source = "aaaa", target = "bbbb", original = ["a","c"], changed = ["c","b"], cost = [1,2]
Output: 12
Explanation: To change the character 'a' to 'b' change the character 'a' to 'c' at a cost of 1, followed by changing the
character 'c' to 'b' at a cost of 2, for a total cost of 1 + 2 = 3. To change all occurrences of 'a' to 'b', a total cost of 3 * 4 = 12
is incurred.
Example 3:
Input: source = "abcd", target = "abce", original = ["a"], changed = ["e"], cost = [10000]
Output: -1
Explanation: It is impossible to convert source to target because the value at index 3 cannot be changed from 'd' to 'e'.
Constraints:
1 <= source.length == target.length <= 105
source, target consist of lowercase English letters.
1 <= cost.length == original.length == changed.length <= 2000
original[i], changed[i] are lowercase English letters.
1 <= cost[i] <= 106
original[i] != changed[i]
Question-8
Constraints:
1 <= s.length <= 1000
s consist of only digits and English letters.
Question-9
Example 1:
Input: strs = ["flower","flow","flight"]
Output: "fl"
Example 2:
Input: strs = ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.
Constraints:
1 <= strs.length <= 200
0 <= strs[i].length <= 200
strs[i] consists of only lowercase English letters.
Question-10
Generate Parentheses
Example 1:
Input: n = 3
Output: ["((()))","(()())","(())()","()(())","()()()"]
Example 2:
Input: n = 1
Output: ["()"]
Wildcard Matching
Given an input string (s) and a pattern (p), implement wildcard pattern
matching with support for '?' and '*' where:
'?' Matches any single character.
'*' Matches any sequence of characters (including the empty sequence).
The matching should cover the entire input string (not partial).
Example 1:
Input: s = "aa", p = "a"
Output: false
Explanation: "a" does not match the entire string "aa".
Question-11
Example 2:
Input: s = "aa", p = "*"
Output: true
Explanation: '*' matches any sequence.
Example 3:
Input: s = "cb", p = "?a"
Output: false
Explanation: '?' matches 'c', but the second letter is 'a', which does not match 'b'.
Constraints:
0 <= s.length, p.length <= 2000
s contains only lowercase English letters.
p contains only lowercase English letters, '?' or '*'.
Question-12
Example 1:
Input: s = "barfoothefoobarman", words = ["foo","bar"]
Output: [0,9]
Explanation:
The substring starting at 0 is "barfoo". It is the concatenation of ["bar","foo"] which is a permutation of words.
The substring starting at 9 is "foobar". It is the concatenation of ["foo","bar"] which is a permutation of words.
Question-12
Example 2:
Input: s = "wordgoodgoodgoodbestword", words = ["word","good","best","word"]
Output: []
Explanation:
There is no concatenated substring.
Example 3:
Input: s = "barfoofoobarthefoobarman", words = ["bar","foo","the"]
Output: [6,9,12]
Explanation:
The substring starting at 6 is "foobarthe". It is the concatenation of ["foo","bar","the"].
The substring starting at 9 is "barthefoo". It is the concatenation of ["bar","the","foo"].
The substring starting at 12 is "thefoobar". It is the concatenation of ["the","foo","bar"].
Constraints:
1 <= s.length <= 104
1 <= words.length <= 5000
1 <= words[i].length <= 30
s and words[i] consist of lowercase English letters.
Question-13
Valid Number
For example, all the following are valid numbers: "2", "0089", "-0.1", "+3.14", "4.", "-.9", "2e10", "-90E3", "3e+7", "+6e-
while the following are not valid numbers: "abc", "1a", "1e", "e3", "99e2.5", "--6", "-+3", "95a54e53".
Formally, a valid number is defined using one of the following definitions:
An integer number followed by an optional exponent.
A decimal number followed by an optional exponent.
An integer number is defined with an optional sign '-' or '+' followed by digits.
A decimal number is defined with an optional sign '-' or '+' followed by one of the following definitions:
Digits followed by a dot '.'.
Digits followed by a dot '.' followed by digits.
A dot '.' followed by digits.
An exponent is defined with an exponent notation 'e' or 'E' followed by an integer number.
The digits are defined as one or more digits.
Question-13
Example 1:
Input: s = "0"
Output: true
Example 2:
Input: s = "e"
Output: false
Example 3:
Input: s = "."
Output: false
Constraints:
1 <= s.length <= 20
s consists of only English letters (both uppercase and lowercase), digits (0-9), plus '+', minus '-',
or dot '.'.
Question-14
Given two strings s and t of lengths m and n respectively, return the minimum
window
Substring of s such that every character in t (including duplicates) is included in the
window. If there is no such substring, return the empty string "".
The testcases will be generated such that the answer is unique.
Example 1:
Input: s = "ADOBECODEBANC", t = "ABC"
Output: "BANC"
Explanation: The minimum window substring "BANC" includes 'A', 'B', and 'C' from
string t.
Question-14
Example 2:
Input: s = "a", t = "a"
Output: "a"
Explanation: The entire string s is the minimum window.
Example 3:
Input: s = "a", t = "aa"
Output: ""
Explanation: Both 'a's from t must be included in the window.
Since the largest window of s only has one 'a', return empty string.
Constraints:
m == s.length
n == t.length
1 <= m, n <= 105
s and t consist of uppercase and lowercase English letters.
Question-15
Palindrome Partitioning II
Given a string s, partition s such that every
substring
of the partition is a
palindrome
.
Return the minimum cuts needed for a palindrome partitioning of s.
Example 1:
Input: s = "aab"
Output: 1
Explanation: The palindrome partitioning ["aa","b"] could be produced using 1 cut.
Question-15
Example 2:
Input: s = "a"
Output: 0
Example 3:
Input: s = "ab"
Output: 1
Constraints:
1 <= s.length <= 2000
s consists of lowercase English letters only.
Question-16
Example 1:
Input: s = "aa", p = "a"
Output: false
Explanation: "a" does not match the entire string "aa".
Question-16
Example 2:
Input: s = "aa", p = "a*"
Output: true
Explanation: '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes
"aa".
Example 3:
Input: s = "ab", p = ".*"
Output: true
Explanation: ".*" means "zero or more (*) of any character (.)".
Constraints:
1 <= s.length <= 20
1 <= p.length <= 20
s contains only lowercase English letters.
p contains only lowercase English letters, '.', and '*'.
It is guaranteed for each appearance of the character '*', there will be a previous valid character to match.
THANK YOU