Question

Boring job One day, Rick got a sequence of N magic numbers. In order to find the latent code in the sequence, he decides to dSample Input Sample Output 2413 15432 42 2314 5 3 2 4 10 Explanation for the first sample: Initially we have sequence S = [2(

Topic:Data structures and algorithms programming

Compiler:C++ 11, flag: -static -std=c++0x

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

#include<bits/stdc++.h>
using namespace std;

int minimum(int a,int b)
{
if(a<b)
return a;
return b;
}

int main()
{
int t,n,k;
int i,j,temp;
int cur_max,ind;
pair <int,int>p;
cin>>t;
while(t--)
{
cin>>n>>k;
deque <pair<int,int>>q; //Array of pairs containing value and its index
deque <pair<int,int>>d; //To store k elements
for(i=1;i<=n;i++)
{
cin>>temp;
q.push_back(make_pair(temp,i));
}
for(i=1;i<=n;i++) //loop n times for printing n elements
{
int loop=minimum(k,q.size());
cur_max=q.front().first;
ind=q.front().second;
for(j=1;j<=loop;j++)
{
p=q.front();
if(cur_max<p.first) //update maximum
{
cur_max=p.first;
ind=p.second;
}
q.pop_front();
d.push_back(p);
}
loop=d.size();
for(j=1;j<=loop;j++)
{
p=d.front();
d.pop_front();
if(ind!=p.second) //This element was not maximum amongst the k elements and will be inserted back
{
p.first--; //reduce by 1
q.push_back(p); //re-insert it
}
}
cout<<ind<<" "; //print the index of the maximum element
}
cout<<endl;
}
return 0;
}

OUTPUT:

C:\Users\uma mahesh\Desktop\per.exe 4 2 2 3 14 2 4 13 5 3 5 3 2 4 10 11 5 4 3 2 Process returned @ (exe) Press any key to con

Add a comment
Know the answer?
Add Answer to:
Topic:Data structures and algorithms programming Compiler:C++ 11, flag: -static -std=c++0x Boring job One day, Rick got...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Programming Question

    Exercise #1:Write a C program that contains the following steps. Read carefully each step as they are not only programming steps but also learning topics that explain how numeric arrays in C work.Write a while loop to input a series of numbers (either from a file or from the keyboard, but using a file will make for easier debugging) into a one dimensional array of type double named x_arr, declared to be 25 elements in length. Count the elements as you...

  • daily21. Computing 1. C programming Please provide comments and write pre-condition and post-condition for the function....

    daily21. Computing 1. C programming Please provide comments and write pre-condition and post-condition for the function. Description Create a project called Daily21. Add a source file called daily21.c into the project. In this exercise, you will practice working with an array of integers. In the main function, you first need to create an integer array with 10 elements in t. Use a loop to read 10 integers from the user and store the values to the array (in the order...

  • this is Matlab. Three images are consecutive and connected. I NEED PROBLEM 2 Chapter 6 Programming in Matlab Week 6 THE ALTERNATING HARMONIC SERIES The alternating harmonic series converges to the na...

    this is Matlab. Three images are consecutive and connected. I NEED PROBLEM 2 Chapter 6 Programming in Matlab Week 6 THE ALTERNATING HARMONIC SERIES The alternating harmonic series converges to the natural log of 2 +--...-In(2) = 0.6931471806 -1--+ Because of this, we can use the alternating harmonic series to approximate the In(2). But how far out do you have to take the series to get a good approximation of the final answer? We can use a while loop to...

  • write in java 1. Assume the availability of a method  named  makeLine that can be passed a non-negative...

    write in java 1. Assume the availability of a method  named  makeLine that can be passed a non-negative integer  n and a character  c and return a String consisting of n identical characters that are all equal to c. Write a method  named  printTriangle that receives two integer  parameters  n and k. If n is negative the method does nothing. If n happens to be an even number, itsvalue is raised to the next odd number (e.g. 4-->5). Then, when k has the value zero, the method prints...

  • C++ Write the following 7 routines as member functions in two separate classes -one involving a...

    C++ Write the following 7 routines as member functions in two separate classes -one involving a dynamic array and other a singularly linked list. -Also write inside the classes other appropriate functions like constructor/s, copy constructor, overloaded assignment operator, destructor, and other operator overloaded functions as needed. 1.To generate 100 random numbers between 1-100 in a randomData.txt file 2.To read the 100 random numbers from randomData.txt and store them in a data structure, 3.print the data in the data structure...

  • import java.util.List; import java.util.ArrayList; import java.util.LinkedList; public class ListPractice {       private static final int[]...

    import java.util.List; import java.util.ArrayList; import java.util.LinkedList; public class ListPractice {       private static final int[] arr = new int[100000];    public static void main(String[] args) {        for(int i=0; i<100000; i++)            arr[i] = i;               //TODO comment out this line        LinkedList<Integer> list = new LinkedList<Integer>();               //TODO uncomment this line        //List<Integer> list = new ArrayList<Integer>();               //TODO change the rest of the...

  • 18.1 Lab Lesson 11 (Part 1 of 1) Part of lab lesson 11 There in one...

    18.1 Lab Lesson 11 (Part 1 of 1) Part of lab lesson 11 There in one part to lab lesson 11. The entire lab will be worth 100 points. Lab lesson 11 part 1 is worth 100 points For part 1 you will have 80 points if you enter the program and successfully run the program tests. An additional 20 points will be based on the style and formatting of your C++ code. Style points The 20 points for coding...

  • Make a program using Java that asks the user to input an integer "size". That integer...

    Make a program using Java that asks the user to input an integer "size". That integer makes and prints out an evenly spaced, size by size 2D array (ex: 7 should make an index of 0-6 for col and rows). The array must be filled with random positive integers less than 100. Then, using recursion, find a "peak" and print out its number and location. (A peak is basically a number that is bigger than all of its "neighbors" (above,...

  • C++ Programz

    Program 0 (50%): In many computer science courses, when you study sorting, you also study “runtime” - and it can be confusing to understand when you first see it.  For example, they say that BubbleSort “runs in O(n2)”  time (pronounced “Big-Oh of n-squared") - but what does that mean?  For simplicity, it means if you have n elements, the worst it could run would be n2 low-level computer operations (like comparisons, assignment statements and so on).  For example, if we...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT