Sum of minimum element of all subarrays of a sorted array

Here given code implementation process.

//  C program for
//  Sum of minimum element of all subarrays of a sorted array
#include <stdio.h>
 //Display array elements
void display(int arr[], int n)
{
    printf("\n\n Array Elements \n [");
    for (int i = 0; i < n; ++i)
    {
        printf(" %d", arr[i]);
    }
    printf(" ]\n");
}
int minSubArraySum(int arr[], int n)
{
    // Initial value
    int result = 0;
    // Executes the loop through by n
    for (int i = 0; i < n; ++i)
    {
        // Calculate resultant value
        result += (arr[i] * (n - i));
    }
    // Display calculated result
    printf(" Sum : %d", result);
}
int main(int argc, char
    const *argv[])
{
    // Sorted Array
    int arr1[] = {
        1 , 2 , 3 , 4
    };
    int arr2[] = {
        1 , 6 , 8 , 9
    };
    // Get the size
    int size = sizeof(arr1) / sizeof(arr1[0]);
    display(arr1, size);
    // arr = [1, 2, 3, 4]
    // [ 1 ] ---> 1
    // [ 1 2 ] ---> 1
    // [ 1 2 3 ] ---> 1
    // [ 1 2 3 4 ] ---> 1
    // [ 2 ] ---> 2
    // [ 2 3 ] ---> 2
    // [ 2 3 4 ] ---> 2
    // [ 3 ] ---> 3
    // [ 3 4 ] ---> 3
    // [ 4 ] ---> 4
    // [1+1+1+1+2+2+2+3+3+4] = 20
    minSubArraySum(arr1, size);
    // Get the size
    size = sizeof(arr2) / sizeof(arr2[0]);
    display(arr2, size);
    // arr = [1, 6, 8, 9]
    // [ 1 ]---> 1
    // [ 1 6 ]---> 1
    // [ 1 6 8 ]---> 1
    // [ 1 6 8 9 ]---> 1
    // [ 6 ]---> 6
    // [ 6 8 ]---> 6
    // [ 6 8 9 ]---> 6
    // [ 8 ]---> 8
    // [ 8 9 ]---> 8
    // [ 9 ]---> 9
    // [1+1+1+1+6+6+6+8+8+9] = 47
    minSubArraySum(arr2, size);
    return 0;
}

Output

 Array Elements
 [ 1 2 3 4 ]
 Sum : 20

 Array Elements
 [ 1 6 8 9 ]
 Sum : 47
/*
    Java program
    Sum of minimum element of all subarrays of a sorted array
*/
public class Subarrays
{
	//Display array elements
	public void display(int[] arr, int n)
	{
		System.out.print("\n\n Array Elements \n [");
		for (int i = 0; i < n; ++i)
		{
			System.out.print(" " + arr[i]);
		}
		System.out.print(" ]\n");
	}
	public void minSubArraySum(int[] arr, int n)
	{
		// Initial value
		int result = 0;
		// Executes the loop through by n
		for (int i = 0; i < n; ++i)
		{
			// Calculate resultant value
			result += (arr[i] * (n - i));
		}
		// Display calculated result
		System.out.print(" Sum : " + result);
	}
	public static void main(String[] args)
	{
		Subarrays task = new Subarrays();
		// Sorted Array
		int[] arr1 =  {
			1 , 2 , 3 , 4
		};
		int[] arr2 =  {
			1 , 6 , 8 , 9
		};
		// Get the size
		int size = arr1.length;
		task.display(arr1, size);
		// arr = [1, 2, 3, 4]
		// [ 1 ] ---> 1
		// [ 1 2 ] ---> 1
		// [ 1 2 3 ] ---> 1
		// [ 1 2 3 4 ] ---> 1
		// [ 2 ] ---> 2
		// [ 2 3 ] ---> 2
		// [ 2 3 4 ] ---> 2
		// [ 3 ] ---> 3
		// [ 3 4 ] ---> 3
		// [ 4 ] ---> 4
		// [1+1+1+1+2+2+2+3+3+4] = 20
		task.minSubArraySum(arr1, size);
		// Get the size
		size = arr2.length;
		task.display(arr2, size);
		// arr = [1, 6, 8, 9]
		// [ 1 ]---> 1
		// [ 1 6 ]---> 1
		// [ 1 6 8 ]---> 1
		// [ 1 6 8 9 ]---> 1
		// [ 6 ]---> 6
		// [ 6 8 ]---> 6
		// [ 6 8 9 ]---> 6
		// [ 8 ]---> 8
		// [ 8 9 ]---> 8
		// [ 9 ]---> 9
		// [1+1+1+1+6+6+6+8+8+9] = 47
		task.minSubArraySum(arr2, size);
	}
}

Output

 Array Elements
 [ 1 2 3 4 ]
 Sum : 20

 Array Elements
 [ 1 6 8 9 ]
 Sum : 47
// Include header file
#include <iostream>
using namespace std;
/*
    C++ program
    Sum of minimum element of all subarrays of a sorted array
*/
class Subarrays
{
	public:
		//Display array elements
		void display(int arr[], int n)
		{
			cout << "\n\n Array Elements \n [";
			for (int i = 0; i < n; ++i)
			{
				cout << " " << arr[i];
			}
			cout << " ]\n";
		}
	void minSubArraySum(int arr[], int n)
	{
		// Initial value
		int result = 0;
		// Executes the loop through by n
		for (int i = 0; i < n; ++i)
		{
			// Calculate resultant value
			result += (arr[i] *(n - i));
		}
		// Display calculated result
		cout << " Sum : " << result;
	}
};
int main()
{
	Subarrays *task = new Subarrays();
	// Sorted Array
	int arr1[] = {
		1 , 2 , 3 , 4
	};
	int arr2[] = {
		1 , 6 , 8 , 9
	};
	// Get the size
	int size = sizeof(arr1) / sizeof(arr1[0]);
	task->display(arr1, size);
	// arr = [1, 2, 3, 4]
	// [ 1 ] ---> 1
	// [ 1 2 ] ---> 1
	// [ 1 2 3 ] ---> 1
	// [ 1 2 3 4 ] ---> 1
	// [ 2 ] ---> 2
	// [ 2 3 ] ---> 2
	// [ 2 3 4 ] ---> 2
	// [ 3 ] ---> 3
	// [ 3 4 ] ---> 3
	// [ 4 ] ---> 4
	// [1+1+1+1+2+2+2+3+3+4] = 20
	task->minSubArraySum(arr1, size);
	// Get the size
	size = sizeof(arr2) / sizeof(arr2[0]);
	task->display(arr2, size);
	// arr = [1, 6, 8, 9]
	// [ 1 ]---> 1
	// [ 1 6 ]---> 1
	// [ 1 6 8 ]---> 1
	// [ 1 6 8 9 ]---> 1
	// [ 6 ]---> 6
	// [ 6 8 ]---> 6
	// [ 6 8 9 ]---> 6
	// [ 8 ]---> 8
	// [ 8 9 ]---> 8
	// [ 9 ]---> 9
	// [1+1+1+1+6+6+6+8+8+9] = 47
	task->minSubArraySum(arr2, size);
	return 0;
}

Output

 Array Elements
 [ 1 2 3 4 ]
 Sum : 20

 Array Elements
 [ 1 6 8 9 ]
 Sum : 47
