Double the first element and move zero to end

Here given code implementation process.

// C Program 
// Double the first element and move zero to end
#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");
}
// Double the first element and move the exist zero to end of array
void arrange(int arr[], int size)
{
	// Loop controlling variables
	int i = 0;
	int j = 0;
	// Execute Loop through by size
	while (i < size - 1)
	{
		if (arr[i] == arr[i + 1] && arr[i] != 0)
		{
			// Double the current element
			arr[i] += arr[i];
			// Set next element as to zero
			arr[i + 1] = 0;
			i += 2;
		}
		else
		{
			i++;
		}
	}
	i = 0;
	// Move zero to end
	while (i < size || j < size)
	{
		if (i < size)
		{
			if (arr[i] != 0)
			{
				arr[j] = arr[i];
				j++;
			}
			i++;
		}
		else
		{
			// Append zeros
			arr[j] = 0;
			j++;
		}
	}
}
int main(int argc, char
	const *argv[])
{
	// Define array of integer elements
	int arr[] = {
		1 , 0 , 0 , 3 , 3 , 3 , 0 , 4 , 4 , 2 , 9 , 0
	};
	// Get the size
	int size = sizeof(arr) / sizeof(arr[0]);
	/*
		Process
		Given array : (1,0,0,3,3,3,0,4,4,2,9,0)
		-----------------------------------------
		// Double first consecutive similar (non zero) elements
		(3,3) fist pair
		// Double first element
		(1,0,0,6,3,3,0,4,4,2,9,0)
		// Change second element value by zero
		(1,0,0,6,0,3,0,4,4,2,9,0)
		// similar way
		(4,4) second pair
		(1,0,0,6,0,3,0,4,4,2,9,0)
		// Double first element	
		(1,0,0,6,0,3,0,8,4,2,9,0)
		// Change second element value by zero
		(1,0,0,6,0,3,0,8,0,2,9,0)
		// Last step, move zero to end of array
		----------------------------------------------
		(1, 6, 3,  8,  2,  9,  0,  0,  0,  0,  0,  0)
		----------------------------------------------
	*/
	printf("\n Array element \n");
	printArray(arr, size);
	// arrange element
	arrange(arr, size);
	printf(" After Arrange \n");
	printArray(arr, size);
	return 0;
}

Output

 Array element
  1  0  0  3  3  3  0  4  4  2  9  0
 After Arrange
  1  6  3  8  2  9  0  0  0  0  0  0
/*
  Java Program for
  Double the first element and move zero to end
*/
class Arrangement
{
	//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");
	}
	// Double the first element and move the exist zero to end of array
	public void arrange(int[] arr, int size)
	{
		// Loop controlling variables
		int i = 0;
		int j = 0;
		// Execute Loop through by size
		while (i < size - 1)
		{
			if (arr[i] == arr[i + 1] && arr[i] != 0)
			{
				// Double the current element
				arr[i] += arr[i];
				// Set next element as to zero
				arr[i + 1] = 0;
				i += 2;
			}
			else
			{
				i++;
			}
		}
		i = 0;
		// Move zero to end
		while (i < size || j < size)
		{
			if (i < size)
			{
				if (arr[i] != 0)
				{
					arr[j] = arr[i];
					j++;
				}
				i++;
			}
			else
			{
				// Append zeros
				arr[j] = 0;
				j++;
			}
		}
	}
	public static void main(String[] args)
	{
		Arrangement task = new Arrangement();
		// Define array of integer elements
		int[] arr = {
			1 , 0 , 0 , 3 , 3 , 3 , 0 , 4 , 4 , 2 , 9 , 0
		};
		// Get the size
		int size = arr.length;
		// Display element
		System.out.print("\n Array element \n");
		task.printArray(arr, size);
		// arrange element
		task.arrange(arr, size);
		System.out.print(" After Arrange \n");
		task.printArray(arr, size);
	}
}

Output

 Array element
 1 0 0 3 3 3 0 4 4 2 9 0
 After Arrange
 1 6 3 8 2 9 0 0 0 0 0 0
// Include header file
#include <iostream>
using namespace std;

/*
  C++ Program for
  Double the first element and move zero to end
*/

class Arrangement
{
	public:
		//Display elements of given array
		void printArray(int arr[], int size)
		{
			for (int i = 0; i < size; ++i)
			{
				cout << " " << arr[i];
			}
			cout << "\n";
		}
	// Double the first element and move the exist zero to end of array
	void arrange(int arr[], int size)
	{
		// Loop controlling variables
		int i = 0;
		int j = 0;
		// Execute Loop through by size
		while (i < size - 1)
		{
			if (arr[i] == arr[i + 1] && arr[i] != 0)
			{
				// Double the current element
				arr[i] += arr[i];
				// Set next element as to zero
				arr[i + 1] = 0;
				i += 2;
			}
			else
			{
				i++;
			}
		}
		i = 0;
		// Move zero to end
		while (i < size || j < size)
		{
			if (i < size)
			{
				if (arr[i] != 0)
				{
					arr[j] = arr[i];
					j++;
				}
				i++;
			}
			else
			{
				// Append zeros
				arr[j] = 0;
				j++;
			}
		}
	}
};
int main()
{
	Arrangement task = Arrangement();
	// Define array of integer elements
	int arr[] = {
		1 , 0 , 0 , 3 , 3 , 3 , 0 , 4 , 4 , 2 , 9 , 0
	};
	// Get the size
	int size = sizeof(arr) / sizeof(arr[0]);
	// Display element
	cout << "\n Array element \n";
	task.printArray(arr, size);
	// arrange element
	task.arrange(arr, size);
	cout << " After Arrange \n";
	task.printArray(arr, size);
	return 0;
}

Output

 Array element
 1 0 0 3 3 3 0 4 4 2 9 0
 After Arrange
 1 6 3 8 2 9 0 0 0 0 0 0
// Include namespace system
using System;
/*
  C# Program for
  Double the first element and move zero to end
*/
public class Arrangement
{
	//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");
	}
	// Double the first element and move the exist zero to end of array
	public void arrange(int[] arr, int size)
	{
		// Loop controlling variables
		int i = 0;
		int j = 0;
		// Execute Loop through by size
		while (i < size - 1)
		{
			if (arr[i] == arr[i + 1] && arr[i] != 0)
			{
				// Double the current element
				arr[i] += arr[i];
				// Set next element as to zero
				arr[i + 1] = 0;
				i += 2;
			}
			else
			{
				i++;
			}
		}
		i = 0;
		// Move zero to end
		while (i < size || j < size)
		{
			if (i < size)
			{
				if (arr[i] != 0)
				{
					arr[j] = arr[i];
					j++;
				}
				i++;
			}
			else
			{
				// Append zeros
				arr[j] = 0;
				j++;
			}
		}
	}
	public static void Main(String[] args)
	{
		Arrangement task = new Arrangement();
		// Define array of integer elements
		int[] arr = {
			1 , 0 , 0 , 3 , 3 , 3 , 0 , 4 , 4 , 2 , 9 , 0
		};
		// Get the size
		int size = arr.Length;
		// Display element
		Console.Write("\n Array element \n");
		task.printArray(arr, size);
		// arrange element
		task.arrange(arr, size);
		Console.Write(" After Arrange \n");
		task.printArray(arr, size);
	}
}

Output

 Array element
 1 0 0 3 3 3 0 4 4 2 9 0
 After Arrange
 1 6 3 8 2 9 0 0 0 0 0 0
<?php
/*
  Php Program for
  Double the first element and move zero to end
*/
class Arrangement
{
	//Display elements of given array
	public	function printArray( & $arr, $size)
	{
		for ($i = 0; $i < $size; ++$i)
		{
			echo " ". $arr[$i];
		}
		echo "\n";
	}
	// Double the first element and move the exist zero to end of array
	public	function arrange( & $arr, $size)
	{
		// Loop controlling variables
		$i = 0;
		$j = 0;
		// Execute Loop through by size
		while ($i < $size - 1)
		{
			if ($arr[$i] == $arr[$i + 1] && $arr[$i] != 0)
			{
				// Double the current element
				$arr[$i] += $arr[$i];
				// Set next element as to zero
				$arr[$i + 1] = 0;
				$i += 2;
			}
			else
			{
				$i++;
			}
		}
		$i = 0;
		// Move zero to end
		while ($i < $size || $j < $size)
		{
			if ($i < $size)
			{
				if ($arr[$i] != 0)
				{
					$arr[$j] = $arr[$i];
					$j++;
				}
				$i++;
			}
			else
			{
				// Append zeros
				$arr[$j] = 0;
				$j++;
			}
		}
	}
}

function main()
{
	$task = new Arrangement();
	// Define array of integer elements
	$arr = array(1, 0, 0, 3, 3, 3, 0, 4, 4, 2, 9, 0);
	// Get the size
	$size = count($arr);
	// Display element
	echo "\n Array element \n";
	$task->printArray($arr, $size);
	// arrange element
	$task->arrange($arr, $size);
	echo " After Arrange \n";
	$task->printArray($arr, $size);
}
main();

Output

 Array element
 1 0 0 3 3 3 0 4 4 2 9 0
 After Arrange
 1 6 3 8 2 9 0 0 0 0 0 0
