📌  相关文章
📜  首先连续增加然后减少的数组元素的总和

📅  最后修改于: 2021-04-27 21:40:36             🧑  作者: Mango

给定一个数组,其中元素首先连续增加,此后再次达到其连续减少的单位第一个数。我们要添加数组的元素。我们可以假设总和没有溢出。

例子:

Input  : arr[] = {5, 6, 7, 6, 5}.
Output : 29 

Input  : arr[] = {10, 11, 12, 13, 12, 11, 10}
Output : 79

一个简单的解决方案是遍历n并添加array的元素。

C++
// Simple C++ method to find sum of the
// elements of array.
#include 
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 code
int 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 decreasing
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 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 code
arr = [10, 11, 12, 13, 12, 11, 10]
n = len(arr)
print(arraySum(arr, n))
  
# This code is contributedc by "Abhishek Sharma 44"


C#
// C# Code for Sum of array elements
// that is first continuously increasing 
// then decreasing
using 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


C++
// Efficient C++ method to find sum of the
// elements of array that is halfway increasing
// and then halfway decreassing
#include 
using namespace std;
  
int arraySum(int arr[], int n)
{
    int x = (n+1)/2;
    return (arr[0] - 1)*n + x*x;
}
  
// Driver code
int 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 decreasing
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(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 decreassing
def arraySum( arr, n):
    x = (n + 1)/2
    return (arr[0] - 1)*n + x * x
      
# Driver code
arr = [10, 11, 12, 13, 12, 11, 10]
n = len(arr)
print(arraySum(arr, n))
  
# This code is contributedc by "Abhishek Sharma 44"


C#
// C# Code for Sum of array elements
// that is first continuously increasing 
// then decreasing
using 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


输出:

79

一个有效的解决方案是应用以下公式。

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

C++

// Efficient C++ method to find sum of the
// elements of array that is halfway increasing
// and then halfway decreassing
#include 
using namespace std;
  
int arraySum(int arr[], int n)
{
    int x = (n+1)/2;
    return (arr[0] - 1)*n + x*x;
}
  
// Driver code
int 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 decreasing
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(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 decreassing
def arraySum( arr, n):
    x = (n + 1)/2
    return (arr[0] - 1)*n + x * x
      
# Driver code
arr = [10, 11, 12, 13, 12, 11, 10]
n = len(arr)
print(arraySum(arr, n))
  
# This code is contributedc by "Abhishek Sharma 44"

C#

// C# Code for Sum of array elements
// that is first continuously increasing 
// then decreasing
using 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


输出:

79