The question goes like this:
Given two arrays, a
& b
(both with positive integers).
A special number
is a number whi
Well, turns out there might be a way after all :). We make use of the fact that the numbers have to be strictly positive to be special numbers at all.
%# in case we need to handle empty inputs: replace empty input with 0 or 1, respectively.
a = sum(a(:)',1);
bIsEmpty = isempty(b);
b = sum(b(:)',1); b = max(b,1);
specialNumber = find(a==1:length(a));
maxAB = max(max(a), max(b));
%# "zeros()"
bigVectorForComparisonA = (1:maxAB)*0;
bigVectorForComparisonB = (1:maxAB)*0;
bigVectorForComparisonA(specialNumber) = 1;
bigVectorForComparisonB(b) = 1;
%# instead of &, we add. Find only the smallest match
specialNumberInB = find(bigVectorForComparisonA + bigVectorForComparisonB == 2,1,'first');
out = sum(specialNumberInB) * ~bIsEmpty; %# sum([]) = 0
For a slightly prettier solution that assumes up to 1 special number in a
specialNumber = min(find(a==(1:length(a)));
out = any(b==specialNumber)*sum(specialNumber);
I was holding off posting my solution because I correctly suspected that this question was a homework assignment. However, since the OP has accepted Jonas's answer, I might as well post mine.
A combination of sum
, length
, any
, and min
does the trick:
function out = stupidTutor(a, b)
a = sum(a, 1); % if a is empty, replace it by a 1-by-0 matrix
specials = a(a == 1:length(a)); % construct the vector of special numbers
b = sum(b, 1); % if b is empty, replace it by a 1-by-0 matrix
% some dyadic-product shenanigans
A = specials' * (b == b);
B = (specials == specials)' * b;
ind = any(A == B, 1);
temp = min(b(ind)); % temp is either a scalar, a 1-by-0 matrix, or []
out = sum(sum(temp, 2), 1); % trick to return 0 in case temp be 1-by-0 or []
% a b result
stupidTutor([9 9 3 9] , [3 4 5]) % 3
stupidTutor([9 9 3 9] , [9 8]) % 0
stupidTutor([9 9 9 9 5], [3 4 5 3]) % 5
stupidTutor([9 9 3 9 5], [3 4 5 3]) % 3
stupidTutor([9 9 3 9 5], [5 4 3 2 1]) % 3
stupidTutor([9 9 3 9] , []) % 0
stupidTutor([] , [3 4 5]) % 0
stupidTutor([] , []) % 0