Max Priority queue based on max-heap - heap

I am trying to implement the MAX-HEAP-INSERT(A,key) function.
It's pseudocode is:
MAX-HEAP-INSERT(A,key)
1 A.heap-size = A.heap-size+1
2 A[A.heap-size] = -inf
3 HEAP-INCREASE-KEY(A,A.heap-size,key)
Where HEAP-INCREASE-KEY(A,A.heap-size,key) is another function which i didn't have trouble understanding.
The heap is based on an array A, and as we know arrays are limited. So, when we increase the A.heap-size won't there be a problem when A.heap-size gets bigger than A.length?

Yes. If this happens you have to increase the size of your array.

Related

Find the number of ways to partition the array

I want number of ways to divide an array of possitive integers such that maximum value of left part of array is greater than or equal to the maximum value of right part of the array.
For example,
6 4 1 2 1 can be divide into:
[[6,4,1,2,1]] [[6][4,1,2,1]] [[6,4][1,2,1]] [[6,4,1][2,1]] [[6,4,1,2][1]] [[6][4,1][2,1]] [[6][4][1,2,1]] [[6][4][1,2][1]] [[6][4,1,2][1]] [[6,4,1][2][1]] [[6][4,1][2][1]] [[6,4][1,2][1]]
which are total 12 ways of partitioning.
I tried a recursive approach but it fails beacause of termination due to exceed of time limit. Also this approach is not giving correct output always.
In this another approach, I took the array ,sort it in decreasing order and then for each element I checked weather it lies on right of the original array, and if does then added it's partitions to it's previous numbers too.
I want an approach to solve this, any implementation or pseudocode or just an idea to do this would be appreciable.
I designed a simple recursive algorithm. I will try to explain on your example;
First, check if [6] is a possible/valid part of a partition.
It is a valid partition because maximum element of ([6]) is bigger than remaining part's ([4,1,2,1]) maximum value.
Since it is a valid partition, we can use recursive part of the algorithm.
concatenate([6],algorithm([4,1,2,1]))
now the partitions
[[6][4,1,2,1]], [[6][4,1][2,1]], [[6][4,1][2,1]] [[6][4][1,2,1]] [[6][4][1,2][1]] [[6][4,1,2][1]]
are in our current solution set.
Check if [6,4] is a possible/valid part of a partition.
Continue like this until reaching [6,4,1,2,1].

If I'm trying to create a Queue in C++, how do I make the Queue of a certain size?

I'm very new to C++ and am rather confused as to how to make a Queue (our first homework assignment).
For the constructor, we're supposed to accept a length that the Queue should be. The Queue's length must in turn be the closest Fibonacci number greater than the given length if it is not already a Fibonacci number. (e.g.: if given 10 in the constructor, make the length 13).
Right now I'm implementing the Queue as an array. Should I have something like int queueContents[]; in the private part of my header, then set it to the closest Fibonacci number in the constructor?
How would I go about declaring it for use throughout the whole file and set its length in my constructor?
You will have a global variable keep track of the size (if you ever needs to grow it for efficiency's sake).
ArrayQueue(int sz)
{
size = getFib(sz);
queueContents = new int[size];
//set front and back to initial value
}
getFib(int sz) {...}
And yes, you need a global variable int *queueContents. (pointer preferred).
Feel free to ask further questions. I recently had to code a basic array-based queue too so (hopefully) I can answer any question you will have. :)
You can calculate the nearest fibonacci number and then have a variable capacity and everytime you insert into the queue, check if this exceeds the capacity..

Finding Maximum of an interval in array using binary trees

