I solved it using Java but not optimized. Can someone share Java solution with optimal solution?
Result; waiting for the feedback.
Good luck.