# Sum the maximum element of all subarrays of a sorted array

Here given code implementation process.

``````//  C program for
//  Sum the maximum 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");
}
void maxSubArrayValueSum(int arr[], int n)
{
int result = 0;
for (int i = 0; i < n; ++i)
{
// Calculate resultant value
result += arr[i] *(i + 1);
}
// 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 ] ---> 2
// [ 1 2 3 ] ---> 3
// [ 1 2 3 4 ] ---> 4
// [ 2 ] ---> 2
// [ 2 3 ] ---> 3
// [ 2 3 4 ] ---> 4
// [ 3 ] ---> 3
// [ 3 4 ] ---> 4
// [ 4 ] ---> 4
// [1+2+3+4+2+3+4+3+4+4] = 30
maxSubArrayValueSum(arr1, size);
// Get the size
size = sizeof(arr2) / sizeof(arr2[0]);
display(arr2, size);
// arr = [1, 6, 8, 9]
// [ 1 ]---> 1
// [ 1 6 ]---> 6
// [ 1 6 8 ]---> 8
// [ 1 6 8 9 ]---> 9
// [ 6 ]---> 6
// [ 6 8 ]---> 8
// [ 6 8 9 ]---> 9
// [ 8 ]---> 8
// [ 8 9 ]---> 9
// [ 9 ]---> 9
// [1+6+8+9+6+8+9+8+9+9] = 73
maxSubArrayValueSum(arr2, size);
return 0;
}``````

#### Output

`````` Array Elements
[ 1 2 3 4 ]
Sum : 30

Array Elements
[ 1 6 8 9 ]
Sum : 73``````
``````/*
Java program
Sum the maximum 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 Array Elements \n [");
for (int i = 0; i < n; ++i)
{
System.out.print(" " + arr[i]);
}
System.out.print(" ]\n");
}
public void maxSubArrayValueSum(int[] arr, int n)
{
int result = 0;
for (int i = 0; i < n; ++i)
{
// Calculate resultant value
result += arr[i] * (i + 1);
}
// Display calculated result
System.out.print(" Sum : " + result);
}
public static void main(String[] args)
{
// Sorted Array
int[] arr1 = {
1 , 2 , 3 , 4
};
int[] arr2 = {
1 , 6 , 8 , 9
};
// Get the size
int size = arr1.length;
// arr = [1, 2, 3, 4]
// [ 1 ] ---> 1
// [ 1 2 ] ---> 2
// [ 1 2 3 ] ---> 3
// [ 1 2 3 4 ] ---> 4
// [ 2 ] ---> 2
// [ 2 3 ] ---> 3
// [ 2 3 4 ] ---> 4
// [ 3 ] ---> 3
// [ 3 4 ] ---> 4
// [ 4 ] ---> 4
// [1+2+3+4+2+3+4+3+4+4] = 30
// Get the size
size = arr2.length;
// arr = [1, 6, 8, 9]
// [ 1 ]---> 1
// [ 1 6 ]---> 6
// [ 1 6 8 ]---> 8
// [ 1 6 8 9 ]---> 9
// [ 6 ]---> 6
// [ 6 8 ]---> 8
// [ 6 8 9 ]---> 9
// [ 8 ]---> 8
// [ 8 9 ]---> 9
// [ 9 ]---> 9
// [1+6+8+9+6+8+9+8+9+9] = 73
}
}``````

#### Output

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

#### Output

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

#### Output

`````` Array Elements
[ 1 2 3 4 ]
Sum : 30
Array Elements
[ 1 6 8 9 ]
Sum : 73``````
``````package main
import "fmt"
/*
Go program
Sum the maximum 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 Array Elements \n [")
for i := 0 ; i < n ; i++ {
fmt.Print(" ", arr[i])
}
fmt.Print(" ]\n")
}
func(this Subarrays) maxSubArrayValueSum(arr[] int, n int) {
var result int = 0
for i := 0 ; i < n ; i++ {
// Calculate resultant value
result += arr[i] * (i + 1)
}
// 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)
// arr = [1, 2, 3, 4]
// [ 1 ] ---> 1
// [ 1 2 ] ---> 2
// [ 1 2 3 ] ---> 3
// [ 1 2 3 4 ] ---> 4
// [ 2 ] ---> 2
// [ 2 3 ] ---> 3
// [ 2 3 4 ] ---> 4
// [ 3 ] ---> 3
// [ 3 4 ] ---> 4
// [ 4 ] ---> 4
// [1+2+3+4+2+3+4+3+4+4] = 30
// Get the size
size = len(arr2)
// arr = [1, 6, 8, 9]
// [ 1 ]---> 1
// [ 1 6 ]---> 6
// [ 1 6 8 ]---> 8
// [ 1 6 8 9 ]---> 9
// [ 6 ]---> 6
// [ 6 8 ]---> 8
// [ 6 8 9 ]---> 9
// [ 8 ]---> 8
// [ 8 9 ]---> 9
// [ 9 ]---> 9
// [1+6+8+9+6+8+9+8+9+9] = 73
}``````

#### Output

`````` Array Elements
[ 1 2 3 4 ]
Sum : 30
Array Elements
[ 1 6 8 9 ]
Sum : 73``````
``````<?php
/*
Php program
Sum the maximum element of all subarrays of a sorted array
*/
class Subarrays
{
// Display array elements
public	function display(\$arr, \$n)
{
echo("\n Array Elements \n [");
for (\$i = 0; \$i < \$n; ++\$i)
{
echo(" ".\$arr[\$i]);
}
echo(" ]\n");
}
public	function maxSubArrayValueSum(\$arr, \$n)
{
\$result = 0;
for (\$i = 0; \$i < \$n; ++\$i)
{
// Calculate resultant value
\$result += \$arr[\$i] * (\$i + 1);
}
// Display calculated result
echo(" Sum : ".\$result);
}
}

