i have given the same solution in java