Replace elements with greatest element on right side

Here given code implementation process.

// C program for
// Replace elements with greatest element on right side
#include <stdio.h>

void printData(int arr[], int n)
{
	for (int i = 0; i < n; ++i)
	{
		// print element
		printf("  %d", arr[i]);
	}
	printf("\n");
}
int maxValue(int a, int b)
{
	if (a > b)
	{
		return a;
	}
	return b;
}
void replace(int arr[], int n)
{
	if (n <= 0)
	{
		return;
	}
	int max = -1;
	int current = 0;
	for (int i = n - 1; i >= 0; --i)
	{
		// Get current element
		current = arr[i];
		// replace value by max element
		arr[i] = max;
		// Change max when need
		max = maxValue(max, current);
	}
}
int main(int argc, char
	const *argv[])
{
	int arr[] = {
		7 , 3 , 10 , 2 , 9 , 7 , 2 , 8 , 3
	};
	// Get the number of element
	int n = sizeof(arr) / sizeof(arr[0]);
	printData(arr, n);
	replace(arr, n);
	printData(arr, n);
	return 0;
}

input

  7  3  10  2  9  7  2  8  3
  10  10  9  9  8  8  8  3  -1
// Include header file
#include <iostream>

using namespace std;
/*
    C++ program
    Replace elements with greatest element on right side
*/
class Manipulation
{
	public:
		// Display array elements
		void printData(int arr[], int n)
		{
			for (int i = 0; i < n; ++i)
			{
				// print element
				cout << "  " << arr[i];
			}
			cout << "\n";
		}
	int maxValue(int a, int b)
	{
		if (a > b)
		{
			return a;
		}
		return b;
	}
	void replace(int arr[], int n)
	{
		if (n <= 0)
		{
			return;
		}
		int max = -1;
		int current = 0;
		for (int i = n - 1; i >= 0; --i)
		{
			// Get current element
			current = arr[i];
			// replace value by max element
			arr[i] = max;
			// Change max when need
			max = this->maxValue(max, current);
		}
	}
};
int main()
{
	Manipulation *task = new Manipulation();
	int arr[] = {
		7 , 3 , 10 , 2 , 9 , 7 , 2 , 8 , 3
	};
	// Get the number of element
	int n = sizeof(arr) / sizeof(arr[0]);
	task->printData(arr, n);
	task->replace(arr, n);
	task->printData(arr, n);
	return 0;
}

input

  7  3  10  2  9  7  2  8  3
  10  10  9  9  8  8  8  3  -1
/*
    Java program
    Replace elements with greatest element on right side
*/
public class Manipulation
{
	// Display array elements
	public void printData(int[] arr, int n)
	{
		for (int i = 0; i < n; ++i)
		{
			// print element
			System.out.print("  " + arr[i]);
		}
		System.out.print("\n");
	}
	public int maxValue(int a, int b)
	{
		if (a > b)
		{
			return a;
		}
		return b;
	}
	public void replace(int[] arr, int n)
	{
		if (n <= 0)
		{
			return;
		}
		int max = -1;
		int current = 0;
		for (int i = n - 1; i >= 0; --i)
		{
			// Get current element
			current = arr[i];
			// replace value by max element
			arr[i] = max;
			// Change max when need
			max = maxValue(max, current);
		}
	}
	public static void main(String[] args)
	{
		Manipulation task = new Manipulation();
		int[] arr = {
			7 , 3 , 10 , 2 , 9 , 7 , 2 , 8 , 3
		};
		// Get the number of element
		int n = arr.length;
		task.printData(arr, n);
		task.replace(arr, n);
		task.printData(arr, n);
	}
}

input

  7  3  10  2  9  7  2  8  3
  10  10  9  9  8  8  8  3  -1
// Include namespace system
using System;
/*
    Csharp program
    Replace elements with greatest element on right side
*/
public class Manipulation
{
	// Display array elements
	public void printData(int[] arr, int n)
	{
		for (int i = 0; i < n; ++i)
		{
			// print element
			Console.Write("  " + arr[i]);
		}
		Console.Write("\n");
	}
	public int maxValue(int a, int b)
	{
		if (a > b)
		{
			return a;
		}
		return b;
	}
	public void replace(int[] arr, int n)
	{
		if (n <= 0)
		{
			return;
		}
		int max = -1;
		int current = 0;
		for (int i = n - 1; i >= 0; --i)
		{
			// Get current element
			current = arr[i];
			// replace value by max element
			arr[i] = max;
			// Change max when need
			max = this.maxValue(max, current);
		}
	}
	public static void Main(String[] args)
	{
		Manipulation task = new Manipulation();
		int[] arr = {
			7 , 3 , 10 , 2 , 9 , 7 , 2 , 8 , 3
		};
		// Get the number of element
		int n = arr.Length;
		task.printData(arr, n);
		task.replace(arr, n);
		task.printData(arr, n);
	}
}

input

  7  3  10  2  9  7  2  8  3
  10  10  9  9  8  8  8  3  -1
<?php
/*
    Php program
    Replace elements with greatest element on right side
*/
class Manipulation
{
	// Display array elements
	public	function printData($arr, $n)
	{
		for ($i = 0; $i < $n; ++$i)
		{
			// print element
			echo("  ".$arr[$i]);
		}
		echo("\n");
	}
	public	function maxValue($a, $b)
	{
		if ($a > $b)
		{
			return $a;
		}
		return $b;
	}
	public	function replace(&$arr, $n)
	{
		if ($n <= 0)
		{
			return;
		}
		$max = -1;
		$current = 0;
		for ($i = $n - 1; $i >= 0; --$i)
		{
			// Get current element
			$current = $arr[$i];
			// replace value by max element
			$arr[$i] = $max;
			// Change max when need
			$max = $this->maxValue($max, $current);
		}
	}
}

function main()
{
	$task = new Manipulation();
	$arr = array(7, 3, 10, 2, 9, 7, 2, 8, 3);
	// Get the number of element
	$n = count($arr);
	$task->printData($arr, $n);
	$task->replace($arr, $n);
	$task->printData($arr, $n);
}
main();

input

  7  3  10  2  9  7  2  8  3
  10  10  9  9  8  8  8  3  -1
/*
    Node JS program
    Replace elements with greatest element on right side
*/
class Manipulation
{
	// Display array elements
	printData(arr, n)
	{
		for (var i = 0; i < n; ++i)
		{
			// print element
			process.stdout.write("  " + arr[i]);
		}
		process.stdout.write("\n");
	}
	maxValue(a, b)
	{
		if (a > b)
		{
			return a;
		}
		return b;
	}
	replace(arr, n)
	{
		if (n <= 0)
		{
			return;
		}
		var max = -1;
		var current = 0;
		for (var i = n - 1; i >= 0; --i)
		{
			// Get current element
			current = arr[i];
			// replace value by max element
			arr[i] = max;
			// Change max when need
			max = this.maxValue(max, current);
		}
	}
}

function main()
{
	var task = new Manipulation();
	var arr = [7, 3, 10, 2, 9, 7, 2, 8, 3];
	// Get the number of element
	var n = arr.length;
	task.printData(arr, n);
	task.replace(arr, n);
	task.printData(arr, n);
}
main();

input

  7  3  10  2  9  7  2  8  3
  10  10  9  9  8  8  8  3  -1
#    Python 3 program
#    Replace elements with greatest element on right side
class Manipulation :
	#  Display list elements
	def printData(self, arr, n) :
		i = 0
		while (i < n) :
			#  print element
			print("  ", arr[i], end = "")
			i += 1
		
		print(end = "\n")
	
	def maxValue(self, a, b) :
		if (a > b) :
			return a
		
		return b
	
	def replace(self, arr, n) :
		if (n <= 0) :
			return
		
		max = -1
		current = 0
		i = n - 1
		while (i >= 0) :
			#  Get current element
			current = arr[i]
			#  replace value by max element
			arr[i] = max
			#  Change max when need
			max = self.maxValue(max, current)
			i -= 1
		
	

def main() :
	task = Manipulation()
	arr = [7, 3, 10, 2, 9, 7, 2, 8, 3]
	#  Get the number of element
	n = len(arr)
	task.printData(arr, n)
	task.replace(arr, n)
	task.printData(arr, n)

if __name__ == "__main__": main()

input

   7   3   10   2   9   7   2   8   3
   10   10   9   9   8   8   8   3   -1
#    Ruby program
#    Replace elements with greatest element on right side
class Manipulation 
	#  Display array elements
	def printData(arr, n) 
		i = 0
		while (i < n) 
			#  print element
			print("  ", arr[i])
			i += 1
		end

		print("\n")
	end

	def maxValue(a, b) 
		if (a > b) 
			return a
		end

		return b
	end

	def replace(arr, n) 
		if (n <= 0) 
			return
		end

		max = -1
		current = 0
		i = n - 1
		while (i >= 0) 
			#  Get current element
			current = arr[i]
			#  replace value by max element
			arr[i] = max
			#  Change max when need
			max = self.maxValue(max, current)
			i -= 1
		end

	end

end

def main() 
	task = Manipulation.new()
	arr = [7, 3, 10, 2, 9, 7, 2, 8, 3]
	#  Get the number of element
	n = arr.length
	task.printData(arr, n)
	task.replace(arr, n)
	task.printData(arr, n)
end

main()

input

  7  3  10  2  9  7  2  8  3
  10  10  9  9  8  8  8  3  -1
/*
    Scala program
    Replace elements with greatest element on right side
*/
class Manipulation()
{
	// Display array elements
	def printData(arr: Array[Int], n: Int): Unit = {
		var i: Int = 0;
		while (i < n)
		{
			// print element
			print("  " + arr(i));
			i += 1;
		}
		print("\n");
	}
	def maxValue(a: Int, b: Int): Int = {
		if (a > b)
		{
			return a;
		}
		return b;
	}
	def replace(arr: Array[Int], n: Int): Unit = {
		if (n <= 0)
		{
			return;
		}
		var max: Int = -1;
		var current: Int = 0;
		var i: Int = n - 1;
		while (i >= 0)
		{
			// Get current element
			current = arr(i);
			// replace value by max element
			arr(i) = max;
			// Change max when need
			max = maxValue(max, current);
			i -= 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Manipulation = new Manipulation();
		var arr: Array[Int] = Array(7, 3, 10, 2, 9, 7, 2, 8, 3);
		// Get the number of element
		var n: Int = arr.length;
		task.printData(arr, n);
		task.replace(arr, n);
		task.printData(arr, n);
	}
}

input

  7  3  10  2  9  7  2  8  3
  10  10  9  9  8  8  8  3  -1
import Foundation;
/*
    Swift 4 program
    Replace elements with greatest element on right side
*/
class Manipulation
{
	// Display array elements
	func printData(_ arr: [Int], _ n: Int)
	{
		var i: Int = 0;
		while (i < n)
		{
			// print element
			print("  ", arr[i], terminator: "");
			i += 1;
		}
		print(terminator: "\n");
	}
	func maxValue(_ a: Int, _ b: Int) -> Int
	{
		if (a > b)
		{
			return a;
		}
		return b;
	}
	func replace(_ arr: inout[Int], _ n: Int)
	{
		if (n <= 0)
		{
			return;
		}
		var max: Int = -1;
		var current: Int = 0;
		var i: Int = n - 1;
		while (i >= 0)
		{
			// Get current element
			current = arr[i];
			// replace value by max element
			arr[i] = max;
			// Change max when need
			max = self.maxValue(max, current);
			i -= 1;
		}
	}
}
func main()
{
	let task: Manipulation = Manipulation();
	var arr: [Int] = [7, 3, 10, 2, 9, 7, 2, 8, 3];
	// Get the number of element
	let n: Int = arr.count;
	task.printData(arr, n);
	task.replace(&arr, n);
	task.printData(arr, n);
}
main();

input

   7   3   10   2   9   7   2   8   3
   10   10   9   9   8   8   8   3   -1
/*
    Kotlin program
    Replace elements with greatest element on right side
*/
class Manipulation
{
	// Display array elements
	fun printData(arr: Array < Int > , n: Int): Unit
	{
		var i: Int = 0;
		while (i < n)
		{
			// print element
			print("  " + arr[i]);
			i += 1;
		}
		print("\n");
	}
	fun maxValue(a: Int, b: Int): Int
	{
		if (a > b)
		{
			return a;
		}
		return b;
	}
	fun replace(arr: Array < Int > , n: Int): Unit
	{
		if (n <= 0)
		{
			return;
		}
		var max: Int = -1;
		var current: Int ;
		var i: Int = n - 1;
		while (i >= 0)
		{
			// Get current element
			current = arr[i];
			// replace value by max element
			arr[i] = max;
			// Change max when need
			max = this.maxValue(max, current);
			i -= 1;
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Manipulation = Manipulation();
	var arr: Array < Int > = arrayOf(7, 3, 10, 2, 9, 7, 2, 8, 3);
	// Get the number of element
	val n: Int = arr.count();
	task.printData(arr, n);
	task.replace(arr, n);
	task.printData(arr, n);
}

input

  7  3  10  2  9  7  2  8  3
  10  10  9  9  8  8  8  3  -1
package main
import "fmt"
/*
    Go program
    Replace elements with greatest element on right side
*/

// Display array elements
func  printData(arr[] int, n int) {
	for i := 0 ; i < n ; i++ {
		// print element
		fmt.Print("  ", arr[i])
	}
	fmt.Print("\n")
}
func  maxValue(a, b int) int {
	if a > b {
		return a
	}
	return b
}
func replace(arr[] int, n int) {
	if n <= 0 {
		return
	}
	var max int = -1
	var current int = 0
	for i := n - 1 ; i >= 0 ; i-- {
		// Get current element
		current = arr[i]
		// replace value by max element
		arr[i] = max
		// Change max when need
		max = maxValue(max, current)
	}
}
func main() {

	var arr = [] int {
		7,
		3,
		10,
		2,
		9,
		7,
		2,
		8,
		3,
	}
	// Get the number of element
	var n int = len(arr)
	printData(arr, n)
	replace(arr, n)
	printData(arr, n)
}

input

  7  3  10  2  9  7  2  8  3
  10  10  9  9  8  8  8  3  -1


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