Questions tagged [sub-array]

A sub-array is a subset of an array.

434 questions
3
votes
2 answers

How to get a subarray in numpy

I have an 3d array and I want to get a sub-array of size (2n+1) centered around an index indx. Using slices I can use y[slice(indx[0]-n,indx[0]+n+1),slice(indx[1]-n,indx[1]+n+1),slice(indx[2]-n,indx[2]+n+1)] which will only get uglier if I want a…
user3799584
  • 917
  • 1
  • 9
  • 18
3
votes
5 answers

Check if all items within sub-array are identical Ruby

Trying to check if all items within sub-arrays are the same. For example, I have a 5x5 board and I want to know if one of the arrays contains all x's: board = [[47, 44, 71, 8, 88], ['x', 'x', 'x', 'x', 'x'], # [83, 85, 97, 'x',…
Nappstir
  • 995
  • 2
  • 20
  • 38
3
votes
1 answer

mongodb update or insert sub-array

ok, I need away to do the following I have the mongo collect as array ( '_id' => new MongoId("50513d8338fc5de706000000"), 'offers' => array ( '0' => array ( 'minspend' => '50.00', 'cashback' => '1.50', 'percentage'…
RussellHarrower
  • 6,470
  • 21
  • 102
  • 204
2
votes
2 answers

Min Increment/Decrement operation to make all subarray sum of length k equal

I am solving a problem where I have been given an array A of length N and an integer 0
Ashish
  • 33
  • 3
2
votes
2 answers

Problem with Divide and Conquer Maximum Consecutive Subarray (MCS)

I want to find a nonempty, contiguous subarray for a given input array of integers, that can have duplicate values. I tried the divide and conquer method to find the maximum consecutive subarray of an array, which returns a different result as…
sij
  • 296
  • 3
  • 13
2
votes
1 answer

Program that read two arrays from the user(L1 and L2) and prints L1 - L2 if L2 is a sub array of L1

I created this program that read two arrays from the user(L1 and L2) and prints L1 - L2 if L2 is a sub array of L1.For example, if L1 ={'a','2','c','d','e'} and L2 ={'2','c','d'} then it should print {'a', 'e'}. But I am having some problem in the…
2
votes
1 answer

How to create subarrays without duplication Swift

I have a Meal structure in my SwiftUI project struct Meal: Identifiable, Codable, Equatable { var id = UUID().uuidString var name: String var time: String var type: String var recommendation: Bool } I also have the…
2
votes
0 answers

Count the number of contiguous subarrays with maximum element as x

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
votes
2 answers

How to find all contiguous sub array combinations of an array and print it

I need to write a program to print all sub arrays of an array in a particular format. Example- I/o: n = 3 A = (1,2,3) where n is the size of the array and A is the array itself. O/p: (1),(2),(3) (1),(2,3) (1,2),(3) (1,2,3) I am able to get all…
meowmee
  • 35
  • 7
2
votes
0 answers

Find the number of sub arrays (continuous) which have sum = K (Taking into account time complexity)

I am trying to find the number of sub arrays which equals to a given sum K. For example, when I have an arr = [1, -1, 1, -1, 1], I created a new array such that: int[] s = new int[arraySize + 1]; s[0] = 0; for (int i = 1; i…
python
  • 43
  • 4
2
votes
6 answers

Grouping Subarrays in Arrays

I'm wrapping my head around the following logic, but I'm still missing something. Given an Array like const testArr = ["F", "F", "C", "C", "F", "C", "F"]. The result array should look like ["F", "F", ["C", "C"], "F", ["C"], "F"]. The code I came up…
2
votes
4 answers

How do I delete the end element of a subarray?

So I've created a numpy array: import numpy as np a = np.array([[1,2,3],[4,5,6],[7,8,9]]) I'm trying to delete the end element of this array's subarray: a[0] = (a[0])[:-1] And encounter this issue: a[0] = (a[0])[:-1] ValueError: could not…
grzyb
  • 23
  • 4
2
votes
0 answers

Maximal contiguous subarray with at most K elements

Is there a way to find in O(N) complexity, a maximal contiguous subarray with at most K elements. Or there isn't such a possibility. for example: A = [4, -2, 6, 4, -2, 6] K = 3 because 6 and 4 in the middle are equal to 10, the function should…
Avraham
  • 61
  • 1
  • 4
2
votes
6 answers

Java: Split array of strings sorted by string length into several arrays by string length

I currently have an array of strings that is sorted by string length for example: String[] array = [a,b,c,ab,cd,abc,abcde,fghij,klmno] How would turn this array into several arrays depending on string size while keeping track of what the string…
timhoangt
  • 23
  • 3
2
votes
1 answer

What is the count of subarrays that will include a particular index 'i'?

Consider an array of 'n' elements, where ai is an element at index i where 1<=i<=N. I need to count the number of sub-arrays(contiguous sub-sequences of the array) that will include a particular index i. For example, consider, A = [1,2,3,4,5] The…