WESUM - Editorial

PROBLEM LINK: WESUM Problem - CodeChef

Practice
Contest

Author: Shreyansh Singh
Tester: Shreyansh Singh
Editorialist: Shreyansh Singh

DIFFICULTY:

CAKEWALK

PREREQUISITES:

Implementation, Math

PROBLEM:

Inputs are given until a -1 is encountered. The output must be the quantity of number that are greater than 30 followed by the fraction defined by the sum of the products of
even numbers with their respective indices divided by the sum of the indices of even numbers.

QUICK EXPLANATION:

Create a dynamic array. Keep adding to the dynamic array until a -1 is seen. Now iterate over the array, print the numbers greater that 30, calculate the sum of the indices of even numbers and print it.

EXPLANATION:

Create a dynamic array (vector in C++). Run a loop that keeps taking in inputs and breaks when a -1 is encountered. Keep pushing all the elements to the array. After that loop, initialize a sum variable to 0.
Run another loop, iterating over the elements of the array constructed previously. Inside that loop print any number which is greater than 30.
Also keep adding to the sum variable the product of the index (1 based) and the number when an even number is encountered. Along with that maintain a denominator variable that sums up all the indices of the even numbers. After the loop divide the numerator sum with the denominator sum, and print the answer maintaining the given precision.

SOLUTIONS:

Setter's Solution

#include <bits/stdc++.h>

using namespace std;

int main() {
int t;
cin >> t;
while(t–) {
int n;
vector v;
while(cin >> n) {
if(n == -1)
break;
v.push_back(n);
}
n = v.size();
int count = 0, sum = 0, den = 0;
for(int i = 0; i < n; ++i) {
int x = v[i];
if(x > 30)
++count;
if(x % 2 == 0) {
int y = i + 1;
den += y;
int z = y * x;
sum += z;
}
}
cout << count << " “;
printf(”%0.2f\n", (double) sum / den);
}
return 0;
}