Count the number of contiguous subarrays with maximum element as x - stack

Given an array of numbers, print the number of subarrays with maximum element as x. For example :
Input :
arr = [1, 2, 3, 3, 1]
x = [3,2,1,4]
output : 11,2,2,0
Subarrays for x = 1:
1
1
Subarrays for x = 2:
2
1 2
Subarrays for x = 3:
1 2 3
1 2 3 3
1 2 3 3 1
2 3
2 3 3
2 3 3 1
3
3 3
3 3 1
3
3 1
There are no subarray with maximum element as 4. So for x = 4 we have to print 0.
My first attempt was to generate all subarrays and count that.The time complexity of this approach is very bad(O(n^3))

Related

How to count 2 columns with a range

A B C
Val 1 2
Val 2 1
Val 3 1
Item 1 Val 1 1
Item 2 Val 2 1
Item 3 Val 3 0
Item 4 Val 1 0
Consider the above sheet. In the first 3 rows I am counting how many times corresponding val# shows up in the sheet. I have done that with: =COUNTIF($B$5:$B, A1) However, I can't figure out how to make it count only if the value matches and column C doesn't have a 1 next to it on same row. Is this possible?
try COUNTIFS:
=COUNTIFS(B$5:B, A1, C$5:C, "<>"&1)
make sure C column is formatted as Number

Count unique values across multiple columns

I have data in this format
A B C D
1 1 1 1
1 1 1 2
1 1 1 3
1 1 1 4
...
4 4 4 4
I want to count number of unique values in each row and print it
output:
A B C D unique-count
1 1 1 1 4
1 1 1 2 3
1 1 1 3 3
1 1 1 4 3
...
4 4 4 4 4

Torch: Concatenating tensors of different dimensions

I have a x_at_i = torch.Tensor(1,i) that grows at every iteration where i = 0 to n. I would like to concatenate all tensors of different sizes into a matrix and fill the remaining cells with zeroes. What is the most idiomatic way to this. For example:
x_at_1 = 1
x_at_2 = 1 2
x_at_3 = 1 2 3
x_at_4 = 1 2 3 4
X = torch.cat(x_at_1, x_at_2, x_at_3, x_at_4)
X = [ 1 0 0 0
1 2 0 0
1 2 3 0
1 2 3 4 ]
If you know n and assuming you have access to your x_at_i easily at each iteration I would try something like
X = torch.Tensor(n, n):zero()
for i = 1, n do
X[i]:narrow(1, 1, i):copy(x_at[i])
end

Doesn't remove duplicate elements from array

I am trying to take input from user in an array .And want to remove duplicate elements but the result is weird .I don't have to use uniq or any other ruby method.Here is my code
digits = []
digits = gets.chomp.to_i
k= digits & digits
puts k
input - 1 2 3 4 1 2 3 <br>
Required output- 1 2 3 4<br>
Getting output 1
gets.chomp returns string "1 2 3 4 1 2 3"
Then you call to_i on that string:
"1 2 3 4 1 2 3".to_i => 1
Consequentially 1 & 1 => 1
You should do this:
digits = gets.chomp.split(' ').map(&:to_i)
k = digits & digits
puts k

How to split a matrix into several matrices based on another vector

If
A = [1 2 3; 4 5 6; 7 8 9]
B = [1 2 2]
I found that
A(B == 1, :) returns [1 2 3] and
A(B == 2, :) returns [4 5 6; 7 8 9]
because
B == 1 returns [1 0 0] and
B == 2 returns [0 1 1]
Given the above examples for A and B is there any easier way to get both the final matrices [1 2 3] and [4 5 6; 7 8 9] in one step i.e. without using a for loop.
Objective is to generate centroids of clusters to which each example(row in A) has been assigned in a K-means clustering problem. I am thinking of passing the resulting matrices to mean() function to generate the centroids.
You can get a cell array
A = [1 2 3; 4 5 6; 7 8 9];
B = [1 2 2];
arrayfun(#(lev) A(B==lev, :), unique(B), 'UniformOutput', false)
returns
ans =
{
[1,1] =
1 2 3
[1,2] =
4 5 6
7 8 9
}

Resources