Move all zero to middle of array

Here given code implementation process.

// C Program 
// Move all zero to middle of array
#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]);
	}
	printf("\n");
}
// 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;
}
// Move all zero elements to middle of array
void moveZero(int arr[], int size)
{
	int left = 0;
	int right = size - 1;
	int flag = 0;
	int i = 1;
	while (left < right && i < right)
	{
		if (arr[left] != 0 && arr[right] != 0)
		{
			// Best case when boundary element is not zero
			left++;
			right--;
			i++;
		}
		else if (arr[i] != 0)
		{
			if (flag == 0 && arr[left] == 0)
			{
				// When need to change the left boundary element  
				swapElement(arr, i, left);
				left++;
				flag = 1;
				i++;
			}
			else if (flag == 1 && arr[right] == 0)
			{
				// When need to change the right boundary element  
				swapElement(arr, i, right);
				right--;
				flag = 0;
				i++;
			}
			else
			{
				// When top two case fail means we need to change its sequence
				swapElement(arr, left, right);
			}
		}
		else
		{
			i++;
		}
	}
}
int main(int argc, char
	const *argv[])
{
	// Define array of integer elements
	int arr1[] = {
		0 , 0 , 0 , 0 , 3 , 1 , 0 , 5 , 0 , 0 , 1 , 0
	};
	int arr2[] = {
		0 , 2 , 3 , 0 , 3 , 0 , 1
	};
	int arr3[] = {
		1 , 2 , 5 , 3 , 4 , 0 , 4
	};
	int arr4[] = {
		0 , 0 , 4 , 0 , 0 , 9
	};
	int arr5[] = {
		0 , 0 , 4 , 0 , 0
	};
	// Test case 1
	int size = sizeof(arr1) / sizeof(arr1[0]);
	printf("  Array Element \n");
	printArray(arr1, size);
	moveZero(arr1, size);
	printf("  After move zero elements at middle  \n");
	printArray(arr1, size);
	// Test case 2
	size = sizeof(arr2) / sizeof(arr2[0]);
	printf("\n  Array Element \n");
	printArray(arr2, size);
	moveZero(arr2, size);
	printf("  After move zero elements at middle  \n");
	printArray(arr2, size);
	// Test case 3
	size = sizeof(arr3) / sizeof(arr3[0]);
	printf("\n  Array Element \n");
	printArray(arr3, size);
	moveZero(arr3, size);
	printf("  After move zero elements at middle  \n");
	printArray(arr3, size);
	// Test case 4
	size = sizeof(arr4) / sizeof(arr4[0]);
	printf("\n  Array Element \n");
	printArray(arr4, size);
	moveZero(arr4, size);
	printf("  After move zero elements at middle  \n");
	printArray(arr4, size);
	// Test case 4
	size = sizeof(arr5) / sizeof(arr5[0]);
	printf("\n  Array Element \n");
	printArray(arr5, size);
	moveZero(arr5, size);
	printf("  After move zero elements at middle  \n");
	printArray(arr5, size);
	return 0;
}

Output

  Array Element
  0  0  0  0  3  1  0  5  0  0  1  0
  After move zero elements at middle
  3  5  0  0  0  0  0  0  0  0  1  1

  Array Element
  0  2  3  0  3  0  1
  After move zero elements at middle
  2  3  0  0  0  1  3

  Array Element
  1  2  5  3  4  0  4
  After move zero elements at middle
  1  5  2  0  4  3  4

  Array Element
  0  0  4  0  0  9
  After move zero elements at middle
  4  0  0  0  0  9

  Array Element
  0  0  4  0  0
  After move zero elements at middle
  4  0  0  0  0
