CH01SA-EDITORIAL

PROBLEM LINK : CodeChef: Practical coding for everyone
practice
Contest

Author : salik_anwer
Tester : salik_anwer
Editorialist : mansha15k

DIFFICULTY :
Easy to Medium

PREREQUISITES :
NA

PROBLEM :
The Avengers and their allies are currently fighting Thanos in a grand battle. Avengers have found a way to defeat Thanos by combining the energy of his attacks with their own energy. There are “n” Avengers, Energy of the "i"th avenger is “E[i]”. If Thanos attacks “x” times, each time, energy of "i"th avenger becomes E[i] | E[i-1], where 1<=i<=n. " | " represents bitwise OR. Print energy of all Avengers after x attacks from Thanos.

QUICK EXPLANATION:
Store the new energies of each avenger back in the array for x no. of times. Display the final array .

EXPLANATION :
For each test case (t), take inputs of “n” and “x” ; followed by the initial energy of each avenger.
New energy of i th avenger = E[i] | E[i-1]; new energy of E[0] remains unchanged.
This is repeated for the no. of attacks of thanos (x), after which the loop terminates.
Final energies are printed.

TIME COMPLEXITY :
O(txn)

SOLUTIONS :
Editorialist’s Solution

#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t–)
{
int n;
int x;
cin>>n>>x;
int E[n],b[n];
for(int i=0;i<n;i++)
cin>>E[i];
while(x)
{
for(int i=0;i<n;i++)
{
if(i==0)
b[i]=E[i];
else
b[i]=(E[i]|E[i-1]);
}
for(int k=0;k<n;k++)
E[k]=b[k];
x–;
}
for(int i=0;i<n;i++)
cout<<b[i]<<" ";

    	    cout<<endl;
    	}
    	return 0;

}
Feel Free to share and discuss your approach here.:smiling_face: