Algorithm


Problem link- https://www.spoj.com/problems/STREETR/

STREETR - Street Trees

no tags 

 

A group of trees is planted along a straight line. KOI is planning to plant more trees so that the distance between two adjacent trees is equal for all trees. For simplicity, each tree can only be planted on an integer coordinate.

For example, if 4 trees were originally planted on coordinates (1, 3, 7, 13), and if KOI plants 3 more trees on coordinates (5, 9, 11), then the distance between two adjacent trees will equal for all trees.

Your task is to calculate the minimal number of trees that KOI can plant so that the distance between two adjacent trees will equal for all trees.

Input

The first line is an integer N (3 <= N <= 100,000), which denotes the number of already planted trees.

The next N lines will have an integer X (1 <= X <= 1,000,000,000), which denotes the coordinate of each tree. 

You can safely assume that the value of X will be unique. 

Output

Output the minimal number of trees that must be planted.

Example

Input:
4
1
3
7
13

Output:
3
Input:
4
2
6
12
18

Output:
5

[Edited] Warning: Some input file contains garbage at the end

 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <map>
#include <list>

using namespace std;

/*int gcd(int a,int b){
	if(b==0)return a;
	return gcd(b,a%b);
}*/

int main(){
		int x;
		scanf("%d",&x);
		int v[x];
		for(int i=0;i<x;i++){
			scanf("%d",&v[i]);
		}
		int value=(v[1]-v[0]);
		for(int i=1;i+1<x;i++){
			value=__gcd(value,v[i+1]-v[i]);
		}
		int ans=0;
		for(int i=0;i+1<x;i++){
			ans+=(v[i+1]-v[i])/value-1;
		}
		printf("%d\n",ans);
	return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
4
1
3
7
13

Output

x
+
cmd
3
Advertisements

Demonstration


SPOJ Solution-Street Trees-Solution in C, C++, Java, Python

Previous
SPOJ Solution - Test Life, the Universe, and Everything - Solution in C, C++, Java, Python