Posted on by Kalkicode
Code Array

# Maximize the median of array elements

Here given code implementation process.

``````// C Program
// Maximize the median of array elements
#include <stdio.h>

//Display elements of given array
void printArray(int arr[], int size)
{
for (int i = 0; i < size; ++i)
{
printf("  %d", arr[i]);
}
}
// Swap the elements of array by given location
void swapElement(int arr[], int front, int tail)
{
int temp = arr[front];
arr[front] = arr[tail];
arr[tail] = temp;
}
// Maximize the median of given array element
void maximizeMedian(int arr[], int size)
{
if (size <= 2)
{
// When less than three element
return;
}
// Get middle element
int mid = size / 2;
// Loop controlling variables
int i = 0;
if (size % 2 == 0)
{
//  When length of array are Even
//  Two middle elements are possible
// Second middle element position
int b = mid - 1;
// Execute loop through by size
// Move two higher element at middle position
while (i < size)
{
if (arr[i] > arr[mid])
{
if (arr[mid] > arr[b])
{
swapElement(arr, mid, b);
}
swapElement(arr, mid, i);
}
else if (arr[i] > arr[b])
{
if (arr[mid] < arr[b])
{
// When second middle is less than first middle
swapElement(arr, mid, b);
}
swapElement(arr, b, i);
}
i++;
}
if (arr[b] > arr[mid])
{
// When first middle is largest to second middle element
swapElement(arr, mid, b);
}
}
else
{
//  When length of array are Odd
//  When only One middle element possible
// Execute loop through by size
while (i < size)
{
if (arr[i] > arr[mid])
{
// Set max value to middle of array
swapElement(arr, i, mid);
}
i++;
}
}
}
int main(int argc, char
const *argv[])
{
// Define array of integer elements
int arr1[] = {
6 , 2 , 8 , 0 , 3 , 5 , 2 , 4 , 5 , 7
};
int arr2[] = {
0 , 8 , 4 , 2 , 3 , 2 , 4
};
// Get the size
int size = sizeof(arr1) / sizeof(arr1[0]);
printf("  Array elements \n");
printArray(arr1, size);
maximizeMedian(arr1, size);
printf("\n  Array maximize median \n");
printArray(arr1, size);
// Get the size
size = sizeof(arr2) / sizeof(arr2[0]);
printf("\n  Array elements \n");
printArray(arr2, size);
maximizeMedian(arr2, size);
printf("\n  Array maximize median \n");
printArray(arr2, size);
return 0;
}``````

#### Output

``````  Array elements
6  2  8  0  3  5  2  4  5  7
Array maximize median
3  2  5  0  7  8  2  4  5  6
Array elements
0  8  4  2  3  2  4
Array maximize median
0  2  4  8  3  2  4``````
``````/*
Java Program
Maximize the median of array elements
*/
public class Maximize
{
//Display elements of given array
public void printArray(int[] arr, int size)
{
for (int i = 0; i < size; ++i)
{
System.out.print("  " + arr[i]);
}
}
// Swap the elements of array by given location
public void swapElement(int[] arr, int front, int tail)
{
int temp = arr[front];
arr[front] = arr[tail];
arr[tail] = temp;
}
// Maximize the median of given array element
public void maximizeMedian(int[] arr, int size)
{
if (size <= 2)
{
// When less than three element
return;
}
// Get middle element
int mid = size / 2;
// Loop controlling variables
int i = 0;
if (size % 2 == 0)
{
//  When length of array are Even
//  Two middle elements are possible
// Second middle element position
int b = mid - 1;
// Execute loop through by size
// Move two higher element at middle position
while (i < size)
{
if (arr[i] > arr[mid])
{
if (arr[mid] > arr[b])
{
swapElement(arr, mid, b);
}
swapElement(arr, mid, i);
}
else if (arr[i] > arr[b])
{
if (arr[mid] < arr[b])
{
// When second middle is less than first middle
swapElement(arr, mid, b);
}
swapElement(arr, b, i);
}
i++;
}
if (arr[b] > arr[mid])
{
// When first middle is largest to second middle element
swapElement(arr, mid, b);
}
}
else
{
//  When length of array are Odd
//  When only One middle element possible
// Execute loop through by size
while (i < size)
{
if (arr[i] > arr[mid])
{
// Set max value to middle of array
swapElement(arr, i, mid);
}
i++;
}
}
}
public static void main(String[] args)
{
// Define array of integer elements
int[] arr1 = {
6 , 2 , 8 , 0 , 3 , 5 , 2 , 4 , 5 , 7
};
int[] arr2 = {
0 , 8 , 4 , 2 , 3 , 2 , 4
};
// Get the size
int size = arr1.length;
System.out.print(" Array elements \n");
System.out.print("\n Array maximize median \n");
// Get the size
size = arr2.length;
System.out.print("\n Array elements \n");
System.out.print("\n Array maximize median \n");
}
}``````

