Kth candy
Practice
1.8 (8 votes)
Binary search algorithm
Medium
Searching algorithm
Algorithms
Binary search
Binary indexed tree
Binary indexed tree
Problem
56% Success 922 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

You have $$N$$ pieces of candies. Every day, a piece of candy is replaced with another one. The order of replacement is described by $$Q$$ queries containing the following:

  • $$i$$: The index of a candy to be replaced 
  • $$x$$: The sweetness of the replacement candy
  • $$K$$: An integer

For each query find the \(K^{th}\) smallest value of sweetness of candy after replacement.

Input format

  • The first line of input contains N, the number of candies.
  • Next line contains N numbers (\(a_1, a_2....a_N\)), where \(a_i\) denotes the sweetness of \(i^{th}\) candy.
  • The third line of input contains two integers $$Q$$ and $$R$$, the number of queries and \(R = 3\) for, number of integers in each query.
  • Followed by $$Q$$ lines each containing three integers $$i$$, $$x$$ and $$K$$.

Output format

For each query output the Kth smallest value of sweetness among the candies.

Constraints

\(1 \le N, Q \le 100000\)
\(1 \le a_i \le 1000000\)
\(1 \le i \le N\)
\(1 \le x, K \le 1000000\).

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:10
Tags:
Basic ProgrammingBit ManipulationBasics of Bit ManipulationBit manipulationXOR
Points:30
13 votes
Tags:
AlgorithmsDijkstra's algorithmEasyFloyd Warshall AlgorithmGraphsShortest Path Algorithm
Points:30
16 votes
Tags:
MathematicsMediumGreedy algorithm