For beginners C++, Beats 100 %
Intuition\n Describe your first thoughts on how to solve this problem. \nThe goal is to determine if a string of digits is "balanced" based on the sums of its d
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe goal is to determine if a string of digits is "balanced" based on the sums of its digits at even and odd indices. To solve this, we can:\n\n- Separate the digits based on whether they\u2019re located at even or odd - indices.\n- Sum up the digits at these even and odd positions.\n- Check if the sums are equal. If they are, the number is balanced; otherwise, it\u2019s not.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- Initialize Sums for Even and Odd Positions: Create two integer variables, even and odd, initialized to zero. These will hold the sum of digits at even and odd indices, respectively.\nTraverse the String:\n- Use a loop to iterate over each character in the string:\n- For characters at even indices (0, 2, 4,...), convert the character to an integer and add it to the even sum.\n- For characters at odd indices (1, 3, 5,...), convert the character to an integer and add it to the odd sum.\n- Compare Sums: After completing the loop, compare the even and odd sums.\n- If they are equal, return true (indicating the number is balanced).\nOtherwise, return false.\n\n# Complexity\n- The time complexity is O(n), where n is the length of the string.\n\n    This is because we need to iterate through each character in     the   string once.\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    bool isBalanced(string num) {\n\n        int even=0;\n        int odd=0;\n        for(int i=0;i<num.length();i+=2){\n            int a = num[i] - \'0\';\n            even = even+a;\n        }\n\n        for(int i=1;i<num.size();i+=2){\n            int a=num[i]-\'0\';\n            odd +=a;\n        }\n        if(odd==even){\n            return 1;\n        }\n        return 0;\n        \n    }\n};\n```
['C++']