Algorithm
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
#define debug(n) cout<<(n)<<endl;
const ll INF = 2e18 + 99;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t--){
ll n;
cin>>n;
ll arr[n];
for(ll i = 0; i < n; i++){
cin>>arr[i];
}
sort(arr,arr+n);
ll count = 0;
for(ll i = 1; i < n; i++){
count = __gcd(count, arr[i] - arr[i-1]);
}
if(count == 0){
cout<<-1<<endl;
continue;
}
cout<<count<<endl;
}
}
Copy The Code &
Try With Live Editor
Input
3
6
1 5 3 1 1 5
8
-1 0 1 -1 0 1 -1 0
4
100 -1000 -1000 -1000
6
1 5 3 1 1 5
8
-1 0 1 -1 0 1 -1 0
4
100 -1000 -1000 -1000
Output
2
1
1100
1
1100
Demonstration
Codeforcess Solution 1593-D1 D1. All are Same ,C++, Java, Js and Python ,1593-D1,Codeforcess Solution