#### Output

`````` Array elements
6  2  8  0  3  5  2  4  5  7
Array maximize median
3  2  5  0  7  8  2  4  5  6
Array elements
0  8  4  2  3  2  4
Array maximize median
0  2  4  8  3  2  4``````
``````// Include header file
#include <iostream>
using namespace std;

/*
C++ Program
Maximize the median of array elements
*/

class Maximize
{
public:
//Display elements of given array
void printArray(int arr[], int size)
{
for (int i = 0; i < size; ++i)
{
cout << "  " << arr[i];
}
}
// Swap the elements of array by given location
void swapElement(int arr[], int front, int tail)
{
int temp = arr[front];
arr[front] = arr[tail];
arr[tail] = temp;
}
// Maximize the median of given array element
void maximizeMedian(int arr[], int size)
{
if (size <= 2)
{
// When less than three element
return;
}
// Get middle element
int mid = size / 2;
// Loop controlling variables
int i = 0;
if (size % 2 == 0)
{
//  When length of array are Even
//  Two middle elements are possible
// Second middle element position
int b = mid - 1;
// Execute loop through by size
// Move two higher element at middle position
while (i < size)
{
if (arr[i] > arr[mid])
{
if (arr[mid] > arr[b])
{
this->swapElement(arr, mid, b);
}
this->swapElement(arr, mid, i);
}
else if (arr[i] > arr[b])
{
if (arr[mid] < arr[b])
{
// When second middle is less than first middle
this->swapElement(arr, mid, b);
}
this->swapElement(arr, b, i);
}
i++;
}
if (arr[b] > arr[mid])
{
// When first middle is largest to second middle element
this->swapElement(arr, mid, b);
}
}
else
{
//  When length of array are Odd
//  When only One middle element possible
// Execute loop through by size
while (i < size)
{
if (arr[i] > arr[mid])
{
// Set max value to middle of array
this->swapElement(arr, i, mid);
}
i++;
}
}
}
};
int main()
{
// Define array of integer elements
int arr1[] = {
6 , 2 , 8 , 0 , 3 , 5 , 2 , 4 , 5 , 7
};
int arr2[] = {
0 , 8 , 4 , 2 , 3 , 2 , 4
};
// Get the size
int size = sizeof(arr1) / sizeof(arr1[0]);
cout << " Array elements \n";
cout << "\n Array maximize median \n";
// Get the size
size = sizeof(arr2) / sizeof(arr2[0]);
cout << "\n Array elements \n";
cout << "\n Array maximize median \n";
return 0;
}``````

#### Output

`````` Array elements
6  2  8  0  3  5  2  4  5  7
Array maximize median
3  2  5  0  7  8  2  4  5  6
Array elements
0  8  4  2  3  2  4
Array maximize median
0  2  4  8  3  2  4``````
``````// Include namespace system
using System;
/*
C# Program
Maximize the median of array elements
*/
public class Maximize
{
//Display elements of given array
public void printArray(int[] arr, int size)
{
for (int i = 0; i < size; ++i)
{
Console.Write("  " + arr[i]);
}
}
// Swap the elements of array by given location
public void swapElement(int[] arr, int front, int tail)
{
int temp = arr[front];
arr[front] = arr[tail];
arr[tail] = temp;
}
// Maximize the median of given array element
public void maximizeMedian(int[] arr, int size)
{
if (size <= 2)
{
// When less than three element
return;
}
// Get middle element
int mid = size / 2;
// Loop controlling variables
int i = 0;
if (size % 2 == 0)
{
//  When length of array are Even
//  Two middle elements are possible
// Second middle element position
int b = mid - 1;
// Execute loop through by size
// Move two higher element at middle position
while (i < size)
{
if (arr[i] > arr[mid])
{
if (arr[mid] > arr[b])
{
swapElement(arr, mid, b);
}
swapElement(arr, mid, i);
}
else if (arr[i] > arr[b])
{
if (arr[mid] < arr[b])
{
// When second middle is less than first middle
swapElement(arr, mid, b);
}
swapElement(arr, b, i);
}
i++;
}
if (arr[b] > arr[mid])
{
// When first middle is largest to second middle element
swapElement(arr, mid, b);
}
}
else
{
//  When length of array are Odd
//  When only One middle element possible
// Execute loop through by size
while (i < size)
{
if (arr[i] > arr[mid])
{
// Set max value to middle of array
swapElement(arr, i, mid);
}
i++;
}
}
}
public static void Main(String[] args)
{
// Define array of integer elements
int[] arr1 = {
6 , 2 , 8 , 0 , 3 , 5 , 2 , 4 , 5 , 7
};
int[] arr2 = {
0 , 8 , 4 , 2 , 3 , 2 , 4
};
// Get the size
int size = arr1.Length;
Console.Write(" Array elements \n");
Console.Write("\n Array maximize median \n");
// Get the size
size = arr2.Length;
Console.Write("\n Array elements \n");
Console.Write("\n Array maximize median \n");
}
}``````

#### Output

`````` Array elements
6  2  8  0  3  5  2  4  5  7
Array maximize median
3  2  5  0  7  8  2  4  5  6
Array elements
0  8  4  2  3  2  4
Array maximize median
0  2  4  8  3  2  4``````
``````<?php
/*
Php Program
Maximize the median of array elements
*/
class Maximize
{
//Display elements of given array
public	function printArray( & \$arr, \$size)
{
for (\$i = 0; \$i < \$size; ++\$i)
{
echo "  ". \$arr[\$i];
}
}
// Swap the elements of array by given location
public	function swapElement( & \$arr, \$front, \$tail)
{
\$temp = \$arr[\$front];
\$arr[\$front] = \$arr[\$tail];
\$arr[\$tail] = \$temp;
}
// Maximize the median of given array element
public	function maximizeMedian( & \$arr, \$size)
{
if (\$size <= 2)
{
// When less than three element
return;
}
// Get middle element
\$mid = intval(\$size / 2);
// Loop controlling variables
\$i = 0;
if (\$size % 2 == 0)
{
//  When length of array are Even
//  Two middle elements are possible
// Second middle element position
\$b = \$mid - 1;
// Execute loop through by size
// Move two higher element at middle position
while (\$i < \$size)
{
if (\$arr[\$i] > \$arr[\$mid])
{
if (\$arr[\$mid] > \$arr[\$b])
{
\$this->swapElement(\$arr, \$mid, \$b);
}
\$this->swapElement(\$arr, \$mid, \$i);
}
else if (\$arr[\$i] > \$arr[\$b])
{
if (\$arr[\$mid] < \$arr[\$b])
{
// When second middle is less than first middle
\$this->swapElement(\$arr, \$mid, \$b);
}
\$this->swapElement(\$arr, \$b, \$i);
}
\$i++;
}
if (\$arr[\$b] > \$arr[\$mid])
{
// When first middle is largest to second middle element
\$this->swapElement(\$arr, \$mid, \$b);
}
}
else
{
//  When length of array are Odd
//  When only One middle element possible
// Execute loop through by size
while (\$i < \$size)
{
if (\$arr[\$i] > \$arr[\$mid])
{
// Set max value to middle of array
\$this->swapElement(\$arr, \$i, \$mid);
}
\$i++;
}
}
}
}