function main()
{
// Sorted Array
\$arr1 = array(1, 2, 3, 4);
\$arr2 = array(1, 6, 8, 9);
// Get the size
\$size = count(\$arr1);
// arr = [1, 2, 3, 4]
// [ 1 ] ---> 1
// [ 1 2 ] ---> 2
// [ 1 2 3 ] ---> 3
// [ 1 2 3 4 ] ---> 4
// [ 2 ] ---> 2
// [ 2 3 ] ---> 3
// [ 2 3 4 ] ---> 4
// [ 3 ] ---> 3
// [ 3 4 ] ---> 4
// [ 4 ] ---> 4
// [1+2+3+4+2+3+4+3+4+4] = 30
// Get the size
\$size = count(\$arr2);
// arr = [1, 6, 8, 9]
// [ 1 ]---> 1
// [ 1 6 ]---> 6
// [ 1 6 8 ]---> 8
// [ 1 6 8 9 ]---> 9
// [ 6 ]---> 6
// [ 6 8 ]---> 8
// [ 6 8 9 ]---> 9
// [ 8 ]---> 8
// [ 8 9 ]---> 9
// [ 9 ]---> 9
// [1+6+8+9+6+8+9+8+9+9] = 73
}
main();``````

#### Output

`````` Array Elements
[ 1 2 3 4 ]
Sum : 30
Array Elements
[ 1 6 8 9 ]
Sum : 73``````
``````/*
Node JS program
Sum the maximum element of all subarrays of a sorted array
*/
class Subarrays
{
// Display array elements
display(arr, n)
{
process.stdout.write("\n Array Elements \n [");
for (var i = 0; i < n; ++i)
{
process.stdout.write(" " + arr[i]);
}
process.stdout.write(" ]\n");
}
maxSubArrayValueSum(arr, n)
{
var result = 0;
for (var i = 0; i < n; ++i)
{
// Calculate resultant value
result += arr[i] * (i + 1);
}
// Display calculated result
process.stdout.write(" Sum : " + result);
}
}

