solving Hard level code chef problem