/*
    Java Program
    Move all zero to middle of array
*/
public class MoveElement
{
	//Display elements of given array
	public void printArray(int[] arr, int size)
	{
		for (int i = 0; i < size; ++i)
		{
			System.out.print("  " + arr[i]);
		}
		System.out.print("\n");
	}
	// 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;
	}
	// Move all zero elements to middle of array
	public void moveZero(int[] arr, int size)
	{
		int left = 0;
		int right = size - 1;
		int flag = 0;
		int i = 1;
		while (left < right && i < right)
		{
			if (arr[left] != 0 && arr[right] != 0)
			{
				// Best case when boundary element is not zero
				left++;
				right--;
				i++;
			}
			else if (arr[i] != 0)
			{
				if (flag == 0 && arr[left] == 0)
				{
					// When need to change the left boundary element  
					swapElement(arr, i, left);
					left++;
					flag = 1;
					i++;
				}
				else if (flag == 1 && arr[right] == 0)
				{
					// When need to change the right boundary element  
					swapElement(arr, i, right);
					right--;
					flag = 0;
					i++;
				}
				else
				{
					// When top two case fail means we need to change its sequence
					swapElement(arr, left, right);
				}
			}
			else
			{
				i++;
			}
		}
	}
	public static void main(String[] args)
	{
		MoveElement task = new MoveElement();
		// Define array of integer elements
		int[] arr1 = {
			0 , 0 , 0 , 0 , 3 , 1 , 0 , 5 , 0 , 0 , 1 , 0
		};
		int[] arr2 = {
			0 , 2 , 3 , 0 , 3 , 0 , 1
		};
		int[] arr3 = {
			1 , 2 , 5 , 3 , 4 , 0 , 4
		};
		int[] arr4 = {
			0 , 0 , 4 , 0 , 0 , 9
		};
		int[] arr5 = {
			0 , 0 , 4 , 0 , 0
		};
		// Test case 1
		int size = arr1.length;
		System.out.print(" Array Element \n");
		task.printArray(arr1, size);
		task.moveZero(arr1, size);
		System.out.print(" After move zero elements at middle \n");
		task.printArray(arr1, size);
		// Test case 2
		size = arr2.length;
		System.out.print("\n Array Element \n");
		task.printArray(arr2, size);
		task.moveZero(arr2, size);
		System.out.print(" After move zero elements at middle \n");
		task.printArray(arr2, size);
		// Test case 3
		size = arr3.length;;
		System.out.print("\n Array Element \n");
		task.printArray(arr3, size);
		task.moveZero(arr3, size);
		System.out.print(" After move zero elements at middle \n");
		task.printArray(arr3, size);
		// Test case 4
		size = arr4.length;;
		System.out.print("\n Array Element \n");
		task.printArray(arr4, size);
		task.moveZero(arr4, size);
		System.out.print(" After move zero elements at middle \n");
		task.printArray(arr4, size);
		// Test case 4
		size = arr5.length;;
		System.out.print("\n Array Element \n");
		task.printArray(arr5, size);
		task.moveZero(arr5, size);
		System.out.print(" After move zero elements at middle \n");
		task.printArray(arr5, size);
	}
}

Output

 Array Element
  0  0  0  0  3  1  0  5  0  0  1  0
 After move zero elements at middle
  3  5  0  0  0  0  0  0  0  0  1  1

 Array Element
  0  2  3  0  3  0  1
 After move zero elements at middle
  2  3  0  0  0  1  3

 Array Element
  1  2  5  3  4  0  4
 After move zero elements at middle
  1  5  2  0  4  3  4

 Array Element
  0  0  4  0  0  9
 After move zero elements at middle
  4  0  0  0  0  9

 Array Element
  0  0  4  0  0
 After move zero elements at middle
  4  0  0  0  0
// Include header file
#include <iostream>

using namespace std;
/*
    C++ Program
    Move all zero to middle of array
*/
class MoveElement
{
	public:
		//Display elements of given array
		void printArray(int arr[], int size)
		{
			for (int i = 0; i < size; ++i)
			{
				cout << "  " << arr[i];
			}
			cout << "\n";
		}
	// 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;
	}
	// Move all zero elements to middle of array
	void moveZero(int arr[], int size)
	{
		int left = 0;
		int right = size - 1;
		int flag = 0;
		int i = 1;
		while (left < right && i < right)
		{
			if (arr[left] != 0 && arr[right] != 0)
			{
				// Best case when boundary element is not zero
				left++;
				right--;
				i++;
			}
			else if (arr[i] != 0)
			{
				if (flag == 0 && arr[left] == 0)
				{
					// When need to change the left boundary element
					this->swapElement(arr, i, left);
					left++;
					flag = 1;
					i++;
				}
				else if (flag == 1 && arr[right] == 0)
				{
					// When need to change the right boundary element
					this->swapElement(arr, i, right);
					right--;
					flag = 0;
					i++;
				}
				else
				{
					// When top two case fail means we need to change its sequence
					this->swapElement(arr, left, right);
				}
			}
			else
			{
				i++;
			}
		}
	}
};
int main()
{
	MoveElement task = MoveElement();
	// Define array of integer elements
	int arr1[] = {
		0 , 0 , 0 , 0 , 3 , 1 , 0 , 5 , 0 , 0 , 1 , 0
	};
	int arr2[] = {
		0 , 2 , 3 , 0 , 3 , 0 , 1
	};
	int arr3[] = {
		1 , 2 , 5 , 3 , 4 , 0 , 4
	};
	int arr4[] = {
		0 , 0 , 4 , 0 , 0 , 9
	};
	int arr5[] = {
		0 , 0 , 4 , 0 , 0
	};
	// Test case 1
	int size = sizeof(arr1) / sizeof(arr1[0]);
	cout << " Array Element \n";
	task.printArray(arr1, size);
	task.moveZero(arr1, size);
	cout << " After move zero elements at middle \n";
	task.printArray(arr1, size);
	// Test case 2
	size = sizeof(arr2) / sizeof(arr2[0]);
	cout << "\n Array Element \n";
	task.printArray(arr2, size);
	task.moveZero(arr2, size);
	cout << " After move zero elements at middle \n";
	task.printArray(arr2, size);
	// Test case 3
	size = sizeof(arr3) / sizeof(arr3[0]);;
	cout << "\n Array Element \n";
	task.printArray(arr3, size);
	task.moveZero(arr3, size);
	cout << " After move zero elements at middle \n";
	task.printArray(arr3, size);
	// Test case 4
	size = sizeof(arr4) / sizeof(arr4[0]);;
	cout << "\n Array Element \n";
	task.printArray(arr4, size);
	task.moveZero(arr4, size);
	cout << " After move zero elements at middle \n";
	task.printArray(arr4, size);
	// Test case 4
	size = sizeof(arr5) / sizeof(arr5[0]);;
	cout << "\n Array Element \n";
	task.printArray(arr5, size);
	task.moveZero(arr5, size);
	cout << " After move zero elements at middle \n";
	task.printArray(arr5, size);
	return 0;
}

