code: public void writeByteBufferSparse(ByteBuffer buf, int nonEmptyBuckets)
    {
      writeByteBufferSerdeHeader(buf, SPARSE_ENCODING_MODE);
      writeByteBufferCommonFields(buf);
  
      buf.putInt(nonEmptyBuckets);
      int bucketsWritten = 0;
      for (int i = 0; i < numBuckets; i++) {
        if (histogram[i] > 0) {
          buf.putInt(i);
          buf.putLong(histogram[i]);
          bucketsWritten += 1;
        }
        if (bucketsWritten == nonEmptyBuckets) {
          break;
        }
      }
    }
docs: Helper method for toBytesSparse
  
  @param buf Destination buffer
  @param nonEmptyBuckets Number of non-empty buckets in the histogram