Open In App

Sum of array elements possible by appending arr[i] / K to the end of the array K times for array elements divisible by K

Last Updated : 28 Jun, 2021
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

Given an array arr[] consisting of N integers and an integer K, the task is to find the sum of the array elements possible by traversing the array and adding arr[i] / K, K number of times at the end of the array, if arr[i] is divisible by K. Otherwise, stop the traversal.

Examples:

Input: arr[] = {4, 6, 8, 2}, K = 2
Output: 44
Explanation:
The following operations are performed:

  1. For arr[0](= 4): arr[0](= 4) is divisible by 2, therefore append 4/2 = 2, 2 numbers of times at the end of the array. Now, the array modifies to {4, 6, 8, 2, 2, 2}.
  2. For arr[1](= 6): arr[1](= 6) is divisible by 2, therefore append 6/2 = 3, 2 number of times at the end of the array. Now, the array modifies to {4, 6, 8, 2, 2, 2, 3, 3}.
  3. For arr[2](= 8): arr[2](= 8) is divisible by 2, therefore append 8/2 = 4, 2 number of times at the end of the array. Now, the array modifies to {4, 6, 8, 2, 2, 2, 3, 3, 4, 4}.
  4. For arr[3](= 2): arr[3](= 2) is divisible by 2, therefore append 2/2 = 1, 2 number of times at the end of the array. Now, the array modifies to {4, 6, 8, 2, 2, 2, 3, 3, 4, 4, 1, 1}.
  5. For arr[4](= 2): arr[4](= 2) is divisible by 2, therefore append 2/2 = 1, 2 number of times at the end of the array. Now, the array modifies to {4, 6, 8, 2, 2, 2, 3, 3, 4, 4, 1, 1, 1, 1}.
  6. For arr[5](= 2): arr[5](= 2) is divisible by 2, therefore append 2/2 = 1, 2 number of times at the end of the array. Now, the array modifies to {4, 6, 8, 2, 2, 2, 3, 3, 4, 4, 1, 1, 1, 1, 1, 1}.

After completing the above steps, the sum of the array elements is = 4 + 6 + 8 + 2 + 2 + 2 + 3 + 3 + 4 + 4 + 1 + 1 + 1 + 1 + 1 + 1 = 44.

Input: arr[] = {4, 6, 8, 9}, K = 2
Output: 45

 

Naive Approach: The simplest approach is to solve the given problem is to traverse the given array and add the value (arr[i]/K) K a number of times at the end of the array. After completing the above steps, print the sum of the array elements.

Below is the implementation of the above approach:

C++
Java Python3 C# JavaScript

Output: 
44

 

Time Complexity: O(N * K * log N)
Auxiliary Space: O(M), M is the maximum element of the array.

Efficient Approach: The above approach can also be optimized based on the following observations:

  • If arr[i] is divisible by K, then adding arr[i] / K, K times increases the sum by arr[i].
  • Therefore, the idea is to only push the arr[i] / K only once at the end of the vector.

Follow the steps below to solve the problem:

  • Initialize a variable, say sum as 0 that stores the sum of all the array elements array A[].
  • Initialize an array, say A[] and store all the array elements arr[] in A[].
  • Initialize a variable, say flag as 0 that stores whether the element is to be added at the end of the array or not.
  • Traverse the array A[] and perform the following steps:
    • If the value flag is 0 and A[i] is divisible by K, then push A[i] at the end of V.
    • Otherwise, update the value of flag as 1.
    • Increment the value of the sum by V[i % N].
  • After completing the above steps, print the value of the sum as the resultant sum.

Below is the implementation of the above approach:

C++
Java Python3 C# JavaScript

Output: 
44

 


Time Complexity: O(N * log N) 
Auxiliary Space: O(N * log N)


Similar Reads