Maximum prefix sum possible by merging two given arrays

Here given code implementation process.

/*
    Java program for
    Maximum prefix sum possible by merging two given arrays
*/
public class PrefixSum
{
	public int maxPerfix(int[] record, int size)
	{
		int max = record[0];
		for (int i = 1; i < size; ++i)
		{
			record[i] = record[i - 1] + record[i];
			if (record[i] > max)
			{
				// Change prefix sum
				max = record[i];
			}
		}
		if (max < 0)
		{
			// Negative prefix are not suitable in given record
			max = 0;
		}
		return max;
	}
	public void findMaxPerfix(int[] a, int b[], int n, int m)
	{
		int p1 = maxPerfix(a, n);
		int p2 = maxPerfix(b, m);
		// Result is combination of max perfix in given a and b
		System.out.print(p1 + p2);
	}
	public static void main(String[] args)
	{
		PrefixSum task = new PrefixSum();
		int[] a = {
			-4 , 5 , 1 , -3 , -5 , 2
		};
		int[] b = {
			1 , 2 , -2 , 3 , -4 , 3 , 0
		};
		int n = a.length;
		int m = b.length;
		// [-4, 5, 1][1, 2, -2, 3]  longest max prefix in a and b
		// [-4, 1, 5, 2, 1, -2, 3]  any merge combination 
		//  Sum : 6
		task.findMaxPerfix(a, b, n, m);
	}
}

Output

6
// Include header file
#include <iostream>
using namespace std;
/*
    C++ program for
    Maximum prefix sum possible by merging two given arrays
*/
class PrefixSum
{
	public: int maxPerfix(int record[], int size)
	{
		int max = record[0];
		for (int i = 1; i < size; ++i)
		{
			record[i] = record[i - 1] + record[i];
			if (record[i] > max)
			{
				// Change prefix sum
				max = record[i];
			}
		}
		if (max < 0)
		{
			// Negative prefix are not suitable in given record
			max = 0;
		}
		return max;
	}
	void findMaxPerfix(int a[], int b[], int n, int m)
	{
		int p1 = this->maxPerfix(a, n);
		int p2 = this->maxPerfix(b, m);
		// Result is combination of max perfix in given a and b
		cout << p1 + p2;
	}
};
int main()
{
	PrefixSum *task = new PrefixSum();
	int a[] = {
		-4 , 5 , 1 , -3 , -5 , 2
	};
	int b[] = {
		1 , 2 , -2 , 3 , -4 , 3 , 0
	};
	int n = sizeof(a) / sizeof(a[0]);
	int m = sizeof(b) / sizeof(b[0]);
	// [-4, 5, 1][1, 2, -2, 3]  longest max prefix in a and b
	// [-4, 1, 5, 2, 1, -2, 3]  any merge combination 
	//  Sum : 6
	task->findMaxPerfix(a, b, n, m);
	return 0;
}

Output

6
// Include namespace system
using System;
/*
    Csharp program for
    Maximum prefix sum possible by merging two given arrays
*/
public class PrefixSum
{
	public int maxPerfix(int[] record, int size)
	{
		int max = record[0];
		for (int i = 1; i < size; ++i)
		{
			record[i] = record[i - 1] + record[i];
			if (record[i] > max)
			{
				// Change prefix sum
				max = record[i];
			}
		}
		if (max < 0)
		{
			// Negative prefix are not suitable in given record
			max = 0;
		}
		return max;
	}
	public void findMaxPerfix(int[] a, int[] b, int n, int m)
	{
		int p1 = this.maxPerfix(a, n);
		int p2 = this.maxPerfix(b, m);
		// Result is combination of max perfix in given a and b
		Console.Write(p1 + p2);
	}
	public static void Main(String[] args)
	{
		PrefixSum task = new PrefixSum();
		int[] a = {
			-4 , 5 , 1 , -3 , -5 , 2
		};
		int[] b = {
			1 , 2 , -2 , 3 , -4 , 3 , 0
		};
		int n = a.Length;
		int m = b.Length;
		// [-4, 5, 1,1, 2, -2, 3]  longest max prefix in a and b
		// [-4, 1, 5, 2, 1, -2, 3]  any merge combination 
		//  Sum : 6
		task.findMaxPerfix(a, b, n, m);
	}
}

Output

6
package main
import "fmt"
/*
    Go program for
    Maximum prefix sum possible by merging two given arrays
*/

func maxPerfix(record[] int, size int) int {
	var max int = record[0]
	for i := 1 ; i < size ; i++ {
		record[i] = record[i - 1] + record[i]
		if record[i] > max {
			// Change prefix sum
			max = record[i]
		}
	}
	if max < 0 {
		// Negative prefix are not suitable in given record
		max = 0
	}
	return max
}
func findMaxPerfix(a[] int, b[] int, n int, m int) {
	var p1 int =  maxPerfix(a, n)
	var p2 int = maxPerfix(b, m)
	// Result is combination of max perfix in given a and b
	fmt.Print(p1 + p2)
}
func main() {
	
	var a = [] int {-4, 5, 1, -3, -5, 2}
	var b = [] int {1, 2, -2, 3, -4, 3, 0}
	var n int = len(a)
	var m int = len(b)
	// [-4, 5, 1][1, 2, -2, 3]  longest max prefix in a and b
	// [-4, 1, 5, 2, 1, -2, 3]  any merge combination 
	//  Sum : 6
	findMaxPerfix(a, b, n, m)
}

Output

6
<?php
/*
    Php program for
    Maximum prefix sum possible by merging two given arrays
*/
class PrefixSum
{
	public	function maxPerfix($record, $size)
	{
		$max = $record[0];
		for ($i = 1; $i < $size; ++$i)
		{
			$record[$i] = $record[$i - 1] + $record[$i];
			if ($record[$i] > $max)
			{
				// Change prefix sum
				$max = $record[$i];
			}
		}
		if ($max < 0)
		{
			// Negative prefix are not suitable in given record
			$max = 0;
		}
		return $max;
	}
	public	function findMaxPerfix($a, $b, $n, $m)
	{
		$p1 = $this->maxPerfix($a, $n);
		$p2 = $this->maxPerfix($b, $m);
		// Result is combination of max perfix in given a and b
		echo($p1 + $p2);
	}
}

function main()
{
	$task = new PrefixSum();
	$a = array(-4, 5, 1, -3, -5, 2);
	$b = array(1, 2, -2, 3, -4, 3, 0);
	$n = count($a);
	$m = count($b);
	// [-4, 5, 1][1, 2, -2, 3]  longest max prefix in a and b
	// [-4, 1, 5, 2, 1, -2, 3]  any merge combination 
	//  Sum : 6
	$task->findMaxPerfix($a, $b, $n, $m);
}
main();

Output

6
/*
    Node JS program for
    Maximum prefix sum possible by merging two given arrays
*/
class PrefixSum
{
	maxPerfix(record, size)
	{
		var max = record[0];
		for (var i = 1; i < size; ++i)
		{
			record[i] = record[i - 1] + record[i];
			if (record[i] > max)
			{
				// Change prefix sum
				max = record[i];
			}
		}
		if (max < 0)
		{
			// Negative prefix are not suitable in given record
			max = 0;
		}
		return max;
	}
	findMaxPerfix(a, b, n, m)
	{
		var p1 = this.maxPerfix(a, n);
		var p2 = this.maxPerfix(b, m);
		// Result is combination of max perfix in given a and b
		console.log( p1 + p2);
	}
}

function main()
{
	var task = new PrefixSum();
	var a = [-4, 5, 1, -3, -5, 2];
	var b = [1, 2, -2, 3, -4, 3, 0];
	var n = a.length;
	var m = b.length;
	// [-4, 5, 1][1, 2, -2, 3]  longest max prefix in a and b
	// [-4, 1, 5, 2, 1, -2, 3]  any merge combination 
	//  Sum : 6
	task.findMaxPerfix(a, b, n, m);
}
main();

Output

6
#    Python 3 program for
#    Maximum prefix sum possible by merging two given arrays
class PrefixSum :
	def maxPerfix(self, record, size) :
		max = record[0]
		i = 1
		while (i < size) :
			record[i] = record[i - 1] + record[i]
			if (record[i] > max) :
				#  Change prefix sum
				max = record[i]
			
			i += 1
		
		if (max < 0) :
			#  Negative prefix are not suitable in given record
			max = 0
		
		return max
	
	def findMaxPerfix(self, a, b, n, m) :
		p1 = self.maxPerfix(a, n)
		p2 = self.maxPerfix(b, m)
		#  Result is combination of max perfix in given a and b
		print(p1 + p2, end = "")
	

def main() :
	task = PrefixSum()
	a = [-4, 5, 1, -3, -5, 2]
	b = [1, 2, -2, 3, -4, 3, 0]
	n = len(a)
	m = len(b)
	#  [-4, 5, 1][1, 2, -2, 3]  longest max prefix in a and b
	#  [-4, 1, 5, 2, 1, -2, 3]  any merge combination 
	#   Sum : 6
	task.findMaxPerfix(a, b, n, m)

if __name__ == "__main__": main()

Output

6
#    Ruby program for
#    Maximum prefix sum possible by merging two given arrays
class PrefixSum 
	def maxPerfix(record, size) 
		max = record[0]
		i = 1
		while (i < size) 
			record[i] = record[i - 1] + record[i]
			if (record[i] > max) 
				#  Change prefix sum
				max = record[i]
			end

			i += 1
		end

		if (max < 0) 
			#  Negative prefix are not suitable in given record
			max = 0
		end

		return max
	end

	def findMaxPerfix(a, b, n, m) 
		p1 = self.maxPerfix(a, n)
		p2 = self.maxPerfix(b, m)
		#  Result is combination of max perfix in given a and b
		print(p1 + p2)
	end

end

def main() 
	task = PrefixSum.new()
	a = [-4, 5, 1, -3, -5, 2]
	b = [1, 2, -2, 3, -4, 3, 0]
	n = a.length
	m = b.length
	#  [-4, 5, 1][1, 2, -2, 3]  longest max prefix in a and b
	#  [-4, 1, 5, 2, 1, -2, 3]  any merge combination 
	#   Sum : 6
	task.findMaxPerfix(a, b, n, m)
end

main()

Output

6
/*
    Scala program for
    Maximum prefix sum possible by merging two given arrays
*/
class PrefixSum()
{
	def maxPerfix(record: Array[Int], size: Int): Int = {
		var max: Int = record(0);
		var i: Int = 1;
		while (i < size)
		{
			record(i) = record(i - 1) + record(i);
			if (record(i) > max)
			{
				// Change prefix sum
				max = record(i);
			}
			i += 1;
		}
		if (max < 0)
		{
			// Negative prefix are not suitable in given record
			max = 0;
		}
		return max;
	}
	def findMaxPerfix(a: Array[Int], b:
		Array[Int], n: Int, m: Int): Unit = {
		var p1: Int = maxPerfix(a, n);
		var p2: Int = maxPerfix(b, m);
		// Result is combination of max perfix in given a and b
		print(p1 + p2);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: PrefixSum = new PrefixSum();
		var a: Array[Int] = Array(-4, 5, 1, -3, -5, 2);
		var b: Array[Int] = Array(1, 2, -2, 3, -4, 3, 0);
		var n: Int = a.length;
		var m: Int = b.length;
		// [-4, 5, 1][1, 2, -2, 3]  longest max prefix in a and b
		// [-4, 1, 5, 2, 1, -2, 3]  any merge combination 
		//  Sum : 6
		task.findMaxPerfix(a, b, n, m);
	}
}

Output

6
import Foundation;
/*
    Swift 4 program for
    Maximum prefix sum possible by merging two given arrays
*/
class PrefixSum
{
	func maxPerfix(_ record: inout[Int], _ size: Int) -> Int
	{
		var max: Int = record[0];
		var i: Int = 1;
		while (i < size)
		{
			record[i] = record[i - 1] + record[i];
			if (record[i] > max)
			{
				// Change prefix sum
				max = record[i];
			}
			i += 1;
		}
		if (max < 0)
		{
			// Negative prefix are not suitable in given record
			max = 0;
		}
		return max;
	}
	func findMaxPerfix(_ a: inout[Int], _ b: inout[Int], _ n: Int, _ m: Int)
	{
		let p1: Int = self.maxPerfix(&a, n);
		let p2: Int = self.maxPerfix(&b, m);
		// Result is combination of max perfix in given a and b
		print(p1 + p2, terminator: "");
	}
}
func main()
{
	let task: PrefixSum = PrefixSum();
	var a: [Int] = [-4, 5, 1, -3, -5, 2];
	var b: [Int] = [1, 2, -2, 3, -4, 3, 0];
	let n: Int = a.count;
	let m: Int = b.count;
	// [-4, 5, 1][1, 2, -2, 3]  longest max prefix in a and b
	// [-4, 1, 5, 2, 1, -2, 3]  any merge combination 
	//  Sum : 6
	task.findMaxPerfix(&a, &b, n, m);
}
main();

Output

6
/*
    Kotlin program for
    Maximum prefix sum possible by merging two given arrays
*/
class PrefixSum
{
	fun maxPerfix(record: Array < Int > , size: Int): Int
	{
		var max: Int = record[0];
		var i: Int = 1;
		while (i < size)
		{
			record[i] = record[i - 1] + record[i];
			if (record[i] > max)
			{
				// Change prefix sum
				max = record[i];
			}
			i += 1;
		}
		if (max < 0)
		{
			// Negative prefix are not suitable in given record
			max = 0;
		}
		return max;
	}
	fun findMaxPerfix(a: Array < Int > , 
                      b: Array < Int > , 
                      n: Int,
                      m: Int): Unit
	{
		val p1: Int = this.maxPerfix(a, n);
		val p2: Int = this.maxPerfix(b, m);
		// Result is combination of max perfix in given a and b
		print(p1 + p2);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: PrefixSum = PrefixSum();
	val a: Array < Int > = arrayOf(-4, 5, 1, -3, -5, 2);
	val b: Array < Int > = arrayOf(1, 2, -2, 3, -4, 3, 0);
	val n: Int = a.count();
	val m: Int = b.count();
	// [-4, 5, 1][1, 2, -2, 3]  longest max prefix in a and b
	// [-4, 1, 5, 2, 1, -2, 3]  any merge combination 
	//  Sum : 6
	task.findMaxPerfix(a, b, n, m);
}

Output

6


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