My issue
If the array contains atleast one zero it should be possible to make the array equal.
For example
Array = [ 0 6 7 ]
Select i = 2 and i = 1 for 7 times => Array = [ 0 42 7 ]
Then i = 3 and i = 1 for 6 times => Array = [ 0 42 42 ]
Then i = 1 and i = 2 then array becomes [ 42 42 42 ]
Whats wrong with my approach please explain
My code
#include <bits/stdc++.h>
#define int long long
using namespace std;
signed main() {
int t;
cin >> t;
while(t--){
int n;
cin >> n;
int arr[n];
int equals = 0; int zeros = 0;
for(int i = 0 ; i < n ; i++){
cin>>arr[i];
}
int tmp = arr[0];
for(int i = 0 ; i < n ; i++){
if(arr[i] == 0) zeros++;
if(arr[i] == tmp) equals++;
}
// if(n == 1){
// cout << "YES" << endl;
// break;
// }
if( zeros >=1 || equals == n ) cout <<"YES" <<endl;
else cout << "NO" << endl;
}
return 0;
}
Problem Link: Make My Array Equal Practice Coding Problem