Sum of all possible concatenations of array values Given an array of non-negative integers, we need to find the sum of concatenation of elements in the array.

For example - 
given [11, 22] the result should be - 6666 
i.e.,
11 + 11 = 1111
11 + 22 = 1122
22 + 11 = 2211
22 + 22 = 2222

Sum of all those numbers would result in 6666

My code is as below - 

function concatenationsSum(a) {
  var sum = 0;
  for (var i = 0; i &lt; a.length; i++)
    for (var j = 0; j &lt; a.length; j++) 
        sum += Number("" + a[i] + a[j]);
  return sum;
}


Problems like these require brute force since we need to sum each and every combination. This works sure fine for smaller inputs. But if the array is longer, say, 1 ≤ lengthOfArray ≤ 25, how can I handle such large inputs faster? Any suggestions would be appreciated.
I'm always leery of the claim that problems require brute force algorithms. There is, for example, a formula for directly calculating the Nth Fibonacci number.

In the case of this problem, there is a much simpler way to perform the calculation that does not require O(n^2) time. Each element of the array pairs with every other element exactly twice - once as the most significant part, and once as the least significant part. These two parts can be calculated separately in one pass.

The low part is the sum of the elements times the number of elements.

The high part is the sum of (the "offset" for each element times the sum of the elements). The offset is the digit base raised to the number of digits (expressed generally, but this problem always uses base 10). So for a two digit number (11 or 22) the offset is 100.

function concatenationsSum2(a) {
    var lowSum = 0;
    for (var i = 0; i &lt; a.length; i++)
        lowSum += a[i];

    var sum = lowSum * a.length;

    for (var i = 0; i &lt; a.length; i++) {
        var size = a[i].toString().length;
        var offset = iPower(10, size);
        sum = sum + lowSum * offset;
    }

    return sum;
}

function iPower(base, power) {
    var result = 1;
    for (var i = 1; i &lt;= power; i++)
        result *= base;

    return result;
}


And from there, we can simplify even further by combining the two parts.

function concatenationsSum3(a) {
    var lowSum = 0;
    var offsetSum = 0;
    for (var i = 0; i &lt; a.length; i++) {
        lowSum += a[i];

        var size = a[i].toString().length;
        var offset = iPower(10, size);
        offsetSum += offset;
    }

    return lowSum * a.length + lowSum * offsetSum;
}