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

• Difficulty Level : Easy
• Last Updated : 04 Jan, 2021

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

• For every ith operation select the rightmost longest subarray consisting of all 0s 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:

Input: arr[] = { 0, 0, 0, 0, 0}, K = 3
Output: 3 0 1 0 2
Explanation:
In 1st operation selecting the subarray { arr, …, arr} and replacing arr by 1 modifies arr[] to { 0, 0, 1, 0, 0}
In 2nd operation selecting the subarray { arr, …, arr} and replacing arr by 2 modifies arr[] to { 0, 0, 1, 0, 2}
In 3rd operation selecting the subarray { arr, …, arr} and replacing arr 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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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 0s 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 ith operation pop the top element from 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 0s into pq.
• Finally, print the array elements.

Below is the implementation of the above approach:

## C++

 `// C++ program to implement ` `// the above approach ` ` `  ` `  `#include ` `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 > 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 % 2 == 1) { ` `             `  `             `  `             `  `            ``// Update mid ` `            ``mid = sub + sub / 2; ` `             `  `             `  `             `  `            ``// Replacing arr[mid] with i ` `            ``arr[mid] = i; ` `             `  `             `  `             `  `            ``// Insert left half of ` `            ``// the subarray into pq ` `            ``pq.push({ sub / 2, ` `                         ``sub }); ` `             `  `             `  `             `  `            ``// Insert right half of ` `            ``// the subarray into pq ` `            ``pq.push({ sub / 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 + sub / 2; ` `                 `  `                 `  `                 `  `                ``// Replacing mid element ` `                ``// with i ` `                ``arr[mid - 1] = i; ` `                 `  `                 `  `                 `  `                ``// Insert left half of ` `                ``// the subarray into pq ` `                ``pq.push({ sub / 2 - 1,  ` `                               ``sub }); ` `                 `  `                 `  `                 `  `                ``// Insert right half of ` `                ``// the subarray into pq ` `                ``pq.push({ sub / 2, mid }); ` `            ``} ` `             `  `             `  `             `  `            ``// If i is an even number ` `            ``else` `{ ` `                 `  `                 `  `                 `  `                ``// Update mid ` `                ``mid = sub + sub / 2; ` `                 `  `                 `  `                 `  `                ``// Replacing mid element ` `                ``// with i ` `                ``arr[mid - 1] = i; ` `                 `  `                 `  `                 `  `                ``// Insert left half of ` `                ``// the subarray into pq ` `                ``pq.push({ sub / 2, ` `                            ``sub }); ` `                 `  `                 `  `                 `  `                ``// Insert right half of ` `                ``// the subarray into pq ` `                ``pq.push({ sub / 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); ` `    ``int` `K = 3; ` `    ``ReplaceArray(arr, N, K); ` `} `

Output:

```3 0 1 2 0
```

Time Complexity: O(K * log(N))
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up
Recommended Articles
Page :