Multiply one to another

Practice

Author: Aryan KD
Tester: Aryan KD
Editorialist: Aryan KD

DIFFICULTY:

CAKEWALK, SIMPLE, EASY.

PREREQUISITES:

Math .

PROBLEM:

In the program two integer arrays A1 and B1 of size N is given . you have to implement the program to find the product of element of A1 and B1 by Multiplying the first element of A1 with last element of B1 , second element of A1 with second last element of B1 and so on… then return the sum of all the products obtained .

EXPLANATION:

We have given 2 arrays in that array we have to multiply each other with a proper way and later print sum of it

suppose 2 array given of same size we have to first iterate first array from start and second array from last and multiply it with each other and print sum

for example 2 arrays given
n=3
2 1 3
1 2 3
in above example first array element will multiply by last element of second array
that is 2*3=6
1 will multiply by 2 and 3 with 1
and after it we will print sum of all these multiplication

SOLUTIONS:

Setter's Solution

#include<bits/stdc++.h>
using namespace std;
void testcase()
{
int n;
cin>>n;
int a[n];
int b[n];
for(int i=0 ;i<n ;i++){
cin>>a[i];
}
for(int i=0;i<n;i++){
cin>>b[i];
}
int i=0,j=n-1;
int sum=0;
while(i<n && j>=0){
sum+=a[i]*b[j];
i++,j–;
}
cout<<sum;

}
int main()
{
int t;
cin>>t;
while(t–)
{
testcase();
cout<<endl;
}
return 0;
}

Tester's Solution

#include<bits/stdc++.h>
using namespace std;
void testcase()
{
int n;
cin>>n;
int a[n];
int b[n];
for(int i=0 ;i<n ;i++){
cin>>a[i];
}
for(int i=0;i<n;i++){
cin>>b[i];
}
int i=0,j=n-1;
int sum=0;
while(i<n && j>=0){
sum+=a[i]*b[j];
i++,j–;
}
cout<<sum;

}
int main()
{
int t;
cin>>t;
while(t–)
{
testcase();
cout<<endl;
}
return 0;
}

Editorialist's Solution

#include<bits/stdc++.h>
using namespace std;
void testcase()
{
int n;
cin>>n;
int a[n];
int b[n];
for(int i=0 ;i<n ;i++){
cin>>a[i];
}
for(int i=0;i<n;i++){
cin>>b[i];
}
int i=0,j=n-1;
int sum=0;
while(i<n && j>=0){
sum+=a[i]*b[j];
i++,j–;
}
cout<<sum;

}
int main()
{
int t;
cin>>t;
while(t–)
{
testcase();
cout<<endl;
}
return 0;
}