function main()
{
// Define array of integer elements
\$arr1 = array(6, 2, 8, 0, 3, 5, 2, 4, 5, 7);
\$arr2 = array(0, 8, 4, 2, 3, 2, 4);
// Get the size
\$size = count(\$arr1);
echo " Array elements \n";
echo "\n Array maximize median \n";
// Get the size
\$size = count(\$arr2);
echo "\n Array elements \n";
echo "\n Array maximize median \n";
}
main();``````

#### Output

`````` Array elements
6  2  8  0  3  5  2  4  5  7
Array maximize median
3  2  5  0  7  8  2  4  5  6
Array elements
0  8  4  2  3  2  4
Array maximize median
0  2  4  8  3  2  4``````
``````/*
Node Js Program
Maximize the median of array elements
*/
class Maximize
{
//Display elements of given array
printArray(arr, size)
{
for (var i = 0; i < size; ++i)
{
process.stdout.write("  " + arr[i]);
}
}
// Swap the elements of array by given location
swapElement(arr, front, tail)
{
var temp = arr[front];
arr[front] = arr[tail];
arr[tail] = temp;
}
// Maximize the median of given array element
maximizeMedian(arr, size)
{
if (size <= 2)
{
// When less than three element
return;
}
// Get middle element
var mid = parseInt(size / 2);
// Loop controlling variables
var i = 0;
if (size % 2 == 0)
{
//  When length of array are Even
//  Two middle elements are possible
// Second middle element position
var b = mid - 1;
// Execute loop through by size
// Move two higher element at middle position
while (i < size)
{
if (arr[i] > arr[mid])
{
if (arr[mid] > arr[b])
{
this.swapElement(arr, mid, b);
}
this.swapElement(arr, mid, i);
}
else if (arr[i] > arr[b])
{
if (arr[mid] < arr[b])
{
// When second middle is less than first middle
this.swapElement(arr, mid, b);
}
this.swapElement(arr, b, i);
}
i++;
}
if (arr[b] > arr[mid])
{
// When first middle is largest to second middle element
this.swapElement(arr, mid, b);
}
}
else
{
//  When length of array are Odd
//  When only One middle element possible
// Execute loop through by size
while (i < size)
{
if (arr[i] > arr[mid])
{
// Set max value to middle of array
this.swapElement(arr, i, mid);
}
i++;
}
}
}
}

function main()
{
// Define array of integer elements
var arr1 = [6, 2, 8, 0, 3, 5, 2, 4, 5, 7];
var arr2 = [0, 8, 4, 2, 3, 2, 4];
// Get the size
var size = arr1.length;
process.stdout.write(" Array elements \n");
process.stdout.write("\n Array maximize median \n");
// Get the size
size = arr2.length;
process.stdout.write("\n Array elements \n");
process.stdout.write("\n Array maximize median \n");
}
main();``````

#### Output

`````` Array elements
6  2  8  0  3  5  2  4  5  7
Array maximize median
3  2  5  0  7  8  2  4  5  6
Array elements
0  8  4  2  3  2  4
Array maximize median
0  2  4  8  3  2  4``````
``````#  Python 3 Program
#  Maximize the median of array elements

class Maximize :
# Display elements of given array
def printArray(self, arr, size) :
i = 0
while (i < size) :
print("  ", arr[i], end = "")
i += 1

