Algorithm
Problem link- https://www.spoj.com/problems/SCPC11B/
SCPC11B - Alaska
The Alaska Highway runs 1422 miles from Dawson Creek, British Columbia to Delta Junction, Alaska.
Brenda would like to be the first person to drive her new electric car the length of the highway. Her car can travel up to 200 miles once charged at a special charging station. There is a charging station in Dawson Creek, where she begins her journey, and also several charging stations along the way. Can Brenda drive her car from Dawson City to Delta Junction and back?
Input
The input contains several scenario. Each scenario begins with a line containing n, a positive number indicating the number of charging stations. n lines follow, each giving the location of a filling station on the highway, including the one in Dawson City. The location is an integer between 0 and 1422, inclusive, indicating the distance in miles from Dawson Creek. No two filling stations are at the same location. A line containing 0 follows the last scenario.
Output
For each scenario, output a line containing POSSIBLE if Brenda can make the trip.
Otherwise, output a line containing the word IMPOSSIBLE.
Example
Input: 2 0 900 8 1400 1200 1000 800 600 400 200 0 0 Output: IMPOSSIBLE POSSIBLE
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 main(){
int n;
while(true){
bool possible=true;
scanf("%d",&n);
if(n==0)break;
int route[n];
for(int i=0;i<n;i++){
scanf("%d",&route[i]);
}
sort(route,route+n);
int prev=0;
for(int i=0;i<n;i++){
if(route[i]-prev>200){
possible=false;
break;
}
prev=route[i];
}
if(1422-route[n-1]>100){
possible=false;
}
if(possible)
printf("POSSIBLE\n");
else
printf("IMPOSSIBLE\n");
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
0
900
8
1400
1200
1000
800
600
400
200
0
0
Output
POSSIBLE
Demonstration
SPOJ Solution-Alaska-Solution in C, C++, Java, Python