Sum of array elements that is first continuously increasing then decreasing

Given an array where elements are first continuously increasing and after that its continuously decreasing unit first number is reached again. We want to add the elements of array. We may assume that there is no overflow in sum.
Examples:Â
Input : arr[] = {5, 6, 7, 6, 5}.
Output : 29
Input : arr[] = {10, 11, 12, 13, 12, 11, 10}
Output : 79
A simple solution is to traverse through n and add the elements of array.Â
Implementation:
C++
// Simple C++ method to find sum of the// elements of array.#include <iostream>using namespace std;int arraySum(int arr[], int n){Â Â Â Â int sum = 0;Â Â Â Â for (int i = 0; i < n; i++)Â Â Â Â Â Â Â Â sum = sum + arr[i];Â Â Â Â return sum;}Â
// Driver codeint main(){Â Â Â Â int arr[] = {10, 11, 12, 13, 12, 11, 10};Â Â Â Â int n = sizeof(arr) / sizeof(arr[0]);Â Â Â Â cout << arraySum(arr, n);Â Â Â Â return 0;} |
Java
// JAVA Code for Sum of array elements// that is first continuously increasing // then decreasingclass GFG {Â Â Â Â Â Â Â Â Â public static int arraySum(int arr[], int n)Â Â Â Â {Â Â Â Â Â Â Â Â int sum = 0;Â Â Â Â Â Â Â Â for (int i = 0; i < n; i++)Â Â Â Â Â Â Â Â Â Â Â Â sum = sum + arr[i];Â Â Â Â Â Â Â Â return sum;Â Â Â Â }Â Â Â Â Â Â Â Â Â /* Driver program to test above function */Â Â Â Â public static void main(String[] args) Â Â Â Â {Â Â Â Â Â Â Â Â int arr[] = {10, 11, 12, 13, 12, 11, 10};Â Â Â Â Â Â Â Â int n = arr.length;Â Â Â Â Â Â Â Â System.out.print(arraySum(arr, n));Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â }}// This code is contributed by Arnav Kr. Mandal. |
Python3
# Simple python method to find sum of the# elements of array.def arraySum( arr, n):Â Â Â Â _sum = 0Â Â Â Â for i in range(n):Â Â Â Â Â Â Â Â _sum = _sum + arr[i]Â Â Â Â return _sumÂ
# Driver codearr = [10, 11, 12, 13, 12, 11, 10]n = len(arr)print(arraySum(arr, n))Â
# This code is contributed by "Abhishek Sharma 44" |
C#
// C# Code for Sum of array elements// that is first continuously increasing // then decreasingusing System;Â
class GFG {         public static int arraySum(int []arr, int n)    {        int sum = 0;        for (int i = 0; i < n; i++)            sum = sum + arr[i];        return sum;    }         // Driver program    public static void Main()     {        int []arr = {10, 11, 12, 13, 12, 11, 10};        int n = arr.Length;        Console.WriteLine(arraySum(arr, n));                 }}// This code is contributed by vt_m. |
PHP
<?php// Simple PHP method // to find sum of the// elements of array.function arraySum($arr, $n){Â Â Â Â $sum = 0;Â Â Â Â for ($i = 0; $i < $n; $i++)Â Â Â Â Â Â Â Â $sum = $sum + $arr[$i];Â Â Â Â return $sum;}Â
// Driver code$arr = array(10, 11, 12, 13, Â Â Â Â Â Â Â Â Â Â Â Â Â 12, 11, 10);$n = sizeof($arr);echo(arraySum($arr, $n));Â
// This code is contributed by Ajit.?> |
Javascript
<script>Â Â Â Â // Simple Javascript method // to find sum of the// elements of array.function arraySum(arr, n){Â Â Â Â let sum = 0;Â Â Â Â for (let i = 0; i < n; i++)Â Â Â Â Â Â Â Â sum = sum + arr[i];Â Â Â Â return sum;}Â Â Â // Driver codelet arr = [10, 11, 12, 13, Â Â Â Â Â Â Â Â Â Â Â Â Â 12, 11, 10];let n = arr.length;document.write(arraySum(arr, n));Â Â Â // This code is contributed by _saurabh_jaiswal.</script> |
79
Time Complexity : O(n)
Space Complexity : O(1)
An efficient solution is to apply below formula.Â
sum = (arr[0] - 1)*n + ?n/2?2
How does it work?
If we take a closer look, we can notice that the
sum can be written as.
(arr[0] - 1)*n + (1 + 2 + .. x + (x -1) + (x-2) + ..1)
Let us understand above result with example {10, 11,
12, 13, 12, 11, 10}. If we subtract 9 (arr[0]-1) from
this array, we get {1, 2, 3, 2, 1}.
Where x = ceil(n/2) [Half of array size]
As we know that 1 + 2 + 3 + . . . + x = x * (x + 1)/2.
And we have given
= 1 + 2 + 3 + . . . + x + (x - 1) + . . . + 3 + 2 + 1
= (1 + 2 + 3 + . . . + x) + ((x - 1) + . . . + 3 + 2 + 1)
= (x * (x + 1))/2 + ((x - 1) * x)/2
= (x2 + x)/2 + (n2 - x)/2
= (2 * x2)/2
= x2
Implementation:
C++
// Efficient C++ method to find sum of the// elements of array that is halfway increasing// and then halfway decreasing#include <iostream>using namespace std;Â
int arraySum(int arr[], int n){Â Â Â Â int x = (n+1)/2;Â Â Â Â return (arr[0] - 1)*n + x*x;}Â
// Driver codeint main(){Â Â Â Â int arr[] = {10, 11, 12, 13, 12, 11, 10};Â Â Â Â int n = sizeof(arr) / sizeof(arr[0]);Â Â Â Â cout << arraySum(arr, n);Â Â Â Â return 0;} |
Java
// JAVA Code for Sum of array elements// that is first continuously increasing // then decreasingclass GFG {Â Â Â Â Â Â Â Â Â public static int arraySum(int arr[], int n)Â Â Â Â {Â Â Â Â Â Â Â Â int x = (n + 1) / 2;Â Â Â Â Â Â Â Â return (arr[0] - 1) * n + x * x;Â Â Â Â }Â Â Â Â Â Â Â Â Â /* Driver program to test above function */Â Â Â Â public static void main(String[] args) Â Â Â Â {Â Â Â Â Â Â Â Â int arr[] = {10, 11, 12, 13, 12, 11, 10};Â Â Â Â Â Â Â Â int n = arr.length;Â Â Â Â Â Â Â Â System.out.print(arraySum(arr, n));Â Â Â Â Â Â }}// This code is contributed by Arnav Kr. Mandal. |
Python3
# Efficient python method to find sum of the# elements of array that is halfway increasing# and then halfway decreasingdef arraySum( arr, n):    x = (n + 1)/2    return (arr[0] - 1)*n + x * x     # Driver codearr = [10, 11, 12, 13, 12, 11, 10]n = len(arr)print(arraySum(arr, n))Â
# This code is contributed by "Abhishek Sharma 44" |
C#
// C# Code for Sum of array elements// that is first continuously increasing // then decreasingusing System;Â
class GFG {Â Â Â Â Â Â Â Â Â public static int arraySum(int []arr, int n)Â Â Â Â {Â Â Â Â Â Â Â Â int x = (n + 1) / 2;Â Â Â Â Â Â Â Â return (arr[0] - 1) * n + x * x;Â Â Â Â }Â Â Â Â Â Â Â Â Â /* Driver program to test above function */Â Â Â Â public static void Main() Â Â Â Â {Â Â Â Â Â Â Â Â int []arr = {10, 11, 12, 13, 12, 11, 10};Â Â Â Â Â Â Â Â int n = arr.Length;Â Â Â Â Â Â Â Â Console.WriteLine(arraySum(arr, n)); Â Â Â Â }}Â
// This code is contributed by vt_m. |
PHP
<?php// Efficient PHP method to// find sum of the elements // of array that is halfway// increasing and then halfway// decreasingÂ
function arraySum($arr, $n){Â Â Â Â $x = ($n + 1) / 2;Â Â Â Â return ($arr[0] - 1) * Â Â Â Â Â Â Â Â Â Â Â Â $n + $x * $x;}Â
// Driver code$arr = array(10, 11, 12, 13,                12, 11, 10);$n = sizeof($arr);echo(arraySum($arr, $n));Â
// This code is contributed by Ajit.?> |
Javascript
// Efficient Javascript method to// find sum of the elements // of array that is halfway// increasing and then halfway// decreasing   function arraySum(arr, n){    let x = (n + 1) / 2;    return (arr[0] - 1) *             n + x * x;}   // Driver codelet arr = [10, 11, 12, 13,                12, 11, 10];let n = arr.length;document.write(arraySum(arr, n));   // This code is contributed by _saurabh_jaiswal. |
79
Time Complexity: O(1)
Auxiliary Space: O(1)
If you like zambiatek and would like to contribute, you can also write an article using write.zambiatek.com or mail your article to review-team@zambiatek.com. See your article appearing on the zambiatek main page and help other Geeks.Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



