The thing that you need to understand is we use router to connect different subnets while switch to connect to same subnets.
Even if you connect different subnets to same switch they won’t be able to communciate. See suppose two computers A and B have different subnets and are connected to a switch. Now when you want to send a data packet from A to B the data packets will have Destination IP address. A knows the range of IP addresses on its subnet (by subnetting) and therefore A will get to know that the computer B is not on its network. A knows that to communicate with any system outside its network it needs to send the data packet to some router (and not switch). So it will perform ARP and get MAC of router. But if it is not connected to any router (gateway) then nothing would be done here. So a router is needed to connect different subnet masks to make communication to happen.
Now if you want to ask if two subnets should be definitely connected to same router or gateway well this is incomplete question. Let me take an example. A computer C is connected to a router R1 and R1 is connected to a router R2 and router R2 is connected to other computer C2. So of course here routing will go on and data packets from C1 will reach C2. You need to explain the exact configuration of the network you talking about.