Reverse stack in increasing order alternately - c++

What is the most elegant way (less code?) of reversing a stack in increasing order in an alternating manner? (non recursively)
EX.
1 2 3 4 5 6 7 8 9 10
1 [3 2] 4 5 6 [10 9 8 7]

I would use std::reverse. Will this work for you?
http://www.cplusplus.com/reference/algorithm/reverse/

std::stack is designed to be a LIFO (last-in first-out), and so it was not designed for you to change the indexes of values.
If you must change the index of the items than I would recommend using a different list.
(Does anyone know who the original creator to this image so I can give proper citation)

Related

Compute a new list of integers by adding consecutive elements in a list with C++ [duplicate]

This question already has answers here:
Calculate rolling / moving average in C++
(11 answers)
Closed 3 years ago.
I am a beginner in C++. I am wondering how can I compute a new list by adding 8 consecutive elements and then divide them by the number of elements added in a list with C++. For example, the new list is re[], and the list we'll be using is a[], it has 200 elements. so re[i] = (a[i-1]+a[i-2]+a[i-3]+a[i-4]+a[i]+a[i+1]+a[i+2]+a[i+3]+a[i+4])/9
for(int i=4;i<196;i++){
re[i] = (a[i-1]+a[i-2]+a[i-3]+a[i-4]+a[i]+a[i+1]+a[i+2]+a[i+3]+a[i+4])/9
}
However the above code is not applicable to the first 4 elements and the last 4 elements in re[], because a[i] in these cases has no 4 consecutive elements either preceding or following a[i].
So I am wondering how can I do the same using for loop for these elements?
Thanks for any help.
I don't really know what you are doing but you might need to use Modulo Symbol so you wont get out of range
For example:
I want to stick with number 7 not get more and not getting less than 0
I will say for example 1 mod 7 and I will get 1
how? because it takes the reminder of the division
To understand more about it go here
To calculate and try go here
in your example
for(int i=0;i<200;i++){
re[i] = (a[(i-1)%200]+a[(i-2)%200]+a[(i-3)%200]+a[(i-4)%200]+a[(i)%200]+a[(i+1)%200]+a[(i+2)%200]+a[(i+3)%200]+a[(i+4)%200])/9
}

how to select unique elements of a list in q#?

I'm new to q# programming.
I have a number list:
1 3 2 3 2 4 5 2 3 6 4 2 1 6 3 2 1
Now, I want to generate unique set of numbers in Q programming language where final output must be: 1 2 3 4 5 6
There is no library method for this in Q#, so you'd have to implement it yourself. If the range of the possible numbers is small (up to N), you can allocate an extra array of N elements and mark all the numbers that occur in the input array. Otherwise you can sort the input array and return all numbers which differ from the one right before them and right after them.
That being said, I wonder why do you need to do this in Q#? Q# is a domain-specific language, so a lot of things which are one or two library calls in general-purpose languages can be rather inconvenient to do in Q#. It is typically much easier to do them in C# or F# driver and pass the result to Q# code as a parameter.

Finding all values that occurs odd number of times in huge list of positive integers

