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 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.
- 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 <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); } |
Java
import java.util.*; class Tuple { public int x, y; public Tuple( int a, int b) { x = a; y = b; } } class GFG { // Function to print array by replacing the mid // of the righmost longest subarray with count // of operations performed on the array static 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 List<Tuple> pq = new ArrayList<>(); // Insert the array arr[] pq.add( new Tuple(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 Tuple sub = pq.get( 0 ); // Pop top element of pq pq.remove( 0 ); // If length of the subarray // is an odd number if (sub.x % 2 == 1 ) { // Update mid mid = sub.y + sub.x / 2 ; // Replacing arr[mid] with i arr[mid] = i; // Insert left half of // the subarray into pq pq.add( new Tuple(sub.x / 2 , sub.y)); // Insert right half of // the subarray into pq pq.add( new Tuple(sub.x / 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.y + sub.x / 2 ; // Replacing mid element // with i arr[mid - 1 ] = i; // Insert left half of // the subarray into pq pq.add( new Tuple(sub.x / 2 - 1 , sub.y)); // Insert right half of // the subarray into pq pq.add( new Tuple(sub.x / 2 , mid)); } // If i is an even number else { // Update mid mid = sub.y + sub.x / 2 ; // Replacing mid element // with i arr[mid - 1 ] = i; // Insert left half of // the subarray into pq pq.add( new Tuple(sub.x / 2 , sub.y)); // Insert right half of // the subarray into pq pq.add( new Tuple(sub.x / 2 - 1 , (mid + 1 ))); } } pq.sort((a, b) -> (a.x != b.x) ? - a.x + b.x : - a.y + b.y); } // Print array elements for ( int i = 0 ; i < N; i++) System.out.print( arr[i] + " " ); } // Driver Code public static void main(String[] args) { int [] arr = { 0 , 0 , 0 , 0 , 0 }; int N = arr.length; int K = 3 ; replaceArray(arr, N, K); } } // This code is contributed by phasing17. |
Python3
# Python3 program to implement # the above approach # Function to print array by replacing the mid # of the righmost longest subarray with count # of operations performed on the array def ReplaceArray(arr, N, K): # Stores subarray of the form X, Y , # where X is the length and Y is start # index of the subarray pq = []; # Insert the array arr[] pq.append([ N, 0 ]); # Stores index of mid # element of the subarray mid = 0 # Iterate over the range [1, N] for i in range ( 1 , 1 + N): # Stores top element of pq sub = pq[ - 1 ] # Pop top element of pq pq.pop(); # If length of the subarray # is an odd number if (sub[ 0 ] % 2 = = 1 ) : # Update mid mid = int (sub[ 1 ] + int (sub[ 0 ] / 2 )); # Replacing arr[mid] with i arr[mid] = i; # Insert left half of # the subarray into pq pq.append([ int (sub[ 0 ] / 2 ), sub[ 1 ] ]); # Insert right half of # the subarray into pq pq.append([ int (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 ] + int (sub[ 0 ] / 2 ); # Replacing mid element # with i arr[mid - 1 ] = i; # Insert left half of # the subarray into pq pq.append([ int (sub[ 0 ] / 2 ) - 1 , sub[ 1 ] ]); # Insert right half of # the subarray into pq pq.append([ int (sub[ 0 ] / 2 ), mid ]); # If i is an even number else : # Update mid mid = sub[ 1 ] + int (sub[ 0 ] / 2 ); # Replacing mid element # with i arr[mid - 1 ] = i; # Insert left half of # the subarray into pq pq.append([ int (sub[ 0 ] / 2 ), sub[ 1 ] ]); # Insert right half of # the subarray into pq pq.append([ int (sub[ 0 ] / 2 ) - 1 , (mid + 1 ) ]); pq.sort() # Print array elements print ( * arr) # Driver Code arr = [ 0 , 0 , 0 , 0 , 0 ]; N = len (arr) K = 3 ReplaceArray(arr, N, K); # This code is contributed by phasing17. |
C#
// C# program to implement // the above approach using System; using System.Linq; using System.Collections.Generic; class GFG { // Function to print array by replacing the mid // of the righmost longest subarray with count // of operations performed on the array static 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 var pq = new List<Tuple< int , int >>(); // Insert the array arr[] pq.Add(Tuple.Create(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 var sub = pq[0]; // Pop top element of pq pq.RemoveAt(0); // If length of the subarray // is an odd number if (sub.Item1 % 2 == 1) { // Update mid mid = sub.Item2 + sub.Item1 / 2; // Replacing arr[mid] with i arr[mid] = i; // Insert left half of // the subarray into pq pq.Add(Tuple.Create(sub.Item1 / 2, sub.Item2)); // Insert right half of // the subarray into pq pq.Add(Tuple.Create(sub.Item1 / 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.Item2 + sub.Item1 / 2; // Replacing mid element // with i arr[mid - 1] = i; // Insert left half of // the subarray into pq pq.Add(Tuple.Create(sub.Item1 / 2 - 1, sub.Item2)); // Insert right half of // the subarray into pq pq.Add(Tuple.Create(sub.Item1 / 2, mid)); } // If i is an even number else { // Update mid mid = sub.Item2 + sub.Item1 / 2; // Replacing mid element // with i arr[mid - 1] = i; // Insert left half of // the subarray into pq pq.Add(Tuple.Create(sub.Item1 / 2, sub.Item2)); // Insert right half of // the subarray into pq pq.Add(Tuple.Create(sub.Item1 / 2 - 1, (mid + 1))); } } pq = pq.OrderBy(p => -p.Item1).ThenBy(p => -p.Item2).ToList(); } // Print array elements for ( int i = 0; i < N; i++) Console.Write( arr[i] + " " ); } // Driver Code public static void Main( string [] args) { int [] arr = { 0, 0, 0, 0, 0 }; int N = arr.Length; int K = 3; ReplaceArray(arr, N, K); } } |
Javascript
// JS program to implement // the above approach // Function to print array by replacing the mid // of the righmost longest subarray with count // of operations performed on the array function ReplaceArray(arr, N, K) { // Stores subarray of the form { X, Y }, // where X is the length and Y is start // index of the subarray let pq = []; // Insert the array arr[] pq.push([ N, 0 ]); // Stores index of mid // element of the subarray let mid; // Iterate over the range [1, N] for (let i = 1; i <= K; i++) { // Stores top element of pq let sub = pq[pq.length - 1] // Pop top element of pq pq.pop(); // If length of the subarray // is an odd number if (sub[0] % 2 == 1) { // Update mid mid = Math.floor(sub[1] + Math.floor(sub[0] / 2)); // Replacing arr[mid] with i arr[mid] = i; // Insert left half of // the subarray into pq pq.push([ Math.floor(sub[0] / 2), sub[1] ]); // Insert right half of // the subarray into pq pq.push([ Math.floor(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] + Math.floor(sub[0] / 2); // Replacing mid element // with i arr[mid - 1] = i; // Insert left half of // the subarray into pq pq.push([ Math.floor(sub[0] / 2) - 1, sub[1] ]); // Insert right half of // the subarray into pq pq.push([ Math.floor(sub[0] / 2), mid ]); } // If i is an even number else { // Update mid mid = sub[1] + Math.floor(sub[0] / 2); // Replacing mid element // with i arr[mid - 1] = i; // Insert left half of // the subarray into pq pq.push([ Math.floor(sub[0] / 2), sub[1] ]); // Insert right half of // the subarray into pq pq.push([ Math.floor(sub[0] / 2) - 1, (mid + 1) ]); } } pq.sort() } // Print array elements console.log(arr.join( " " )) } // Driver Code let arr = [ 0, 0, 0, 0, 0 ]; let N = arr.length let K = 3 ReplaceArray(arr, N, K); // This code is contributed by phasing17. |
Output
3 0 1 2 0
Time Complexity: O(K * log(N))
Auxiliary Space: O(N)
Please Login to comment...