Output

 Array Element
  0  0  0  0  3  1  0  5  0  0  1  0
 After move zero elements at middle
  3  5  0  0  0  0  0  0  0  0  1  1

 Array Element
  0  2  3  0  3  0  1
 After move zero elements at middle
  2  3  0  0  0  1  3

 Array Element
  1  2  5  3  4  0  4
 After move zero elements at middle
  1  5  2  0  4  3  4

 Array Element
  0  0  4  0  0  9
 After move zero elements at middle
  4  0  0  0  0  9

 Array Element
  0  0  4  0  0
 After move zero elements at middle
  4  0  0  0  0
// Include namespace system
using System;
/*
    C# Program
    Move all zero to middle of array
*/
public class MoveElement
{
	//Display elements of given array
	public void printArray(int[] arr, int size)
	{
		for (int i = 0; i < size; ++i)
		{
			Console.Write("  " + arr[i]);
		}
		Console.Write("\n");
	}
	// 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;
	}
	// Move all zero elements to middle of array
	public void moveZero(int[] arr, int size)
	{
		int left = 0;
		int right = size - 1;
		int flag = 0;
		int i = 1;
		while (left < right && i < right)
		{
			if (arr[left] != 0 && arr[right] != 0)
			{
				// Best case when boundary element is not zero
				left++;
				right--;
				i++;
			}
			else if (arr[i] != 0)
			{
				if (flag == 0 && arr[left] == 0)
				{
					// When need to change the left boundary element
					swapElement(arr, i, left);
					left++;
					flag = 1;
					i++;
				}
				else if (flag == 1 && arr[right] == 0)
				{
					// When need to change the right boundary element
					swapElement(arr, i, right);
					right--;
					flag = 0;
					i++;
				}
				else
				{
					// When top two case fail means we need to change its sequence
					swapElement(arr, left, right);
				}
			}
			else
			{
				i++;
			}
		}
	}
	public static void Main(String[] args)
	{
		MoveElement task = new MoveElement();
		// Define array of integer elements
		int[] arr1 = {
			0 , 0 , 0 , 0 , 3 , 1 , 0 , 5 , 0 , 0 , 1 , 0
		};
		int[] arr2 = {
			0 , 2 , 3 , 0 , 3 , 0 , 1
		};
		int[] arr3 = {
			1 , 2 , 5 , 3 , 4 , 0 , 4
		};
		int[] arr4 = {
			0 , 0 , 4 , 0 , 0 , 9
		};
		int[] arr5 = {
			0 , 0 , 4 , 0 , 0
		};
		// Test case 1
		int size = arr1.Length;
		Console.Write(" Array Element \n");
		task.printArray(arr1, size);
		task.moveZero(arr1, size);
		Console.Write(" After move zero elements at middle \n");
		task.printArray(arr1, size);
		// Test case 2
		size = arr2.Length;
		Console.Write("\n Array Element \n");
		task.printArray(arr2, size);
		task.moveZero(arr2, size);
		Console.Write(" After move zero elements at middle \n");
		task.printArray(arr2, size);
		// Test case 3
		size = arr3.Length;;
		Console.Write("\n Array Element \n");
		task.printArray(arr3, size);
		task.moveZero(arr3, size);
		Console.Write(" After move zero elements at middle \n");
		task.printArray(arr3, size);
		// Test case 4
		size = arr4.Length;;
		Console.Write("\n Array Element \n");
		task.printArray(arr4, size);
		task.moveZero(arr4, size);
		Console.Write(" After move zero elements at middle \n");
		task.printArray(arr4, size);
		// Test case 4
		size = arr5.Length;;
		Console.Write("\n Array Element \n");
		task.printArray(arr5, size);
		task.moveZero(arr5, size);
		Console.Write(" After move zero elements at middle \n");
		task.printArray(arr5, size);
	}
}

Output

 Array Element
  0  0  0  0  3  1  0  5  0  0  1  0
 After move zero elements at middle
  3  5  0  0  0  0  0  0  0  0  1  1

 Array Element
  0  2  3  0  3  0  1
 After move zero elements at middle
  2  3  0  0  0  1  3

 Array Element
  1  2  5  3  4  0  4
 After move zero elements at middle
  1  5  2  0  4  3  4

 Array Element
  0  0  4  0  0  9
 After move zero elements at middle
  4  0  0  0  0  9

 Array Element
  0  0  4  0  0
 After move zero elements at middle
  4  0  0  0  0
