dude u are doing O ( N ^ 2 ) time complexityy code
please read from here the logic and explanation
thank u so much mam âŠi understood the logic