function main()
{
// Sorted Array
var arr1 = [1, 2, 3, 4];
var arr2 = [1, 6, 8, 9];
// Get the size
var size = arr1.length;
// arr = [1, 2, 3, 4]
// [ 1 ] ---> 1
// [ 1 2 ] ---> 2
// [ 1 2 3 ] ---> 3
// [ 1 2 3 4 ] ---> 4
// [ 2 ] ---> 2
// [ 2 3 ] ---> 3
// [ 2 3 4 ] ---> 4
// [ 3 ] ---> 3
// [ 3 4 ] ---> 4
// [ 4 ] ---> 4
// [1+2+3+4+2+3+4+3+4+4] = 30
// Get the size
size = arr2.length;
// arr = [1, 6, 8, 9]
// [ 1 ]---> 1
// [ 1 6 ]---> 6
// [ 1 6 8 ]---> 8
// [ 1 6 8 9 ]---> 9
// [ 6 ]---> 6
// [ 6 8 ]---> 8
// [ 6 8 9 ]---> 9
// [ 8 ]---> 8
// [ 8 9 ]---> 9
// [ 9 ]---> 9
// [1+6+8+9+6+8+9+8+9+9] = 73
}
main();``````

#### Output

`````` Array Elements
[ 1 2 3 4 ]
Sum : 30
Array Elements
[ 1 6 8 9 ]
Sum : 73``````
``````#    Python 3 program
#    Sum the maximum element of all subarrays of a sorted array
class Subarrays :
#  Display list elements
def display(self, arr, n) :
print("\n Array Elements \n [", end = "")
i = 0
while (i < n) :
print(" ", arr[i], end = "")
i += 1

print(" ]")

def maxSubArrayValueSum(self, arr, n) :
result = 0
i = 0
while (i < n) :
#  Calculate resultant value
result += arr[i] * (i + 1)
i += 1

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

def main() :
#  Sorted Array
arr1 = [1, 2, 3, 4]
arr2 = [1, 6, 8, 9]
#  Get the size
size = len(arr1)
#  arr = [1, 2, 3, 4]
#  [ 1 ] ---> 1
#  [ 1 2 ] ---> 2
#  [ 1 2 3 ] ---> 3
#  [ 1 2 3 4 ] ---> 4
#  [ 2 ] ---> 2
#  [ 2 3 ] ---> 3
#  [ 2 3 4 ] ---> 4
#  [ 3 ] ---> 3
#  [ 3 4 ] ---> 4
#  [ 4 ] ---> 4
#  [1+2+3+4+2+3+4+3+4+4] = 30
#  Get the size
size = len(arr2)
#  arr = [1, 6, 8, 9]
#  [ 1 ]---> 1
#  [ 1 6 ]---> 6
#  [ 1 6 8 ]---> 8
#  [ 1 6 8 9 ]---> 9
#  [ 6 ]---> 6
#  [ 6 8 ]---> 8
#  [ 6 8 9 ]---> 9
#  [ 8 ]---> 8
#  [ 8 9 ]---> 9
#  [ 9 ]---> 9
#  [1+6+8+9+6+8+9+8+9+9] = 73

if __name__ == "__main__": main()``````

#### Output

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

print(" ]\n")
end

def maxSubArrayValueSum(arr, n)
result = 0
i = 0
while (i < n)
#  Calculate resultant value
result += arr[i] * (i + 1)
i += 1
end

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

end

def main()
#  Sorted Array
arr1 = [1, 2, 3, 4]
arr2 = [1, 6, 8, 9]
#  Get the size
size = arr1.length
#  arr = [1, 2, 3, 4]
#  [ 1 ] ---> 1
#  [ 1 2 ] ---> 2
#  [ 1 2 3 ] ---> 3
#  [ 1 2 3 4 ] ---> 4
#  [ 2 ] ---> 2
#  [ 2 3 ] ---> 3
#  [ 2 3 4 ] ---> 4
#  [ 3 ] ---> 3
#  [ 3 4 ] ---> 4
#  [ 4 ] ---> 4
#  [1+2+3+4+2+3+4+3+4+4] = 30
#  Get the size
size = arr2.length
#  arr = [1, 6, 8, 9]
#  [ 1 ]---> 1
#  [ 1 6 ]---> 6
#  [ 1 6 8 ]---> 8
#  [ 1 6 8 9 ]---> 9
#  [ 6 ]---> 6
#  [ 6 8 ]---> 8
#  [ 6 8 9 ]---> 9
#  [ 8 ]---> 8
#  [ 8 9 ]---> 9
#  [ 9 ]---> 9
#  [1+6+8+9+6+8+9+8+9+9] = 73
end

