4 Solutions || Brute >> Better >> Optimal || c++ || Faster than 100% || 0ms
Intuition\n\nwe need to concatinate the first and last digits \nfor example [7,52,2,4]\nans = 522 + 14 = 596 ;\nso 52 and 2 make 522 by (52 * 100)  + 2\nsimilar
# Intuition\n\nwe need to concatinate the first and last digits \nfor example [7,52,2,4]\nans = 522 + 14 = 596 ;\nso 52 and 2 make 522 by (52 * 100)  + 2\nsimilarly \nfor 7 and 4 \n(7 * 10) + 4 = 74\n\nin colclusion we just need to find the power of 10 which we need to multiply the first digit \n\n\n\n# Approach 1 \n// credit goes to ganesh_2023 for this solution\nwe can use a while loop and keep dividing last element to find out the power needed\n\n# Code\n\nclass Solution {\npublic:\n    long long findTheArrayConcVal(vector<int>& nums) {\n        long long int ans=0,x,b;\n        int i = 0, j = nums.size()-1;\n        while(i<j){\n            x = nums[j], b = 0;\n            while(x){\n                b++;\n                x /= 10;\n            }\n            ans = ans + nums[i]*1LL*pow(10,b)+nums[j];\n            i++;\n            j--;\n        }\n        if(i==j){\n            ans += nums[i];\n        }return ans;\n    }\n};\n\n# Approach 2\nusing log and pow \n\n# Code\nclass Solution {\npublic:\n    long long findTheArrayConcVal(vector<int>& nums) {\n        int i=0;\n        int j=nums.size()-1;\n        long long ans=0;\n        while(i<j)\n        {\n            ans = ans + (nums[i] * pow(10, (int)log10(nums[j]) + 1 )) + nums[j];\n            i++;\n            j--;\n        }\n        if(i==j) ans += nums[i];\n        return ans;\n    }\n};\n\n# Approach 3\nusing stol and to_string() and simply adding\n\n# Code\n\nclass Solution {\npublic:\n    long long findTheArrayConcVal(vector<int>& nums) {\n\n        int i = 0;\n        int j = nums.size()-1;\n        long long ans = 0;\n\n        while(i<j)\n        {\n            string temp = to_string(nums[i]) + to_string(nums[j]);\n            ans += stol(temp);\n            i++;\n            j--;\n        }\n        if(i==j)\n        ans += nums[i];\n        return ans;\n        \n    }\n};\n\n# approach 4\nthis is the fastest and is feasiable only beacause of the constraints given\n\nwe simply use if else to find the power \n\n# Code\n\nclass Solution {\npublic:\n    long long findTheArrayConcVal(vector<int>& nums) {\n        \n        int i=0;\n        int j=nums.size()-1;\n        long long concat =0;\n        while(i<=j)\n        {\n            if(i==j)\n            {\n                concat += nums[i];\n                break;\n            }\n            if(nums[j]<10)\n            {\n                concat += nums[i]*10 + nums[j];\n                i++;\n                j--;\n                continue;\n            }\n            if(nums[j]<100)\n            {\n                concat += nums[i]*100 + nums[j];\n                i++;\n                j--;\n                continue;\n            }\n            if(nums[j]<1000)\n            {\n                concat += nums[i]*1000 + nums[j];\n                i++;\n                j--;\n                continue;\n            }\n            if(nums[j]<10000)\n            {\n                concat += nums[i]*10000 + nums[j];\n                i++;\n                j--;\n                continue;\n            }\n            if(nums[j]<100000)\n            {\n                concat += nums[i]*100000 + nums[j];\n                i++;\n                j--;\n                continue;\n            }\n        }\n        return concat;\n        \n    }\n};\n
['C++']