InfyTQ Practice Problem - Day 23
InfyTQ Practice Problem - Day 23
InfyTQ Practice Problem - Day 23
Note: Since there can be multiple solutions, the driver code will print 1 if your answer is correct, otherwise, it
will return 0. In case there's no profit the driver code will print the string "No Profit" for a correct solution.
Example 1:
Input:
N=7
A[] = {100,180,260,310,40,535,695}
Output:
1
Explanation:
One possible solution is (0 3) (4 6) We can buy stock on day 0, and sell it on 3rd day, which will give us
maximum profit. Now, we buy stock on day 4 and sell it on day 6.
Example 2:
Input:
N=5
A[] = {4,2,2,2,4}
Output:
1
Explanation:
There are multiple possible solutions. one of them is (3 4) We can buy stock on day 3, and sell it on 4th day,
which will give us maximum profit.
Your Task:
The task is to complete the function stockBuySell() which takes an array A[] and N as input parameters and
finds the days of buying and selling stock. The function must return a 2D list of integers containing all the buy-
sell pairs i.e. first value of pair will represent the day on which you buy the stock and second value represent the
day on which you sell that stock. If there is No Profit, return an empty list.
Constraints:
2 ≤ N ≤ 106
0 ≤ A[i] ≤ 106
Question 2: Largest subarray with 0 sum
Given an array having both positive and negative integers. The task is to compute the length of the largest
subarray with sum 0.
Example 1:
Input:
N=8
A[] = {15,-2,2,-8,1,7,10,23}
Output: 5
Explanation: The largest subarray with sum 0 will be -2 2 -8 1 7.
Your Task:
You just have to complete the function maxLen() which takes two arguments an array A and n, where n is the
size of the array A and returns the length of the largest subarray with 0 sum.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 <= N <= 105
-1000 <= A[i] <= 1000, for each valid i
Question 3: Longest Common Prefix in an Array
Given a array of N strings, find the longest common prefix among all strings present in the array.
Example 1:
Input:
N=4
arr[] = {geeksforgeeks, geeks, geek,
geezer}
Output: gee
Explanation: "gee" is the longest common prefix in all the given strings.
Example 2:
Input:
N=2
arr[] = {hello, world}
Output: -1
Explanation: There's no common prefix in the given strings.
Your Task:
You don't need to read input or print anything. Your task is to complete the
function longestCommonPrefix() which takes the string array arr[] and its size N as inputs and returns the
longest common prefix common in all the strings in the array. If there's no prefix common in all the strings,
return "-1".
Constraints:
1 ≤ N ≤ 103
1 ≤ |arri| ≤ 103