// Include namespace system
using System;
/*
    Csharp program
    Sum of minimum element of all subarrays of a sorted array
*/
public class Subarrays
{
	//Display array elements
	public void display(int[] arr, int n)
	{
		Console.Write("\n\n Array Elements \n [");
		for (int i = 0; i < n; ++i)
		{
			Console.Write(" " + arr[i]);
		}
		Console.Write(" ]\n");
	}
	public void minSubArraySum(int[] arr, int n)
	{
		// Initial value
		int result = 0;
		// Executes the loop through by n
		for (int i = 0; i < n; ++i)
		{
			// Calculate resultant value
			result += (arr[i] * (n - i));
		}
		// Display calculated result
		Console.Write(" Sum : " + result);
	}
	public static void Main(String[] args)
	{
		Subarrays task = new Subarrays();
		// Sorted Array
		int[] arr1 = {
			1 , 2 , 3 , 4
		};
		int[] arr2 = {
			1 , 6 , 8 , 9
		};
		// Get the size
		int size = arr1.Length;
		task.display(arr1, size);
		// arr = [1, 2, 3, 4]
		// [ 1 ] ---> 1
		// [ 1 2 ] ---> 1
		// [ 1 2 3 ] ---> 1
		// [ 1 2 3 4 ] ---> 1
		// [ 2 ] ---> 2
		// [ 2 3 ] ---> 2
		// [ 2 3 4 ] ---> 2
		// [ 3 ] ---> 3
		// [ 3 4 ] ---> 3
		// [ 4 ] ---> 4
		// [1+1+1+1+2+2+2+3+3+4] = 20
		task.minSubArraySum(arr1, size);
		// Get the size
		size = arr2.Length;
		task.display(arr2, size);
		// arr = [1, 6, 8, 9]
		// [ 1 ]---> 1
		// [ 1 6 ]---> 1
		// [ 1 6 8 ]---> 1
		// [ 1 6 8 9 ]---> 1
		// [ 6 ]---> 6
		// [ 6 8 ]---> 6
		// [ 6 8 9 ]---> 6
		// [ 8 ]---> 8
		// [ 8 9 ]---> 8
		// [ 9 ]---> 9
		// [1+1+1+1+6+6+6+8+8+9] = 47
		task.minSubArraySum(arr2, size);
	}
}

Output

 Array Elements
 [ 1 2 3 4 ]
 Sum : 20

 Array Elements
 [ 1 6 8 9 ]
 Sum : 47
package main
import "fmt"
/*
    Go program
    Sum of minimum element of all subarrays of a sorted array
*/
type Subarrays struct {}
func getSubarrays() * Subarrays {
	var me *Subarrays = &Subarrays {}
	return me
}
//Display array elements
func(this Subarrays) display(arr[] int, n int) {
	fmt.Print("\n\n Array Elements \n [")
	for i := 0 ; i < n ; i++ {
		fmt.Print(" ", arr[i])
	}
	fmt.Print(" ]\n")
}
func(this Subarrays) minSubArraySum(arr[] int, n int) {
	// Initial value
	var result int = 0
	// Executes the loop through by n
	for i := 0 ; i < n ; i++ {
		// Calculate resultant value
		result += (arr[i] * (n - i))
	}
	// Display calculated result
	fmt.Print(" Sum : ", result)
}
func main() {
	var task * Subarrays = getSubarrays()
	// Sorted Array
	var arr1 = [] int {
		1,
		2,
		3,
		4,
	}
	var arr2 = [] int {
		1,
		6,
		8,
		9,
	}
	// Get the size
	var size int = len(arr1)
	task.display(arr1, size)
	// arr = [1, 2, 3, 4]
	// [ 1 ] ---> 1
	// [ 1 2 ] ---> 1
	// [ 1 2 3 ] ---> 1
	// [ 1 2 3 4 ] ---> 1
	// [ 2 ] ---> 2
	// [ 2 3 ] ---> 2
	// [ 2 3 4 ] ---> 2
	// [ 3 ] ---> 3
	// [ 3 4 ] ---> 3
	// [ 4 ] ---> 4
	// [1+1+1+1+2+2+2+3+3+4] = 20
	task.minSubArraySum(arr1, size)
	// Get the size
	size = len(arr2)
	task.display(arr2, size)
	// arr = [1, 6, 8, 9]
	// [ 1 ]---> 1
	// [ 1 6 ]---> 1
	// [ 1 6 8 ]---> 1
	// [ 1 6 8 9 ]---> 1
	// [ 6 ]---> 6
	// [ 6 8 ]---> 6
	// [ 6 8 9 ]---> 6
	// [ 8 ]---> 8
	// [ 8 9 ]---> 8
	// [ 9 ]---> 9
	// [1+1+1+1+6+6+6+8+8+9] = 47
	task.minSubArraySum(arr2, size)
}

Output

 Array Elements
 [ 1 2 3 4 ]
 Sum : 20

 Array Elements
 [ 1 6 8 9 ]
 Sum : 47
