Easy C++ Solution || 2 Approaches with explanation || SOP🔥
Approach - 1 -  TC: O(n^2), SC: O(n)\n## Code\n\nclass Solution {\npublic:\n    int numberOfBeams(vector<string>& bank) {\n        int n = bank.size();\n       
# Approach - 1 -  TC: O(n^2), SC: O(n)\n## Code\n```\nclass Solution {\npublic:\n    int numberOfBeams(vector<string>& bank) {\n        int n = bank.size();\n         vector<int>devicesInRow(n,0);\n         for(int i=0;i<n;i++){\n             for(auto c : bank[i]){\n                 if(c==\'1\'){\n                     devicesInRow[i]++;\n                 }\n             }\n         }\n          int laserbeams=0;\n         for(int i=0;i<n;i++){\n             for(int j=i+1;j<n;j++){\n             if(devicesInRow[i]!=0 && devicesInRow[j]!=0){\n                        laserbeams+= (devicesInRow[i]*devicesInRow[j]);\n                        break;\n               }\n             }\n         }\n         return laserbeams;\n    }\n};\n```\n\n## Approach\n1. Iterate through each row of the `bank` and count the number of devices in each row, storing the counts in an array `devicesInRow`.\n2. Initialize a variable `laserbeams` to 0, which will store the total number of laser beams formed.\n3. Iterate through pairs of rows (i, j) where i is less than j. Check if both rows have at least one device, i.e., `devicesInRow[i] != 0` and `devicesInRow[j] != 0`.\n4. If both rows have devices, increment `laserbeams` by the product of the number of devices in the two rows, i.e., `laserbeams += devicesInRow[i] * devicesInRow[j]`.\n5. Break out of the inner loop if a pair is found to avoid double-counting devices in multiple pairs.\n6. Finally, return the total number of laser beams.\n\n## Complexity\n- Time complexity: O(n^2), where n is the number of rows in the `bank`. The nested loops iterate through all pairs of rows.\n- Space complexity: O(n) for the `devicesInRow` array, which stores the count of devices in each row.\n\n\n\n# Approach - 2 -  TC: O(n^2), SC: O(1)\n\n## Code\n```\nclass Solution {\n    int countDevicesInRow(string bankRow){\n        int cnt=0;\n        for(int i=0;i<bankRow.size();i++){\n            if(bankRow[i]==\'1\'){\n                cnt++;\n            }\n        }\n        return cnt;\n    }\npublic:\n    int numberOfBeams(vector<string>& bank) {\n          int laserbeams=0;\n          int j=0;\n\n          for(int i=0;i<bank.size();i++){\n          \n              int count_devices_row1=countDevicesInRow(bank[i]);\n              int count_devices_row2=0;\n                 j=i+1;\n              while(count_devices_row2==0 && j<bank.size()){\n                count_devices_row2=countDevicesInRow(bank[j]);\n                  j++;\n              }\n              laserbeams+=(count_devices_row1*count_devices_row2);         \n          }\n          return laserbeams;\n    }\n};\n```\n\n\n## Approach:\n\n1. **Count Devices in Each Row:**\n   - Defines a separate function `countDevicesInRow` to calculate the number of devices in a row.\n   - Iterates through each character in a row, increments a counter (`cnt`) for each \'1\' encountered, and returns the total count.\n\n2. **Calculate Laser Beams:**\n   - Iterates through each row of the `bank` vector.\n   - For each row, calculates the number of devices using the `countDevicesInRow` function.\n   - Searches for the next row with devices in the second part of the pair using a while loop.\n   - Calculates the number of laser beams formed by multiplying the device counts of the two rows.\n\n## Complexity:\n\n- **Time Complexity:** O(n^2)\n  - The outer loop runs \'n\' times.\n  - The inner while loop can potentially run up to \'n\' times in the worst case.\n  - Therefore, the overall time complexity is O(n^2).\n\n- **Space Complexity:** O(1)\n  - Constant space is used as no additional data structures are created that depend on the input size.\n\n\n\n\n\n\n\n\n\n\n\n\n
['C++']