A sub-array is a subset of an array.
Questions tagged [sub-array]
434 questions
6
votes
2 answers
Find if array can be divided into two subarrays of equal sum if any one element can be deleted
Given a array of numbers find if there is a way to delete/remove a number from the array and make one partition in the array( dividing the array into two subarrays ) such that sum of elements in subarray1 is equal to sum of elements in subarray2.
A…

code_it
- 131
- 7
6
votes
2 answers
find minimum-length subarray that has all numbers
File input.txt consists of two lines: first has integer number N space then integer number K (1 ≤ N,K ≤ 250000). Second has N space-delimeted integers, where each integer is less than or equal to K. It is guaranteed that each integer from 1 to K is…

DoctorMoisha
- 1,613
- 14
- 25
5
votes
2 answers
Swift - Subarrays of specific length
I have an array lets say [1, 2, 3, 4]. I have to check if an element or any combination of the elements sums to a specific number.
Examples
5, 1 + 4 = 5 and 2 + 3 = 5.
6, 1 + 2 + 3 = 6 and 2 + 4 = 6
On way could be to create a power-set of the…

Adeel Miraj
- 2,472
- 3
- 22
- 32
5
votes
4 answers
Frequency of each element of an array considering all contiguos subarrays
Consider an array A = [5,1,7,2,3]
All contiguos subarrays = { [5], [1], [7], [2], [3], [5,1], [1,7], [7,2], [2,3],
[5,1,7], [1,7,2], [7,2,3], [5,1,7,2], [1,7,2,3], [5,1,7,2,3] }
Replace all the arrays in the above set with maximum element in…

cryptomanic
- 5,986
- 3
- 18
- 30
5
votes
5 answers
Matching sub-array in array. Scheme in scheme
Ok, consider this:
I have a big array containing arrays, -1, a and b.
The -1 means the field is empty:
var board = [
[-1,-1, a],
[-1,-1, b],
[ b,-1, a]
]
Now i want to check smaller arrays agains this:
var solutions = [
[
…

Andreas Louv
- 46,145
- 13
- 104
- 123
4
votes
2 answers
How can I divide an array into K sub-arrays such that the sum of the number of duplicate elements in all the sub-array is minimum?
As an example, let the array be A={1,1,2,1,2} and K=3. A can be divided into {1}, {1,2} and {1,2}. So, number of duplicate elements in those sub-arrays is 0, 0 and 0. So, the sum of them is 0.

Atul Kumar Ashish
- 41
- 2
4
votes
4 answers
How to find maximum sum subarray of size between [L, R]
Given an array of both positive and negative integers, how would one find the maximum sum subarray (contiguous subarray) of length between L and R inclusive?
For example:
If the array is
-1 3 -2 5 3 -5 2 2
and L = 1 and R = 2, the answer would be…

Rahul
- 181
- 1
- 2
- 13
4
votes
2 answers
slicing a numpy array with characters
I have a text file made as:
0.01 1 0.1 1 10 100 a
0.02 3 0.2 2 20 200 b
0.03 2 0.3 3 30 300 c
0.04 1 0.4 4 40 400 d
I read it as a list A and then converted to a numpy array, that is:
>>> A
array([['0.01', '1', '0.1', '1', '10', '100', 'a'],
…

urgeo
- 645
- 1
- 9
- 19
4
votes
6 answers
How to sum the two largests elements in a list?
I have a list like this:
[1, 2, 5, 2, 7, 3, 9, 5...]
Is there an effective way to find the sum of the 2 largest elements here without:
for i in range():
for j in range():
I've found this:
"Maximum subarray problem"
But I've not completely…
user9088429
4
votes
2 answers
OCaml Array Slice?
I'm learning OCaml, and one thing that I have not been able to discover how to do is taking a slice of an array. For example, if I want to extract the subarray of 3 elements starting from index 2, I have to do: [|array.(2); array.(3); array.(4)|].…

Aman Dureja
- 93
- 3
- 9
4
votes
1 answer
C++: Find the maximum integer in an array of sub-arrays
I'm facing an issue where I want to write an algorithm that can return the max element of each consecutive sub-array of k elements within a larger array, and have these max elements read into their own array, like so:
Given int array = {3, 7, 20, 6,…

Rich
- 41
- 3
4
votes
1 answer
partition a 2D array without looping c++
Im fairly new to c++ and am trying to program strassen's algorithm to multiply matrices. Part of the algorithm requires me to partition a matrix into four parts e.g
4 5 6 7
6 7 8 9
1 2 3 4
2 3 5 6
partitioned:
4 5 6 7
6 7 8 9
1 2 3 4
2 3 5…

user2471711
- 85
- 1
- 8
4
votes
3 answers
Get the sub array in a bidimensional array having a particular key/value pair
I have a large PHP array, similar to:
$list = array(
array(
'id' = '3243'
'link' = 'fruits'
'lev' = '1'
),
array(
'id' = '6546'
'link' = 'apple'
'lev' = '2'
),
…

Irfanullah Jan
- 3,336
- 4
- 24
- 34
3
votes
4 answers
Sort a multi-dimensional array by the size of its sub-arrays
I have this multidimensional array:
Array
(
[0] => Array
(
[0] => 2012-02-26 07:15:00
)
[1] => Array
(
[0] => 2012-02-26 17:45:00
[1] => 2012-02-26 18:55:00
)
[2] => Array
…

peter
- 4,289
- 12
- 44
- 67
3
votes
0 answers
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 elements must be adjacent (form a continuous…

Altrey
- 31
- 4