<?php
/*
    Php Program
    Move all zero to middle of array
*/
class MoveElement
{
	//Display elements of given array
	public	function printArray( & $arr, $size)
	{
		for ($i = 0; $i < $size; ++$i)
		{
			echo "  ". $arr[$i];
		}
		echo "\n";
	}
	// Swap the elements of array by given location
	public	function swapElement( & $arr, $front, $tail)
	{
		$temp = $arr[$front];
		$arr[$front] = $arr[$tail];
		$arr[$tail] = $temp;
	}
	// Move all zero elements to middle of array
	public	function moveZero( & $arr, $size)
	{
		$left = 0;
		$right = $size - 1;
		$flag = 0;
		$i = 1;
		while ($left < $right && $i < $right)
		{
			if ($arr[$left] != 0 && $arr[$right] != 0)
			{
				// Best case when boundary element is not zero
				$left++;
				$right--;
				$i++;
			}
			else if ($arr[$i] != 0)
			{
				if ($flag == 0 && $arr[$left] == 0)
				{
					// When need to change the left boundary element
					$this->swapElement($arr, $i, $left);
					$left++;
					$flag = 1;
					$i++;
				}
				else if ($flag == 1 && $arr[$right] == 0)
				{
					// When need to change the right boundary element
					$this->swapElement($arr, $i, $right);
					$right--;
					$flag = 0;
					$i++;
				}
				else
				{
					// When top two case fail means we need to change its sequence
					$this->swapElement($arr, $left, $right);
				}
			}
			else
			{
				$i++;
			}
		}
	}
}

function main()
{
	$task = new MoveElement();
	// Define array of integer elements
	$arr1 = array(0, 0, 0, 0, 3, 1, 0, 5, 0, 0, 1, 0);
	$arr2 = array(0, 2, 3, 0, 3, 0, 1);
	$arr3 = array(1, 2, 5, 3, 4, 0, 4);
	$arr4 = array(0, 0, 4, 0, 0, 9);
	$arr5 = array(0, 0, 4, 0, 0);
	// Test case 1
	$size = count($arr1);
	echo " Array Element \n";
	$task->printArray($arr1, $size);
	$task->moveZero($arr1, $size);
	echo " After move zero elements at middle \n";
	$task->printArray($arr1, $size);
	// Test case 2
	$size = count($arr2);
	echo "\n Array Element \n";
	$task->printArray($arr2, $size);
	$task->moveZero($arr2, $size);
	echo " After move zero elements at middle \n";
	$task->printArray($arr2, $size);
	// Test case 3
	$size = count($arr3);;
	echo "\n Array Element \n";
	$task->printArray($arr3, $size);
	$task->moveZero($arr3, $size);
	echo " After move zero elements at middle \n";
	$task->printArray($arr3, $size);
	// Test case 4
	$size = count($arr4);;
	echo "\n Array Element \n";
	$task->printArray($arr4, $size);
	$task->moveZero($arr4, $size);
	echo " After move zero elements at middle \n";
	$task->printArray($arr4, $size);
	// Test case 4
	$size = count($arr5);;
	echo "\n Array Element \n";
	$task->printArray($arr5, $size);
	$task->moveZero($arr5, $size);
	echo " After move zero elements at middle \n";
	$task->printArray($arr5, $size);
}
main();

Output

 Array Element
  0  0  0  0  3  1  0  5  0  0  1  0
 After move zero elements at middle
  3  5  0  0  0  0  0  0  0  0  1  1

 Array Element
  0  2  3  0  3  0  1
 After move zero elements at middle
  2  3  0  0  0  1  3

 Array Element
  1  2  5  3  4  0  4
 After move zero elements at middle
  1  5  2  0  4  3  4

 Array Element
  0  0  4  0  0  9
 After move zero elements at middle
  4  0  0  0  0  9

 Array Element
  0  0  4  0  0
 After move zero elements at middle
  4  0  0  0  0
/*
    Node Js Program
    Move all zero to middle of array
*/
class MoveElement
{
	//Display elements of given array
	printArray(arr, size)
	{
		for (var i = 0; i < size; ++i)
		{
			process.stdout.write("  " + arr[i]);
		}
		process.stdout.write("\n");
	}
	// Swap the elements of array by given location
	swapElement(arr, front, tail)
	{
		var temp = arr[front];
		arr[front] = arr[tail];
		arr[tail] = temp;
	}
	// Move all zero elements to middle of array
	moveZero(arr, size)
	{
		var left = 0;
		var right = size - 1;
		var flag = 0;
		var i = 1;
		while (left < right && i < right)
		{
			if (arr[left] != 0 && arr[right] != 0)
			{
				// Best case when boundary element is not zero
				left++;
				right--;
				i++;
			}
			else if (arr[i] != 0)
			{
				if (flag == 0 && arr[left] == 0)
				{
					// When need to change the left boundary element
					this.swapElement(arr, i, left);
					left++;
					flag = 1;
					i++;
				}
				else if (flag == 1 && arr[right] == 0)
				{
					// When need to change the right boundary element
					this.swapElement(arr, i, right);
					right--;
					flag = 0;
					i++;
				}
				else
				{
					// When top two case fail means we need to change its sequence
					this.swapElement(arr, left, right);
				}
			}
			else
			{
				i++;
			}
		}
	}
}

