Clarification on heaps - do they have to be filled to be a valid heap? - 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

Related

memory issues on a native c++ app on windows

I'm investigating a bad_alloc crashes for a multithreaded native cpp app, from WinDbg it's clearly happening on allocating large object on heap (mostly basic_string ctor or some array allocation with new operator). From the !address -summary and memory analysis from DebugDiag it's seems like app memory usage is very high but heap size is still very small (around 70 MB).
LFH Key : 0x233116ff
Termination on corruption : ENABLED
Heap Flags Reserv Commit Virt Free List UCR Virt Lock Fast
(k) (k) (k) (k) length blocks cont. heap
-----------------------------------------------------------------------------
05f60000 00000002 68964 56804 68964 8411 37570 13 2 29701
External fragmentation 14 % (37570 free blocks)
072a0000 00001002 60 4 60 2 1 1 0 0
096f0000 00001002 60 4 60 2 1 1 0 0
1f430000 00001002 60 4 60 2 1 1 0 0
-----------------------------------------------------------------------------
I want to dig deep into the memory from the usage table and find out the cause of higher memory allocation, any suggestion on how to proceed further?

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.

Can't figure out how to program in outputs for the given situations

So, I'm working on an assignment for my intro to computer science class. The assignment is as follows.
There is an organism whose population can be determined according to
the following rules:
The organism requires at least one other organism to propagate. Thus,
if the population goes to 1, then the organism will become extinct in
one time cycle (e.g. one breeding season). In an unusual turn of
events, an even number of organisms is not a good thing. The
organisms will form pairs and from each pair, only one organism will
survive If there are an odd number of organisms and this number is
greater than 1 (e.g., 3,5,7,9,…), then this is good for population
growth. The organisms cannot pair up and in one time cycle, each
organism will produce 2 other organisms. In addition, one other
organism will be created. (As an example, let us say there are 3
organisms. Since 3 is an odd number greater than 1, in one time
cycle, each of the 3 organisms will produce 2 others. This yields 6
additional organisms. Furthermore, there is one more organism
produced so the total will be 10 organisms, 3 originals, 6 produced by
the 3, and then 1 more.)
A: Write a program that tests initial populations from 1 to 100,000.
Find all populations that do not eventually become extinct.
Write your answer here:
B: Find the value of the initial population that eventually goes
extinct but that has the largest number of time cycles before it does.
Write your answer here:
The general idea of what I have so far is (lacking sytanx) is this with P representing the population
int generations = 0;
{
if (P is odd) //I'll use a modulus modifier to divide by two and if the result is not 0 then I'll know it's odd
P = 3P + 1
else
P = 1/2 P
generations = generations + 1
}
The problem for me is that I'm uncertain how to tell what numbers will not go extinct or how to figure out which population takes the longest time to go extinct. Any suggestions would be helpful.
Basically what you want to do is this: wrap your code into a while-loop that exits if either P==1 or generations > someMaxValue.
Wrap this construct into a for-loop that counts from 1 to 100,000 and uses this count to set the initial P.
If you always store the generations after your while-loop (e.g. into an array) you can then search for the greatest element in the array.
This problem can actually be harder than it looks at the first sight. First, you should use memorization to speed things up - for example, with 3 you get 3 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1 -> 0, so you know the answer for all those numbers as well (note that every power of 2 will extinct).
But as pointed out by #Jerry, the problem is with the generations which eventually do not extinct - it will be difficult to say when to actually stop. The only chance is that there will (always) be a recurrence (number of organisms you already passed once when examining the current number of organisms), then you can say for sure that the organisms will not extinct.
Edit: I hacked a solution quickly and if it is correct, you are lucky - every population between 1-100,000 seems to eventually extinct (as my program terminated so I didn't actually need to check for recurrences). Will not give you the solution for now so that you can try by yourself and learn, but according to my program the largest number of cycles is 351 (and the number is close to 3/4 of the range). According to the google search for Collatz conjecture, that is a correct number (they say 350 to go to population of 1, where I'm adding one extra cycle to 0), also the initial population number agrees.
One additional hint: Check for integer overflow, and use 64-bit integer (unsigned __int64, unsigned long long) to calculate the population growth, as with 32-bit unsignet int, there is already an overflow in the range of 1-100,000 (the population can indeed grow much higher intermediately) - that was a problem in my initial solution, although it did not change the result. With 64-bit ints I was able to calculate up to 100,000,000 in relatively decent time (didn't try more; optimized release MSVC build), for that I had to limit the memo table to first 80,000,000 items to not go out of memory (compiled in 32-bit with LARGEADDRESSAWARE to be able to use up to 4 GB of memory - when compiled 64-bit the table could of course be larger).

Reverse stack in increasing order alternately

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)