/*
  Node Js Program for
  Double the first element and move zero to end
*/
class Arrangement
{
	//Display elements of given array
	printArray(arr, size)
	{
		for (var i = 0; i < size; ++i)
		{
			process.stdout.write(" " + arr[i]);
		}
		process.stdout.write("\n");
	}
	// Double the first element and move the exist zero to end of array
	arrange(arr, size)
	{
		// Loop controlling variables
		var i = 0;
		var j = 0;
		// Execute Loop through by size
		while (i < size - 1)
		{
			if (arr[i] == arr[i + 1] && arr[i] != 0)
			{
				// Double the current element
				arr[i] += arr[i];
				// Set next element as to zero
				arr[i + 1] = 0;
				i += 2;
			}
			else
			{
				i++;
			}
		}
		i = 0;
		// Move zero to end
		while (i < size || j < size)
		{
			if (i < size)
			{
				if (arr[i] != 0)
				{
					arr[j] = arr[i];
					j++;
				}
				i++;
			}
			else
			{
				// Append zeros
				arr[j] = 0;
				j++;
			}
		}
	}
}

function main()
{
	var task = new Arrangement();
	// Define array of integer elements
	var arr = [1, 0, 0, 3, 3, 3, 0, 4, 4, 2, 9, 0];
	// Get the size
	var size = arr.length;
	// Display element
	process.stdout.write("\n Array element \n");
	task.printArray(arr, size);
	// arrange element
	task.arrange(arr, size);
	process.stdout.write(" After Arrange \n");
	task.printArray(arr, size);
}
main();

Output

 Array element
 1 0 0 3 3 3 0 4 4 2 9 0
 After Arrange
 1 6 3 8 2 9 0 0 0 0 0 0
#   Python 3 Program for
#   Double the first element and move zero to end

class Arrangement :
	# Display elements of given array
	def printArray(self, arr, size) :
		i = 0
		while (i < size) :
			print(" ", arr[i], end = "")
			i += 1
		
		print(end = "\n")
	
	#  Double the first element and move the exist zero to end of array
	def arrange(self, arr, size) :
		#  Loop controlling variables
		i = 0
		j = 0
		#  Execute Loop through by size
		while (i < size - 1) :
			if (arr[i] == arr[i + 1] and arr[i] != 0) :
				#  Double the current element
				arr[i] += arr[i]
				#  Set next element as to zero
				arr[i + 1] = 0
				i += 2
			else :
				i += 1
			
		
		i = 0
		#  Move zero to end
		while (i < size or j < size) :
			if (i < size) :
				if (arr[i] != 0) :
					arr[j] = arr[i]
					j += 1
				
				i += 1
			else :
				#  Append zeros
				arr[j] = 0
				j += 1

def main() :
	task = Arrangement()
	#  Define array of integer elements
	arr = [1, 0, 0, 3, 3, 3, 0, 4, 4, 2, 9, 0]
	#  Get the size
	size = len(arr)
	#  Display element
	print("\n Array element ")
	task.printArray(arr, size)
	#  arrange element
	task.arrange(arr, size)
	print(" After Arrange ")
	task.printArray(arr, size)

if __name__ == "__main__": main()

Output

 Array element
  1  0  0  3  3  3  0  4  4  2  9  0
 After Arrange
  1  6  3  8  2  9  0  0  0  0  0  0
#   Ruby Program for
#   Double the first element and move zero to end

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

		print("\n")
	end

	#  Double the first element and move the exist zero to end of array
	def arrange(arr, size) 
		#  Loop controlling variables
		i = 0
		j = 0
		#  Execute Loop through by size
		while (i < size - 1) 
			if (arr[i] == arr[i + 1] && arr[i] != 0) 
				#  Double the current element
				arr[i] += arr[i]
				#  Set next element as to zero
				arr[i + 1] = 0
				i += 2
			else 
				i += 1
			end

		end

		i = 0
		#  Move zero to end
		while (i < size || j < size) 
			if (i < size) 
				if (arr[i] != 0) 
					arr[j] = arr[i]
					j += 1
				end

				i += 1
			else 
				#  Append zeros
				arr[j] = 0
				j += 1
			end

		end

	end

end

def main() 
	task = Arrangement.new()
	#  Define array of integer elements
	arr = [1, 0, 0, 3, 3, 3, 0, 4, 4, 2, 9, 0]
	#  Get the size
	size = arr.length
	#  Display element
	print("\n Array element \n")
	task.printArray(arr, size)
	#  arrange element
	task.arrange(arr, size)
	print(" After Arrange \n")
	task.printArray(arr, size)
end

main()

Output

 Array element 
 1 0 0 3 3 3 0 4 4 2 9 0
 After Arrange 
 1 6 3 8 2 9 0 0 0 0 0 0
/*
  Scala Program for
  Double the first element and move zero to end
*/
class Arrangement
{
	//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");
	}
	// Double the first element and move the exist zero to end of array
	def arrange(arr: Array[Int], size: Int): Unit = {
		// Loop controlling variables
		var i: Int = 0;
		var j: Int = 0;
		// Execute Loop through by size
		while (i < size - 1)
		{
			if (arr(i) == arr(i + 1) && arr(i) != 0)
			{
				// Double the current element
				arr(i) += arr(i);
				// Set next element as to zero
				arr(i + 1) = 0;
				i += 2;
			}
			else
			{
				i += 1;
			}
		}
		i = 0;
		// Move zero to end
		while (i < size || j < size)
		{
			if (i < size)
			{
				if (arr(i) != 0)
				{
					arr(j) = arr(i);
					j += 1;
				}
				i += 1;
			}
			else
			{
				// Append zeros
				arr(j) = 0;
				j += 1;
			}
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Arrangement = new Arrangement();
		// Define array of integer elements
		var arr: Array[Int] = Array(1, 0, 0, 3, 3, 3, 0, 4, 4, 2, 9, 0);
		// Get the size
		var size: Int = arr.length;
		// Display element
		print("\n Array element \n");
		task.printArray(arr, size);
		// arrange element
		task.arrange(arr, size);
		print(" After Arrange \n");
		task.printArray(arr, size);
	}
}

Output

 Array element
 1 0 0 3 3 3 0 4 4 2 9 0
 After Arrange
 1 6 3 8 2 9 0 0 0 0 0 0
/*
  Swift 4 Program for
  Double the first element and move zero to end
*/
class Arrangement
{
	//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");
	}
	// Double the first element and move the exist zero to end of array
	func arrange(_ arr: inout[Int], _ size: Int)
	{
		// Loop controlling variables
		var i: Int = 0;
		var j: Int = 0;
		// Execute Loop through by size
		while (i < size - 1)
		{
			if (arr[i] == arr[i + 1] && arr[i]  != 0)
			{
				// Double the current element
				arr[i] += arr[i];
				// Set next element as to zero
				arr[i + 1] = 0;
				i += 2;
			}
			else
			{
				i += 1;
			}
		}
		i = 0;
		// Move zero to end
		while (i < size || j < size)
		{
			if (i < size)
			{
				if (arr[i]  != 0)
				{
					arr[j] = arr[i];
					j += 1;
				}
				i += 1;
			}
			else
			{
				// Append zeros
				arr[j] = 0;
				j += 1;
			}
		}
	}
}
func main()
{
	let task: Arrangement = Arrangement();
	// Define array of integer elements
	var arr: [Int] = [1, 0, 0, 3, 3, 3, 0, 4, 4, 2, 9, 0];
	// Get the size
	let size: Int = arr.count;
	// Display element
	print("\n Array element ");
	task.printArray(arr, size);
	// arrange element
	task.arrange(&arr, size);
	print(" After Arrange ");
	task.printArray(arr, size);
}
main();

Output

 Array element
  1  0  0  3  3  3  0  4  4  2  9  0
 After Arrange
  1  6  3  8  2  9  0  0  0  0  0  0
/*
  Kotlin Program for
  Double the first element and move zero to end
*/
class Arrangement
{
	//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");
	}
	// Double the first element and move the exist zero to end of array
	fun arrange(arr: Array < Int > , size: Int): Unit
	{
		// Loop controlling variables
		var i: Int = 0;
		var j: Int = 0;
		// Execute Loop through by size
		while (i < size - 1)
		{
			if (arr[i] == arr[i + 1] && arr[i] != 0)
			{
				// Double the current element
				arr[i] += arr[i];
				// Set next element as to zero
				arr[i + 1] = 0;
				i += 2;
			}
			else
			{
				i += 1;
			}
		}
		i = 0;
		// Move zero to end
		while (i < size || j < size)
		{
			if (i < size)
			{
				if (arr[i] != 0)
				{
					arr[j] = arr[i];
					j += 1;
				}
				i += 1;
			}
			else
			{
				// Append zeros
				arr[j] = 0;
				j += 1;
			}
		}
	}
}
fun main(args: Array < String > ): Unit
{
	var task: Arrangement = Arrangement();
	// Define array of integer elements
	var arr: Array < Int > = arrayOf(1, 0, 0, 3, 3, 3, 0, 4, 4, 2, 9, 0);
	// Get the size
	var size: Int = arr.count();
	// Display element
	print("\n Array element \n");
	task.printArray(arr, size);
	// arrange element
	task.arrange(arr, size);
	print(" After Arrange \n");
	task.printArray(arr, size);
}

Output

 Array element
 1 0 0 3 3 3 0 4 4 2 9 0
 After Arrange
 1 6 3 8 2 9 0 0 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