<?php
/*
    Php program
    Sum of minimum element of all subarrays of a sorted array
*/
class Subarrays
{
	//Display array elements
	public	function display($arr, $n)
	{
		echo("\n\n Array Elements \n [");
		for ($i = 0; $i < $n; ++$i)
		{
			echo(" ".$arr[$i]);
		}
		echo(" ]\n");
	}
	public	function minSubArraySum($arr, $n)
	{
		// Initial value
		$result = 0;
		// Executes the loop through by n
		for ($i = 0; $i < $n; ++$i)
		{
			// Calculate resultant value
			$result += ($arr[$i] * ($n - $i));
		}
		// Display calculated result
		echo(" Sum : ".$result);
	}
}

function main()
{
	$task = new Subarrays();
	// Sorted Array
	$arr1 = array(1, 2, 3, 4);
	$arr2 = array(1, 6, 8, 9);
	// Get the size
	$size = count($arr1);
	$task->display($arr1, $size);
	// arr = [1, 2, 3, 4]
	// [ 1 ] ---> 1
	// [ 1 2 ] ---> 1
	// [ 1 2 3 ] ---> 1
	// [ 1 2 3 4 ] ---> 1
	// [ 2 ] ---> 2
	// [ 2 3 ] ---> 2
	// [ 2 3 4 ] ---> 2
	// [ 3 ] ---> 3
	// [ 3 4 ] ---> 3
	// [ 4 ] ---> 4
	// [1+1+1+1+2+2+2+3+3+4] = 20
	$task->minSubArraySum($arr1, $size);
	// Get the size
	$size = count($arr2);
	$task->display($arr2, $size);
	// arr = [1, 6, 8, 9]
	// [ 1 ]---> 1
	// [ 1 6 ]---> 1
	// [ 1 6 8 ]---> 1
	// [ 1 6 8 9 ]---> 1
	// [ 6 ]---> 6
	// [ 6 8 ]---> 6
	// [ 6 8 9 ]---> 6
	// [ 8 ]---> 8
	// [ 8 9 ]---> 8
	// [ 9 ]---> 9
	// [1+1+1+1+6+6+6+8+8+9] = 47
	$task->minSubArraySum($arr2, $size);
}
main();

Output

 Array Elements
 [ 1 2 3 4 ]
 Sum : 20

 Array Elements
 [ 1 6 8 9 ]
 Sum : 47
/*
    Node JS program
    Sum of minimum element of all subarrays of a sorted array
*/
class Subarrays
{
	//Display array elements
	display(arr, n)
	{
		process.stdout.write("\n\n Array Elements \n [");
		for (var i = 0; i < n; ++i)
		{
			process.stdout.write(" " + arr[i]);
		}
		process.stdout.write(" ]\n");
	}
	minSubArraySum(arr, n)
	{
		// Initial value
		var result = 0;
		// Executes the loop through by n
		for (var i = 0; i < n; ++i)
		{
			// Calculate resultant value
			result += (arr[i] * (n - i));
		}
		// Display calculated result
		process.stdout.write(" Sum : " + result);
	}
}

function main()
{
	var task = new Subarrays();
	// Sorted Array
	var arr1 = [1, 2, 3, 4];
	var arr2 = [1, 6, 8, 9];
	// Get the size
	var size = arr1.length;
	task.display(arr1, size);
	// arr = [1, 2, 3, 4]
	// [ 1 ] ---> 1
	// [ 1 2 ] ---> 1
	// [ 1 2 3 ] ---> 1
	// [ 1 2 3 4 ] ---> 1
	// [ 2 ] ---> 2
	// [ 2 3 ] ---> 2
	// [ 2 3 4 ] ---> 2
	// [ 3 ] ---> 3
	// [ 3 4 ] ---> 3
	// [ 4 ] ---> 4
	// [1+1+1+1+2+2+2+3+3+4] = 20
	task.minSubArraySum(arr1, size);
	// Get the size
	size = arr2.length;
	task.display(arr2, size);
	// arr = [1, 6, 8, 9]
	// [ 1 ]---> 1
	// [ 1 6 ]---> 1
	// [ 1 6 8 ]---> 1
	// [ 1 6 8 9 ]---> 1
	// [ 6 ]---> 6
	// [ 6 8 ]---> 6
	// [ 6 8 9 ]---> 6
	// [ 8 ]---> 8
	// [ 8 9 ]---> 8
	// [ 9 ]---> 9
	// [1+1+1+1+6+6+6+8+8+9] = 47
	task.minSubArraySum(arr2, size);
}
main();

