Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //ra = [0, 1, 2, 3, 4]
- ra = [67, 1, 2, 3, 4, 5, 6]
- //ra = [2, 1]
- if(ra.size() <= 1) {
- println "Cant say"
- return
- }
- // lets do a binary search to find the point where
- // we can get into the sequence which is greater than the rest of the array
- for(place = ra.size() - 1; place > 0; place = (int)(place/2)) {
- if(ra[place] > ra[ra.size()-1]) {
- //from this point iterate until the order is maintained
- for(;ra[place] > ra[ra.size()-1]; place++) {
- }
- break;
- }
- }
- if(place == 0 && ra[0] > ra[ra.size() - 1]) { //edge case
- println "Right rotated by 1"
- }
- else if(place == 0) {
- println "Can't say"
- } else {
- println "Right rotated by ${place}"
- }
Add Comment
Please, Sign In to add comment