511 questions
3
votes
2
answers
195
views
Max subArray => IllegalArgumentException: 6 > 5
The method is working as intended, but while I was trying multiple cases, I got this weird issue, so hopefully someone can understand why and explain it to me, since 6 > 5 looks logically sound to ...
1
vote
2
answers
136
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
0
answers
67
views
Sum of comparisons and exchange between two sorting methods and their comparison (shell and quicksort)
I'm trying to make a code that counts Shell and Merge comparisons and swaps, making a sum between the sum of the comps and swaps. The vectors are a subArray, such that, given: 4 (line break here) 3 6 ...
0
votes
0
answers
19
views
Maximum Count of Subarray having sum >= k and length >l
For a given array we need to find the maximum number of subarrays possible. Conditions:
1.The sum of each subarray should be >= minSum
2.The length of the subarray should be >= minLen
We need to ...
1
vote
3
answers
169
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 ...
20
votes
3
answers
2k
views
Efficient way to find sum of largest x elements in a subarray
I have a 1-indexed array of positive integers, and I want to make several queries to it, all in the form, 'what is the sum of the largest x integers in the subarray 1 to y inclusive?' This array is ...
0
votes
0
answers
89
views
Finding sums of all subarrays of an array
This is a snippet from this code to finding sums of all subarrays of a given array, but this doesn't work as intended as the problem seems to be lying with pre-incrementing value of i in the equation.
...
-4
votes
1
answer
62
views
What are some efficient ways I can generate distinct halves of an integer array?
Given an integer array, I want to print out all the distinct pairs of halves of the array. [2,1] [3,2] and [3,2] [2,1] can be considered distinct.
I'm sure there's a brute force way, but I want ...
1
vote
3
answers
103
views
How do I stack arrays horizontally?
I have an array:
> a
array([[[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]],
[[17, 18, 19, 20],
[21, 22, 23, 24],
[25, 26, 27, 28],
...
1
vote
3
answers
69
views
How do I change values in my subarrays into the avarage of those values?
I have an array:
> a
array([[1, 2, 3], [2, 3, 4], [3, 4, 5]])
I want to change those values to the avarage of those values, so the output will be:
> a
array([[2, 2, 2], [3, 3, 3], [4, 4, 4]])
...
1
vote
0
answers
369
views
Subarray XOR having odd number of set bits
How can I find the number of subarrays where bitwise XOR of the element of the subarray has odd number of set bits.
Basically I need to find the solution in linear time. I could solve in O(N^2).
What ...
0
votes
1
answer
384
views
SubArray selection in swift
I want to do a sub-array selection Python-like (ArraySlice) in Swift.
This is working for me, but I know it's not nice. To make it work I used .suffix embedded to .prefix method. I'm wondering if ...
0
votes
1
answer
285
views
Continuous subarray with given sum
I am trying to create a continuous subArray from a given array which is equal to the given sum and return the first and last index as an ArrayList. This is the solution i could think of which actually ...
1
vote
1
answer
161
views
Sumproduct of sub-arrays in excel
I need to find a performant and smart way to redesign the formula or the tables on which it depends (COSTO_DUMMY and GG_TARGET). Can you help me, please? I can add new support tables if needed
...
-3
votes
2
answers
555
views
Find all the Subarrays in O(nlog(n)) time complexity JavaScript
how to get all the subarrays in O(nlog(n)) time complexity using javaScript
I try with a nested loop but the time complexity is in O(n*n). I heard about some prefix solutions but have no information
0
votes
2
answers
92
views
i have initialized a variable with zero at the top of the function so during a loop the value changes?
public class maxsubarraysum {
public static void main(String[] args) {
int numbers[] = { 1, -2, 6, -1, 3 };
printmsasum(numbers);
}
public static void printmsasum(int ...
0
votes
1
answer
692
views
How can I find the largest subarray sum if all the elements are negative and I have set the maxSum to 0, which is used to compare other elements?
Can someone help me with this. My code is working fine but consider a case of [-2,-1]. Since my maxSum is set to 0. My output is coming 0 instead of-1. How should I modify my code?
Sample i/o:
Input: ...
0
votes
1
answer
989
views
Cut a sequence of length N into subsequences such that the sum of each subarray is less than M and the cut minimizes the sum of max of each part
Given an integer array sequence a_n of length N, cut the sequence into several parts such that every one of which is a consequtive subsequence of the original sequence.
Every part must satisfy the ...
0
votes
0
answers
115
views
Max Sum of All K consecutive sub arrays from 1 to n of an array using sliding window
This is my code and it does well for positive numbers given in the array but when one of the input numbers is negative it doesn't work
#include <stdio.h>
int main() {
int n;
scanf(&...
0
votes
1
answer
79
views
Divide Array in subarrays by local peaks
Hey there i have an numpy array y with over 4000 values.
data=pd.read_csv('samplesdata.csv',sep=";", decimal=",",encoding='latin-1')
sensor_data=data[['Euklidische Norm']]
...
-3
votes
1
answer
190
views
How can I check if a sorted subarray with size 'k' , is contained in an bigger sorted array with size 'n' (k <= n) this complexity O(logn)?
I would like to find if a sorted subarray v' with size 'k' is contained in another sorted array with size 'n'. We know that k <= n. However, it would be so easy with a double for loop or something ...
0
votes
0
answers
115
views
Numpy subarray given by center and radius
Is there a function or more concise notation to get b or bb in the code snippet below?
import numpy as np
a = np.arange(12).reshape(3, 4)
c = (1, 2)
r = 1
b = a[c[0]-r:c[0]+r, c[1]-r:c[1]+r]
bb = a[c[...
0
votes
1
answer
444
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 ...
-1
votes
1
answer
57
views
I'm getting the error ArrayIndexOutOfBoundsException: index -25 out of bonds for length 16
This is my current code for a program to find the maximum subarray. I am getting the out of bounds error for 3 lines: I'm getting the error ArrayIndexOutOfBoundsException: index -25 out of bonds for ...
0
votes
1
answer
616
views
find the total number of subarrays with the ratio of 0's and 1's equal to x:y
question
given an array of elements 0, 1, 2 with find the total number of subarrays with the ratio of 0's and 1's equal to x:y.
input
5
1 1
0 1 2 0 1
output
6
\\5 is the size of array 0 1 2 0 1 ...