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:
**11**010**111.**It has 3 indices one (1, 6, and 7) specified that Si = Si+1 = 1. Which are nan minimum imaginable number of specified indices.

- Binary representations of 6, 5, and 3 are 110, 101, and 11 respectively. String S is formed by merging each binary representations:
**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.

- Initial X[]: {3, 6, 5}

**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.

- Create a adaptable let’s opportunity
- 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

**Output**

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

**Editor:**Naga

**Beritaja.com**at