H1B transfer during stay in India

Company X had my H1B visa approved last year and I worked in the US for a year. I had to return back to my home country after the project ended abruptly. I have spent around 4 month in my home country and my H1B with company X is still Valid. will I be allowed to transfer my H1B visa to company Y and travel on this? I want to know this as I have already spent 4 months without being on a US payroll.

Y can file a cap-exempt petition (aka H-1 transfer) for you even when you are not in US. In addition to regular H-1 documents, you also need to show 797 copy (to prove that you have already been counted in H-1 cap) and payslips/W-2 for the period you were inside US on H-1 (to prove that you maintained legal status when inside US on H-1).

Thanks Saurabh, I asked this question as I was told that the transfer had to take place within 3 months of my getting back to India. Can you please confirm that this is not a mandatory requirement!!

That is not true. It can be filed even after 3 months. You are cap-exempt for up to 6 years from the date you last left US.