7831. Sum without maximal

 

The array of n integers is given. Find the sum of all elements in the array that are not equal to the maximum element.

 

Input. The first line contains one integer n (n ≤ 100). The second line contains n integers, each with an absolute value not exceeding 100.

 

Output. Print the sum of all elements in the array that are not equal to the maximum element.

 

Sample input

Sample output

5

5 9 3 4 6

18

 

 

SOLUTION

arrays

 

Algorithm analysis

Let’s find the maximum element of the array. Then compute the sum of all numbers that are not equal to the maximum. Note that there can be multiple occurrences of the maximum element in the array.

 

Example

The largest element in the given example is 9.

The sum of elements not equal to the maximum is 5 + 3 + 4 + 6 = 18.

 

Algorithm implementation

Declare the array.

 

int m[101];

 

Read the input data.

 

scanf("%d",&n);

for(i = 0; i < n; i++)

  scanf("%d",&m[i]);

 

Compute the maximum element mx.

 

mx = -100;

for(i = 0; i < n; i++)

  if (m[i] > mx) mx = m[i];

 

Find the sum s of all elements in the array that are not equal to the maximum mx.

 

s = 0;

for(i = 0; i < n; i++)

 

If the current element m[i] is not equal to the maximum mx, add it to the sum s.

 

  if (m[i] != mx) s += m[i];

 

Print the sum.

 

printf("%d\n",s);

 

Algorithm implementation – STL

Read the input data.

 

scanf("%d", &n);

v.resize(n);

for (i = 0; i < n; i++)

  scanf("%d", &v[i]);

 

Compute the maximum element mx.

 

mx = *max_element(v.begin(), v.end());

 

Replace all occurrences of mx in the array with 0.

 

replace(v.begin(), v.end(), mx, 0);

 

Compute the sum of all elements in the modified array (with the maximum elements replaced by 0).

 

s = accumulate(v.begin(), v.end(), 0);

 

Print the answer – the sum of all elements excluding the maximum ones.

 

printf("%d\n", s);

 

Java implementation

 

import java.util.*;

 

public class Main

{

  public static void main(String[] args)

  {

    Scanner con = new Scanner(System.in);

    int n = con.nextInt();

    int m[] = new int[n];

    for (int i = 0; i < n; i++)

      m[i] = con.nextInt();

   

    int max = -100;

    for(int i = 0; i < n; i++)

      if (m[i] > max) max = m[i];

 

    int s = 0;

    for(int i = 0; i < n; i++)

      if (m[i] != max) s += m[i];

   

    System.out.println(s);

    con.close();

  }

}

 

Python implementation

Read the input data.

 

n = int(input())

lst = list(map(int,input().split()))

 

Find the maximum element mx in the list lst.

 

mx = max(lst)

 

In the variable sum compute the sum of all elements in the list lst that are not equal to the maximum value mx.

 

sum = 0

for x in lst:

  if x != mx: sum += x

 

Print the answer.

 

print(sum)

 

Python implementation summation

Read the input data.

 

n = int(input())

lst = list(map(int,input().split()))

 

Find the maximum element mx in the list lst.

 

mx = max(lst)

 

In the variable res compute the sum of all elements in the list lst that are not equal to the maximum value mx.

 

res = sum(x for x in lst if x != mx)

 

Print the answer.

 

print(res)