Header Ad

HackerEarth The rise of the weird... things [1] problem solution

In this HackerEarth The rise of the weird... things [1] problem solution Bangalore City, where peace prevails most of the time. Not everyone is a huge fan of peace, though. Certainly not Mr. XYZ, whose identity is not known to us - yet. Mr. XYZ has somehow managed to bring vampires and zombies to Bangalore City to attack and destroy the city.

Fatal Eagle, an ordinary citizen of the city is extremely worried on seeing his city being attacked by these weird creatures. But, as of now, he has no power to stop these creatures from their silent attacks. He wants to analyze these creatures firstly. He figured out some things about these creatures, like:
  1. Zombies have power in terms of an EVEN number.
  2. Vampires have power in terms of an ODD number.
If he sees a zombie or a vampire, he marks them in his list with their power. After generating the entire list of power of these creatures, he decides to arrange this data in the following manner:
  • All the zombies arranged in sorted manner of their power, followed by the total power of zombies.
  • All the vampires arranged in sorted manner of their power, followed by the total power of vampires.
You've to help him produce the following list to help him save his city.


HackerEarth The rise of the weird... things [1] problem solution


HackerEarth The rise of the weird... things [1] problem solution.

n = int(raw_input())
l = map(int, raw_input().split())
od, ev = [], []
sod, sev = 0, 0
for i in l:
if i%2==0:
ev.append(i)
sev = sev + i
else:
od.append(i)
sod = sod + i
ev.append(sev)
od.append(sod)
ev, od = sorted(ev), sorted(od)
final = ev + od
for i in xrange(len(final)):
if i==len(final)-1:
print final[i]
else:
print final[i],

Second solution

#include <bits/stdc++.h>

void assert_digit() {char c = getchar(); assert('0' <= c && c <= '9'); ungetc(c, stdin);}

using namespace std;

int N;
vector<int> even, odd;

int main()
{
assert_digit();
scanf("%d", &N);
assert(getchar()=='\n');
assert(1<=N && N<=1000);
int a;
for(int i=0; i<N; i++)
{
assert_digit();
scanf("%d", &a);
if(i!=N-1)
assert(getchar()==' ');
assert(1<=a && a<=1000);
if(a%2==0)
even.push_back(a);
else
odd.push_back(a);
}
assert(getchar()==EOF);
sort(even.begin(), even.end());
sort(odd.begin(), odd.end());
for(auto& it: even)
printf("%d ", it);
printf("%d ", accumulate(even.begin(), even.end(), 0));
for(auto& it: odd)
printf("%d ", it);
printf("%d\n", accumulate(odd.begin(), odd.end(), 0));
return 0;
}

Post a Comment

0 Comments