1708D - Difference Array - CodeForces Solution


brute force sortings *1900

Please click on ads to support us..

C++ Code:

// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>

#define endl "\n"
#define x first
#define y second

using namespace std;
typedef long long ll;
typedef pair<int,int> PII;

const int N = 1000010;

int a[N];
int T,n,m;
int dx[4] = {0,1,-1,0},dy[4] = {1,0,0,-1};

void solve(){
	vector<int> a,b;
	cin >> n;
	int cnt = 0;
	for(int i=1;i<=n;i++){
		int x;
		cin >> x;
		if(!x) cnt++;
		else a.push_back(x);
	}
	while(a.size()>1){
		b.clear();
		if(cnt) b.push_back(a[0]),cnt--;
		for(int i=1;i<a.size();i++){
			int d = a[i] - a[i-1];
			if(!d) cnt++;
			else b.push_back(d);
		}
		sort(b.begin(),b.end());
		a = b;
	}
	if(!a.size()) cout << 0 << endl;
	else cout << a[0] << endl;
}

int main()
{
	ios_base::sync_with_stdio(false), cin.tie(nullptr);
	cin >> T;
	while(T--)
	solve();
	
    return 0;
}


Comments

Submit
0 Comments
More Questions

1478A - Nezzar and Colorful Balls
1581B - Diameter of Graph
404A - Valera and X
908A - New Year and Counting Cards
146A - Lucky Ticket
1594C - Make Them Equal
1676A - Lucky
1700B - Palindromic Numbers
702C - Cellular Network
1672C - Unequal Array
1706C - Qpwoeirut And The City
1697A - Parkway Walk
1505B - DMCA
478B - Random Teams
1705C - Mark and His Unfinished Essay
1401C - Mere Array
1613B - Absent Remainder
1536B - Prinzessin der Verurteilung
1699B - Almost Ternary Matrix
1545A - AquaMoon and Strange Sort
538B - Quasi Binary
424A - Squats
1703A - YES or YES
494A - Treasure
48B - Land Lot
835A - Key races
1622C - Set or Decrease
1682A - Palindromic Indices
903C - Boxes Packing
887A - Div 64