511 questions
3
votes
2
answers
199
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
155
views
How to get maximum average of subarray?
I have been working this leet code questions
https://leetcode.com/problems/maximum-average-subarray-i/description/
I have been able to create a solution after understanding the sliding window ...
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) ...
0
votes
1
answer
132
views
How to optimize this code: Leetcode input was unexpectedly ( Memory Limit Exceeded )
Leetcode question: Given a binary array nums, return the maximum length of a contiguous subarray with an equal number of 0 and 1.
Leetcode entered a very long input of to the function and its IDE ...
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 ...
-1
votes
1
answer
49
views
Get Index of Sub Array in MongoDB
I have the following array:
const universitiesSchema = new Schema({
name: {
type: String,
required: [true, 'Required']
},
users: {
type:[Schema.Types.ObjectId],
}
});
I would ...
0
votes
0
answers
21
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 ...
0
votes
1
answer
53
views
Why aren't large numbers working for this Ackerman Subarray Solution?
You are given two inputs for this program; <the_number> <checking_number>
For every sub-"array" you have, you need to sum up the digits of that sub-"array" and check if ...
1
vote
3
answers
170
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
36
views
compilation process in some java question about relate to 2d arrays
public static boolean ContainedArr(boolean[][] photo, boolean[][] sub) { //find if sub is sub array in photo
if (sub.length>photo.length ||sub[0].length>photo[0].length) { //sub bigger ...
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 ...
-2
votes
1
answer
66
views
why is it different to print statement within the inner loop and outside the inner loop?
I was asked to print the number of negative subarrays. I used three loops to fix a starting position, ending position and a loop to print elements of subarray. I need to use count to check if the sum ...
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.
...
-5
votes
1
answer
165
views
how this tricky problem could be solved in python for creating subarray
write a program to create subset of given array the value inside the array should be target value and length of array should be k
program that i have tried
arr = [1, 2, 8, 6, 4, 9, 5]
k = 3
target = ...
-4
votes
1
answer
66
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 ...
-2
votes
2
answers
83
views
Issue with ArrayList array generation [closed]
You are given an array A of N integers.
Return a 2D array consisting of all the subarrays of the array.
I had tried to get subarrays in temp as per loop and add that subarray directly to ans(2d ...
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
votes
1
answer
54
views
How to perform grouping in php [duplicate]
I have the following array,in which, I have an item 'CONSECUTIVO_GRUPO', which I want to take as a reference to create a group of 'RESPONSES' in an array of three positions, CODIGO, ID_MODULO, VALOR. ...
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]])
...
0
votes
4
answers
2k
views
Why does my code return 12 instead of 12.75 for Leetcode problem #643? (Maximum Average Subarray)
Question:
You are given an integer array nums consisting of n elements, and an integer k.Find a contiguous subarray whose length is equal to k that has the maximum average value and return this value....
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 ...
1
vote
3
answers
921
views
How to get the arrays not contained in another array?
I have two Python arrays with arrays inside:
first = [[0,10],[0,11],[0,12],[0,13]]
second = [[0,10],[0,11]]
and I want to get as a result the array that is in the first one, but not in the other:
...
0
votes
1
answer
392
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 ...
1
vote
2
answers
504
views
Count subarrays in A with sum less than k
For the question below:
Given an array A of N non-negative numbers and a non-negative number B,you need to find the number of subarrays in A with a sum less than B.
I have found 2 solutions:
Brute ...
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 ...
0
votes
0
answers
430
views
Counting segments containing certain subsegments
I have an array of N integers, and I have to find the number of such segments that they contain within themselves segments whose cumulative sum is equal to zero.
For example, if I have an array [5, -5,...
1
vote
1
answer
162
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
...
0
votes
1
answer
346
views
How can the sub-array of an array be filtered in JSONata
In the following array how can the object with "step":"2" be removed from the sub-array "subGroupB" only when "code":"code-333"?
{
"mainGroup&...
-3
votes
2
answers
557
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
-1
votes
2
answers
122
views
How to do a consecutive subarrays in JavaScript [duplicate]
I am studying algorithms in JavaScript; I am trying to generate consecutive subarrays but I don't know to do it.
I have the following array:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
I want to split it in ...
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 ...
1
vote
1
answer
520
views
Check if item exists in same position in each nested list - Python
I'm writing a Connect Four script. The script takes a list of moves as input ("A_Red, "B_Yellow", "G_Red", etc), which I'm sorting into a matrix. Each nested list represents a ...
0
votes
1
answer
693
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: ...
3
votes
0
answers
135
views
Resetting an array of numbers by repeated subtraction from a sub-array
There is an array of numbers between 0 and 6 (base 7). For example {0 4 6 2 2 0 3 1}. Its maximum length can be 501 elements.
Each step we can subtract a number from any number of elements. All of the ...
0
votes
1
answer
990
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 ...
6
votes
1
answer
573
views
Taking views of views allocate in Julia
I noticed that taking views of non-"fast linear indexed" subarray allocates, while this is possible on classical vectors.
Any idea, how to make this allocation free?
Here is an illustration ...
0
votes
0
answers
118
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(&...
1
vote
2
answers
75
views
Sort data in column of a multi-dimensional array in a descending direction while preserving keys
I have the following array that I'm attempting to sort each scores array by answer from high to low.
$array = [
503 => [
'scores' => [
4573 => ['answer' => 100],
...
0
votes
1
answer
88
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']]
...
0
votes
0
answers
32
views
Why is my solution to the k subarray product problem not working?
I implemented a solution that uses xor to know if a product has been computed before. It recursively gets all the possible products. Here's the code:
#include <stdio.h>
int driver(int *arr, int ...
-3
votes
1
answer
197
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 ...
1
vote
3
answers
3k
views
Algorithm to generate all Subarrays from initial Array
How to generate all subarrays from an initial array?
Let's consider an array: [1,1,1,1].
I would like to generate all possible subarrays (in no particular order).
Expected result:
[1], [1], [1], [1],
...
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
144
views
How to search a matrix/array for a subarray containing a specific string (Javascript)
I have a matrix along the lines of this:
const chartypes = [
["bad boy","masc"],
["celebrity","neut"],
["activist","neut"],
[...
0
votes
1
answer
446
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
185
views
Sub arrays in python
How to print the sum of user desired subarray from a given list [1,2,3,4,5,6] using slice method in python ?
I've got success till the slice method and displaying the subarray , but I am not able to ...
-1
votes
1
answer
58
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
0
answers
122
views
Automating for loop between multiple subarrays in Python
I have multiple dbf files in a directory that I am trying to loop through and convert into excel files. However, because the loop times out after running for a while I divided the array list into ten ...
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 ...
0
votes
1
answer
375
views
Question Regarding Binary Subarrays With Sum
I was interested in finding a solution to a problem that provided a binary array called nums - which, for example, could consist of 10101, or 00000, or any such combination of 0 and 1's - from this, ...