Avg CTC: 16 to 20lpa
Job Roles: Software Engineering, Java Developer, Full Stack Developer
Job Locations: Hyderbad, Bengaluru
Job Link: Software Engineering Roles
Multiply the Subarray Sums
Problem Statement
Implement the following function:
int ProductOfSums(int arr[], int n);
The function accepts a positive integer array 'arr' of length 'n' as its argument. The array can be divided into two parts, where first part of array (starting from 0 up to i) is in ascending order and second part of it (starting from i up to n-1) is in descending order. Implement the function to find the sum of first part (ascending order) and sum of second part (descending order). Return the product of sum of first and second part.
• Return -1, if n < 3 or array is null (or None in case of python).
• Computed value lies within integer range.
arr: 3 8 14 12 10 7 4
1175
Sum:
• Sum of first part (ascending order) = 3 + 8 + 14 = 25.
• Sum of second part (descending order) = 14 + 12 + 10 + 7 ÷ 4 = 47.
Product of sums = 25 * 47 = 1175. Thus, output is 1175.
The custom input format for the above case:
7
3 8 14 12 10 7 4
(The first line represents the size of the array 'n' and the second line represents the elements of the array 'arr')
arr: 5 6 7 8 4 3
390
The custom input format for the above case:
6
5 6 7 8 4 3
of the array 'arp')
(The first line represents the size of the array 'n' and the second line represents the elements of the array ‘arr’)
Implement the following function:
int MinSizeArray (int* arr, int n, int k);
The function accepts a positive integer array 'arr' consisting of 'n' elements and a positive integer 'k' as its argument.
Remove the elements of array according to following rules:
•Exactly three elements are removed in one turn.
•The removed three elements must be adjacent in array, i.e., arr[i], arr[j+1], arr[j+2]. And the second element must be k greater than the first element and the third element must be k greater than the second element, i.e., arr[j+1] - arr[i] = k and arr[j+2] - arr[j+1] = k.
Implement the function to remove the elements from array until no further triplets can be removed and return the size of the smallest possible such array.
Note : Return -1 if 'arr' is empty.
Example:
Input:
k: 2
arr: 2 4 6 8 10 12 8 10 6 8
Output:
1
Explanation:
One way of removing elements from array is:
•Elements {8, 10, 12} at positions {3, 4, 5} respectively are removed to get an array {2, 4, 6, 8, 10, 6, 8}.
•Then, elements {6, 8, 10} at positions {2, 3, 4} respectively are removed to get an array {2, 4, 6, 8}.
•Then, elements {2, 4, 6} at positions {0, 1, 2} respectively are removed to get an array {8}.
Size of final array is 1. Thus, output is 1.
The custom input format for the above case:
2
10
2 4 6 8 10 12 8 10 6 8
(The first line represents 'k', the second line represents 'n', the third line represents the elements of the array 'arr')
Sample input
k: 1
arr: 12 13 14 15 16 19
Sample Output
3
arr[i]
there are two possibilities.1) Either the element is not removed.
2) Or the element is removed (if it follows rules of removal). When an element is removed, there are again two possibilities.
a) It may be removed directly, i.e., initial `arr[i+1] is arr[i]+k` and `arr[i+2]` is `arr[i] + 2*k`.
b) There exist `x` and `y` such that `arr[x] – arr[i] = k, arr[y] – arr[x] = k`, and subarrays `arr[i+1…x-1]` & `arr[x+1…y-1]` can be completely removed.
// Returns size of minimum possible size of `arr[low..high]` after removing elements according to given rules
`findMinSize(arr[], low, high, k)`
1) If `high-low+1 < 3`, return `high-low+1` // If there are less than 3 elements in `arr[low..high]`
2) `result = 1 + findMinSize(arr, low+1, high)` // Consider the case when `arr[low]` is not considered as part of any triplet to be removed. Initialize the result using this case
3)
For all i from low+1 to high // Case when arr[low] is part of some triplet and removed. Try all possible triplets that have `arr[low]`
For all j from i+1 to high
Update result if all of the following conditions are met
a) arr[i] - arr[low] = k
b) arr[j] - arr[i] = k
c) findMinSize(arr, low+1, i-1, k) returns 0
d) findMinSize(arr, i+1, j-1, k) also returns 0
e) The result calculated for this triplet (low, i, j) is smaller than the existing result.
4) Return result
Time Complexity: O(N^2), N
is the size of the array.
The time complexity of the proposed solution is O (N4 )
as Time complexity = no. of states * work done per state
i.e N^2 * N^2