Questions tagged [array-algorithms]

Array Algorithms are defined as functional algorithms where each step of the algorithm results in a function being applied to an array, producing an array result

Array Algorithms are defined as functional algorithms where each step of the algorithm results in a function being applied to an array, producing an array result. Array algorithms are compared with non-array algorithms.

A brief rationale for teaching array algorithms is given together with an example which shows that array algorithms sometimes lead to surprising results.

Example of such algorithms:

  • Sorting
  • Searching an number in array
  • Finding most frequent number in a list
  • Maximum Sum Subset
322 questions
22
votes
16 answers

Given an array of integers, find the first missing positive integer in linear time and constant space

In other words, find the lowest positive integer that does not exist in the array. The array can contain duplicates and negative numbers as well. This question was asked by Stripe in it's programming interview. I have devised a solution for the same…
14
votes
4 answers

Given an array [a1b2c3d4] convert to [abcd1234]

Constraints : O(1) space O(n) Time It is not a homework question just a interesting question I came across. Here are some solutions I could think of but nothing that does it in given constraints. Method 1 *With O(n) memory * Divide the array in…
amit modi
  • 1,098
  • 1
  • 11
  • 26
10
votes
2 answers

Finding overlapping data in arrays

We are writing a C# application that will help to remove unnecessary data repeaters. A repeater can only be removed in the case that all data it receives are received by other repeaters. What we need as a first step is explained bellow: I have…
genichm
  • 525
  • 7
  • 18
9
votes
5 answers

How to find the permutation of a sort in Java

I want to sort an array and find the index of each element in the sorted order. So for instance if I run this on the array: [3,2,4] I'd get: [1,0,2] Is there an easy way to do this in Java?
user491880
  • 4,709
  • 4
  • 28
  • 49
8
votes
4 answers

Can I find the max/min value in an unsorted Array in sub linear time?

Is it possible? If not, given an array of size n, how do I know if its better to just sort the array?
user779420
  • 359
  • 2
  • 3
  • 12
8
votes
4 answers

Linq get values not shared across multiple lists

What's the most efficient way to write a method that will compare n lists and return all the values that do not appear in all lists, so that var lists = new List> { new List { 1, 2, 3, 4 }, …
Brad Urani
  • 1,429
  • 1
  • 16
  • 29
8
votes
4 answers

Sorting Coordinate Points c++

in an application I measure a lot of 2d coordinates (x,y) of a pattern. This pattern consists of a set of points on grid with fixed pitches in x and y direction. These coordinates all have a score for quality and are sorted on this score. What I…
Nichole Grace
  • 213
  • 1
  • 2
  • 11
7
votes
2 answers

Algorithm to find non-dominated pairs

Given are pairs of integers (a1,b1),...,(an,bn). Pair i is "dominated" by pair j if ai < aj and bi < bj. What is an algorithm to quickly determine the list of pairs that are not dominated by any other pair? We can check all the pairs, and for each…
John
  • 4,596
  • 11
  • 37
  • 43
6
votes
4 answers

How to find a 2 unpaired elements in array?

You have an array with n=2k+2 elements where 2 elements haven't pair. Example for 8 elemets array: 1 2 3 47 3 1 2 0. "47" and "0" haven't pair in array. If I have array where only 1 element has't pair, I solve this problem with XOR. But I have 2…
rodart
  • 77
  • 4
6
votes
2 answers

What's the best algorithm for finding the sum of all items in an arbitrary sub array

I have a problem, and an OK-ish solution. I'm hoping there's a better solution out there. The problem I have an array with around 200,000 integers. Given two indices, i1 and i2, I need to calculate the sum of all elements between i1 and i2. Each…
Bernie Sumption
  • 444
  • 6
  • 11
6
votes
7 answers

Weird bug in Javascript splice method

I have an array which contains "Zeros" and I want to move all of the "Zeros" to the last indexes of the array. The expected output is: [1,2,3,0,0,0,0] But instead I get: [1,2,0,3,0,0,0] let a = [0, 1, 2, 0, 0, 3, 0]; let count = 0; let len =…
6
votes
2 answers

Data Structures: If Heaps are trees, why are they implemented internally with Lists or arrays?

I'm giving myself a refresher course in data structures and algorithms (and learning new stuff -- I was an Information Systems major in college instead of Computer Science, so I didn't get a formal education in these things), and I've been working…
Ari Roth
  • 5,392
  • 2
  • 31
  • 46
6
votes
4 answers

Median of medians algorithm: why divide the array into blocks of size 5

In median-of-medians algorithm, we need to divide the array into chunks of size 5. I am wondering how did the inventors of the algorithms came up with the magic number '5' and not, may be, 7, or 9 or something else?
Darth.Vader
  • 5,079
  • 7
  • 50
  • 90
5
votes
1 answer

Sorting an array of integers in nlog(n) time without using comparison operators

Imagine there's have an array of integers but you aren't allowed to access any of the values (so no Arr[i] > Arr[i+1] or whatever). The only way to discern the integers from one another is by using a query() function: this function takes a subset of…
5
votes
3 answers

finding a pair of integers in a sorted array which sum to K

Given a sorted array of integers, how can we find a pair of integers that sum to K? e.g. array = 1,3,5,6,10, K = 6, the answer is 1 and 5. Time complexity should be minimized.
akshay
  • 5,235
  • 14
  • 39
  • 49
1
2 3
21 22