greedy *800

Please click on ads to support us..

Python Code:

t = int(input())
for k in range(t):
    n = int(input())
    a = list(map(int, input().split(' ')))
    s = sum(a)
    if(s % n == 0 and s//n in a):
        print('YES')
    else:
        print('NO')
            
            

C++ Code:

#include<bits/stdc++.h>
#define int long long
#define endl '\n'
#define fi first
#define se second
#define rep(i,a,n) for(int i=a;i<n;i++)
#define per(i,a,n) for(int i=n-1;i>=a;i--)
#define pb push_back
#define yes {cout<<"Yes"<<endl;return;}
#define no {cout<<"No"<<endl;return;}
using namespace std;
const int mod = 1e9+7;
int a[55];
void solve(){
    int n;cin>>n;
    int sum=0;rep(i,1,n+1){
        cin>>a[i];sum+=a[i];
    }
    rep(i,1,n+1){
        if(sum-a[i]==(n-1)*a[i]){
            yes
        }
    }
    no
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int t=1;cin>>t;
    while(t--){
        solve();
    }
}


Comments

Submit
0 Comments
More Questions

436. Find Right Interval
435. Non-overlapping Intervals
406. Queue Reconstruction by Height
380. Insert Delete GetRandom O(1)
332. Reconstruct Itinerary
368. Largest Divisible Subset
377. Combination Sum IV
322. Coin Change
307. Range Sum Query - Mutable
287. Find the Duplicate Number
279. Perfect Squares
275. H-Index II
274. H-Index
260. Single Number III
240. Search a 2D Matrix II
238. Product of Array Except Self
229. Majority Element II
222. Count Complete Tree Nodes
215. Kth Largest Element in an Array
198. House Robber
153. Find Minimum in Rotated Sorted Array
150. Evaluate Reverse Polish Notation
144. Binary Tree Preorder Traversal
137. Single Number II
130. Surrounded Regions
129. Sum Root to Leaf Numbers
120. Triangle
102. Binary Tree Level Order Traversal
96. Unique Binary Search Trees
75. Sort Colors