Cody

# Problem 1506. Maximum Intra-Vector Swaps

Solution 549826

Submitted on 25 Dec 2014
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

### Test Suite

Test Status Code Input and Output
1   Pass
tic feval(@assignin,'caller','score',2000);

2   Pass
%% % 2 1 global cseq cseq{1}=1; % Gift answer max_seq=find_max_swap_seq(2); assert(isequal(1:2,unique(max_seq))) cseq{2}=max_seq; seq=max_seq; count=0; while seq(1)>1 count=count+1; seq(1:seq(1))=seq(seq(1):-1:1); end fprintf('count %i\n',count) fprintf('%i ',max_seq) fprintf('\n')

count 0 1 2

3   Pass
%% % 3 2 global cseq max_seq=find_max_swap_seq(3); assert(isequal(1:3,unique(max_seq))) cseq{3}=max_seq; seq=max_seq; count=0; while seq(1)>1 count=count+1; seq(1:seq(1))=seq(seq(1):-1:1); end fprintf('count %i\n',count) fprintf('%i ',max_seq) fprintf('\n')

count 2 2 3 1

4   Pass
%% % 4 4 global cseq max_seq=find_max_swap_seq(4); assert(isequal(1:4,unique(max_seq))) cseq{4}=max_seq; seq=max_seq; count=0; while seq(1)>1 count=count+1; seq(1:seq(1))=seq(seq(1):-1:1); end fprintf('count %i\n',count) fprintf('%i ',max_seq) fprintf('\n')

count 1 4 2 3 1

5   Pass
%% % 5 7 global cseq max_seq=find_max_swap_seq(5); assert(isequal(1:5,unique(max_seq))) cseq{5}=max_seq; seq=max_seq; count=0; while seq(1)>1 count=count+1; seq(1:seq(1))=seq(seq(1):-1:1); end fprintf('count %i\n',count) fprintf('%i ',max_seq) fprintf('\n')

count 6 4 1 3 5 2

6   Pass
%% % 6 10 global cseq max_seq=find_max_swap_seq(6); assert(isequal(1:6,unique(max_seq))) cseq{6}=max_seq; seq=max_seq; count=0; while seq(1)>1 count=count+1; seq(1:seq(1))=seq(seq(1):-1:1); end fprintf('count %i\n',count) fprintf('%i ',max_seq) fprintf('\n')

count 1 6 5 3 4 2 1

7   Pass
%% % 7 16 global cseq max_seq=find_max_swap_seq(7); assert(isequal(1:7,unique(max_seq))) cseq{7}=max_seq; seq=max_seq; count=0; while seq(1)>1 count=count+1; seq(1:seq(1))=seq(seq(1):-1:1); end fprintf('count %i\n',count) fprintf('%i ',max_seq) fprintf('\n')

count 3 7 4 3 5 1 2 6

8   Pass
%% % 8 22 global cseq max_seq=find_max_swap_seq(8); assert(isequal(1:8,unique(max_seq))) cseq{8}=max_seq; seq=max_seq; count=0; while seq(1)>1 count=count+1; seq(1:seq(1))=seq(seq(1):-1:1); end fprintf('count %i\n',count) fprintf('%i ',max_seq) fprintf('\n')

count 5 3 8 4 7 6 2 5 1

9   Pass
%% % 9 30 global cseq max_seq=find_max_swap_seq(9); assert(isequal(1:9,unique(max_seq))) cseq{9}=max_seq; seq=max_seq; count=0; while seq(1)>1 count=count+1; seq(1:seq(1))=seq(seq(1):-1:1); end fprintf('count %i\n',count) fprintf('%i ',max_seq) fprintf('\n')

count 1 5 8 7 6 1 4 3 9 2

10   Pass
%% %10 38 global cseq max_seq=find_max_swap_seq(10); assert(isequal(1:10,unique(max_seq))) cseq{10}=max_seq; seq=max_seq; count=0; while seq(1)>1 count=count+1; seq(1:seq(1))=seq(seq(1):-1:1); end fprintf('count %i\n',count) fprintf('%i ',max_seq) fprintf('\n')

