function [Z,gradZ]=computeGradBackward(lambda,blockSizes)
if size(lambda,1)==1, lambda=lambda';end %lambda is a column vector
n=size(lambda,1)-1;k=(n+1)/2;
if rem(k,1)~=0, disp('error: k not integer');return; end


% compute the normalization term Z
eval(['load runtimeFiles\ForwardVec' num2str(length(blockSizes)) ';']);
Z = block(end,k+1);
gradZ=zeros(1,length(lambda));

%compute the last entry in the gradient vector
convRes = fastConv(block(end-1,:), computeGtildeVector(lambda(end),k));
gradZ(n+1) = convRes(k+1);

if lambda==lambda(1)*ones(size(lambda)),
    gradZ = gradZ(n+1) * ones(1,length(lambda));
    return;
end

%compute the first entry of gradient vector
eval(['load runtimeFiles\BackwardVec' num2str(0) ';']);
convRes = fastConv(block(2,:), computeGtildeVector(lambda(1),k));
gradZ(1) = convRes(k+1);

% compute the rest of the gradient of Z
for i=1:length(blockSizes)-1,
    indices=sum(blockSizes(1:i-1))+1 : sum(blockSizes(1:i));
    eval(['load runtimeFiles\BackwardVec' num2str(i) ';']);
    blockB=block;clear block;
    eval(['load runtimeFiles\ForwardVec'  num2str(i) ';']);    
    disp(['block number ' num2str(i)]);
    res =fastConv(block,blockB);
    gradZ(indices+1) = sum(fliplr(res).*computeGtildeVector(lambda(indices+1),k),2);
end