alternative peak and valley algorithm confusion [closed] - algorithm

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Wondering if anyone have worked on similar problem before? And my question is, why {8, 6} are peaks? I think 8 is peak, but since 6 is smaller than 8, it should not be a peak? Thanks.
In an array of integers, a "peak" is an element which is greater than or equal to the adjacent integers and a "valley" us an element which is less than or equal to the adjacent integers. For example, in the array {5,8,6,2,3,4,6} {8,6} are peaks and {5,2} are valleys. Given an array of integers, sort the array into an alternating sequence of peaks and valleys.
Example,
Input: {5,3,1,2,3}
Output: {5,1,3,2,3}
thanks in advance,
Lin

The 6 referred to is the 2nd 6 at the end of the sequence. This fits the description well (if not made very clear) and is backed up by 5 being a valley.
An alternating sequence of peaks and valleys is a sequence such that either all of the elements in odd-numbered positions are peaks and those in even-numbered positions are valleys, or vice-versa. The output sequence in the example demonstrates the elements of the input sequence sorted in such a way.

Related

Which number appeared once? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 6 years ago.
Improve this question
Given a list of 2n-1 numbers: all between 1 to n, all but one occur twice. Determine the number that occurs only once. Multiple ways preferred.
I think the problem is at fault, how can you determine which number without knowing the list of numbers?
[O(1) space, O(n) time]: Just take the XOR of all the numbers. Since all the numbers occur two times except one, XOR of those numbers will be zero and the single occurring number will be the result.
[O(1) space, O(n) time]: As said by user3386109 in comments, we can sum all the given numbers and compare that to the sum of numbers in the range [1, n] which will be n*(n+1) (since all numbers are supposed to occur twice). The difference of the two numbers is the answer.
[O(n) space, O(n) time]: Create an array of size n and keep the count of all the elements in the array at their corresponding positions. At the end, traverse the array, and find the number whose count is only 1.

Possible Array combinations based on constraints [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
How many unique arrays of m elements exist such that they contain numbers in the range [1,n] and there exists atleast one subsequence {1,2,3,4....n}?
Constraints: m > n
I thought of combinations approach. But there will be repetitions.
In my approach, I first lay out all the numbers from 1 to n.
For example, if m=n+1, answer is n^2. (n spots available, each number in range [1,n])
Now, I think there might be a DP relation for further calculation, but I am not being able to figure it out.
Here's an example for n=3 and m=5. The green squares are the subsequence. The subsequence consists of the first 1 in the array, the first 2 that's after the first 1, etc. Squares that aren't part of the subsequence can either take n values if they are after the end of the subsequence, or n-1 values otherwise.
So the answer to this example is 1*9 + 3*6 + 6*4 = 51, which is easily verified by brute force. The coefficients 1,3,6 appear to be related to Pascal's triangle. The rest is left to the reader.

What's the minimal column sums difference? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Imagine you are given a matrix of positive integer numbers (maximum 25*15, value of number does not exceed 3000000). When you do column sums and pick the smallest and the largest one, the difference between them must be the smallest possible.
You can swap numbers in every row (permute rows), not in column, how many times you want.
How would you solve this task?
I'm not asking for your code but your ideas.
Thanks in advance
I would make an attempt to solve the problem using Simulated Annealing. Here is a sketch of the plan:
Let the distance to optimize the difference between the max and min column sums.
Set the goal to be 0 (i.e., try to reach as close as possible to a matrix with no difference between sums)
Initialize the problem by calculating the array of sums of all columns to their current value.
Let a neighbor of the current matrix be the matrix that results from swapping two entries in the same row of the matrix.
Represent neighbors by their row index and two swapping column indexes.
When accepting a neighbor, do not compute all sums again. Just adjust the array of sums in the columns that have been swapped and by the difference of the swap (which you can deduce from the swapped row index)
Step 6 is essential for the sake of performance (large matrices).
The bad news is that this problem without the limits is NP-hard, and exact dynamic programming at scale seems out of the question. I think that my first approach would be large-neighborhood local search: repeatedly choose a random submatrix (rows and columns) small enough to be amenable to brute force and choose the optimal permutations while leaving the rest of the matrix undisturbed.

How many combinations are there to put n chapters into k(<n)volumes? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Also, how can algorithmicly generate them within polynomial of n? Pseudo-code is ok.
This problem comes down to selecting k-1 borders, which divide n elements in k parts. As such, k-1 positions need to be selected out of n+k-1 positions, resulting in
possibilities. As an example, let's say we need to divide four sweets among three children. In this case, a first possibility would be to put the two borders on the first two positions, leaving the four sweets in positions 3-6:
As such, the first two children get no candy, while the third child gets all four. Another possibility would be to put the first border at position 2 and the second border at position 4:
Now the first two children get one candy each (positions 1 and 3), while the third child gets two (positions 5-6). Iterating over all positions results in a total of 15 possibilities.
Note that the answer is different when all volumes need to contain at least one chapter. In this case, k items are already given to one volume each, leaving n-k chapters. As such, there are
possibilities. Note that the condition k<n is important in this case.

Proposing an O(logm) algorithm for the following [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need to propose an algorithm for the following: let us assume that we have an array consisting of zeros and ones. The array is filled with zeros from the beginning of the array to the index m, and all remaining indexes are filled with ones. I need to find this index m in O(logm) time. Here is what i thought: I think this is like binary search, first i look at the middle element of the array, if that is zero, then i forget about the left part of the array and do the same for the right part, and continue like this until i encounter a one. If the middle element is one, then i forget about the right part and do the same for left part of the array. Is this a correct O(logm) solution? Thanks
It is not "like" a binary search - it is a binary search. Unfortunately, it is O(logN), not O(logM).
To find the borderline in O(logM), start from the other end: try positions {1, 2, 4, 8, 16, ... 2^i} and so on, until you hit a 1. Then do a binary search on the interval between 2^i and 2^(i+1), where 2^i+1 is the first position where you discovered a 1.
Finding the first 1 takes O(logM), because the index is doubled on each iteration. After that, the binary search takes another O(logM), because the length of the interval 2^i..2^(i+1) is less than M as well.

Resources