main()``````

#### Output

`````` Array Elements
[ 1 2 3 4 ]
Sum : 30
Array Elements
[ 1 6 8 9 ]
Sum : 73``````
``````/*
Scala program
Sum the maximum element of all subarrays of a sorted array
*/
class Subarrays()
{
// Display array elements
def display(arr: Array[Int], n: Int): Unit = {
print("\n Array Elements \n [");
var i: Int = 0;
while (i < n)
{
print(" " + arr(i));
i += 1;
}
print(" ]\n");
}
def maxSubArrayValueSum(arr: Array[Int], n: Int): Unit = {
var result: Int = 0;
var i: Int = 0;
while (i < n)
{
// Calculate resultant value
result += arr(i) * (i + 1);
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;
// arr = [1, 2, 3, 4]
// [ 1 ] ---> 1
// [ 1 2 ] ---> 2
// [ 1 2 3 ] ---> 3
// [ 1 2 3 4 ] ---> 4
// [ 2 ] ---> 2
// [ 2 3 ] ---> 3
// [ 2 3 4 ] ---> 4
// [ 3 ] ---> 3
// [ 3 4 ] ---> 4
// [ 4 ] ---> 4
// [1+2+3+4+2+3+4+3+4+4] = 30
// Get the size
size = arr2.length;
// arr = [1, 6, 8, 9]
// [ 1 ]---> 1
// [ 1 6 ]---> 6
// [ 1 6 8 ]---> 8
// [ 1 6 8 9 ]---> 9
// [ 6 ]---> 6
// [ 6 8 ]---> 8
// [ 6 8 9 ]---> 9
// [ 8 ]---> 8
// [ 8 9 ]---> 9
// [ 9 ]---> 9
// [1+6+8+9+6+8+9+8+9+9] = 73
}
}``````

#### Output

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

#### Output

`````` Array Elements
[  1  2  3  4 ]
Sum :  30
Array Elements
[  1  6  8  9 ]
Sum :  73``````
``````/*
Kotlin program
Sum the maximum element of all subarrays of a sorted array
*/
class Subarrays
{
// Display array elements
fun display(arr: Array < Int > , n: Int): Unit
{
print("\n Array Elements \n [");
var i: Int = 0;
while (i < n)
{
print(" " + arr[i]);
i += 1;
}
print(" ]\n");
}
fun maxSubArrayValueSum(arr: Array < Int > , n: Int): Unit
{
var result: Int = 0;
var i: Int = 0;
while (i < n)
{
// Calculate resultant value
result += arr[i] * (i + 1);
i += 1;
}
// Display calculated result
print(" Sum : " + result);
}
}
fun main(args: Array < String > ): Unit
{
// 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();
// arr = [1, 2, 3, 4]
// [ 1 ] ---> 1
// [ 1 2 ] ---> 2
// [ 1 2 3 ] ---> 3
// [ 1 2 3 4 ] ---> 4
// [ 2 ] ---> 2
// [ 2 3 ] ---> 3
// [ 2 3 4 ] ---> 4
// [ 3 ] ---> 3
// [ 3 4 ] ---> 4
// [ 4 ] ---> 4
// [1+2+3+4+2+3+4+3+4+4] = 30
// Get the size
size = arr2.count();
// arr = [1, 6, 8, 9]
// [ 1 ]---> 1
// [ 1 6 ]---> 6
// [ 1 6 8 ]---> 8
// [ 1 6 8 9 ]---> 9
// [ 6 ]---> 6
// [ 6 8 ]---> 8
// [ 6 8 9 ]---> 9
// [ 8 ]---> 8
// [ 8 9 ]---> 9
// [ 9 ]---> 9
// [1+6+8+9+6+8+9+8+9+9] = 73
}``````

#### Output

`````` Array Elements
[ 1 2 3 4 ]
Sum : 30
Array Elements
[ 1 6 8 9 ]
Sum : 73``````

## Comment

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.