Questions tagged [sub-array]

A sub-array is a subset of an array.

434 questions
3
votes
3 answers

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 O(n^2). But need solution for O(nlogn). Example…
Prachi Palod
  • 31
  • 1
  • 2
3
votes
1 answer

Intuition behind calculating values for 'atmost K' and 'atmost K-1' to get the answer for 'equals K'

I am trying to solve an algorithm problem: Given an array nums of positive integers, call a (contiguous, not necessarily distinct) subarray of nums "good" if the number of different integers in that subarray is exactly k. For example, [1,2,3,1,2]…
J. Doe
  • 1,291
  • 2
  • 10
  • 19
3
votes
5 answers

Java Deque (Finding the max number of unique integers from subarrays.)

I was trying to solve a HackerRank problem on Java Deque. My code passed all the cases apart from the ones which have 100,000 inputs. Problem: In this problem, you are given N integers. You need to find the maximum number of unique integers among…
Code Hard
  • 43
  • 4
3
votes
2 answers

Split array of subarrays by subarrays sum size Ruby

I have an array of subarrays: arr = [["a", "b", "c"], ["a", "b"], ["a", "b", "c"], ["a", "c"], ["c", "v"], ["c", "f"], ["e", "a"], ["a", "b", "v"], ["a", "n", "c"], ["a", "b", "m"], ["a", "c"], ["a", "c", "g"]] I want to put elements…
mila002
  • 327
  • 3
  • 14
3
votes
2 answers

How to find N maximum product subarrays of M elements of a Numpy array?

I have a Numpy array, and I need to find the N maximum product subarrays of M elements. For example, I have the array p = [0.1, 0.2, 0.8, 0.5, 0.7, 0.9, 0.3, 0.5] and I want to find the 5 highest product subarrays of 3 elements. Is there a "fast"…
Andrea Belle
  • 95
  • 1
  • 4
3
votes
1 answer

Making Maximum Length Ascending Sub array from an array with only 3 valid moves

I need to solve this problem with DP and here is the problem: we have an array and we want to make a ascending sub array with maximum size with 2 conditions: We can just traverse the array one time from left to right. We have just two valid moves…
3
votes
2 answers

How many contiguous subarrays with max. n unique numbers

I found a programming challenge online and was wondering if there is a more efficient solution to this. The problem: You are given a list of n numbers along with a number X which refers to the maximum number of different numbers that can be…
Quiti
  • 132
  • 8
3
votes
1 answer

numpy slicing and indexing different results

In numpy subarrays obtained through any of slicing, masking or fancy indexing operations are just views to the original array, which can be demonstrated as follows: $ python3 Python 3.5.2 (default, Nov 23 2017, 16:37:01) [GCC 5.4.0 20160609] on…
Leon
  • 31,443
  • 4
  • 72
  • 97
3
votes
3 answers

Get a sub array from multidimensional array using php

I want to get a value form a multidimensional array using PHP. I pass the key to the function and if the key contains the value (i.e. without any array value), it will return that. But if that key contains an array value, it will return the whole…
3
votes
2 answers

Randomly sample sub-arrays from a 2D array in python

Problem: Let's say I have a 2D array from which I want to randomly sample (using Monte-Carlo) smaller 2D sub-arrays as shown by the black patches in the figure below. I am looking for an efficient method of doing this. Prospective (but partial)…
user11
  • 191
  • 1
  • 3
  • 11
3
votes
4 answers

The Java way to copy a subarray into an initialized array

outputArray[w:lastIndex] = array[v:lastIndex] I want to copy a subarray into another sub array which has already been initialized. Is there any inbuilt function which can check : 1) Number of elements to be copied are same. 2) That it is not…
technazi
  • 888
  • 4
  • 21
  • 42
3
votes
3 answers

Length of largest substring adds up to S

I was asked the following question in an interview and I couldn't give the optimal answer to this. Problem: Write a program that can find the length of the largest contiguous sub-array that sums up to S. Given an array of variable size and an…
kishoredbn
  • 2,007
  • 4
  • 28
  • 47
3
votes
4 answers

Finding product of all elements of all sub arrays of an array

I am having an array A which is having n elements. I want to find out the multiplication of all the elements in all the sub arrays possible of array A. I am expecting the solution to be implemented with the help of DP. I want to store all the…
vivek sehgal
  • 59
  • 2
  • 4
3
votes
3 answers

Sort array of objects by subarray property value in javascript

I have an array of JavaScript objects: var people = [ { "name": "Edward", "age": 100, "wallet": { "location": "home", "cash": 500 }, "bank": { "location": "bank street", "cash": 22100 } }, { …
Beeq
  • 33
  • 3
3
votes
6 answers

ruby - split an array into sub arrays when a value changes and ignore/delete that value

I want to split the following array into sub arrays so that the sub-arrays start and finish when the 1's start and finish... a=[1,1,0,0,1,0,1,1,1] so I end up with this as a new array... => [[1,1],[1],[1,1,1]] anyone got any ideas...?
treeseal7
  • 739
  • 8
  • 22
1 2
3
28 29