Hello Subhash,
Shopify enables you to integrate an external 'calculation service' within your store to dynamically calculate the shipping costs. Shopify calls this a Carrier Service or Real-time carrier shipping.
This Carrier Service can dynamically calculate the shipping costs based on the cart contents (products and shipping address). It is possible to create a Carrier Service which calculates the shipping costs according to the rules you've provided.
Keep in mind this Carrier Service can only be added if you're on the Shopify Unlimited plan or higher. You could also take a look in the Shopify App Store, maybe an App with this or similar functionality has been developed already.
Cheers,
Harm-Jan