I'm studying Binary trees! and i have a problem in this Homework.
I have to use binary trees to solve this problem
here is the problem :
You are given a list of integers. You then need to answer a number of questions of the form: "What is the maximum value of the elements of the list content between the A index and the index B?".
example :
INPUT :
10
2 4 3 5 7 19 3 8 6 7
4
1 5
3 6
8 10
3 9
OUTPUT:
7
19
8
19
TIME LIMITS AND MEMORY (Language: C + +)
Time: 0.5s on a 1GHz machine.
Memory: 16000 KB
CONSTRAINTS
1 <= N <= 100000, where N is the number of elements in the list.
1 <= A, B <= N, where A, B are the limits of a range.
1 <= I <= 10 000, where I is the number of intervals.
Please do not give me the solution just a hint !
Thanks so much !
As already discussed in the comments, to make things simple, you can add entries to the array to make its size a power of two, so the binary tree has the same depth for all leaves. It doesn't really matter what elements you add to this list, as you won't use these computed values in the actual algorithm.
In the binary tree, you have to compute the maxima in a bottom-up manner. These values then tell you the maximum of the whole range these nodes are representing; this is the major idea of the tree.
What remains is splitting a query into such tree nodes, so they represent the original interval using less nodes than the size of the interval. Figure out "the pattern" of the intervals the tree nodes represent. Then figure out a way to split the input interval into as few nodes as possible. Maybe start with the trivial solution: just split the input in leave nodes, i.e. single elements. Then figure out how you can "combine" multiple elements from the interval using inner nodes from the tree. Find an algorithm doing this for you by not using the tree (since this would require a linear time in the number of elements, but the whole idea of the tree is to make it logarithmic).
Write some code which works with an interval of size 0. It will be very simple.
Then write some for a interval of size 1. It will still be simple.
Then write some for an interval of size 2. It may need a comparison. It will still be simple.
Then write some for an interval of size 3. It may involve a choice of which interval of size 2 to compare. This isn't too hard.
Once you've done this, it should be easy to make it work with any interval size.
An array would be the best data structure for this problem.
But given you need to use a binary tree, I would store (index, value) in the binary
tree and key on index.

Mapping two set of values in C++

My sincere apologies for such a naive question. I know this is simple. But nothing comes to my mind now.
I am using C++. I'm a bit concerned about efficiency since this is targeted for an embedded hardware with very less processing power and RAM.
I have 2 integer arrays with 50 members local to a function. I need to determine what is the corresponding number in the second array when an element in the first array is specified and vice versa. I have the information that the element provided to me for look-up belongs to which array i.e. array 1 or array 2.
Ex : Array1 => 500 200 1000 300 .....
Array2 => 250 170 500 400 .....
Input 500 , Output will be 250
Input 400 , Output will be 300
input 200 , Output will be 170 and so on
I think an array look-up will be least efficient. Is stl::map the best option or do i have to look for any efficient search algorithms? I would like to know if you have to do this, which option you will be choosing.
Any thoughts?
You can use std::map for readability and a little efficiency as well, though in your case efficiency is of small matter
std::map<int,int> mapping;
.... //populate
cout <<mapping[200]; //170
This is only 1 way (Array 1 -> Array 2) though. Im not sure if any easier way to do the other way, but create a second map.
To support reverse lookup, or going from (Array 2 -> Array 1), Reverse map lookup suggests using Boost.Bimap
According to me there are 2 ways of doing it both have already been suggested;
put the both arrays in a map as key pair value and traverse map to find the corresponding value or key.
Traverse the array for which the input is there and calculate the index. Get the value for that index int he other array.
I would go for the second solution as it easier. Moreover with only 50 elements in a static array you don't need to worry about performance.

Constant time "is bool array all 0's?"

I have a boolean array:
bool * arr = new bool[n];
I want to figure out in constant time if there are any 1's in this array; can this be done?
I know bitset has a none() method which does the same thing, but this array needs to be dynamically sized, and boost's dynamic_bitset isn't really an option.
Edited for clarity
Short answer: No. You have to examine half the elements on average, and that takes O(n) time.
Long answer: Yes. If you're prepared to add some extra O(1) complexity to your write operations. Just keep track of every 0->1 and 1->0 with an up/down counter.
Note: I'm assuming that in the general case you have bool *arr = new bool[n];. For a constant-sized array, then yes of course the query will be constant time!
Not out of the box, but you can easily maintain a count of 1s:
increment it whenever 0 turns into 1
and decrement it when 1 turns into 0.
Once you have done that, comparing the count to 0 is a constant-time operation.
So, essentially, you trade slightly degraded write performance in exchange to answering "are there any 1s left" faster. Whether this is the right tradeoff, only you can know...
If you're able to control access to the array, you could write a wrapper that maintains a count of the number of 1's in the array as items are updated.