Hello Sir,
Problem : STREETR
Link : spoj.pl/problems/STREETR/
I tried it so many times but i am unable to understand why i am getting TLE
all the time please sir help me to solve this problem.
It will be great help for me thanx
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
using namespace std;
int gcd(int x,int y)
{
if(y>x)return gcd(y,x);
if(x==y)return x;
if(x%y==0)return y;
return gcd(x,x-y);
}
int gcdlist( int a[], int n ) {
int mygcd = 0;
mygcd = gcd( a[0], a[1]);
for( int i = 1; i < n; i++ ) {
mygcd = gcd( mygcd, a[i]);
}
return mygcd;
}
int main()
{
int n, k = 0, c = 0, cont = 0, min = 0;
int a[111111], b[111111];
cin >> n;
cin >> a[0];
k = a[0];
for( int i = 1; i < n; i++ ) {
cin >> a[i];
c = a[i] - k;
b[i-1] = c;
k = a[i];
}
min = gcdlist(b, n-1);
cont = (a[n-1] - a[0])/min + 1;
cout << cont - n << endl;
return 0;
}