1077C - Good Array - CodeForces Solution


*1300

Please click on ads to support us..

Python Code:

n = int(input())
arr = list(map(int, input().split()))
mx1 = arr.index(max(arr))
x = max(arr)
arr[mx1] = 0
mx2 = arr.index(max(arr))
arr[mx1] = x
s = sum(arr)
ans = []
for i in range(n):
    if i == mx1:
        if s - arr[mx1] - arr[mx2] == arr[mx2]:
            ans.append(i + 1)
    else:
        if s - arr[i] - arr[mx1] == arr[mx1]:
            ans.append(i + 1)
print(len(ans))
print(*ans)

C++ Code:

#include<iostream>
#include<string>
#include<algorithm>
#include<iomanip>
#include<math.h>
#include<utility>
#include <vector>
using namespace std;


int main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    long long  n, sum = 0;
    cin >> n;
   vector<long long > a(n), f(n), arr(n);
    for (int y = 0; y < n; ++y)
    {
        cin >> a[y];
        f[y] = a[y];
        sum += a[y];
    }
    long long b,mm=0;
    bool k = 0;
    sort(f.begin(), f.end());
    for (int y = 0; y < n; ++y)
    {
        long long  h = sum - a[y];
        b = h / 2;
       
        if (h % 2 == 0)
        {
            int count = upper_bound(f.begin(), f.end(), b) - lower_bound(f.begin(), f.end(), b);
           
            if ((binary_search(f.begin(), f.end(), b) && (b != a[y] ||count>=2)))
            {
                a[y] = 0;
                k = 1;
                arr[mm++] = y+1;
            }
        }
    }
    if (k)
    {
        cout << mm << "\n";
        for (int i = 0; i < mm; ++i)
        {
            cout << arr[i] << " ";
        }
       
    }
    else
    {
        sum = 0;
        cout << sum << "\n";
    }
}


Comments

Submit
0 Comments
More Questions

1609A - Divide and Multiply
149B - Martian Clock
205A - Little Elephant and Rozdil
1609B - William the Vigilant
978B - File Name
1426B - Symmetric Matrix
732B - Cormen --- The Best Friend Of a Man
1369A - FashionabLee
1474B - Different Divisors
1632B - Roof Construction
388A - Fox and Box Accumulation
451A - Game With Sticks
768A - Oath of the Night's Watch
156C - Cipher
545D - Queue
459B - Pashmak and Flowers
1538A - Stone Game
1454C - Sequence Transformation
165B - Burning Midnight Oil
17A - Noldbach problem
1350A - Orac and Factors
1373A - Donut Shops
26A - Almost Prime
1656E - Equal Tree Sums
1656B - Subtract Operation
1656A - Good Pairs
1367A - Short Substrings
87A - Trains
664A - Complicated GCD
1635D - Infinite Set