# Replace the middle element of the longest subarray of 0s from the right exactly K times

Given an array **arr[]** of size **N**, consisting of **0**s initially, and a positive integer **K**, the task is to print the array elements by performing the following operations exactly **K** times.

- For every
**i**operation select the rightmost longest subarray consisting of all^{th}**0**s and replace the mid element of the subarray by**i**. - If two middle elements exist, then check if
**i**is an even number or not. If found to be true, then replace the rightmost middle element with**i**. - Otherwise, replace the leftmost middle element with
**i**.**Examples:**Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the

**DSA Self Paced Course**at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer**Complete Interview Preparation Course****.**In case you wish to attend

**live classes**with experts, please refer**DSA Live Classes for Working Professionals**and**Competitive Programming Live for Students**.**Input:**arr[] = { 0, 0, 0, 0, 0}, K = 3**Output:**3 0 1 0 2**Explanation:**

In 1st operation selecting the subarray { arr[0], …, arr[4]} and replacing arr[2] by 1 modifies arr[] to { 0, 0, 1, 0, 0}

In 2nd operation selecting the subarray { arr[3], …, arr[4]} and replacing arr[4] by 2 modifies arr[] to { 0, 0, 1, 0, 2}

In 3rd operation selecting the subarray { arr[0], …, arr[1]} and replacing arr[1] by 3 modifies arr[] to { 0, 3, 1, 0, 2}

Therefore, the required output is 3 0 1 0 2.**Input:**arr[] = { 0, 0, 0, 0, 0, 0, 0 }, K = 7**Output:**7 3 6 1 5 2 4**Approach:**The problem can be solved using Greedy technique. The idea is to use the Priority Queue to select the rightmost longest subarray with all 0s. Follow the steps below to solve the problem:- Initialize a Priority Queue, say
**pq**, to store the subarrays of the form**{ X, Y}**where**X**denotes the length of the subarray and**Y**denotes the starting index of the subarray. - Initially maximum length of the subarray with all
**0**s is**N**and start index of the subarray is**0**. Therefore, Insert**{ N, 0 }**into**pq**. - Iterate over the range
**[1, K]**using variable**i**. For every**i**operation pop the top element from^{th}**pq**and check if length of the popped element is an odd number or not. If found to be true then replace the mid element of the subarray with**i**. - Otherwise, if
**i**is an even number then replace the rightmost mid element of the subarray with**i**. Otherwise, replace the leftmost mid element of the subarray with**i**. - After replacing the mid element with
**i**, insert the left half of the subarray and right half of the subarray containing all**0**s into**pq**. - Finally, print the array elements.

Below is the implementation of the above approach:

## C++

`// C++ program to implement`

`// the above approach`

`#include <bits/stdc++.h>`

`using`

`namespace`

`std;`

`// Function to print array by replacing the mid`

`// of the righmost longest subarray with count`

`// of operations performed on the array`

`void`

`ReplaceArray(`

`int`

`arr[],`

`int`

`N,`

`int`

`K)`

`{`

`// Stores subarray of the form { X, Y },`

`// where X is the length and Y is start`

`// index of the subarray`

`priority_queue<vector<`

`int`

`> > pq;`

`// Insert the array arr[]`

`pq.push({ N, 0 });`

`// Stores index of mid`

`// element of the subarray`

`int`

`mid;`

`// Iterate over the range [1, N]`

`for`

`(`

`int`

`i = 1; i <= K; i++) {`

`// Stores top element of pq`

`vector<`

`int`

`> sub = pq.top();`

`// Pop top element of pq`

`pq.pop();`

`// If length of the subarray`

`// is an odd number`

`if`

`(sub[0] % 2 == 1) {`

`// Update mid`

`mid = sub[1] + sub[0] / 2;`

`// Replacing arr[mid] with i`

`arr[mid] = i;`

`// Insert left half of`

`// the subarray into pq`

`pq.push({ sub[0] / 2,`

`sub[1] });`

`// Insert right half of`

`// the subarray into pq`

`pq.push({ sub[0] / 2,`

`(mid + 1) });`

`}`

`// If length of the current`

`// subarray is an even number`

`else`

`{`

`// If i is`

`// an odd number`

`if`

`(i % 2 == 1) {`

`// Update mid`

`mid = sub[1] + sub[0] / 2;`

`// Replacing mid element`

`// with i`

`arr[mid - 1] = i;`

`// Insert left half of`

`// the subarray into pq`

`pq.push({ sub[0] / 2 - 1,`

`sub[1] });`

`// Insert right half of`

`// the subarray into pq`

`pq.push({ sub[0] / 2, mid });`

`}`

`// If i is an even number`

`else`

`{`

`// Update mid`

`mid = sub[1] + sub[0] / 2;`

`// Replacing mid element`

`// with i`

`arr[mid - 1] = i;`

`// Insert left half of`

`// the subarray into pq`

`pq.push({ sub[0] / 2,`

`sub[1] });`

`// Insert right half of`

`// the subarray into pq`

`pq.push({ sub[0] / 2 - 1,`

`(mid + 1) });`

`}`

`}`

`}`

`// Print array elements`

`for`

`(`

`int`

`i = 0; i < N; i++)`

`cout << arr[i] <<`

`" "`

`;`

`}`

`// Driver Code`

`int`

`main()`

`{`

`int`

`arr[] = { 0, 0, 0, 0, 0 };`

`int`

`N =`

`sizeof`

`(arr) /`

`sizeof`

`(arr[0]);`

`int`

`K = 3;`

`ReplaceArray(arr, N, K);`

`}`

**Output:**3 0 1 2 0

**Time Complexity:**O(K * log(N))**Auxiliary Space:**O(N) - Initialize a Priority Queue, say