function main()
{
	var task = new MoveElement();
	// Define array of integer elements
	var arr1 = [0, 0, 0, 0, 3, 1, 0, 5, 0, 0, 1, 0];
	var arr2 = [0, 2, 3, 0, 3, 0, 1];
	var arr3 = [1, 2, 5, 3, 4, 0, 4];
	var arr4 = [0, 0, 4, 0, 0, 9];
	var arr5 = [0, 0, 4, 0, 0];
	// Test case 1
	var size = arr1.length;
	process.stdout.write(" Array Element \n");
	task.printArray(arr1, size);
	task.moveZero(arr1, size);
	process.stdout.write(" After move zero elements at middle \n");
	task.printArray(arr1, size);
	// Test case 2
	size = arr2.length;
	process.stdout.write("\n Array Element \n");
	task.printArray(arr2, size);
	task.moveZero(arr2, size);
	process.stdout.write(" After move zero elements at middle \n");
	task.printArray(arr2, size);
	// Test case 3
	size = arr3.length;;
	process.stdout.write("\n Array Element \n");
	task.printArray(arr3, size);
	task.moveZero(arr3, size);
	process.stdout.write(" After move zero elements at middle \n");
	task.printArray(arr3, size);
	// Test case 4
	size = arr4.length;;
	process.stdout.write("\n Array Element \n");
	task.printArray(arr4, size);
	task.moveZero(arr4, size);
	process.stdout.write(" After move zero elements at middle \n");
	task.printArray(arr4, size);
	// Test case 4
	size = arr5.length;;
	process.stdout.write("\n Array Element \n");
	task.printArray(arr5, size);
	task.moveZero(arr5, size);
	process.stdout.write(" After move zero elements at middle \n");
	task.printArray(arr5, size);
}
main();

Output

 Array Element
  0  0  0  0  3  1  0  5  0  0  1  0
 After move zero elements at middle
  3  5  0  0  0  0  0  0  0  0  1  1

 Array Element
  0  2  3  0  3  0  1
 After move zero elements at middle
  2  3  0  0  0  1  3

 Array Element
  1  2  5  3  4  0  4
 After move zero elements at middle
  1  5  2  0  4  3  4

 Array Element
  0  0  4  0  0  9
 After move zero elements at middle
  4  0  0  0  0  9

 Array Element
  0  0  4  0  0
 After move zero elements at middle
  4  0  0  0  0
#  Python 3 Program
#  Move all zero to middle of array

class MoveElement :
	# Display elements of given array
	def printArray(self, arr, size) :
		i = 0
		while (i < size) :
			print("  ", arr[i], end = "")
			i += 1
		
		print(end = "\n")
	
	#  Swap the elements of array by given location
	def swapElement(self, arr, front, tail) :
		temp = arr[front]
		arr[front] = arr[tail]
		arr[tail] = temp
	
	#  Move all zero elements to middle of array
	def moveZero(self, arr, size) :
		left = 0
		right = size - 1
		flag = 0
		i = 1
		while (left < right and i < right) :
			if (arr[left] != 0 and arr[right] != 0) :
				#  Best case when boundary element is not zero
				left += 1
				right -= 1
				i += 1
			
			elif(arr[i] != 0) :
				if (flag == 0 and arr[left] == 0) :
					#  When need to change the left boundary element
					self.swapElement(arr, i, left)
					left += 1
					flag = 1
					i += 1
				
				elif(flag == 1 and arr[right] == 0) :
					#  When need to change the right boundary element
					self.swapElement(arr, i, right)
					right -= 1
					flag = 0
					i += 1
				else :
					#  When top two case fail means we need to change its sequence
					self.swapElement(arr, left, right)
				
			else :
				i += 1
			
		
	

def main() :
	task = MoveElement()
	#  Define array of integer elements
	arr1 = [0, 0, 0, 0, 3, 1, 0, 5, 0, 0, 1, 0]
	arr2 = [0, 2, 3, 0, 3, 0, 1]
	arr3 = [1, 2, 5, 3, 4, 0, 4]
	arr4 = [0, 0, 4, 0, 0, 9]
	arr5 = [0, 0, 4, 0, 0]
	#  Test case 1
	size = len(arr1)
	print(" Array Element ")
	task.printArray(arr1, size)
	task.moveZero(arr1, size)
	print(" After move zero elements at middle ")
	task.printArray(arr1, size)
	#  Test case 2
	size = len(arr2)
	print("\n Array Element ")
	task.printArray(arr2, size)
	task.moveZero(arr2, size)
	print(" After move zero elements at middle ")
	task.printArray(arr2, size)
	#  Test case 3
	size = len(arr3)
	print("\n Array Element ")
	task.printArray(arr3, size)
	task.moveZero(arr3, size)
	print(" After move zero elements at middle ")
	task.printArray(arr3, size)
	#  Test case 4
	size = len(arr4)
	print("\n Array Element ")
	task.printArray(arr4, size)
	task.moveZero(arr4, size)
	print(" After move zero elements at middle ")
	task.printArray(arr4, size)
	#  Test case 4
	size = len(arr5)
	print("\n Array Element ")
	task.printArray(arr5, size)
	task.moveZero(arr5, size)
	print(" After move zero elements at middle ")
	task.printArray(arr5, size)