#  Swap the elements of array by given location
def swapElement(self, arr, front, tail) :
temp = arr[front]
arr[front] = arr[tail]
arr[tail] = temp

#  Maximize the median of given array element
def maximizeMedian(self, arr, size) :
if (size <= 2) :
#  When less than three element
return

#  Get middle element
mid = int(size / 2)
#  Loop controlling variables
i = 0
if (size % 2 == 0) :
#   When length of array are Even
#   Two middle elements are possible
#  Second middle element position
b = mid - 1
#  Execute loop through by size
#  Move two higher element at middle position
while (i < size) :
if (arr[i] > arr[mid]) :
if (arr[mid] > arr[b]) :
self.swapElement(arr, mid, b)

self.swapElement(arr, mid, i)

elif(arr[i] > arr[b]) :
if (arr[mid] < arr[b]) :
#  When second middle is less than first middle
self.swapElement(arr, mid, b)

self.swapElement(arr, b, i)

i += 1

if (arr[b] > arr[mid]) :
#  When first middle is largest to second middle element
self.swapElement(arr, mid, b)

else :
#   When length of array are Odd
#   When only One middle element possible
#  Execute loop through by size
while (i < size) :
if (arr[i] > arr[mid]) :
#  Set max value to middle of array
self.swapElement(arr, i, mid)

i += 1

def main() :
#  Define array of integer elements
arr1 = [6, 2, 8, 0, 3, 5, 2, 4, 5, 7]
arr2 = [0, 8, 4, 2, 3, 2, 4]
#  Get the size
size = len(arr1)
print(" Array elements ")
print("\n Array maximize median ")
#  Get the size
size = len(arr2)
print("\n Array elements ")
print("\n Array maximize median ")

if __name__ == "__main__": main()``````

#### Output

`````` Array elements
6   2   8   0   3   5   2   4   5   7
Array maximize median
3   2   5   0   7   8   2   4   5   6
Array elements
0   8   4   2   3   2   4
Array maximize median
0   2   4   8   3   2   4``````
``````#  Ruby Program
#  Maximize the median of array elements

class Maximize
# Display elements of given array
def printArray(arr, size)
i = 0
while (i < size)
print("  ", arr[i])
i += 1
end

end

#  Swap the elements of array by given location
def swapElement(arr, front, tail)
temp = arr[front]
arr[front] = arr[tail]
arr[tail] = temp
end

#  Maximize the median of given array element
def maximizeMedian(arr, size)
if (size <= 2)
#  When less than three element
return
end

#  Get middle element
mid = size / 2
#  Loop controlling variables
i = 0
if (size % 2 == 0)
#   When length of array are Even
#   Two middle elements are possible
#  Second middle element position
b = mid - 1
#  Execute loop through by size
#  Move two higher element at middle position
while (i < size)
if (arr[i] > arr[mid])
if (arr[mid] > arr[b])
self.swapElement(arr, mid, b)
end

self.swapElement(arr, mid, i)
elsif(arr[i] > arr[b])
if (arr[mid] < arr[b])
#  When second middle is less than first middle
self.swapElement(arr, mid, b)
end

self.swapElement(arr, b, i)
end

i += 1
end

if (arr[b] > arr[mid])
#  When first middle is largest to second middle element
self.swapElement(arr, mid, b)
end

else
#   When length of array are Odd
#   When only One middle element possible
#  Execute loop through by size
while (i < size)
if (arr[i] > arr[mid])
#  Set max value to middle of array
self.swapElement(arr, i, mid)
end

i += 1
end

end

end

end

def main()
#  Define array of integer elements
arr1 = [6, 2, 8, 0, 3, 5, 2, 4, 5, 7]
arr2 = [0, 8, 4, 2, 3, 2, 4]
#  Get the size
size = arr1.length
print(" Array elements \n")
print("\n Array maximize median \n")
#  Get the size
size = arr2.length
print("\n Array elements \n")
print("\n Array maximize median \n")
end

main()``````

#### Output

`````` Array elements
6  2  8  0  3  5  2  4  5  7
Array maximize median
3  2  5  0  7  8  2  4  5  6
Array elements
0  8  4  2  3  2  4
Array maximize median
0  2  4  8  3  2  4``````
``````/*
Scala Program
Maximize the median of array elements
*/
class Maximize
{
//Display elements of given array
def printArray(arr: Array[Int], size: Int): Unit = {
var i: Int = 0;
while (i < size)
{
print("  " + arr(i));
i += 1;
}
}
// Swap the elements of array by given location
def swapElement(arr: Array[Int], front: Int, tail: Int): Unit = {
var temp: Int = arr(front);
arr(front) = arr(tail);
arr(tail) = temp;
}
// Maximize the median of given array element
def maximizeMedian(arr: Array[Int], size: Int): Unit = {
if (size <= 2)
{
// When less than three element
return;
}
// Get middle element
var mid: Int = (size / 2).toInt;
// Loop controlling variables
var i: Int = 0;
if (size % 2 == 0)
{
//  When length of array are Even
//  Two middle elements are possible
// Second middle element position
var b: Int = mid - 1;
// Execute loop through by size
// Move two higher element at middle position
while (i < size)
{
if (arr(i) > arr(mid))
{
if (arr(mid) > arr(b))
{
this.swapElement(arr, mid, b);
}
this.swapElement(arr, mid, i);
}
else if (arr(i) > arr(b))
{
if (arr(mid) < arr(b))
{
// When second middle is less than first middle
this.swapElement(arr, mid, b);
}
this.swapElement(arr, b, i);
}
i += 1;
}
if (arr(b) > arr(mid))
{
// When first middle is largest to second middle element
this.swapElement(arr, mid, b);
}
}
else
{
//  When length of array are Odd
//  When only One middle element possible
// Execute loop through by size
while (i < size)
{
if (arr(i) > arr(mid))
{
// Set max value to middle of array
this.swapElement(arr, i, mid);
}
i += 1;
}
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Maximize = new Maximize();
// Define array of integer elements
var arr1: Array[Int] = Array(6, 2, 8, 0, 3, 5, 2, 4, 5, 7);
var arr2: Array[Int] = Array(0, 8, 4, 2, 3, 2, 4);
// Get the size
var size: Int = arr1.length;
print(" Array elements \n");
print("\n Array maximize median \n");
// Get the size
size = arr2.length;
print("\n Array elements \n");
print("\n Array maximize median \n");
}
}``````

#### Output

`````` Array elements
6  2  8  0  3  5  2  4  5  7
Array maximize median
3  2  5  0  7  8  2  4  5  6
Array elements
0  8  4  2  3  2  4
Array maximize median
0  2  4  8  3  2  4``````
``````/*
Swift 4 Program
Maximize the median of array elements
*/
class Maximize
{
//Display elements of given array
func printArray(_ arr: [Int], _ size: Int)
{
var i: Int = 0;
while (i < size)
{
print("  ", arr[i], terminator: "");
i += 1;
}
}
// Swap the elements of array by given location
func swapElement(_ arr: inout[Int], _ front: Int, _ tail: Int)
{
let temp: Int = arr[front];
arr[front] = arr[tail];
arr[tail] = temp;
}
// Maximize the median of given array element
func maximizeMedian(_ arr: inout[Int], _ size: Int)
{
if (size <= 2)
{
// When less than three element
return;
}
// Get middle element
let mid: Int = size / 2;
// Loop controlling variables
var i: Int = 0;
if (size % 2 == 0)
{
//  When length of array are Even
//  Two middle elements are possible
// Second middle element position
let b: Int = mid - 1;
// Execute loop through by size
// Move two higher element at middle position
while (i < size)
{
if (arr[i] > arr[mid])
{
if (arr[mid] > arr[b])
{
self.swapElement(&arr, mid, b);
}
self.swapElement(&arr, mid, i);
}
else if (arr[i] > arr[b])
{
if (arr[mid] < arr[b])
{
// When second middle is less than first middle
self.swapElement(&arr, mid, b);
}
self.swapElement(&arr, b, i);
}
i += 1;
}
if (arr[b] > arr[mid])
{
// When first middle is largest to second middle element
self.swapElement(&arr, mid, b);
}
}
else
{
//  When length of array are Odd
//  When only One middle element possible
// Execute loop through by size
while (i < size)
{
if (arr[i] > arr[mid])
{
// Set max value to middle of array
self.swapElement(&arr, i, mid);
}
i += 1;
}
}
}
}
func main()
{
// Define array of integer elements
var arr1: [Int] = [6, 2, 8, 0, 3, 5, 2, 4, 5, 7];
var arr2: [Int] = [0, 8, 4, 2, 3, 2, 4];
// Get the size
var size: Int = arr1.count;
print(" Array elements ");
print("\n Array maximize median ");
// Get the size
size = arr2.count;
print("\n Array elements ");
print("\n Array maximize median ");
}
main();``````

#### Output

`````` Array elements
6   2   8   0   3   5   2   4   5   7
Array maximize median
3   2   5   0   7   8   2   4   5   6
Array elements
0   8   4   2   3   2   4
Array maximize median
0   2   4   8   3   2   4``````
``````/*
Kotlin Program
Maximize the median of array elements
*/
class Maximize
{
//Display elements of given array
fun printArray(arr: Array <Int> , size: Int): Unit
{
var i: Int = 0;
while (i < size)
{
print("  " + arr[i]);
i += 1;
}
}
// Swap the elements of array by given location
fun swapElement(arr: Array <Int> , front: Int, tail: Int): Unit
{
var temp: Int = arr[front];
arr[front] = arr[tail];
arr[tail] = temp;
}
// Maximize the median of given array element
fun maximizeMedian(arr: Array <Int> , size: Int): Unit
{
if (size <= 2)
{
// When less than three element
return;
}
// Get middle element
var mid: Int = size / 2;
// Loop controlling variables
var i: Int = 0;
if (size % 2 == 0)
{
//  When length of array are Even
//  Two middle elements are possible
// Second middle element position
var b: Int = mid - 1;
// Execute loop through by size
// Move two higher element at middle position
while (i < size)
{
if (arr[i] > arr[mid])
{
if (arr[mid] > arr[b])
{
this.swapElement(arr, mid, b);
}
this.swapElement(arr, mid, i);
}
else if (arr[i] > arr[b])
{
if (arr[mid] < arr[b])
{
// When second middle is less than first middle
this.swapElement(arr, mid, b);
}
this.swapElement(arr, b, i);
}
i += 1;
}
if (arr[b] > arr[mid])
{
// When first middle is largest to second middle element
this.swapElement(arr, mid, b);
}
}
else
{
//  When length of array are Odd
//  When only One middle element possible
// Execute loop through by size
while (i < size)
{
if (arr[i] > arr[mid])
{
// Set max value to middle of array
this.swapElement(arr, i, mid);
}
i += 1;
}
}
}
}
fun main(args: Array <String> ): Unit
{
// Define array of integer elements
var arr1: Array <Int> = arrayOf(6, 2, 8, 0, 3, 5, 2, 4, 5, 7);
var arr2: Array <Int> = arrayOf(0, 8, 4, 2, 3, 2, 4);
// Get the size
var size: Int = arr1.count();
print(" Array elements \n");
print("\n Array maximize median \n");
// Get the size
size = arr2.count();
print("\n Array elements \n");
print("\n Array maximize median \n");
}``````

#### Output

`````` Array elements
6  2  8  0  3  5  2  4  5  7
Array maximize median
3  2  5  0  7  8  2  4  5  6
Array elements
0  8  4  2  3  2  4
Array maximize median
0  2  4  8  3  2  4``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post