Solved problem3 in python
Solved problem4 in java