Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

Maximum Pairwise Modular Sum codechef solution in c++

#include<iostream>
#define ll long long
#include<vector>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;

void solve()
{
	ll n, m;
	cin >> n >> m;
	vector<ll> v(n);
	for (ll i = 0; i < n; i++)
	{
		cin >> v[i];
	}
	sort(v.begin(), v.end());
	ll mx = INT_MIN;
	for (ll i = 0; i < n; i++)
	{
		mx = max(mx, v[i] - v[n - 1] + ((v[i] + v[n - 1] % m) + m));
	}
	cout << mx << "
";
}

int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		solve();
	}
	return 0;
}
Source by www.codegrepper.com #
 
PREVIOUS NEXT
Tagged: #Maximum #Pairwise #Modular #Sum #codechef #solution
ADD COMMENT
Topic
Name
6+1 =