Header Ad

HackerEarth Equal elements problem solution

In this HackerEarth Equal elements, problem-solution you are given an integer array A consisting of N elements. You can perform the following operations on array A:
  1. Choose any element and increase or decrease it by 3 for 1 coin.
  2. Choose any element and increase or decrease it by 2 for free.
You are required to spend the minimum number of coins in order to make all the elements in array A equal.


HackerEarth Equal elements problem solution


HackerEarth Equal elements problem solution.

#include<bits/stdc++.h>
using namespace std;
#define FIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define mod 1000000007
#define endl "\n"
#define test ll txtc; cin>>txtc; while(txtc--)
typedef long long int ll;
typedef long double ld;
int main() {
FIO;
test
{
ll n; cin>>n;
vector<ll>a(n);
ll odd=0;
for(int i=0;i<n;i++){
cin>>a[i];
odd+=(a[i]&1);
}
ll ans=min(odd,n-odd);
cout<<ans<<endl;
}
return 0;
}

Second solution

t = int(input())
while t > 0:
t -= 1
n = int(input())
a = list(map(int, input().split()))
print(min(sum(x % 2 == i for x in a) for i in range(2)))

Post a Comment

0 Comments