I came across this question from a colleague.
Q: Given a huge list (say some thousands)of positive integers & has many values repeating in the list, how to find those values occurring odd number of times?
Like 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 1 2 3 4 5 6 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1...
Here,
1 occrus 8 times
2 occurs 7 times (must be listed in output)
3 occurs 6 times
4 occurs 5 times (must be listed in output)
& so on... (the above set of values is only for explaining the problem but really there would be any positive numbers in the list in any order).
Originally we were looking at deriving a logic (to be based on c).
I suggested the following,
Using a hash table and the values from the list as an index/key to the table, keep updating the count in the corresponding index every time when the value is encountered while walking through the list; however, how to decide on the size of the hash table?? I couldn't say it surely though it might require Hashtable as big as the list.
Once the list is walked through & the hash table is populated (with the 'count' number of occurrences for each values/indices), only way to find/list the odd number of times occurring value is to walk through the table & find it out? Is that's the only way to do?
This might not be the best solution given this scenario.
Can you please suggest on any other efficient way of doing it so??
I sought in SO, but there were queries/replies on finding a single value occurring odd number of times but none like the one I have mentioned.
The relevance for this question is not known but seems to be asked in his interview...
Please suggest.
Thank You,
If the values to be counted are bounded by even a moderately reasonable limit then you can just create an array of counters, and use the values to be counted as the array indices. You don't need a tight bound, and "reasonable" is somewhat a matter of platform. I would not hesitate to take this approach for a bound (and therefore array size) sufficient for all uint16_t values, and that's not a hard limit:
#define UPPER_BOUND 65536
uint64_t count[UPPER_BOUND];
void count_values(size_t num_values, uint16_t values[num_values]) {
size_t i;
memset(count, 0, sizeof(count));
for (i = 0; i < num_values; i += 1) {
count[values[i]] += 1;
)
}
Since you only need to track even vs. odd counts, though, you really only need one bit per distinct value in the input. Squeezing it that far is a bit extreme, but this isn't so bad:
#define UPPER_BOUND 65536
uint8_t odd[UPPER_BOUND];
void count_values(size_t num_values, uint16_t values[num_values]) {
size_t i;
memset(odd, 0, sizeof(odd));
for (i = 0; i < num_values; i += 1) {
odd[values[i]] ^= 1;
)
}
At the end, odd[i] contains 1 if the value i appeared an odd number of times, and it contains 0 if i appeared an even number of times.
On the other hand, if the values to be counted are so widely distributed that an array would require too much memory, then the hash table approach seems reasonable. In that case, however, you are asking the wrong question. Rather than
how to decide on the size of the hash table?
you should be asking something along the lines of "what hash table implementation doesn't require me to manage the table size manually?" There are several. Personally, I have used UTHash successfully, though as of recently it is no longer maintained.
You could also use a linked list maintained in order, or a search tree. No doubt there are other viable choices.
You also asked
Once the list is walked through & the hash table is populated (with the 'count' number of occurrences for each values/indices), only way to find/list the odd number of times occurring value is to walk through the table & find it out? Is that's the only way to do?
If you perform the analysis via the general approach we have discussed so far then yes, the only way to read out the result is to iterate through the counts. I can imagine alternative, more complicated, approaches wherein you switch numbers between lists of those having even counts and those having odd counts, but I'm having trouble seeing how whatever efficiency you might gain in readout could fail to be swamped by the efficiency loss at the counting stage.
In your specific case, you can walk the list and toggle the value's existence in a set. The resulting set will contain all of the values that appeared an odd number of times. However, this only works for that specific predicate, and the more generic count-then-filter algorithm you describe will be required if you wanted, say, all of the entries that appear an even number of times.
Both algorithms should be O(N) time and worst-case O(N) space, and the constants will probably be lower for the set-based algorithm, but you'll need to benchmark it against your data. In practice, I'd run with the more generic algorithm unless there was a clear performance problem.

Clarification on heaps - do they have to be filled to be a valid heap?

Aka
1
2 3
Is a valid heap?
Whereas
1
2
Is not, as the tree is not filled up on all levels?
Or does the structure property of heaps only specify that the heap is just filled out such that there is no "gap" between elements in level order. Meaning that the second heap is a valid heap as well?
Or does the structure property for heaps just require that the heap is FULL, aka every parent has 0 or two chidren?
So
1
2 3
4 7 9 99
Is a valid heap, as is
1
2 3
4 7
BUT NOT
1
2 3
4 7 9
?
This is mostly a question on terminology. However, almost always a heap is defined as a rooted tree, where:
for every node except root its key is not less than the key of its parent
every node has 0, 1 or 2 children and
the tree is almost a complete binary tree but for, possibly, the last level. The last level should be filled from left to right.
So, these are valid heaps:
1 5
2 3 9 8
3 6 9 11
And these are not:
1 5
2 9 8
3 13 9 10

Find the numbers missing

If we have an array of all the numbers up to N (N < 10), what is the best way to find all the numbers that are missing.
Example:
N = 5
1 5 3 2 3
Output: 1 5 4 2 3
In the ex, the number 4 was the missing one and there were 2 3s, so we replaced the first one with 4 and now the array is complete - all the numbers up to 5 are there.
Is there any simple algorithm that can do this ?
Since N is really small, you can use F[i] = k if number i appears k times.
int F[10]; // make sure to initialize it to 0
for ( int i = 0; i < N; ++i )
++F[ numbers[i] ];
Now, to replace the duplicates, traverse your number array and if the current number appears more than once, decrement its count and replace it with a number that appears 0 times and increment that number's count. You can keep this O(N) if you keep a list of numbers that don't appear at all. I'll let you figure out what exactly needs to be done, as this sounds like homework.
Assume all numbers within the range 1 ≤ x ≤ N.
Keep 2 arrays of size N. output, used (as an associative array). Initialize them all to 0.
Scan from the right, fill in values to output unless it is used.
Check for unused values, and put them into the empty (zero) slots of output in order.
O(N) time complexity, O(N) space complexity.
You can use a set data structure - one for all the numbers up to N, one for the numbers you actually saw, and use a set difference.
One way to do this would be to look at each element of the array in sequence, and see whether that element has been seen before in elements that you've already checked. If so, then change that number to one you haven't seen before, and proceed.
Allow me to introduce you to my friend Schlemiel the Painter. Discovery of a more efficient method is left as a challenge for the reader.
This kind of looks like homework, please let us know if it isn't. I'll give you a small hint, and then I'll improve my answer if you confirm this isn't homework.
My tip for now is this: If you were to do this by hand, how would you do it? Would you write out an extra list of numbers of some time, would you read through the list (how many times?)? etc.
For simple problems, sometimes modelling your algorithm after an intuitive by-hand approach can work well.
Here's a link I read just today that may be helpful.
http://research.swtch.com/2008/03/using-uninitialized-memory-for-fun-and.html