Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- NSMutableArray *array = [[NSMutableArray alloc]initWithArray:q];
- int bribeCounter = 0;
- for(int i=0;i<array.count;i++){
- if([array[i] intValue] > i+3){
- printf("Too chaotic\n");
- return;
- }
- }
- //Loop backwards
- for(int j=array.count-1;j>=0;j--){
- //If value at index doesn't equal what it should
- while([array[j] intValue] != j+1){
- //loop down until we find value
- for(int k=j-1; k>=0; k--){
- //When we find value we're looking for, swap it forward one, repeat
- if([array[k]intValue] == j+1){
- NSNumber *x = array[k];
- NSNumber *y = array[k+1];
- array[k] = y;
- array[k+1] = x;
- bribeCounter++;
- break;
- }
- }
- }
- }
- printf("%d\n",bribeCounter);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement