Guest User

Untitled

a guest
Jun 25th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 0.71 KB | None | 0 0
  1. //ra = [0, 1, 2, 3, 4]
  2. ra = [67, 1, 2, 3, 4, 5, 6]
  3. //ra = [2, 1]
  4.  
  5. if(ra.size() <= 1) {
  6.     println "Cant say"
  7.     return
  8. }
  9.  
  10. // lets do a binary search to find the point where
  11. // we can get into the sequence which is greater than the rest of the array
  12.  
  13. for(place = ra.size() - 1; place > 0; place = (int)(place/2)) {
  14.     if(ra[place] > ra[ra.size()-1]) {
  15.         //from this point iterate until the order is maintained
  16.         for(;ra[place] > ra[ra.size()-1]; place++) {
  17.         }
  18.         break;
  19.     }
  20. }
  21.  
  22. if(place == 0 && ra[0] > ra[ra.size() - 1]) { //edge case
  23.     println "Right rotated by 1"
  24. }
  25. else if(place == 0) {
  26.     println "Can't say"
  27. } else {
  28.     println "Right rotated by ${place}"
  29. }
Add Comment
Please, Sign In to add comment