Created
June 14, 2012 18:10
-
-
Save srinathgs/2931889 to your computer and use it in GitHub Desktop.
Solution to the Jolly Jumper problem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <vector> | |
| #include <sstream> | |
| using namespace std; | |
| int main() | |
| { | |
| string buf; | |
| int n; | |
| while(true){ | |
| getline(cin,buf); //read ONE full line | |
| if(buf.empty()){ | |
| break; | |
| } | |
| istringstream inp(buf); // input string stream to read the full line input | |
| inp>>n; | |
| vector<bool> diffNums(n,false); | |
| bool jollyJump = true; | |
| int prev,curr; | |
| inp>>prev; | |
| for(int i = 1;i < n;i++){ | |
| int diff; | |
| inp>>curr; | |
| if(!curr>0){ | |
| jollyJump = false; // number < 0 | |
| break; | |
| } | |
| diff = prev > curr ? prev - curr : curr - prev; // find the difference (We can use abs function in cmath) | |
| if(diff<=0 || diff >= n || diffNums[diff-1]){ | |
| jollyJump = false; // difference is not valid. | |
| break; | |
| } | |
| diffNums[diff-1] = true; | |
| prev = curr; | |
| } | |
| cout<< (jollyJump?"Jolly":"Not jolly") <<endl; //print result | |
| } | |
| return 0; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Problem statement here.
http://programming-challenges.com/pg.php?page=downloadproblem&probid=110201&format=html