function [minmax,jnt] = optnum(numnp,numel,idiff,jmem,memjt)
%CALCULATION OF NEW NODAL NUMBERS
global xord yord

minmax=idiff;
ndiffbreak = 0;


for ik=1:numnp

    for j=1:numnp
        joint(j)=0;
        newjt(j)=0;
    end

    max=0;
    i=1;
    newjt(1)=ik;
    joint(ik)=1;
    k=1;

    for kcounter =1:8*numnp

        k4=jmem(newjt(i));

        if(k4~=0)

            jsub=(newjt(i) - 1)*8;

            for  jj=1:k4
                k5=memjt(jsub + jj);

                if(joint(k5)>0)
                    continue
                end

                k=k + 1;
                newjt(k)=k5;
                joint(k5)=k;

                ndiff=abs(i-k);

                if(ndiff>=minmax)
                    ndiffbreak = 1;
                    break
                end

                if(ndiff>max)
                    max=ndiff;
                end

            end

            if(ndiffbreak==1)
                break
            end

            if(k==numnp)
                break
            end

            i = i+1;


        elseif(k4==0)

            i=i + 1;

        end



    end


    if(ndiffbreak==1)
        ndiffbreak = 0;
        continue
    end

    minmax=max;

    for j=1:numnp
        jnt(j)=joint(j);
    end

end


if(idiff<=minmax)
    for j=1:numnp
        jnt(j)=j;
    end
end

