Sum of Lengths of Non-Overlapping SubArrays - arrays

Given an array arr[] of N elements, the task is to find the maximum sum of lengths of all non-overlapping subarrays with K as the maximum element in the subarray.
Example 1:
Input: N = 9, K = 4
arr[] = {2, 1, 4, 9, 2, 3, 8, 3, 4}
Output: 5
Explanation: {2, 1, 4} => Length = 3
{3, 4} => Length = 2
So, 3 + 2 = 5 is the answer.
{2, 1, 4, 9, 2, 3, 8, 3, 4} in this array we can have {2,1,4}, {2,3} and {3,4 } subarray which satisfies the required condition but second subarray is not mentioned. Please tell me Why?

Related

Finding indices of unique rows and columns in a 2D array and the minimum sum of the elements in those positions

i have stumbled upon a problem where i am given a 5x5 matrix in the form of a 2D array and i am supposed to find the minimum sum of 5 elements where each element should be in unique row and column and print the indices of those elements and the minimum sum.
The problem gave 3 test cases as example.
Test case 1:
{
{5, 4, 4, 1, 6},
{1, 3, 2, 4, 6},
{3, 2, 3, 2, 6},
{0, 4, 5, 4, 6},
(6, 6, 6, 6, 6}
};
Output: (3,0) (2,1) (1,2) (0,3) (4,4)
Minimum sum: 11
Test case 2:
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0}
};
Output: (0,0) (1,1) (2,2) (3,3) (4,4)
Minimum sum: 0
Test case 3:
{
{1, 2, 3, 4, 5},
{5, 4, 3, 2, 1},
{1, 2, 7, 4, 5},
{5, 4, 3, 2, 1},
{1, 2, 3, 4, 5},
};
Output: (0,0) (2,1) (4,2) (1,3) (3,4)
Minimum sum: 9
I would like to understand what is meant when they say unique row and column and all i can see from the test cases is that the column indices are starting from 0 and increasing by one for each pair of indices.I would like to know how to approach this problem.
Unique row and column means that no two elements share a row or a column.
Here, I've highlighted the selected numbers.
You can see that when a number is selected, No other number in that same column is also selected. No other number in that same row is selected.
5 4 4 1 6
1 3 2 4 6
3 2 3 2 6
0 4 5 4 6
6 6 6 6 6
1 + 2 + 2 + 0 + 6 = 11

Counting inversions in a changing array

You have an array A[] of size (1 ≤ N ≤ 10^5). For each of i = 0, 1, 2, ..., N - 1, we want to determine the number of inversions in the array if all entries greater than i are decreased to i.
An inversion is defined as two entries A[i] and A[j] where A[i] > A[j] and i < j.
Example:
A[] = {3, 2, 1, 5, 2, 0, 5}
i = 0: {0, 0, 0, 0, 0, 0, 0} Inversions: 0
i = 1: {1, 1, 1, 1, 1, 0, 1} Inversions: 5
i = 2: {2, 2, 1, 2, 2, 0, 2} Inversions: 7
i = 3: {3, 2, 1, 3, 2, 0, 3} Inversions: 10
i = 4: {3, 2, 1, 4, 2, 0, 4} Inversions: 10
i = 5: {3, 2, 1, 5, 2, 0, 5} Inversions: 10
i = 6: {3, 2, 1, 5, 2, 0, 5} Inversions: 10
So your output would be:
0
5
7
10
10
10
10
I know how to find the number of inversions in an array through MergeSort in O(NlogN). However, if I was to explicitly generate every array for each value of i, it would be an O(N^2logN) algorithm which wouldn't pass in time.
One observation I made was that the inversions increase as i increases. This makes sense because when all entries are 0, there will be no inversions (as it is sorted), but as you keep increasing the maximum entry value, the entry can become larger than entries that previously were of the same value.
So you could start with an A[] with only 0s, and keep increasing i. You can use your answer for previous values of i to determine the answer for larger values of i. Still, if you scanned through each array you would still get an O(N^2) algorithm.
How can I solve this problem?
I'll take a stab at this. We're going to consider queries in descending order, so from i = N-1, ..., down to 0. First of all, notice that when we're shrinking all A[j] > i to i, any A[j] = i will no longer cause an inversion with elements larger than it of smaller index.
For example, say we have A = [1, 2, 5, 4] and we shrink A[2] to 4. Then we have A = [1, 2, 4, 4] and our single inversion disappears. Thus, for each j, we can count the number of elements in A with smaller index and larger value, and denote this V[j], the "number of inversions it contributes". We find the total number of inversions in the original array, and then for each i = N-1,...,0 we remove V[j] from the total number of inversions for all j such that V[j] = i.
Let's apply this to the example given.
A = [3, 2, 1, 5, 2, 0, 5]
V = [0, 1, 2, 0, 2, 5, 0]
Then, going through i = 6, 5, 4, 3, 2, 1:
i = 6: A = [3, 2, 1, 5, 2, 0, 5], res = 10 (original calculation using merge sort)
i = 5: A = [3, 2, 1, 5, 2, 0, 5], res = 10 (subtract nothing because V[3] = V[6] = 0)
i = 4: A = [3, 2, 1, 4, 2, 0, 4], res = 10 (subtract nothing because no occurrences of 4)
i = 3: A = [3, 2, 1, 3, 2, 0, 3], res = 10 (10 - V[0] = 10)
i = 2: A = [2, 2, 1, 2, 2, 0, 2], res = 7 (10 - V[1] - V[4] = 10 - 1 - 2 = 7)
i = 1: A = [1, 1, 1, 1, 1, 0, 1], res = 5 (7 - V[2] = 7 - 2 = 5)
i = 0: A = [0, 0, 0, 0, 0, 0, 0], res = 0 (5 - V[5] = 5 - 5 = 0)
And we get our desired outputs. Implementation details can vary; you can find the number of elements greater than A[j] with lower index using a Fenwick Tree or something similar. This algorithm runs in O(NlogN) time.

