Efficient method of finding single unsorted element in array - arrays

Assume an array of length n, which is sorted. (values are arbitrary, can be negative, not only ints)
One element is out of place, for example
-1, 2, 3.0, 4, 5, 10, 6, 7, 8, 9, 11, 12, 13, 14
n can be large.
Is there a way better than o(n) to find that element?

Related

Find the maximum sum among all segments formed after removing one element in every iteration

An array A of size N having positive integers is given. Another array B having permutation of numbers 1 to N is given. Your task is to perform N steps :
At i-th step, you have to remove element A[B[i]] from array A.
After removing the elements, the array might get broken in multiple segments. Find the segment with maximum element sum after each step of removal.
Return an array of size N having maximum sum after each step.
eg:
A: [10, 20, 5, 17, 22] B: [4, 1, 3, 5, 2]
Ans: [35, 25, 22, 20, 0]
eg-2
A: [10, 20, 5, 17, 2, 7, 8, 22, 13, 14, 19, 9] B: [7, 4, 8, 10, 1, 9, 3, 12, 5, 6, 2, 11]
Ans: [77,77,55,35,28,28,28,20,20,20,19,0]
will get solution in O(n2) but i want solution in o(n)

How do you subdivide an array into n number of subarrays, such that the sum of each subarray is as equal as possible?

So an example of the question is as follows:
Let's say we want to subdivide [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] into 3 sub-arrays.
While I imagine there are many correct answers one of them would be, [10, 8], [9, 7, 2], [1, 3, 4, 5, 6]. The reason being that here the sum of the sub arrays is, 18, 18, 19, meaning they are as close to equal as they can possibly be.
Is there an algorithm that can consistently return such an answer given any starting array and any number of sub-arrays to divide into? (Assuming that: length of the starting array > number of sub-arrays)
(PS If you want to show your logic in code I feel the most comfortable with python.)

How to represent a nested array as one-dimensional array?

How to represent (serialise) an arbitrarily nested array, as a one-dimensional array of values (and meta-data) so that the original nested array and it's structure can be recreated from the serialised one-dimensional array?
I'm looking for a space efficient algorithms for this problem.
For example:
[
[
[1, 2, 3, 4]
],
[
[5, 6, 7, 8],
[9, 10]
]
]
Should be serialised / deserialised to / from something like this
[/* elements of meta data* /, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
You could represent a nested array with a sequence that first contains the length of that array (also providing the information that the following content is a sub-array) and then the elements themselves.
If you only have positive numbers as your values, but can store negative numbers, you could use negative numbers as the indicator for sub-arrays (if not, you can of course just use an offset O, which is the highest number you want to store, and treat all numbers greater then O as indicator for a new sub-array). The serialized version of your example would then look like this:
[-2, -1, -4, 1, 2, 3, 4, -2, -4, 5, 6, 7, 8, -2, 9, 10]
To understand better how it's working, here is an indented version of the same serialized array:
[-2,
-1,
-4
1, 2, 3, 4
-2
-4
5, 6, 7, 8
-2
9, 10
]
This structure can be serialized and deserialized in linear time using a recursive algorithm.

Find first inversion for an element in an array

Given an Array A with indices 1 <= i <= n, find the very first j, i + 1 <= j <= n, such that A[j] > A[i]. To find the total number of inversions for any given element, we create a copy of A and mergesort that copy. Then for each i in A, we perform a binary search on the copy of A(call it B) to locate it's position and the number of inversion for that element is one less then it's position in the copy of A. I understand this, but i'm having trouble determining how to modify this approach to locate the minimum possible conversion.
Example: A = {8, 3, 34, 13, 1, 2, 21, 5}
After copy and merge: B = {1, 2, 3, 5, 8, 13, 21, 34}
indices: {5, 6, 2, 8, 1, 4, 7, 3}
We see A[1] = 8, do a binary search on Array B, we find 8 at position 5, the algorithm should return 34 (index 3). If no inversion exists, return -1.
Complexity should be O(nlogn), i've sat through this problem for a few hours and can't think of a solution, all I could think of is finding the number of inversion but not the index j. Any help would be appreciated.

irregular slicing/copying in numpy array

Suppose I have an array with 10 elements, e.g. a=np.arange(10). If I want to create another array with the 1st, 3rd, 5th, 7th, 9th, 10th elements of the original array, i.e. b=np.array([0,2,4,6,8,9]), how can I do it efficiently?
thanks
a[[0, 2, 4, 6, 8, 9]]
Index a with a list or array representing the desired indices. (Not 1, 3, 5, 7, 9, 10, because indexing starts from 0.) It's a bit confusing that the indices and the values are the same here, so have a different example:
>>> a = np.array([5, 4, 6, 3, 7, 2, 8, 1, 9, 0])
>>> a[[0, 2, 4, 6, 8, 9]]
array([5, 6, 7, 8, 9, 0])
Note that this creates a copy, not a view. Also, note that this might not generalize to multiple axes the way you expect.

Resources