Is this a graph problem or something else? - arrays

I've come across a problem that at first looks like the well known maximum sum subarray problem but there's a twist that makes things much more complex.
Suppose you have two arrays each containing the same amount of "1"s and "-1"s. Additionally, suppose each "1" in the first array has a corresponding or sibling "1" in the second array. Same for each "-1". The task is to find the optimal subarrays, one in the first array and one in the second, such that their combined sum is the largest (maximal) with the added constraint that an element in one subarray only counts towards the sum if the other subarray contains its sibling.
Anyone know what kind of problem this is? It looks like it could be a graph problem in disguise but I'm not sure which. If there's optimal substructure here I don't see that either. I know it can be solved by complete search but surely there's a faster way.
Below is an example of the setup to the problem.
Here the optimal solution is subarray [2..9] in the first array with subarray [4..9] in the second array for a sum of 8.

Related

sort array in fewest operations with k operations

I was at an interview, and the interviewer asked me to make a function that would find a list of operations that would sort an array in the fewest operations.
The allowed operations were swapping any two numbers in the array.
For example, given the array [0,3,4,1], two possible answers would be
[swap(1,2), swap(2,3)]
[swap(2,3), swap(1,2)]
I had already seen a question like that here, so I solved it with a modified version of the solution there.
However, after that, the interviewer changed the question a bit.
The goal was still to find the shortest list of operations to sort the array, but now the operations were rotating the array to left, rotating the array to the right, and swapping the numbers at index 0 and 1.
I tried to solve it with backtracking and a hash table, but my implementation did not work, and I also feel that there is a general solution to this problem given any k operations. For example, what if we were allowed to swap the two numbers in the middle or to rotate the array by two elements to the left but only one element to the right?
How would you solve this?

In an ordered array, is it generally faster to find out an item is not in the array than to find out it is?

In an ordered array, is it generally faster to find out an item is not in the array than to find out it is?
Our teacher asked this question, mostly we said no but he said the answer is yes, it is faster. I really got no clue whatsoever how come even that is possible. After all, to find out that the item is not in the array we must make the most comparisons but if it is in the array we will possibly find it before that.
Can anybody explain? Thanks in advance.
Consider the following construction: Let's define a target area inside the array which contains the places where the element in question could be. When the search algorithm starts, the target area is the whole array because we have not looked at anything in the array yet.
Now suppose you do a binary search (which would be most appropriate for ordered arrays). You would look at the element in the middle of the array. If that element is smaller than the element you're looking for, you know that the element you're looking for must be on its right. Therefore the target area has been reduced to the right half of the array. Same in the opposite case: If the element you looked at was bigger, then the new target area is the left half of the array.
As you see, every step of the search algorithm will reduce the target area in some way, so that you get closer and closer to your answer. That holds true for each search algorithm. For example, if you were to iterate linearly through the elements, you would be reducing the target area by one element in each step.
Regardless of whether you're checking for inclusion (whether the item is in the array) or exclusion (whether it's not in the array), your algorithm stops at one of two situations:
While trying to narrow down the search area, you happen to pick an element from the array that is just the item that you were looking for. At this point, the inclusion test would return true, or the exclusion test would return false.
The target area is exhausted (i.e., reduced to an empty set). In this case, inclusion yields false, exclusion yields true.
From this reasoning follows that the inclusion and exclusion test are entirely symmetrical, therefore I agree with your "no". But please, do ask your teacher to explain his reasoning and post it here.

Number of swaps performed by Bubble-Sort on a given array

I have been given an array and I'm asked to find out the number of Swaps required to sort the array using Bubble Sort
Now we know that, we can find the comparisons by n(n-1)/2 but what I need is the number of actual swaps
My first instinct was to use bubble sort and with each swap(), I incremented a Swap variable. But the time complexity of this is a very slow process and I'd like your help to find an optimized way to solve my dilemma
P.S.: I also need to compare whether it is faster to sort it in ascending or descending.... Sorting it twice doubles the time.
Edit:
Sorry if I wan't clear enough. I want to find the swaps without using Bubble Sort at all.
Regard the applied swap() to a[i] and a[i+1] as a bubble-up of a[i].
Now, asking how many swaps are going to happen is the same as asking how many bubble-up operations are going to happen. Well, and how many do we have of those?
Each a[i] will bubble-up for each position j > i, where a[j]<a[i]. In words a[i] will bubble-up for each position to the right, where the elements value at that position is smaller than a[i] itself. A pair of elements satisfying this condition is what is known as an inversion of a[].
So reformulating your question we could ask: What is the total number of inversions in a[]? (a.k.a. What is the inversion number of a[]?)
This is a well known problem and besides some obvious approaches running in O(n^2) a typical approach to this problem is to tweak merge-sort a bit in order to find this number. And since merge-sort runs in O(n*log(n)) you get the same running time to find the inversion number of a[].
Now that you know that you can tweak merge-sort, I suggest you give it a try on your own on how to do it exactly.
Hint: The main question you have to answer is: When positioning a single element during a merge-step of two arrays, how many inversion did I fix? Then simply add up all of those.
In case you still are stuck after giving it some thought, you can have a look at some full blown solutions here:
http://www.geeksforgeeks.org/counting-inversions/
Counting inversions in an array

How to find an almost sorted array?

I'm trying to create a program that will select the fastest sorting algorithm for a particular array of integers. I'm trying to check off the condition "is almost sorted," and was wondering what common practice to find this in the industry is.
Assume that there is a sorted array available to the coder. The two possible solutions I can think of are:
Loop through both lists simultaneously. Compare values at the index, find the percentage of correctly placed values. I understand that this is pretty quick (just O(N)), but it can be wildly inaccurate... what if everything is shifted by one space? This algorithm will give 0, but insertion sort will take a single run to do order this.
Find how far something is shifted from it's correct position in either direction (w/ wraparound). This seems to be a better solution, but could be pretty slow (O(N^2), since we might have to loop through a sorted list for every unsorted object, which could be corrected A BIT by comparing the value in a while loop).
Are there others? If not, which do I pick?
Thanks!

Finding the closest value to n, without going over, in a sorted array

For this problem I have a sorted Array of doubles. I need to be able to quickly and efficiently find index of the value that is closest to, but not over, n. Efficiency is key, the assignment states is can be done in O(log n) so I assume it can be done with some sort of modified binary search.
I know this has been asked before but all answers I found either assumed an unsorted array or simply looped through the entire array comparing differences.
Any guidance is appreciated. Thank you.
Yes, you can do it with a modified binary search; just look for the number as usual and;
If you find it, it's the correct number (close, but not over)
If you don't find it, if the last number you check is under, that's your number (you've already looked at the number above once and found it to be too high)
If you find it and the last number you check is over, I think you get how to find the highest one under.
Just remember that there are some error conditions to consider too and you'll do fine :)

Resources