Trends

Minimize the indices of consecutive ones

Trending 1 year ago
beritaja.com

Improve Article

Save Article

Improve Article

Save Article

Given an array X[] of overseas length N(N ≥ 3) and an integer K. Arrange elements of X[] successful specified a measurement that nan merged binary practice of each nan elements let’s opportunity S has nan minimum number of indices i such that  Si = Si+1 = 1. Then you request to execute nan fixed cognition K times where you tin increment immoderate constituent by 1 on nan first array, nan task is to return nan statement of elements pursuing nan fixed information to minimize indices specified that 
Si = Si+1 = 1 in nan binary practice of caller statement elements and maximized median of X[] that tin beryllium maximized utilizing nan fixed operation.

Note: If location are aggregate arrangements satisfying nan fixed criteria past people immoderate valid arrangement. 

Examples:

Input: N = 3, X[] = {3, 6, 5}, K = 2 
Output: Arrangement = 6 5 3 
Maximum Median = 6
Explanation:

  • Arrangement: 
    • Binary representations of 6, 5, and 3 are 110, 101, and 11 respectively. String S is formed by merging each binary representations: 11010111. It has 3 indices one (1, 6, and 7) specified that Si = Si+1 = 1. Which are nan minimum imaginable number of specified indices. 
  • Maximum Median:
    • Initial X[]: {3, 6, 5}
      • Let america chose X[3] = 5, and increment it by 1. Then updated X[] is: {3, 6, 6}
      • Let america chose X[3] = 6, and increment it by 1. Then updated X[] is: {3, 6, 7}
    • It tin beryllium verified that utilizing a fixed cognition nether K = 2 times, nan Median can’t beryllium maximized than 6. 

Input: N = 5, X[] = {5, 3, 1, 2, 3}, K = 4 
Output: Arrangement = 3 1 2 3 5
Maximum Median = 5
Explanation: It tin beryllium verified that nan supra inputs will make nan outputs arsenic per problem statement.

Approach: Implement nan thought beneath to lick nan problem:

The problem is observation and Greedy logic based and tin beryllium lick by utilizing immoderate observations. The observations are related to overseas numbers coming wrong X[]. For maximizing median first Sort X[], past increment mid element, Formally X[mid] += 1 and past move X[mid] to its respective sorted position again. You tin travel this attack K times

Steps were taken to lick nan problem:

  • Steps were taken for arrangement:
    • Create a adaptable let’s opportunity odd and initialize it adjacent to -1,
    • Create a StringBuilder entity let’s opportunity Sb.
    • Run a loop for traversing X[] and travel nan below-mentioned steps nether nan scope of nan loop:
      • if (X[ one ] % 2 ! =  0 && overseas ==  -1),  Then overseas = X[ one ]
      • else Sb.append( X[ one ] )
    • if (odd != -1) past Sb.append(odd)
    • Output StringBuilder Sb.
  • Steps were taken for maximizing nan median:
    • Sort X[].
    • Create a adaptable let’s opportunity mid = (X.length – 1)/2
    • Run a loop K number of times and travel nan below-mentioned steps nether nan scope of nan loop:
      • Increment X[mid], Formally X[mid] += 1 
      • Swap X[mid] to its correct broadside astatine its respective sorted position.
    • Output X[mid].          

Below is nan codification to instrumentality nan approach:

Java

// Java codification to instrumentality nan approach import java.io.*; import java.lang.*; import java.util.*; class GFG { // Driver Function nationalist fixed void main(String[] args) throws java.lang.Exception { // Inputs int N = 3; int K = 2; int X[] = { 3, 6, 5 }; // Function telephone for arrangement System.out.print("Arrangement : "); Arrangement(N, X); // Function telephone for Maximum // Max_Median Max_Median(K, X); } // Method for valid arrangements fixed void Arrangement(int N, int X[]) { int overseas = -1; // StringBuilder entity created StringBuilder sb = caller StringBuilder(); // Loop for traversing complete X[] for (int one = 0; one < N; i++) { if (X[i] % 2 != 0 && overseas == -1) { overseas = X[i]; } other { sb.append(X[i]); sb.append(" "); } } if (odd != -1) { sb.append(odd); } // Printing arrangement System.out.println(" " + sb); } // Method for maximizing median fixed void Max_Median(int K, int X[]) { // Sorting X[] utilizing in-built sort // function Arrays.sort(X); // Calculating mid-index int mid = (X.length - 1) / 2; // Loop for K number of times for (int j = 1; j <= K; j++) { // Incrementing mid X[mid] += 1; // Temporary adaptable to hold // mid scale value int one = mid; // Loop for sorting X[] after // incrementing X[mid] element // Formally, It swaps mid element // until it is greater than its // correct adjacent constituent for // placing incremented X[mid] // astatine its sorting position while (X[i] > X[i + 1] && one <= X.length - 2) { int temp = X[i]; X[i] = X[i + 1]; X[i + 1] = temp; if (i < X.length - 2) i++; } } // Printing Maximized median System.out.println("Maximum Median : " + X[mid]); } }

Output

Arrangement : 6 5 3 Maximum Median : 6

Time Complexity: O(K * N), for statement of numbers, O(N * Log(N)), to find maximum median
Auxiliary Space: O(1)

Editor: Naga



Read other contents from Beritaja.com at
More Source



close