Questions tagged [arrays]

A sequential random-access data structure whose size can typically not be changed after creation.

576 questions
66
votes
3 answers

In-place algorithm for interleaving an array

You are given an array of $2n$ elements $$a_1, a_2, \dots, a_n, b_1, b_2, \dots b_n$$ The task is to interleave the array, using an in-place algorithm such that the resulting array looks like $$b_1, a_1, b_2, a_2, \dots , b_n, a_n$$ If the in-place…
Aryabhata
  • 6,291
  • 2
  • 36
  • 47
53
votes
6 answers

Keeping a String Secret in (Open) Source Code

I have finished developing an app for Android and intend to publish it with GPL -- I want it to be open source. However, the nature of the application (a game) is that it asks riddles and has the answers coded into the string resource. I can't…
Nevermore
  • 667
  • 1
  • 5
  • 9
38
votes
4 answers

How to measure "sortedness"

I'm wondering if there is a standard way of measuring the "sortedness" of an array? Would an array which has the median number of possible inversions be considered maximally unsorted? By that I mean it's basically as far as possible from being…
Robert S. Barnes
  • 2,941
  • 5
  • 25
  • 24
34
votes
5 answers

Adding elements to a sorted array

What would be the fastest way of doing this (from an algorithmic perspective, as well as a practical matter)? I was thinking something along the following lines. I could add to the end of an array and then use bubblesort as it has a best case…
soandos
  • 1,143
  • 2
  • 10
  • 23
26
votes
2 answers

What is the advantage of heaps over sorted arrays?

I'm fairly new to heaps and am trying to wrap my head around why min and max heaps are represented as trees when a sorted array appears to both provide min / max properties by default. And a follow up: what is the advantage of dealing with the…
Nick Olinger
  • 363
  • 1
  • 3
  • 6
25
votes
3 answers

Array access is O(1) implies a fixed index size, which implies O(1) array traversal?

Arrays are generally presented as data structures with $\Theta(N)$ traversal and $\Theta(1)$ random element access. However, this seems inconsistent: if array access is really $\Theta(1)$, this means that the size of an element index is bounded by…
23
votes
7 answers

One element that differs in two arrays. How to find it efficiently?

I am preparing for a coding interview and I can't really figure out the most efficient way to solve this problem. Let's say we have two arrays consisting of numbers that are unsorted. Array 2 contains a number that Array 1 does not. Both arrays have…
21
votes
1 answer

Is there an existing data structure that is of fixed size, and will push the oldest/last element out if a new element is inserted?

I'm looking for a data structure that will push its oldest/last element out if a new element is inserted. For example, let D represent the structure. D contains 3 elements of the type Number D's default values will be initialized to 1, 2 and 3. $$D…
Greg M
  • 313
  • 2
  • 8
21
votes
3 answers

Is there an algorithm which finds sorted subsequences of size three in $O(n)$ time?

I want to prove or disprove the existence of an algorithm which, given an array $A$ of integers, finds three indices $i, j$ and $k$ such that $i < j < k$ and $A[i] < A[j] < A[k]$ (or finds that there is no such triple) in linear time. This is not a…
20
votes
3 answers

Time complexity $O(m+n)$ Vs $O(n)$

Consider this algorithm iterating over $2$ arrays $(A$ and $B)$ size of $ A = n$ size of $ B = m$ Please note that $m \leq n$ The algorithm is as follows for every value in A: // code for every value in B: // code The time complexity of…
19
votes
1 answer

Saving on array initialization

I recently read that it is possible to have arrays which need not be initialized, i.e. it is possible to use them without having to spend any time trying to set each member to the default value. i.e. you can start using the array as if it has been…
Aryabhata
  • 6,291
  • 2
  • 36
  • 47
16
votes
2 answers

Largest sum divisible by n

I asked this question on StackOverflow, but I think here is a more appropriate place. This is a problem from Introduction to algorithms course: You have an array $a$ with $n$ positive integers (the array doesn't need to be sorted or the elements…
16
votes
7 answers

How to implement two stacks in one array?

I want to begin by saying that this is NOT a homework question. I am reading Introduction to Algorithms - the famous CLRS text to become a better programmer. I am trying to solve the problems and exercises given in the book by myself. I am trying…
Suyash Gupta
  • 161
  • 1
  • 1
  • 3
15
votes
9 answers

How to find 5 repeated values in O(n) time?

Suppose you have an array of size $n \geq 6$ containing integers from $1$ to $n − 5$, inclusive, with exactly five repeated. I need to propose an algorithm that can find the repeated numbers in $O(n)$ time. I cannot, for the life of me, think of…
darylnak
  • 275
  • 2
  • 7
14
votes
1 answer

Counting inversion pairs

A classic application of divide and conquer is to solve the following problem: Given an array $a[1\dots n]$ of distinct, comparable elements, count the number of inversion pairs in the array: pairs $(i,j)$ such that $a[i] \gt a[j]$ and $i \lt…
Aryabhata
  • 6,291
  • 2
  • 36
  • 47
1
2 3
38 39