if __name__ == "__main__": main()

Output

 Array Element
   0   0   0   0   3   1   0   5   0   0   1   0
 After move zero elements at middle
   3   5   0   0   0   0   0   0   0   0   1   1

 Array Element
   0   2   3   0   3   0   1
 After move zero elements at middle
   2   3   0   0   0   1   3

 Array Element
   1   2   5   3   4   0   4
 After move zero elements at middle
   1   5   2   0   4   3   4

 Array Element
   0   0   4   0   0   9
 After move zero elements at middle
   4   0   0   0   0   9

 Array Element
   0   0   4   0   0
 After move zero elements at middle
   4   0   0   0   0
#  Ruby Program
#  Move all zero to middle of array

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

		print("\n")
	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

	#  Move all zero elements to middle of array
	def moveZero(arr, size) 
		left = 0
		right = size - 1
		flag = 0
		i = 1
		while (left < right && i < right) 
			if (arr[left] != 0 && arr[right] != 0) 
				#  Best case when boundary element is not zero
				left += 1
				right -= 1
				i += 1
			elsif(arr[i] != 0) 
				if (flag == 0 && arr[left] == 0) 
					#  When need to change the left boundary element
					self.swapElement(arr, i, left)
					left += 1
					flag = 1
					i += 1
				elsif(flag == 1 && arr[right] == 0) 
					#  When need to change the right boundary element
					self.swapElement(arr, i, right)
					right -= 1
					flag = 0
					i += 1
				else 
					#  When top two case fail means we need to change its sequence
					self.swapElement(arr, left, right)
				end

			else 
				i += 1
			end

		end

	end

end

def main() 
	task = MoveElement.new()
	#  Define array of integer elements
	arr1 = [0, 0, 0, 0, 3, 1, 0, 5, 0, 0, 1, 0]
	arr2 = [0, 2, 3, 0, 3, 0, 1]
	arr3 = [1, 2, 5, 3, 4, 0, 4]
	arr4 = [0, 0, 4, 0, 0, 9]
	arr5 = [0, 0, 4, 0, 0]
	#  Test case 1
	size = arr1.length
	print(" Array Element \n")
	task.printArray(arr1, size)
	task.moveZero(arr1, size)
	print(" After move zero elements at middle \n")
	task.printArray(arr1, size)
	#  Test case 2
	size = arr2.length
	print("\n Array Element \n")
	task.printArray(arr2, size)
	task.moveZero(arr2, size)
	print(" After move zero elements at middle \n")
	task.printArray(arr2, size)
	#  Test case 3
	size = arr3.length
	print("\n Array Element \n")
	task.printArray(arr3, size)
	task.moveZero(arr3, size)
	print(" After move zero elements at middle \n")
	task.printArray(arr3, size)
	#  Test case 4
	size = arr4.length
	print("\n Array Element \n")
	task.printArray(arr4, size)
	task.moveZero(arr4, size)
	print(" After move zero elements at middle \n")
	task.printArray(arr4, size)
	#  Test case 4
	size = arr5.length
	print("\n Array Element \n")
	task.printArray(arr5, size)
	task.moveZero(arr5, size)
	print(" After move zero elements at middle \n")
	task.printArray(arr5, size)
end

main()

Output

 Array Element 
  0  0  0  0  3  1  0  5  0  0  1  0
 After move zero elements at middle 
  3  5  0  0  0  0  0  0  0  0  1  1

 Array Element 
  0  2  3  0  3  0  1
 After move zero elements at middle 
  2  3  0  0  0  1  3

 Array Element 
  1  2  5  3  4  0  4
 After move zero elements at middle 
  1  5  2  0  4  3  4

 Array Element 
  0  0  4  0  0  9
 After move zero elements at middle 
  4  0  0  0  0  9

 Array Element 
  0  0  4  0  0
 After move zero elements at middle 
  4  0  0  0  0
