Find maximum average subarray of k length

Here given code implementation process.

/*
  C program for
  Find maximum average subarray of k length
*/
#include <stdio.h>

// Display array elements
void printArray(int arr[], int n)
{
	for (int i = 0; i < n; ++i)
	{
		printf("  %d", arr[i]);
	}
}
void findMaxAvgSubarray(int arr[], int n, int k)
{
	if (n < k)
	{
		return;
	}
	// Define auxiliary variables
	int start = 0;
	int sum = 0;
	int result = 0;
	// Calculate sum of all array elements
	for (int i = 0; i < k; i++)
	{
		sum += arr[i];
	}
	// Assign sum value to result
	result = sum;
	// Find position of subarray
	for (int i = k; i < n; i++)
	{
		// Add current element in subarray
		// And remove first element of previous subarray
		sum += arr[i] - arr[i - k];
		if (sum > result)
		{
			// Get starting point of resultant subarray
			start = (i - k + 1);
			// Change result
			result = sum;
		}
	}
	// Display array element
	printArray(arr, n);
	// Display calculated result
	printf("\n Maximum subarray of size %d between : [%d-%d] \n", 
           k, start, start + k - 1);
}
int main()
{
	// Input array
	int arr[] = {
		2 , 4 , -3 , 1 , 3 , 7 , -9 , 10 , 4 , -4 , 2 , 8
	};
	// Get the number of elements in array
	int n = sizeof arr / sizeof arr[0];
	// subarray size
	int k = 4;
	// Given k = 4
	// [7, -9, 10, 4] resultant subarray
	findMaxAvgSubarray(arr, n, k);
	return 0;
}

Output

  2  4  -3  1  3  7  -9  10  4  -4  2  8
 Maximum subarray of size 4 between : [5-8]
/*
  Java program for
  Find maximum average subarray of k length
*/
public class Subarray
{
	// Display array elements
	public void printArray(int[] arr, int n)
	{
		for (int i = 0; i < n; ++i)
		{
			System.out.print(" " + arr[i]);
		}
	}
	public void findMaxAvgSubarray(int[] arr, int n, int k)
	{
		if (n < k)
		{
			return;
		}
		// Define auxiliary variables
		int start = 0;
		int sum = 0;
		int result = 0;
		// Calculate sum of all array elements
		for (int i = 0; i < k; i++)
		{
			sum += arr[i];
		}
		// Assign sum value to result
		result = sum;
		// Find position of subarray
		for (int i = k; i < n; i++)
		{
			// Add current element in subarray
			// And remove first element of previous subarray
			sum += arr[i] - arr[i - k];
			if (sum > result)
			{
				// Get starting point of resultant subarray
				start = (i - k + 1);
				// Change result
				result = sum;
			}
		}
		// Display array element
		printArray(arr, n);
		// Display calculated result
		System.out.print("\n Maximum subarray of size " + 
                         k + " between : [" + start + "-" + 
                         (start + k - 1) + "] \n");
	}
	public static void main(String[] args)
	{
		Subarray task = new Subarray();
		// Input array
		int[] arr = {
			2 , 4 , -3 , 1 , 3 , 7 , -9 , 10 , 4 , -4 , 2 , 8
		};
		// Get the number of elements in array
		int n = arr.length;
		// subarray size
		int k = 4;
		// Given k = 4
		// [7, -9, 10, 4] resultant subarray
		task.findMaxAvgSubarray(arr, n, k);
	}
}

Output

 2 4 -3 1 3 7 -9 10 4 -4 2 8
 Maximum subarray of size 4 between : [5-8]
// Include header file
#include <iostream>
using namespace std;
/*
  C++ program for
  Find maximum average subarray of k length
*/
class Subarray
{
	public:
		// Display array elements
		void printArray(int arr[], int n)
		{
			for (int i = 0; i < n; ++i)
			{
				cout << " " << arr[i];
			}
		}
	void findMaxAvgSubarray(int arr[], int n, int k)
	{
		if (n < k)
		{
			return;
		}
		// Define auxiliary variables
		int start = 0;
		int sum = 0;
		int result = 0;
		// Calculate sum of all array elements
		for (int i = 0; i < k; i++)
		{
			sum += arr[i];
		}
		// Assign sum value to result
		result = sum;
		// Find position of subarray
		for (int i = k; i < n; i++)
		{
			// Add current element in subarray
			// And remove first element of previous subarray
			sum += arr[i] - arr[i - k];
			if (sum > result)
			{
				// Get starting point of resultant subarray
				start = (i - k + 1);
				// Change result
				result = sum;
			}
		}
		// Display array element
		this->printArray(arr, n);
		// Display calculated result
		cout << "\n Maximum subarray of size " << k 
      		 << " between : [" << start << "-" 
      		 << (start + k - 1) << "] \n";
	}
};
int main()
{
	Subarray *task = new Subarray();
	// Input array
	int arr[] = {
		2 , 4 , -3 , 1 , 3 , 7 , -9 , 10 , 4 , -4 , 2 , 8
	};
	// Get the number of elements in array
	int n = sizeof(arr) / sizeof(arr[0]);
	// subarray size
	int k = 4;
	// Given k = 4
	// [7, -9, 10, 4] resultant subarray
	task->findMaxAvgSubarray(arr, n, k);
	return 0;
}

Output

 2 4 -3 1 3 7 -9 10 4 -4 2 8
 Maximum subarray of size 4 between : [5-8]
// Include namespace system
using System;
/*
  Csharp program for
  Find maximum average subarray of k length
*/
public class Subarray
{
	// Display array elements
	public void printArray(int[] arr, int n)
	{
		for (int i = 0; i < n; ++i)
		{
			Console.Write(" " + arr[i]);
		}
	}
	public void findMaxAvgSubarray(int[] arr, int n, int k)
	{
		if (n < k)
		{
			return;
		}
		// Define auxiliary variables
		int start = 0;
		int sum = 0;
		int result = 0;
		// Calculate sum of all array elements
		for (int i = 0; i < k; i++)
		{
			sum += arr[i];
		}
		// Assign sum value to result
		result = sum;
		// Find position of subarray
		for (int i = k; i < n; i++)
		{
			// Add current element in subarray
			// And remove first element of previous subarray
			sum += arr[i] - arr[i - k];
			if (sum > result)
			{
				// Get starting point of resultant subarray
				start = (i - k + 1);
				// Change result
				result = sum;
			}
		}
		// Display array element
		this.printArray(arr, n);
		// Display calculated result
		Console.Write("\n Maximum subarray of size " + 
                      k + " between : [" + start + "-" + 
                      (start + k - 1) + "] \n");
	}
	public static void Main(String[] args)
	{
		Subarray task = new Subarray();
		// Input array
		int[] arr = {
			2 , 4 , -3 , 1 , 3 , 7 , -9 , 10 , 4 , -4 , 2 , 8
		};
		// Get the number of elements in array
		int n = arr.Length;
		// subarray size
		int k = 4;
		// Given k = 4
		// [7, -9, 10, 4] resultant subarray
		task.findMaxAvgSubarray(arr, n, k);
	}
}

Output

 2 4 -3 1 3 7 -9 10 4 -4 2 8
 Maximum subarray of size 4 between : [5-8]
package main
import "fmt"
/*
  Go program for
  Find maximum average subarray of k length
*/

// Display array elements
func printArray(arr[] int, n int) {
	for i := 0 ; i < n ; i++ {
		fmt.Print(" ", arr[i])
	}
}
func findMaxAvgSubarray(arr[] int, n int, k int) {
	if n < k {
		return
	}
	// Define auxiliary variables
	var start int = 0
	var sum int = 0
	var result int = 0
	// Calculate sum of all array elements
	for i := 0 ; i < k ; i++ {
		sum += arr[i]
	}
	// Assign sum value to result
	result = sum
	// Find position of subarray
	for i := k ; i < n ; i++ {
		// Add current element in subarray
		// And remove first element of previous subarray
		sum += arr[i] - arr[i - k]
		if sum > result {
			// Get starting point of resultant subarray
			start = (i - k + 1)
			// Change result
			result = sum
		}
	}
	// Display array element
	printArray(arr, n)
	// Display calculated result
	fmt.Print("\n Maximum subarray of size ", 
		k, " between : [", start, "-", (start + k - 1), "] \n")
}
func main() {
	
	// Input array
	var arr = [] int {2 , 4 , -3 , 1 , 3 , 7 , -9 , 10 , 4 , -4 , 2 , 8}
	// Get the number of elements in array
	var n int = len(arr)
	// subarray size
	var k int = 4
	// Given k = 4
	// [7, -9, 10, 4] resultant subarray
	findMaxAvgSubarray(arr, n, k)
}

Output

 2 4 -3 1 3 7 -9 10 4 -4 2 8
 Maximum subarray of size 4 between : [5-8]
<?php
/*
  Php program for
  Find maximum average subarray of k length
*/
class Subarray
{
	// Display array elements
	public	function printArray($arr, $n)
	{
		for ($i = 0; $i < $n; ++$i)
		{
			echo(" ".$arr[$i]);
		}
	}
	public	function findMaxAvgSubarray($arr, $n, $k)
	{
		if ($n < $k)
		{
			return;
		}
		// Define auxiliary variables
		$start = 0;
		$sum = 0;
		$result = 0;
		// Calculate sum of all array elements
		for ($i = 0; $i < $k; $i++)
		{
			$sum += $arr[$i];
		}
		// Assign sum value to result
		$result = $sum;
		// Find position of subarray
		for ($i = $k; $i < $n; $i++)
		{
			// Add current element in subarray
			// And remove first element of previous subarray
			$sum += $arr[$i] - $arr[$i - $k];
			if ($sum > $result)
			{
				// Get starting point of resultant subarray
				$start = ($i - $k + 1);
				// Change result
				$result = $sum;
			}
		}
		// Display array element
		$this->printArray($arr, $n);
		// Display calculated result
		echo("\n Maximum subarray of size ".$k.
			" between : [".$start.
			"-".($start + $k - 1).
			"] \n");
	}
}

function main()
{
	$task = new Subarray();
	// Input array
	$arr = array(2, 4, -3, 1, 3, 7, -9, 10, 4, -4, 2, 8);
	// Get the number of elements in array
	$n = count($arr);
	// subarray size
	$k = 4;
	// Given k = 4
	// [7, -9, 10, 4] resultant subarray
	$task->findMaxAvgSubarray($arr, $n, $k);
}
main();

Output

 2 4 -3 1 3 7 -9 10 4 -4 2 8
 Maximum subarray of size 4 between : [5-8]
/*
  Node JS program for
  Find maximum average subarray of k length
*/
class Subarray
{
	// Display array elements
	printArray(arr, n)
	{
		for (var i = 0; i < n; ++i)
		{
			process.stdout.write(" " + arr[i]);
		}
	}
	findMaxAvgSubarray(arr, n, k)
	{
		if (n < k)
		{
			return;
		}
		// Define auxiliary variables
		var start = 0;
		var sum = 0;
		var result = 0;
		// Calculate sum of all array elements
		for (var i = 0; i < k; i++)
		{
			sum += arr[i];
		}
		// Assign sum value to result
		result = sum;
		// Find position of subarray
		for (var i = k; i < n; i++)
		{
			// Add current element in subarray
			// And remove first element of previous subarray
			sum += arr[i] - arr[i - k];
			if (sum > result)
			{
				// Get starting point of resultant subarray
				start = (i - k + 1);
				// Change result
				result = sum;
			}
		}
		// Display array element
		this.printArray(arr, n);
		// Display calculated result
		process.stdout.write("\n Maximum subarray of size " + 
                             k + " between : [" + start + "-" + 
                             (start + k - 1) + "] \n");
	}
}

function main()
{
	var task = new Subarray();
	// Input array
	var arr = [2, 4, -3, 1, 3, 7, -9, 10, 4, -4, 2, 8];
	// Get the number of elements in array
	var n = arr.length;
	// subarray size
	var k = 4;
	// Given k = 4
	// [7, -9, 10, 4] resultant subarray
	task.findMaxAvgSubarray(arr, n, k);
}
main();

Output

 2 4 -3 1 3 7 -9 10 4 -4 2 8
 Maximum subarray of size 4 between : [5-8]
#  Python 3 program for
#  Find maximum average subarray of k length
class Subarray :
	#  Display list elements
	def printArray(self, arr, n) :
		i = 0
		while (i < n) :
			print(" ", arr[i], end = "")
			i += 1
		
	
	def findMaxAvgSubarray(self, arr, n, k) :
		if (n < k) :
			return
		
		#  Define auxiliary variables
		start = 0
		sum = 0
		result = 0
		i = 0
		#  Calculate sum of all list elements
		while (i < k) :
			sum += arr[i]
			i += 1
		
		#  Assign sum value to result
		result = sum
		i = k
		#  Find position of sublist
		while (i < n) :
			#  Add current element in sublist
			#  And remove first element of previous sublist
			sum += arr[i] - arr[i - k]
			if (sum > result) :
				#  Get starting point of resultant sublist
				start = (i - k + 1)
				#  Change result
				result = sum
			
			i += 1
		
		#  Display list element
		self.printArray(arr, n)
		#  Display calculated result
		print("\n Maximum subarray of size", 
              k ,"between : [", start ,"-", (start + k - 1) ,"] ")
	

def main() :
	task = Subarray()
	#  Input list
	arr = [2, 4, -3, 1, 3, 7, -9, 10, 4, -4, 2, 8]
	#  Get the number of elements in list
	n = len(arr)
	#  sublist size
	k = 4
	#  Given k = 4
	#  [7, -9, 10, 4] resultant sublist
	task.findMaxAvgSubarray(arr, n, k)

if __name__ == "__main__": main()

Output

  2  4  -3  1  3  7  -9  10  4  -4  2  8
 Maximum subarray of size 4 between : [ 5 - 8 ]
#  Ruby program for
#  Find maximum average subarray of k length
class Subarray 
	#  Display array elements
	def printArray(arr, n) 
		i = 0
		while (i < n) 
			print(" ", arr[i])
			i += 1
		end

	end

	def findMaxAvgSubarray(arr, n, k) 
		if (n < k) 
			return
		end

		#  Define auxiliary variables
		start = 0
		sum = 0
		result = 0
		i = 0
		#  Calculate sum of all array elements
		while (i < k) 
			sum += arr[i]
			i += 1
		end

		#  Assign sum value to result
		result = sum
		i = k
		#  Find position of subarray
		while (i < n) 
			#  Add current element in subarray
			#  And remove first element of previous subarray
			sum += arr[i] - arr[i - k]
			if (sum > result) 
				#  Get starting point of resultant subarray
				start = (i - k + 1)
				#  Change result
				result = sum
			end

			i += 1
		end

		#  Display array element
		self.printArray(arr, n)
		#  Display calculated result
		print("\n Maximum subarray of size ", 
              k ," between : [", start ,"-", (start + k - 1) ,"] \n")
	end

end

def main() 
	task = Subarray.new()
	#  Input array
	arr = [2, 4, -3, 1, 3, 7, -9, 10, 4, -4, 2, 8]
	#  Get the number of elements in array
	n = arr.length
	#  subarray size
	k = 4
	#  Given k = 4
	#  [7, -9, 10, 4] resultant subarray
	task.findMaxAvgSubarray(arr, n, k)
end

main()

Output

 2 4 -3 1 3 7 -9 10 4 -4 2 8
 Maximum subarray of size 4 between : [5-8] 
/*
  Scala program for
  Find maximum average subarray of k length
*/
class Subarray()
{
	// Display array elements
	def printArray(arr: Array[Int], n: Int): Unit = {
		var i: Int = 0;
		while (i < n)
		{
			print(" " + arr(i));
			i += 1;
		}
	}
	def findMaxAvgSubarray(arr: Array[Int], 
      						n: Int, k: Int): Unit = {
		if (n < k)
		{
			return;
		}
		// Define auxiliary variables
		var start: Int = 0;
		var sum: Int = 0;
		var result: Int = 0;
		var i: Int = 0;
		// Calculate sum of all array elements
		while (i < k)
		{
			sum += arr(i);
			i += 1;
		}
		// Assign sum value to result
		result = sum;
		i = k;
		// Find position of subarray
		while (i < n)
		{
			// Add current element in subarray
			// And remove first element of previous subarray
			sum += arr(i) - arr(i - k);
			if (sum > result)
			{
				// Get starting point of resultant subarray
				start = (i - k + 1);
				// Change result
				result = sum;
			}
			i += 1;
		}
		// Display array element
		printArray(arr, n);
		// Display calculated result
		print("\n Maximum subarray of size " + 
              k + " between : [" + start + "-" + 
              (start + k - 1) + "] \n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Subarray = new Subarray();
		// Input array
		var arr: Array[Int] = Array(2, 4, -3, 1, 3, 7, -9, 10, 4, -4, 2, 8);
		// Get the number of elements in array
		var n: Int = arr.length;
		// subarray size
		var k: Int = 4;
		// Given k = 4
		// [7, -9, 10, 4] resultant subarray
		task.findMaxAvgSubarray(arr, n, k);
	}
}

Output

 2 4 -3 1 3 7 -9 10 4 -4 2 8
 Maximum subarray of size 4 between : [5-8]
import Foundation;
/*
  Swift 4 program for
  Find maximum average subarray of k length
*/
class Subarray
{
	// Display array elements
	func printArray(_ arr: [Int], _ n: Int)
	{
		var i: Int = 0;
		while (i < n)
		{
			print(" ", arr[i], terminator: "");
			i += 1;
		}
	}
	func findMaxAvgSubarray(_ arr: [Int], _ n: Int, _ k: Int)
	{
		if (n < k)
		{
			return;
		}
		// Define auxiliary variables
		var start: Int = 0;
		var sum: Int = 0;
		var result: Int = 0;
		var i: Int = 0;
		// Calculate sum of all array elements
		while (i < k)
		{
			sum += arr[i];
			i += 1;
		}
		// Assign sum value to result
		result = sum;
		i = k;
		// Find position of subarray
		while (i < n)
		{
			// Add current element in subarray
			// And remove first element of previous subarray
			sum += arr[i] - arr[i - k];
			if (sum > result)
			{
				// Get starting point of resultant subarray
				start = (i - k + 1);
				// Change result
				result = sum;
			}
			i += 1;
		}
		// Display array element
		self.printArray(arr, n);
		// Display calculated result
		print("\n Maximum subarray of size", 
              k ,"between : [", start ,"-", (start + k - 1) ,"] ");
	}
}
func main()
{
	let task: Subarray = Subarray();
	// Input array
	let arr: [Int] = [2, 4, -3, 1, 3, 7, -9, 10, 4, -4, 2, 8];
	// Get the number of elements in array
	let n: Int = arr.count;
	// subarray size
	let k: Int = 4;
	// Given k = 4
	// [7, -9, 10, 4] resultant subarray
	task.findMaxAvgSubarray(arr, n, k);
}
main();

Output

  2  4  -3  1  3  7  -9  10  4  -4  2  8
 Maximum subarray of size 4 between : [ 5 - 8 ]
/*
  Kotlin program for
  Find maximum average subarray of k length
*/
class Subarray
{
	// Display array elements
	fun printArray(arr: Array < Int > , n: Int): Unit
	{
		var i: Int = 0;
		while (i < n)
		{
			print(" " + arr[i]);
			i += 1;
		}
	}
	fun findMaxAvgSubarray(arr: Array < Int > ,
                           n: Int, k: Int): Unit
	{
		if (n < k)
		{
			return;
		}
		// Define auxiliary variables
		var start: Int = 0;
		var sum: Int = 0;
		var result: Int;
		var i: Int = 0;
		// Calculate sum of all array elements
		while (i < k)
		{
			sum += arr[i];
			i += 1;
		}
		// Assign sum value to result
		result = sum;
		i = k;
		// Find position of subarray
		while (i < n)
		{
			// Add current element in subarray
			// And remove first element of previous subarray
			sum += arr[i] - arr[i - k];
			if (sum > result)
			{
				// Get starting point of resultant subarray
				start = (i - k + 1);
				// Change result
				result = sum;
			}
			i += 1;
		}
		// Display array element
		this.printArray(arr, n);
		// Display calculated result
		print("\n Maximum subarray of size " + 
              k + " between : [" + start + "-" + (start + k - 1) + "] \n");
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Subarray = Subarray();
	// Input array
	val arr: Array < Int > = arrayOf(2, 4, -3, 1, 3, 7, -9, 10, 4, -4, 2, 8);
	// Get the number of elements in array
	val n: Int = arr.count();
	// subarray size
	val k: Int = 4;
	// Given k = 4
	// [7, -9, 10, 4] resultant subarray
	task.findMaxAvgSubarray(arr, n, k);
}

Output

 2 4 -3 1 3 7 -9 10 4 -4 2 8
 Maximum subarray of size 4 between : [5-8]


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