count 2 5 3 4 8 7 6 1 10 2 9

11   Pass
%% %11 51 global cseq max_seq=find_max_swap_seq(11); assert(isequal(1:11,unique(max_seq))) cseq{11}=max_seq; seq=max_seq; count=0; while seq(1)>1 count=count+1; seq(1:seq(1))=seq(seq(1):-1:1); end fprintf('count %i\n',count) fprintf('%i ',max_seq) fprintf('\n')

count 5 9 7 1 6 3 2 4 5 8 10 11

12   Pass
%% %13 80 case 12 global cseq max_seq=find_max_swap_seq(13); assert(isequal(1:13,unique(max_seq))) cseq{12}=max_seq; seq=max_seq; count=0; while seq(1)>1 count=count+1; seq(1:seq(1))=seq(seq(1):-1:1); end fprintf('count %i\n',count) fprintf('%i ',max_seq) fprintf('\n')

count 9 9 13 8 7 2 11 1 6 5 3 4 10 12

13   Pass
%% %17 159 case 13 global cseq max_seq=find_max_swap_seq(17); assert(isequal(1:17,unique(max_seq))) cseq{13}=max_seq; seq=max_seq; count=0; while seq(1)>1 count=count+1; seq(1:seq(1))=seq(seq(1):-1:1); end fprintf('count %i\n',count) fprintf('%i ',max_seq) fprintf('\n')

count 33 3 11 5 10 6 14 15 2 12 7 17 9 1 4 8 16 13

14   Pass
%% %19 221 case 14 global cseq max_seq=find_max_swap_seq(19); assert(isequal(1:19,unique(max_seq))) cseq{14}=max_seq; seq=max_seq; count=0; while seq(1)>1 count=count+1; seq(1:seq(1))=seq(seq(1):-1:1); end fprintf('count %i\n',count) fprintf('%i ',max_seq) fprintf('\n')

count 29 7 13 6 2 3 14 5 17 11 8 4 1 16 19 12 10 15 9 18

15   Pass
%% %23 382 case 15 global cseq max_seq=find_max_swap_seq(23); assert(isequal(1:23,unique(max_seq))) cseq{15}=max_seq; seq=max_seq; count=0; while seq(1)>1 count=count+1; seq(1:seq(1))=seq(seq(1):-1:1); end fprintf('count %i\n',count) fprintf('%i ',max_seq) fprintf('\n')

count 23 6 23 3 20 11 21 17 7 2 4 15 10 5 12 13 8 19 22 16 9 18 1 14

16   Pass
%% %29 689 case 16 global cseq max_seq=find_max_swap_seq(29); assert(isequal(1:29,unique(max_seq))) cseq{16}=max_seq; seq=max_seq; count=0; while seq(1)>1 count=count+1; seq(1:seq(1))=seq(seq(1):-1:1); end fprintf('count %i\n',count) fprintf('%i ',max_seq) fprintf('\n')

count 40 11 29 26 24 22 6 13 7 27 17 19 3 20 9 16 18 2 14 21 10 4 28 23 5 1 15 25 12 8

17   Pass
%% %31 819 case 17 global cseq max_seq=find_max_swap_seq(31); assert(isequal(1:31,unique(max_seq))) cseq{17}=max_seq; seq=max_seq; count=0; while seq(1)>1 count=count+1; seq(1:seq(1))=seq(seq(1):-1:1); end fprintf('count %i\n',count) fprintf('%i ',max_seq) fprintf('\n') toc

count 8 21 4 18 24 30 13 12 27 14 9 20 16 11 6 3 26 23 25 7 22 29 10 28 8 2 5 17 19 15 31 1 Elapsed time is 0.064634 seconds.

18   Fail
%% global cseq total=0; for i=2:17 seq=cseq{i}; count=0; while seq(1)>1 count=count+1; seq(1:seq(1))=seq(seq(1):-1:1); end total=total+count; end % 2531 is optimal sum total=sum([0 1 2 4 7 10 16 22 30 38 51 80 159 221 382 689 819])-total; assert(total<2001); % Minimum performance requirement toc feval(@assignin,'caller','score',min(2000,total));

Error: Assertion failed.