1

For each test case t, I need to input n, the number of elements in an array, and input those elements. Then I have to subtract the smallest element in the array from the sum of all the other elements. Here is my code, but I keep getting TLE:

#include <bits/stdc++.h>

int main(void) {
    int t;
    std::cin >> t;
    while (t --) {
        int n, sum = 0, a, k = 100000;
        std::cin >> n;
        while (n --) {
            std::cin >> a;
            if (a < k) {
                k = a;
            } else {
                sum += a;
            }
            n --;
        }
        std::cout << abs(sum - k) << "\n";
    }
}

Sample Input:

3
5
20 16 8 2 13
6
16 9 12 20 15 14
4
2 2 2 2

Sample Output:

55
68
4
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
code06
  • 35
  • 7
  • 1
    You call `n--` twice. If `n` starts as odd, then you'll go from 1 to -1, never hitting 0. That's the cause of your infinite loop. This does NOT address the correctness of your code, but that's a story... for another time. – JohnFilleau Nov 08 '20 at 16:00
  • You are not only substracting the min, but also all the intermediate min values. Calculate the global sum, and then substract twice the min. – Damien Nov 08 '20 at 16:00
  • This reads like it's from some contest/challenge/competitive coding/hacking site. Is it? If your goal is to learn C++, you won't learn anything there. In nearly all cases, like this one, the correct solution is based on a mathematical or a programming trick. If you don't know what the trick is and attempt to code a brute-force approach, the program runs slow and fails for that reason. If you're trying to learn C++, you won't learn anything from meaningless online contest sites [but only from a good C++ textbook](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Sam Varshavchik Nov 08 '20 at 16:08
  • @Damien Thanks a lot. I combined your idea with the stuff Vlad said and it worked. :D – code06 Nov 08 '20 at 16:44
  • `-->` goes-to operator could make your code more readable ;) – Kostas Nov 08 '20 at 17:13

1 Answers1

0

In this if statement

if (a < k) {

you are comparing each entered value with the current value of k and if it is less than k you are not adding it to sum.

For example for this sequence of numbers

20 16 8 2 13

20 is less than the initial value of k. So now k is equal to 20 and the number is not added to sum.. The next number 16 is also less than the current value of k. And again it is not added to sum.

You need to sum all numbers and at the same time find the minimum number. And then after the loop subtract the minimal number from sum.

Also this statement

 n --;

is redundant.

The program can look the following way

#include <iostream>

int main() 
{
    unsigned int t = 0;
    
    std::cin >> t;
    
    while ( t-- )
    {
        unsigned int n = 0;
        long long int sum = 0;
        int min = 0;
        bool first = true;
        
        std::cin >> n;
        
        while ( n-- )
        {
            int x;
            
            std::cin >> x;
            
            if ( first || x < min )
            {
                first = false;
                min = x;
            }
            sum += x;
        }
        
        std::cout << ( sum < min ? min - sum : sum - min ) << '\n';
    }
    
    return 0;
}

For the input

1
5
20 16 8 2 13

the program output is

57
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335