How can I find the maximum value in this queue? - c++11

I want to find maximum value in this queue, How can I solve this?
queue<pair<int , int> > q;
for(int i = 1; i <= n; i++){
int p;
cin >> p;
q.push(make_pair(p, i));
}

I hope I understood your issue correctly if not please leave a comment, please check this code and I hope that it will do whatever you need.
queue<pair<int , int> > q;
int max=-1;//you can use INT_MIN in case you're accepting negative numbers too
for(int i = 1; i <= n; i++)
{
int p;
cin >> p;
//if you need to be based ONLY on user input
if(p > max) max = p;
//if you need to be based on BOTH user input and current i
//if(p+i > max) max = p+I;
//if you need to be based on ONLY current i
//if(i > max) max = i;
q.push(make_pair(p, i));
}

Related

Coin making problem in DP - getting wrong answer using 2dimensional memo table

When I am passing this Input I am getting wrong answer
coin[] = {5,6}
Amount(W) = 10
my answer = 1
Correct Answer should be 2 i.e {5,5}
void coin_make(int W, vector<int> coin){
int n = coin.size();
int dp[n+1][W+1];
for(int i = 0; i <=W; i++){
dp[0][i] = INT_MAX;
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= W; j++){
if(coin[i-1] == j){
dp[i][j] = 1;
}
else if(coin[i-1] > j){
dp[i][j] = dp[i-1][j];
}
else {
dp[i][j] = min(dp[i-1][j],
1 + dp[i][j-coin[i-1]]);
}
}
}
cout<<dp[n][W];}
You're overflowing on dp[1][6], since you try to calculate 1 + INT_MAX. This error propagates further and finally the answer is not correct. When I ran it on my machine, I got -2147483648. You should use some other constant as "infinity" to prevent overflows (e.g. 2e9 (or -1, but this would require some additional if statements)). Then the code will work fine on your provided test case.

Sort an array of numbers into ascending order and find the position of a number within the list

I have produced an unsorted array of values of which I would like to put into an ascending order and determine the new position of the last number.
I have previously attempted successfully at other smaller input but got stuck on the last one where the the list consists of 1824300 values and the terminal just wouldn't run the sorting algorithm at all...
#include <stdio.h>
int main(void)
{
signed value = 16239, num = 1824300, i, j;
signed temp;
signed arr[num];
arr[0] = value;
printf("Your initial array is:\n");
printf("%i\n", arr[0]);
for (i = 1; i < num; i++)
{
value = (value*31334)%31337;
arr[i]= value;
printf("%i: ", i);
printf("%i\n", arr[i]);
}
// Insertion sort
for(i = 1; i < num; i++)
{
j = i;
temp = arr[j];
while((j > 0) && (arr[j - 1] > temp))
{
arr[j] = arr[j -1];
arr[j - 1] = temp;
j--;
}
}
insertion sort //
printf("Your sorted array is:\n");
for(i = 0; i < num; i++)
{
printf("%i: ", i);
printf("%i\n", arr[i]);
}
return 0;
}
Can someone please help me on it?
P.S. I am completely new to programming so my code might be very inefficient and messy so sorry about that!!
Thanks a lot!!!
So basically below is what I did at the end. I just inserted a simple counter! and it worked fine... Thank you for everyone who tried to help. Your answers are valuable to me and I am still learning how to implement the algorithms into codes which is a bit difficult for me since I have no programming experience before :((( The algorithms are not tricky to understand at all though...
signed count;
count = 0;
for (i = 0; i < num; i++)
{
if (arr[i] <= value)
{
count = count + 1;
}
}
printf("This is the index of your output in a sorted list: \n");
printf("%i\n", count);

Maximum Of all subarrays of size K, using double ended queue

I have googled and found a solution for this,
void maxSlidingWindow(int A[], int n, int w, int B[]) {
deque<int> Q;
for (int i = 0; i < w; i++) {
while (!Q.empty() && A[i] >= A[Q.back()])
Q.pop_back();
Q.push_back(i);
}
for (int i = w; i < n; i++) {
B[i-w] = A[Q.front()];
while (!Q.empty() && A[i] >= A[Q.back()])
Q.pop_back();
while (!Q.empty() && Q.front() <= i-w)
Q.pop_front();
Q.push_back(i);
}
B[n-w] = A[Q.front()];
}
But I couldn't get this solution. For example, if you take the example {10,5,3,2}
After the first for loop, the Dequeue will be like this, 3(rear)->5->10(head).
When this comes to the second for loop
10 is saved in B[0].
,
and the list will be like this,
2(rear)-> 3 -> 5 -> 10(head).
But, here '5' should be at front. Could someone please explain this code with an example.
It seems you missed this condition: Q.front() <= i-w
which removes too old front items.
You can see also my comments in this answer

Atomic Exchange Sorting Algorithm in MultiGPU

How can atomic exchange sorting algorithm can be implemented in MultiGPU? Is there references available??
It would help if you point out an algorithm that may be used, as a guideline to help answer this question.
So, I pulled an algorithm from: http://www.codingunit.com/exchange-sort-algorithm
Here is the basic algorithm:
int main(void)
{
int array[5]; // An array of integers.
int length = 5; // Lenght of the array.
int i, j;
int temp;
//Some input
for (i = 0; i < 5; i++)
{
cout << "Enter a number: ";
cin >> array[i];
}
//Algorithm
for(i = 0; i < (length -1); i++)
{
for (j=(i + 1); j < length; j++)
{
if (array[i] < array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
//Some output
for (i = 0; i < 5; i++)
{
cout << array[i] << endl;
}
}
You may want to look at this page for some source code that may help:
http://www.bealto.com/gpu-sorting.html
But, if you use OpenCL and the equation above, you may want to do something like this:
Open a connection to each card.
Then, where they have the outer loop, send each of those, perhaps in a round-robin to each card.
You will need to then do a final sort on one GPU to finish, but you may want to use a different algorithm, as this algorithm is best on a single-threaded CPU.

Maximum value of column in matrix?

How can i find the maximum value of a column in a matrix using only 2 for's in Java?
for(int i = 1; i< N; i++)
for(int j = 1; j < M; j++)
i want to find the maxim for each column
public int findMaxInCol(int colIndex){
int max = Integer.Min;
for(int row=0;row<Matrix.Rows;row++){
if(matrix[row][colIndex] > max){
max = matrix[row]colIndex];
}
}
return max;
}
void int findMaxOfMaxes() {
int maxOfMaxs = Integer.min;
for(int col=0;col<j;col++){
int maxInCol = findMaxInCol(col);
if( maxInCol > maxOfMaxs)
maxOfMaxs = maxInCol;
}
return maxOfMaxs
}
//pseudocode
//editing after finding that you wanted max in a matrix. You are right, you need 2 for loops.

Resources