Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2020
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. NSMutableArray *array = [[NSMutableArray alloc]initWithArray:q];
  2.    
  3.     int bribeCounter = 0;
  4.    
  5.     for(int i=0;i<array.count;i++){
  6.         if([array[i] intValue] > i+3){
  7.             printf("Too chaotic\n");
  8.             return;
  9.         }
  10.     }
  11.    
  12.     //Loop backwards
  13.     for(int j=array.count-1;j>=0;j--){
  14.        
  15.         //If value at index doesn't equal what it should
  16.         while([array[j] intValue] != j+1){
  17.            
  18.             //loop down until we find value
  19.             for(int k=j-1; k>=0; k--){
  20.                
  21.                 //When we find value we're looking for, swap it forward one, repeat
  22.                 if([array[k]intValue] == j+1){
  23.                     NSNumber *x = array[k];
  24.                     NSNumber *y = array[k+1];
  25.                     array[k] = y;
  26.                     array[k+1] = x;
  27.                     bribeCounter++;
  28.                     break;
  29.                 }
  30.             }
  31.         }
  32.     }
  33.     printf("%d\n",bribeCounter);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement