All Questions
Tagged with sub-array data-structures
9 questions
1
vote
2
answers
129
views
How do I create key-value pairs by looping over subarrays in Ruby?
I'm trying to write a Ruby program which will parse the following TSV file and loop over each record, adding each shop name (last column) as the key in a hash and the associated price (second column) ...
1
vote
3
answers
150
views
Sum of maximum element of all subarray not including the first and last element
I am trying to make an algorithm that calculates the sum of each maximum element in a subarray not including the first and last elements. The naive approach is obvious but I do not want that.
Here is ...
0
votes
1
answer
412
views
How to find largest subarray of sum k
Let's say you have given an array of size N, which can have a positive and a negative number.
we need to return the length of the largest subarray of sum equal to k. I tried to use the sliding window ...
0
votes
1
answer
87
views
How to solve this permutation related problem in O(n^2) complexity?
There are 2 arrays A and B with lengths N and M you have to delete 1 element at a time from B and check if there exists a permutation of B in A
Explanation:
Example:
N = 5
A = [1,2,2,1,1]
M = 3
b = [1,...
0
votes
2
answers
288
views
Is this a wrong approach to find whether array is subarray of another array?
bool isSubset(int arr1[], int m,int arr2[], int n){
set<int> hashset;
for (int i = 0; i < m; i++){
hashset.insert(arr1[i]);
}
for (int i = 0; i < n; i++) {
...
3
votes
3
answers
5k
views
Find number of distinct contiguous subarrays with at most k odd elements
Given an integer array nums, find number of distinct contiguous subarrays with at most k odd elements. Two subarrays are distinct when they have at least one different element.
I was able to do it in ...
0
votes
1
answer
31
views
Mantain a count of subarrays that satisfy the condition usning recursion
So I made a recursive function that gives me all the subarrays , I want to apply a condition on those sub-arrays and then keep a count of subarrays that satisfy the condition. But Initialization of ...
1
vote
2
answers
321
views
Kadane algorithm in Scala explanation
I am interested in learning how to implement the Kadane (maximum subarray sum) algorithm in scala with foldLeft function. I run through this example on stack overflow, however I am not sure I ...
1
vote
1
answer
186
views
Possibly simpler O(n) solution to find the Sub-array of length K (or more) with the maximum average
I saw this question on a coding competition site.
Suppose you are given an array of n integers and an integer k (n<= 10^5, 1<=k<=n). How to find the sub-array(contiguous) with maximum ...