/*
    Scala Program
    Move all zero to middle of array
*/
class MoveElement
{
	//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;
		}
		print("\n");
	}
	// 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;
	}
	// Move all zero elements to middle of array
	def moveZero(arr: Array[Int], size: Int): Unit = {
		var left: Int = 0;
		var right: Int = size - 1;
		var flag: Int = 0;
		var i: Int = 1;
		while (left < right && i < right)
		{
			if (arr(left) != 0 && arr(right) != 0)
			{
				// Best case when boundary element is not zero
				left += 1;
				right -= 1;
				i += 1;
			}
			else if (arr(i) != 0)
			{
				if (flag == 0 && arr(left) == 0)
				{
					// When need to change the left boundary element
					this.swapElement(arr, i, left);
					left += 1;
					flag = 1;
					i += 1;
				}
				else if (flag == 1 && arr(right) == 0)
				{
					// When need to change the right boundary element
					this.swapElement(arr, i, right);
					right -= 1;
					flag = 0;
					i += 1;
				}
				else
				{
					// When top two case fail means we need to change its sequence
					this.swapElement(arr, left, right);
				}
			}
			else
			{
				i += 1;
			}
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: MoveElement = new MoveElement();
		// Define array of integer elements
		var arr1: Array[Int] = Array(0, 0, 0, 0, 3, 1, 0, 5, 0, 0, 1, 0);
		var arr2: Array[Int] = Array(0, 2, 3, 0, 3, 0, 1);
		var arr3: Array[Int] = Array(1, 2, 5, 3, 4, 0, 4);
		var arr4: Array[Int] = Array(0, 0, 4, 0, 0, 9);
		var arr5: Array[Int] = Array(0, 0, 4, 0, 0);
		// Test case 1
		var size: Int = arr1.length;
		print(" Array Element \n");
		task.printArray(arr1, size);
		task.moveZero(arr1, size);
		print(" After move zero elements at middle \n");
		task.printArray(arr1, size);
		// Test case 2
		size = arr2.length;
		print("\n Array Element \n");
		task.printArray(arr2, size);
		task.moveZero(arr2, size);
		print(" After move zero elements at middle \n");
		task.printArray(arr2, size);
		// Test case 3
		size = arr3.length;
		print("\n Array Element \n");
		task.printArray(arr3, size);
		task.moveZero(arr3, size);
		print(" After move zero elements at middle \n");
		task.printArray(arr3, size);
		// Test case 4
		size = arr4.length;
		print("\n Array Element \n");
		task.printArray(arr4, size);
		task.moveZero(arr4, size);
		print(" After move zero elements at middle \n");
		task.printArray(arr4, size);
		// Test case 4
		size = arr5.length;
		print("\n Array Element \n");
		task.printArray(arr5, size);
		task.moveZero(arr5, size);
		print(" After move zero elements at middle \n");
		task.printArray(arr5, size);
	}
}

Output

 Array Element
  0  0  0  0  3  1  0  5  0  0  1  0
 After move zero elements at middle
  3  5  0  0  0  0  0  0  0  0  1  1

 Array Element
  0  2  3  0  3  0  1
 After move zero elements at middle
  2  3  0  0  0  1  3

 Array Element
  1  2  5  3  4  0  4
 After move zero elements at middle
  1  5  2  0  4  3  4

 Array Element
  0  0  4  0  0  9
 After move zero elements at middle
  4  0  0  0  0  9

 Array Element
  0  0  4  0  0
 After move zero elements at middle
  4  0  0  0  0
/*
    Swift 4 Program
    Move all zero to middle of array
*/
class MoveElement
{
	//Display elements of given array
	func printArray(_ arr: [Int], _ size: Int)
	{
		var i: Int = 0;
		while (i < size)
		{
			print("  ", arr[i], terminator: "");
			i += 1;
		}
		print(terminator: "\n");
	}
	// 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;
	}
	// Move all zero elements to middle of array
	func moveZero(_ arr: inout[Int], _ size: Int)
	{
		var left: Int = 0;
		var right: Int = size - 1;
		var flag: Int = 0;
		var i: Int = 1;
		while (left < right && i < right)
		{
			if (arr[left] != 0 && arr[right] != 0)
			{
				// Best case when boundary element is not zero
				left += 1;
				right -= 1;
				i += 1;
			}
			else if (arr[i] != 0)
			{
				if (flag == 0 && arr[left] == 0)
				{
					// When need to change the left boundary element
					self.swapElement(&arr, i, left);
					left += 1;
					flag = 1;
					i += 1;
				}
				else if (flag == 1 && arr[right] == 0)
				{
					// When need to change the right boundary element
					self.swapElement(&arr, i, right);
					right -= 1;
					flag = 0;
					i += 1;
				}
				else
				{
					// When top two case fail means we need to change its sequence
					self.swapElement(&arr, left, right);
				}
			}
			else
			{
				i += 1;
			}
		}
	}
}
func main()
{
	let task: MoveElement = MoveElement();
	// Define array of integer elements
	var arr1: [Int] = [0, 0, 0, 0, 3, 1, 0, 5, 0, 0, 1, 0];
	var arr2: [Int] = [0, 2, 3, 0, 3, 0, 1];
	var arr3: [Int] = [1, 2, 5, 3, 4, 0, 4];
	var arr4: [Int] = [0, 0, 4, 0, 0, 9];
	var arr5: [Int] = [0, 0, 4, 0, 0];
	// Test case 1
	var size: Int = arr1.count;
	print(" Array Element ");
	task.printArray(arr1, size);
	task.moveZero(&arr1, size);
	print(" After move zero elements at middle ");
	task.printArray(arr1, size);
	// Test case 2
	size = arr2.count;
	print("\n Array Element ");
	task.printArray(arr2, size);
	task.moveZero(&arr2, size);
	print(" After move zero elements at middle ");
	task.printArray(arr2, size);
	// Test case 3
	size = arr3.count;
	print("\n Array Element ");
	task.printArray(arr3, size);
	task.moveZero(&arr3, size);
	print(" After move zero elements at middle ");
	task.printArray(arr3, size);
	// Test case 4
	size = arr4.count;
	print("\n Array Element ");
	task.printArray(arr4, size);
	task.moveZero(&arr4, size);
	print(" After move zero elements at middle ");
	task.printArray(arr4, size);
	// Test case 4
	size = arr5.count;
	print("\n Array Element ");
	task.printArray(arr5, size);
	task.moveZero(&arr5, size);
	print(" After move zero elements at middle ");
	task.printArray(arr5, size);
}
main();

