Question

In: Computer Science

Given an array of positive integers a, your task is to calculate the sum of every...

Given an array of positive integers a, your task is to calculate the sum of every possible a[i] ∘a[j], where a[i]∘a[j] is the concatenation of the string representations of a[i] and a[j] respectively.

Example

  • For a = [10, 2], the output should be concatenationsSum(a) = 1344.
    • a[0] ∘a[0] = 10 ∘10 = 1010,
    • a[0] ∘a[1] = 10 ∘2 = 102,
    • a[1] ∘a[0] = 2 ∘10 = 210,
    • a[1] ∘a[1] = 2 ∘2 = 22.

So the sum is equal to 1010 + 102 + 210 + 22 = 1344.

  • For a = [8], the output should be concatenationsSum(a) = 88.

There is only one number in a, and a[0] ∘a[0] = 8 ∘8 = 88, so the answer is 88.

Input/Output

  • [execution time limit] 3 seconds (java)
  • [input] array.integer a

A non-empty array of positive integers.

Guaranteed constraints:
1 ≤ a.length ≤ 105,
1 ≤ a[i] ≤ 106.

  • [output] integer64
    • The sum of all a[i] ∘a[j]s. It's guaranteed that the answer is less than 253.

[Java] Syntax Tips

Solutions

Expert Solution

The answer to the question is given below.

The java code is given below.


import java.util.Scanner; /* scanner package for user input */

public class concatenationsSum /* concatenationsSum class for calculations */
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of elements in the array : ");
int n=sc.nextInt();   
System.out.println("Enter the elements of array: ");
int value;
int[] arr=new int[n]; /* array for storing n elements */
int sum=0; /* intializing sum as 0 */
for(int i=0;i<n;i++)
{
value=sc.nextInt();
arr[i]=value;
}
/* perfroming all concatenation's sum using two loops */
for(int i=0;i<n;i++)
{
String str1 = Integer.toString(arr[i]); /* converting integer to string by toString() function */
for(int j=0;j<n;j++)
{
String str2 = Integer.toString(arr[j]);
String str3=str1+str2;
int num=Integer.parseInt(str3); /* converting String to integer by parseInt() function */
sum=sum+num; /* adding every num to sum */
}
}
System.out.println("Total concatenationsum is : "+sum); /* printing the value of sum */
}
}



The screenshot of the running code is given below.





The screenshot of sample input and output is given below in the image.







If the answer helped please upvote it means a lot. For any query please comment.



Related Solutions

Using Java please You are given an array of integers arr. Your task is to count...
Using Java please You are given an array of integers arr. Your task is to count the number of contiguous subarrays, such that each element of the subarray appears at least twice. E.g For arr = [0, 0, 0], the output should be duplicatesOnSegment(arr) = 3.
The subset-sum problem is defined as follows. Given a set of n positive integers, S =...
The subset-sum problem is defined as follows. Given a set of n positive integers, S = {a1, a2, a3, ..., an} and positive integer W, is there a subset of S whose elements sum to W? Design a dynamic program to solve the problem. (Hint: uses a 2-dimensional Boolean array X, with n rows and W+1 columns, i.e., X[i, j] = 1,1 <= i <= n, 0 <= j <= W, if and only if there is a subset of...
We have an array A of size n. There are only positive integers in this array....
We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. a. Design an efficient algorithm to find the maximum difference between any two...
We have an array A of size n. There are only positive integers in this array....
We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. a. Design an efficient algorithm to find the maximum difference between any two...
Array indices must be positive integers or logical values?
How can I fix this MATLAB error Index in position 2 is invalid. Array indices must be positive integers or logical values?
Consider the problem of finding if an array contains a pair of integers with a sum...
Consider the problem of finding if an array contains a pair of integers with a sum of 100. The array contains n integers. a. Define the signature (header) of a C++ function that solves this problem (hint: inputs/outputs of function) b. Write the pseudocode or C++ body of the function (extra credit: write the most efficient algorithm) c. What is the asymptotic complexity of your algorithm? d. What would be the complexity of the best algorithm for this problem if...
Write an application that uses multithreading to compute the sum of the integers in an array...
Write an application that uses multithreading to compute the sum of the integers in an array of size 100,000. You can populate the array with random numbers and your application should display the sum. (JAVA)
You are given an array of arrays a. Your task is to group the arrays a[i]...
You are given an array of arrays a. Your task is to group the arrays a[i] by their mean values, so that arrays with equal mean values are in the same group, and arrays with different mean values are in different groups. Each group should contain a set of indices (i, j, etc), such that the corresponding arrays (a[i], a[j], etc) all have the same mean. Return the set of groups as an array of arrays, where the indices within...
Given an array of positive integers except one negative integer. Develop a divide-conquer algorithm to find...
Given an array of positive integers except one negative integer. Develop a divide-conquer algorithm to find the index of the negative integer, and compute its average complexity.
Given an array of positive integers except one negative integer. Develop a divide-conquer algorithm to find...
Given an array of positive integers except one negative integer. Develop a divide-conquer algorithm to find the index of the negative integer, and compute its average complexity. Please provide a solution in Java
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT