Find least average subarray of k length

Here given code implementation process.

/*
  C program for
  Find least 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 findMiniAvgSubarray(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 Least subarray of size %d between : [%d-%d] \n", 
           k, start, start + k - 1);
}
int main()
{
	// Input array
	int arr[] = {
		2 , 4 , 6 , 9 , 2 , -5 , 5 , 7 , 3 , 8
	};
	// Get the number of elements in array
	int n = sizeof arr / sizeof arr[0];
	// subarray size
	int k = 4;
	// Given k = 4
	// [2, -5,  5, 7] resultant subarray
	findMiniAvgSubarray(arr, n, k);
	return 0;
}

Output

  2  4  6  9  2  -5  5  7  3  8
 Least subarray of size 4 between : [4-7]
/*
  Java program for
  Find least 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 findMiniAvgSubarray(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 Least 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 , 6 , 9 , 2 , -5 , 5 , 7 , 3 , 8
		};
		// Get the number of elements in array
		int n = arr.length;
		// subarray size
		int k = 4;
		// Given k = 4
		// [2, -5,  5, 7] resultant subarray
		task.findMiniAvgSubarray(arr, n, k);
	}
}

Output

 2 4 6 9 2 -5 5 7 3 8
 Least subarray of size 4 between : [4-7]
// Include header file
#include <iostream>
using namespace std;
/*
  C++ program for
  Find least 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 findMiniAvgSubarray(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 Least subarray of size " << 
          k << " between : [" << start << "-" << 
          (start + k - 1) << "] \n";
	}
};
int main()
{
	Subarray *task = new Subarray();
	// Input array
	int arr[] = {
		2 , 4 , 6 , 9 , 2 , -5 , 5 , 7 , 3 , 8
	};
	// Get the number of elements in array
	int n = sizeof(arr) / sizeof(arr[0]);
	// subarray size
	int k = 4;
	// Given k = 4
	// [2, -5,  5, 7] resultant subarray
	task->findMiniAvgSubarray(arr, n, k);
	return 0;
}

Output

 2 4 6 9 2 -5 5 7 3 8
 Least subarray of size 4 between : [4-7]
// Include namespace system
using System;
/*
  Csharp program for
  Find least 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 findMiniAvgSubarray(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 Least 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 , 6 , 9 , 2 , -5 , 5 , 7 , 3 , 8
		};
		// Get the number of elements in array
		int n = arr.Length;
		// subarray size
		int k = 4;
		// Given k = 4
		// [2, -5,  5, 7] resultant subarray
		task.findMiniAvgSubarray(arr, n, k);
	}
}

Output

 2 4 6 9 2 -5 5 7 3 8
 Least subarray of size 4 between : [4-7]
package main
import "fmt"
/*
  Go program for
  Find least 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 findMiniAvgSubarray(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 Least subarray of size ", 
		k, " between : [", start, "-", (start + k - 1), "] \n")
}
func main() {
	
	// Input array
	var arr = [] int { 2 , 4 , 6 , 9 , 2 , -5 , 5 , 7 , 3 , 8 }
	// Get the number of elements in array
	var n int = len(arr)
	// subarray size
	var k int = 4
	// Given k = 4
	// [2, -5,  5, 7] resultant subarray
	findMiniAvgSubarray(arr, n, k)
}

Output

 2 4 6 9 2 -5 5 7 3 8
 Least subarray of size 4 between : [4-7]
<?php
/*
  Php program for
  Find least 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 findMiniAvgSubarray($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 Least subarray of size ".$k.
			" between : [".$start.
			"-".($start + $k - 1).
			"] \n");
	}
}

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

Output

 2 4 6 9 2 -5 5 7 3 8
 Least subarray of size 4 between : [4-7]
/*
  Node JS program for
  Find least 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]);
		}
	}
	findMiniAvgSubarray(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 Least subarray of size " + 
           k + " between : [" + start + "-" + (start + k - 1) + "] \n");
	}
}

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

Output

 2 4 6 9 2 -5 5 7 3 8
 Least subarray of size 4 between : [4-7]
#  Python 3 program for
#  Find least 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 findMiniAvgSubarray(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 Least subarray of size ", k ,
              " between :[", start ,"-", (start + k - 1) ,"] ")
	

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

if __name__ == "__main__": main()

Output

  2  4  6  9  2  -5  5  7  3  8
 Least subarray of size  4  between :[ 4 - 7 ]
#  Ruby program for
#  Find least 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 findMiniAvgSubarray(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 Least subarray of size ", k ,
              " between : [", start ,"-", (start + k - 1) ,"] \n")
	end

end

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

main()

Output

 2 4 6 9 2 -5 5 7 3 8
 Least subarray of size 4 between : [4-7] 
/*
  Scala program for
  Find least 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 findMiniAvgSubarray(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 Least 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, 6, 9, 2, -5, 5, 7, 3, 8);
		// Get the number of elements in array
		var n: Int = arr.length;
		// subarray size
		var k: Int = 4;
		// Given k = 4
		// [2, -5,  5, 7] resultant subarray
		task.findMiniAvgSubarray(arr, n, k);
	}
}

Output

 2 4 6 9 2 -5 5 7 3 8
 Least subarray of size 4 between : [4-7]
import Foundation;
/*
  Swift 4 program for
  Find least 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 findMiniAvgSubarray(_ 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 Least subarray of size", 
              k ,"between : [", start ,"-", (start + k - 1) ,"] ");
	}
}
func main()
{
	let task: Subarray = Subarray();
	// Input array
	let arr: [Int] = [2, 4, 6, 9, 2, -5, 5, 7, 3, 8];
	// Get the number of elements in array
	let n: Int = arr.count;
	// subarray size
	let k: Int = 4;
	// Given k = 4
	// [2, -5,  5, 7] resultant subarray
	task.findMiniAvgSubarray(arr, n, k);
}
main();

Output

  2  4  6  9  2  -5  5  7  3  8
 Least subarray of size 4 between : [ 4 - 7 ]
/*
  Kotlin program for
  Find least 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 findMiniAvgSubarray(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 Least 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, 6, 9, 2, -5, 5, 7, 3, 8);
	// Get the number of elements in array
	val n: Int = arr.count();
	// subarray size
	val k: Int = 4;
	// Given k = 4
	// [2, -5,  5, 7] resultant subarray
	task.findMiniAvgSubarray(arr, n, k);
}

Output

 2 4 6 9 2 -5 5 7 3 8
 Least subarray of size 4 between : [4-7]


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