Output

 Array Elements
 [ 1 2 3 4 ]
 Sum : 20

 Array Elements
 [ 1 6 8 9 ]
 Sum : 47
#    Python 3 program
#    Sum of minimum element of all subarrays of a sorted array
class Subarrays :
	# Display list elements
	def display(self, arr, n) :
		print("\n\n Array Elements \n [", end = "")
		i = 0
		while (i < n) :
			print(" ", arr[i], end = "")
			i += 1
		
		print(" ]")
	
	def minSubArraySum(self, arr, n) :
		#  Initial value
		result = 0
		i = 0
		#  Executes the loop through by n
		while (i < n) :
			#  Calculate resultant value
			result += (arr[i] * (n - i))
			i += 1
		
		#  Display calculated result
		print(" Sum : ", result, end = "")
	

def main() :
	task = Subarrays()
	#  Sorted Array
	arr1 = [1, 2, 3, 4]
	arr2 = [1, 6, 8, 9]
	#  Get the size
	size = len(arr1)
	task.display(arr1, size)
	#  arr = [1, 2, 3, 4]
	#  [ 1 ] ---> 1
	#  [ 1 2 ] ---> 1
	#  [ 1 2 3 ] ---> 1
	#  [ 1 2 3 4 ] ---> 1
	#  [ 2 ] ---> 2
	#  [ 2 3 ] ---> 2
	#  [ 2 3 4 ] ---> 2
	#  [ 3 ] ---> 3
	#  [ 3 4 ] ---> 3
	#  [ 4 ] ---> 4
	#  [1+1+1+1+2+2+2+3+3+4] = 20
	task.minSubArraySum(arr1, size)
	#  Get the size
	size = len(arr2)
	task.display(arr2, size)
	#  arr = [1, 6, 8, 9]
	#  [ 1 ]---> 1
	#  [ 1 6 ]---> 1
	#  [ 1 6 8 ]---> 1
	#  [ 1 6 8 9 ]---> 1
	#  [ 6 ]---> 6
	#  [ 6 8 ]---> 6
	#  [ 6 8 9 ]---> 6
	#  [ 8 ]---> 8
	#  [ 8 9 ]---> 8
	#  [ 9 ]---> 9
	#  [1+1+1+1+6+6+6+8+8+9] = 47
	task.minSubArraySum(arr2, size)

if __name__ == "__main__": main()

Output

 Array Elements
 [  1  2  3  4 ]
 Sum :  20

 Array Elements
 [  1  6  8  9 ]
 Sum :  47
#    Ruby program
#    Sum of minimum element of all subarrays of a sorted array
class Subarrays 
	# Display array elements
	def display(arr, n) 
		print("\n\n Array Elements \n [")
		i = 0
		while (i < n) 
			print(" ", arr[i])
			i += 1
		end

		print(" ]\n")
	end

	def minSubArraySum(arr, n) 
		#  Initial value
		result = 0
		i = 0
		#  Executes the loop through by n
		while (i < n) 
			#  Calculate resultant value
			result += (arr[i] * (n - i))
			i += 1
		end

		#  Display calculated result
		print(" Sum : ", result)
	end

end