Sum every 3 elements of an array in ruby

Is there any efficient and short way to sum every 3 elements of an array?
ar = [1, 2, 3, 4, 5, 6, 7, 8, 1]
sum_ar = [6, 15, 16]
first 3 elements 1+2+3=6
next 3 elements 4+5+6=15
next 3 elements 7+8+1=15
...
if there are only two elements left, sum them
I could do something like this:
y=0
s=ar.size/3
((0..s).step(3).to_a).each do |i|
sum_ar[y]=ar[i..i+2].inject(:+)
y=y+1
end
but then I will miss the elements in case of such an array, where the size is not an exactly multiply of a 3:
ar=[1, 2, 3, 4, 5, 6, 7, 8]
A short way with Enumerable#each_slice:
[1, 2, 3, 4].each_slice(3).map { |e| e.inject(:+) } # => [6, 4]

Given an unsorted array, Find the maximum subtraction between two elements in the array

I've got this question from an Interview in Microsoft: Given an unsorted array, Find the maximum subtraction between two elements in the array is a way that:
(Index1, Index2) = arr[Index2] - arr[Index1]. Index1<Index2.
Example:
given the array: [1, 5, 3, 2, 7, 9, 4, 3] -> Output: (1,9)=8.
given the array: [4, 9, 2, 3, 6, 3, 8, 1] -> Output: (2,8)=6.
The naive solution works in O(n^2) times: Scan the first index for subtraction with all other indexes and save the max value, Continue to the next index and so on.
Is there any way to optimize this?
Fairly simple when you write it down. Rephrasing the problem, you want to find the largest element to the right of each element. Now given the first example, this is:
[1, 5, 3, 2, 7, 9, 4, 3]
=>
[9, 9, 9, 9, 9, 4, 3]
Now, notice the maximums array is just the cumulative maximums from the right. Given this property it is easy to construct an O(n) time algorithm.
Implementation in python:
def find_max(xs):
ys = []
cur_max = float('-inf')
for x in reversed(xs):
cur_max = max(x, cur_max)
ys.append(cur_max)
ys = ys[::-1][1:]
return max(y - x for x, y in zip(xs, ys))
We can also construct the maximums array lazily, doing so gives us O(1) memory, which is the best possible:
def find_max(xs):
cur_max = float('-inf')
cum_max = xs[-1]
for i in range(len(xs) - 2, -1, -1):
cur_max = max(cur_max, cum_max - xs[i])
cum_max = max(cum_max, xs[i])
return cur_max
I think this is correct and O(nlogn): Split in the middle and select from right the max, from left the min value. Also split the the other 2 quarters, if one of them gives bigger result continue on that branch recursively.
Second example:
4, 9, 2, 3| 6, 3, 8, 1 -> 2 and 8
4, 9| 2, 3, 6, 3, 8, 1 -> 4 and 8
4, 9, 2, 3, 6, 3| 8, 1 -> 2 and 8
So working on the right split:
4, 9, 2, 3, 6, 3, 8| 1 -> 2 and 1
Selecting the 2 and 8 option. It also works for the first example.

How get sets of integer which appear in at least K or more than K sub-array(s) from array. Each set must have exactly L elements ?

I want to write program with input an array which contains N sub-array(s). Each sub-array is an array of integers which has M element(s). N and M can be very large (e.g. 1.000.000). It assume that the arrays can be stored in physical memory. The program would output: All sets of integers which appear in at least K or more than K sub-array(s). Each set must have exactly L elements.
Ex:
Input:
N = 5
{1, 2, 3, 4, 5}
{1, 3, 2}
{1, 4, 3, 2, 9}
{2, 3, 4, 7, 9}
{3, 4, 5, 9, 10}
Output:
1. In case K = 3, L = 2
{1, 2}
{1, 3}
{2, 3}
{2, 4}
{3, 4}
{3, 9}
{4, 9}
2. In case K = 3, L = 3
{1, 2, 3}
{2, 3, 4}
How can I do ?
thanks

Resources