1698C - 3SUM Closure - CodeForces Solution


brute force data structures *1300

Please click on ads to support us..

Python Code:

import sys
input = sys.stdin.readline
ii = lambda: int(input())
mi = lambda: map(int,input().split())
li = lambda: list(map(int,input().split()))
from collections import defaultdict,Counter,deque
from bisect import bisect_left,bisect_right
from heapq import heappush,heappop,heapify

for _ in range(ii()):
    n = ii()
    nums = li()
    pos, neg = 0, 0
    for num in nums:
        if num < 0: neg += 1
        if num > 0: pos += 1
    if len(nums) < 5:
        is3sum = True
        for i in range(len(nums)):
            for j in range(i+1,len(nums)):
                for k in range(j+1,len(nums)):
                    if nums[i] + nums[j] + nums[k] not in set(nums):
                        is3sum = False
        print('YES' if is3sum else 'NO')
    elif pos > 1 or neg > 1:
        print('NO')
    else:
        nums.sort()
        if pos == 1 and neg == 1 and -nums[0] != nums[-1]:
            print('NO')
        else:
            print('YES')
        

C++ Code:

#include <bits/stdc++.h>

#define ll long long
using namespace std;
const int N = 5e5 + 1;


int solve() {


    int n;
    cin >> n;
    vector<int> v;

    bool ans = 1, found_zero = 0;

    for (int i = 0; i < n; ++i) {
        int x;
        cin >> x;
        if (x != 0) {
            v.push_back(x);
        } else if (!found_zero) {
            found_zero = 1;
            v.push_back(0);
        }
    }
// -3 -2 2 0 3 -3 2 -4
    if (v.size() <= 5) {
        for (int i = 0; i < v.size(); ++i) {
            for (int j = i+1; j < v.size(); ++j) {
                for (int k = j+1; k < v.size(); ++k) {
                    if (find(v.begin(), v.end(), v[i] + v[j] + v[k]) == v.end()) {
                        ans = 0;
                    }
                }
            }
        }

    } else {
        ans = 0;
    }


    cout << (ans ? "YES" : "NO") << '\n';

    return 0;
}



int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int test_cases;
    cin >> test_cases;
    while (test_cases--) {
        solve();
    }

    return 0;
}


Comments

Submit
0 Comments
More Questions

1629A - Download More RAM
1629C - Meximum Array
1629D - Peculiar Movie Preferences
1629E - Grid Xor
1629F1 - Game on Sum (Easy Version)
2148. Count Elements With Strictly Smaller and Greater Elements
2149. Rearrange Array Elements by Sign
2150. Find All Lonely Numbers in the Array
2151. Maximum Good People Based on Statements
2144. Minimum Cost of Buying Candies With Discount
Non empty subsets
1630A - And Matching
1630B - Range and Partition
1630C - Paint the Middle
1630D - Flipping Range
1328A - Divisibility Problem
339A - Helpful Maths
4A - Watermelon
476A - Dreamoon and Stairs
1409A - Yet Another Two Integers Problem
977A - Wrong Subtraction
263A - Beautiful Matrix
180C - Letter
151A - Soft Drinking
1352A - Sum of Round Numbers
281A - Word Capitalization
1646A - Square Counting
266A - Stones on the Table
61A - Ultra-Fast Mathematician
148A - Insomnia cure