def main() 
	task = Subarrays.new()
	#  Sorted Array
	arr1 = [1, 2, 3, 4]
	arr2 = [1, 6, 8, 9]
	#  Get the size
	size = arr1.length
	task.display(arr1, size)
	#  arr = [1, 2, 3, 4]
	#  [ 1 ] ---> 1
	#  [ 1 2 ] ---> 1
	#  [ 1 2 3 ] ---> 1
	#  [ 1 2 3 4 ] ---> 1
	#  [ 2 ] ---> 2
	#  [ 2 3 ] ---> 2
	#  [ 2 3 4 ] ---> 2
	#  [ 3 ] ---> 3
	#  [ 3 4 ] ---> 3
	#  [ 4 ] ---> 4
	#  [1+1+1+1+2+2+2+3+3+4] = 20
	task.minSubArraySum(arr1, size)
	#  Get the size
	size = arr2.length
	task.display(arr2, size)
	#  arr = [1, 6, 8, 9]
	#  [ 1 ]---> 1
	#  [ 1 6 ]---> 1
	#  [ 1 6 8 ]---> 1
	#  [ 1 6 8 9 ]---> 1
	#  [ 6 ]---> 6
	#  [ 6 8 ]---> 6
	#  [ 6 8 9 ]---> 6
	#  [ 8 ]---> 8
	#  [ 8 9 ]---> 8
	#  [ 9 ]---> 9
	#  [1+1+1+1+6+6+6+8+8+9] = 47
	task.minSubArraySum(arr2, size)
end

main()

Output


 Array Elements 
 [ 1 2 3 4 ]
 Sum : 20

 Array Elements 
 [ 1 6 8 9 ]
 Sum : 47
/*
    Scala program
    Sum of minimum element of all subarrays of a sorted array
*/
class Subarrays()
{
	//Display array elements
	def display(arr: Array[Int], n: Int): Unit = {
		print("\n\n Array Elements \n [");
		var i: Int = 0;
		while (i < n)
		{
			print(" " + arr(i));
			i += 1;
		}
		print(" ]\n");
	}
	def minSubArraySum(arr: Array[Int], n: Int): Unit = {
		// Initial value
		var result: Int = 0;
		var i: Int = 0;
		// Executes the loop through by n
		while (i < n)
		{
			// Calculate resultant value
			result += (arr(i) * (n - i));
			i += 1;
		}
		// Display calculated result
		print(" Sum : " + result);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Subarrays = new Subarrays();
		// Sorted Array
		var arr1: Array[Int] = Array(1, 2, 3, 4);
		var arr2: Array[Int] = Array(1, 6, 8, 9);
		// Get the size
		var size: Int = arr1.length;
		task.display(arr1, size);
		// arr = [1, 2, 3, 4]
		// [ 1 ] ---> 1
		// [ 1 2 ] ---> 1
		// [ 1 2 3 ] ---> 1
		// [ 1 2 3 4 ] ---> 1
		// [ 2 ] ---> 2
		// [ 2 3 ] ---> 2
		// [ 2 3 4 ] ---> 2
		// [ 3 ] ---> 3
		// [ 3 4 ] ---> 3
		// [ 4 ] ---> 4
		// [1+1+1+1+2+2+2+3+3+4] = 20
		task.minSubArraySum(arr1, size);
		// Get the size
		size = arr2.length;
		task.display(arr2, size);
		// arr = [1, 6, 8, 9]
		// [ 1 ]---> 1
		// [ 1 6 ]---> 1
		// [ 1 6 8 ]---> 1
		// [ 1 6 8 9 ]---> 1
		// [ 6 ]---> 6
		// [ 6 8 ]---> 6
		// [ 6 8 9 ]---> 6
		// [ 8 ]---> 8
		// [ 8 9 ]---> 8
		// [ 9 ]---> 9
		// [1+1+1+1+6+6+6+8+8+9] = 47
		task.minSubArraySum(arr2, size);
	}
}

Output

 Array Elements
 [ 1 2 3 4 ]
 Sum : 20

 Array Elements
 [ 1 6 8 9 ]
 Sum : 47
import Foundation;
/*
    Swift 4 program
    Sum of minimum element of all subarrays of a sorted array
*/
class Subarrays
{
	//Display array elements
	func display(_ arr: [Int], _ n: Int)
	{
		print("\n\n Array Elements \n [", terminator: "");
		var i: Int = 0;
		while (i < n)
		{
			print(" ", arr[i], terminator: "");
			i += 1;
		}
		print(" ]");
	}
	func minSubArraySum(_ arr: [Int], _ n: Int)
	{
		// Initial value
		var result: Int = 0;
		var i: Int = 0;
		// Executes the loop through by n
		while (i < n)
		{
			// Calculate resultant value
			result += (arr[i] * (n - i));
			i += 1;
		}
		// Display calculated result
		print(" Sum : ", result, terminator: "");
	}
}
func main()
{
	let task: Subarrays = Subarrays();
	// Sorted Array
	let arr1: [Int] = [1, 2, 3, 4];
	let arr2: [Int] = [1, 6, 8, 9];
	// Get the size
	var size: Int = arr1.count;
	task.display(arr1, size);
	// arr = [1, 2, 3, 4]
	// [ 1 ] ---> 1
	// [ 1 2 ] ---> 1
	// [ 1 2 3 ] ---> 1
	// [ 1 2 3 4 ] ---> 1
	// [ 2 ] ---> 2
	// [ 2 3 ] ---> 2
	// [ 2 3 4 ] ---> 2
	// [ 3 ] ---> 3
	// [ 3 4 ] ---> 3
	// [ 4 ] ---> 4
	// [1+1+1+1+2+2+2+3+3+4] = 20
	task.minSubArraySum(arr1, size);
	// Get the size
	size = arr2.count;
	task.display(arr2, size);
	// arr = [1, 6, 8, 9]
	// [ 1 ]---> 1
	// [ 1 6 ]---> 1
	// [ 1 6 8 ]---> 1
	// [ 1 6 8 9 ]---> 1
	// [ 6 ]---> 6
	// [ 6 8 ]---> 6
	// [ 6 8 9 ]---> 6
	// [ 8 ]---> 8
	// [ 8 9 ]---> 8
	// [ 9 ]---> 9
	// [1+1+1+1+6+6+6+8+8+9] = 47
	task.minSubArraySum(arr2, size);
}
main();

Output

 Array Elements
 [  1  2  3  4 ]
 Sum :  20

 Array Elements
 [  1  6  8  9 ]
 Sum :  47
/*
    Kotlin program
    Sum of minimum element of all subarrays of a sorted array
*/
class Subarrays
{
	//Display array elements
	fun display(arr: Array < Int > , n: Int): Unit
	{
		print("\n\n Array Elements \n [");
		var i: Int = 0;
		while (i < n)
		{
			print(" " + arr[i]);
			i += 1;
		}
		print(" ]\n");
	}
	fun minSubArraySum(arr: Array < Int > , n: Int): Unit
	{
		// Initial value
		var result: Int = 0;
		var i: Int = 0;
		// Executes the loop through by n
		while (i < n)
		{
			// Calculate resultant value
			result += (arr[i] * (n - i));
			i += 1;
		}
		// Display calculated result
		print(" Sum : " + result);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Subarrays = Subarrays();
	// Sorted Array
	val arr1: Array < Int > = arrayOf(1, 2, 3, 4);
	val arr2: Array < Int > = arrayOf(1, 6, 8, 9);
	// Get the size
	var size: Int = arr1.count();
	task.display(arr1, size);
	// arr = [1, 2, 3, 4]
	// [ 1 ] ---> 1
	// [ 1 2 ] ---> 1
	// [ 1 2 3 ] ---> 1
	// [ 1 2 3 4 ] ---> 1
	// [ 2 ] ---> 2
	// [ 2 3 ] ---> 2
	// [ 2 3 4 ] ---> 2
	// [ 3 ] ---> 3
	// [ 3 4 ] ---> 3
	// [ 4 ] ---> 4
	// [1+1+1+1+2+2+2+3+3+4] = 20
	task.minSubArraySum(arr1, size);
	// Get the size
	size = arr2.count();
	task.display(arr2, size);
	// arr = [1, 6, 8, 9]
	// [ 1 ]---> 1
	// [ 1 6 ]---> 1
	// [ 1 6 8 ]---> 1
	// [ 1 6 8 9 ]---> 1
	// [ 6 ]---> 6
	// [ 6 8 ]---> 6
	// [ 6 8 9 ]---> 6
	// [ 8 ]---> 8
	// [ 8 9 ]---> 8
	// [ 9 ]---> 9
	// [1+1+1+1+6+6+6+8+8+9] = 47
	task.minSubArraySum(arr2, size);
}

Output

 Array Elements
 [ 1 2 3 4 ]
 Sum : 20

 Array Elements
 [ 1 6 8 9 ]
 Sum : 47


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