HLPCHEF- Editorial

PROBLEM LINK:

Practice
Contest

Author: Setter’s name
Tester: Tester’s name

DIFFICULTY:

CAKEWALK,EASY.

PREREQUISITES:

Simple Array and Loops.

PROBLEM:

Baby Chef has an array A of n numbers. Output the array B where B[i] is the average of first i numbers in the array A.

QUICK EXPLANATION:

You can simply apply set an iterator i and calculate average of the array till that iterator and store it at position B[i].

EXPLANATION:

  1. First we will set an iterator that will iterate throughout the length of the array.
  2. Maintain a variable named sum.
  3. Iterating the array update the value of sum by operation sum = sum+ a[i].
  4. Finally store average in array B by B[i] = sum/(i+1).

SOLUTIONS:

Setter's Solution
#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		cin>>n;
		vector<int>v;
		int a;
		vector<int>v1;
		for(int i=0;i<n;i++)
		{
			cin>>a;
			v.push_back(a);
		}
		int i = 1;
		while(i<=n)
		{
		int avg=0;
		int sum=0;
			for(int x=0;x<i;x++)
				sum=sum+v[x];
			avg=(sum)/i;
			v1.emplace_back(avg);
			i++;
		}
		for(int i:v1)
		cout<<i<<" ";
		cout<<"\n";
	
	}
	
	return 0;
}
Tester's Solution
t = int(input())
for x in range(0,t):
	size = int(input())
	arr = map(int,input().split())
	c = 1
	sumn =0
	for x in arr:
		sumn +=x
		print(sumn//c,end=" ")
		c=c+1
	print()