Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

Minimizing the dot product codechef in c++

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

class solution
{
public:
	void dot_product()
	{
		int n;
		cin >> n;

		vector<int> v1(n);
		vector<int> v2(n);

		for (int i = 0; i < n; i++) cin >> v1[i];
		for (int i = 0; i < n; i++) cin >> v2[i];

		sort(v1.begin(), v1.end());
		sort(v2.begin(), v2.end(), greater<int>());

		int sum = 0;
		for (int i = 0; i < n; i++)
		{
			sum += (v1[i] * v2[i]);
		}
		cout << sum << "
";
	}
};

int main()
{
	solution ss;

	int t;
	cin >> t;

	while (t--)
	{
		ss.dot_product();
	}
	
	return 0;
}
Source by www.codegrepper.com #
 
PREVIOUS NEXT
Tagged: #Minimizing #dot #product #codechef
ADD COMMENT
Topic
Name
2+8 =