Output

 Array Element
   0   0   0   0   3   1   0   5   0   0   1   0
 After move zero elements at middle
   3   5   0   0   0   0   0   0   0   0   1   1

 Array Element
   0   2   3   0   3   0   1
 After move zero elements at middle
   2   3   0   0   0   1   3

 Array Element
   1   2   5   3   4   0   4
 After move zero elements at middle
   1   5   2   0   4   3   4

 Array Element
   0   0   4   0   0   9
 After move zero elements at middle
   4   0   0   0   0   9

 Array Element
   0   0   4   0   0
 After move zero elements at middle
   4   0   0   0   0
/*
    Kotlin Program
    Move all zero to middle of array
*/
class MoveElement
{
	//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;
		}
		print("\n");
	}
	// 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;
	}
	// Move all zero elements to middle of array
	fun moveZero(arr: Array<Int>, size: Int): Unit
	{
		var left: Int = 0;
		var right: Int = size - 1;
		var flag: Int = 0;
		var i: Int = 1;
		while (left<right && i<right)
		{
			if (arr[left] != 0 && arr[right] != 0)
			{
				// Best case when boundary element is not zero
				left += 1;
				right -= 1;
				i += 1;
			}
			else
			if (arr[i] != 0)
			{
				if (flag == 0 && arr[left] == 0)
				{
					// When need to change the left boundary element
					this.swapElement(arr, i, left);
					left += 1;
					flag = 1;
					i += 1;
				}
				else
				if (flag == 1 && arr[right] == 0)
				{
					// When need to change the right boundary element
					this.swapElement(arr, i, right);
					right -= 1;
					flag = 0;
					i += 1;
				}
				else
				{
					// When top two case fail means we need to change its sequence
					this.swapElement(arr, left, right);
				}
			}
			else
			{
				i += 1;
			}
		}
	}
}
fun main(args: Array<String>): Unit
{
	var task: MoveElement = MoveElement();
	// Define array of integer elements
	var arr1: Array<Int> = arrayOf(0, 0, 0, 0, 3, 1, 0, 5, 0, 0, 1, 0);
	var arr2: Array<Int> = arrayOf(0, 2, 3, 0, 3, 0, 1);
	var arr3: Array<Int> = arrayOf(1, 2, 5, 3, 4, 0, 4);
	var arr4: Array<Int> = arrayOf(0, 0, 4, 0, 0, 9);
	var arr5: Array<Int> = arrayOf(0, 0, 4, 0, 0);
	// Test case 1
	var size: Int = arr1.count();
	print(" Array Element \n");
	task.printArray(arr1, size);
	task.moveZero(arr1, size);
	print(" After move zero elements at middle \n");
	task.printArray(arr1, size);
	// Test case 2
	size = arr2.count();
	print("\n Array Element \n");
	task.printArray(arr2, size);
	task.moveZero(arr2, size);
	print(" After move zero elements at middle \n");
	task.printArray(arr2, size);
	// Test case 3
	size = arr3.count();
	print("\n Array Element \n");
	task.printArray(arr3, size);
	task.moveZero(arr3, size);
	print(" After move zero elements at middle \n");
	task.printArray(arr3, size);
	// Test case 4
	size = arr4.count();
	print("\n Array Element \n");
	task.printArray(arr4, size);
	task.moveZero(arr4, size);
	print(" After move zero elements at middle \n");
	task.printArray(arr4, size);
	// Test case 4
	size = arr5.count();
	print("\n Array Element \n");
	task.printArray(arr5, size);
	task.moveZero(arr5, size);
	print(" After move zero elements at middle \n");
	task.printArray(arr5, size);
}

Output

 Array Element
  0  0  0  0  3  1  0  5  0  0  1  0
 After move zero elements at middle
  3  5  0  0  0  0  0  0  0  0  1  1

 Array Element
  0  2  3  0  3  0  1
 After move zero elements at middle
  2  3  0  0  0  1  3

 Array Element
  1  2  5  3  4  0  4
 After move zero elements at middle
  1  5  2  0  4  3  4

 Array Element
  0  0  4  0  0  9
 After move zero elements at middle
  4  0  0  0  0  9

 Array Element
  0  0  4  0  0
 After move zero elements at middle
  4  0  0  0  0


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.

New Comment







© 2021, kalkicode.com, All rights reserved