ACtually I was deciding the offset array based on the totalElementLength, but now changed to the maxoffset.