Sum of array elements possible by appending arr[i] / K to the end of the array K times for array elements divisible by K
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:
- 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}.
- 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}.
- 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}.
- 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}.
- 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}.
- 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++
#include <bits/stdc++.h>
using namespace std;
int sum( int arr[], int N, int K)
{
int sum = 0;
vector< long long > v;
for ( int i = 0; i < N; i++) {
v.push_back(arr[i]);
}
for ( int i = 0;
i < v.size(); i++) {
if (v[i] % K == 0) {
long long x = v[i] / K;
for ( int j = 0; j < K; j++) {
v.push_back(x);
}
}
else
break ;
}
for ( int i = 0; i < v.size(); i++)
sum = sum + v[i];
return sum;
}
int main()
{
int arr[] = { 4, 6, 8, 2 };
int K = 2;
int N = sizeof (arr) / sizeof (arr[0]);
cout << sum(arr, N, K);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
static int sum( int arr[], int N, int K)
{
int sum = 0 ;
ArrayList<Integer> v = new ArrayList<>();
for ( int i = 0 ; i < N; i++)
{
v.add(arr[i]);
}
for ( int i = 0 ; i < v.size(); i++)
{
if (v.get(i) % K == 0 )
{
int x = v.get(i) / K;
for ( int j = 0 ; j < K; j++)
{
v.add(x);
}
}
else
break ;
}
for ( int i = 0 ; i < v.size(); i++)
sum = sum + v.get(i);
return sum;
}
public static void main(String[] args)
{
int arr[] = { 4 , 6 , 8 , 2 };
int K = 2 ;
int N = arr.length;
System.out.println(sum(arr, N, K));
}
}
|
Python3
def summ(arr, N, K):
sum = 4
v = [i for i in arr]
for i in range ( len (v)):
if (v[i] % K = = 0 ):
x = v[i] / / K
for j in range (K):
v.append(x)
else :
break
for i in range ( len (v)):
sum = sum + v[i]
return sum
if __name__ = = '__main__' :
arr = [ 4 , 6 , 8 , 2 ]
K = 2
N = len (arr)
print (summ(arr, N, K))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int sum( int [] arr, int N, int K)
{
int sum = 0;
List< int > v = new List< int >();
for ( int i = 0; i < N; i++) {
v.Add(arr[i]);
}
for ( int i = 0; i < v.Count; i++) {
if (v[i] % K == 0) {
int x = v[i] / K;
for ( int j = 0; j < K; j++) {
v.Add(x);
}
}
else
break ;
}
for ( int i = 0; i < v.Count; i++)
sum = sum + v[i];
return sum;
}
public static void Main( string [] args)
{
int [] arr = { 4, 6, 8, 2 };
int K = 2;
int N = arr.Length;
Console.WriteLine(sum(arr, N, K));
}
}
|
Javascript
<script>
function sum(arr, N, K)
{
var sum = 0;
var v = [];
for ( var i = 0; i < N; i++) {
v.push(arr[i]);
}
for ( var i = 0;
i < v.length; i++) {
if (v[i] % K == 0) {
var x = v[i] / K;
for ( var j = 0; j < K; j++) {
v.push(x);
}
}
else
break ;
}
for ( var i = 0; i < v.length; i++)
sum = sum + v[i];
return sum;
}
var arr = [4, 6, 8, 2];
var K = 2;
var N = arr.length;
document.write( sum(arr, N, K));
</script>
|
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++
#include <bits/stdc++.h>
using namespace std;
int sum( int arr[], int N, int K)
{
int sum = 0;
vector< int > v;
for ( int i = 0; i < N; i++) {
v.push_back(arr[i]);
}
bool flag = 0;
for ( int i = 0; i < v.size(); i++) {
if (!flag && v[i] % K == 0)
v.push_back(v[i] / K);
else {
flag = 1;
}
sum = sum + v[i % N];
}
return sum;
}
int main()
{
int arr[] = { 4, 6, 8, 2 };
int K = 2;
int N = sizeof (arr) / sizeof (arr[0]);
cout << sum(arr, N, K);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static int sum( int arr[], int N, int K)
{
int sum = 0 ;
ArrayList<Integer> v = new ArrayList<Integer>();
for ( int i = 0 ; i < N; i++) {
v.add(arr[i]);
}
boolean flag = false ;
for ( int i = 0 ; i < v.size(); i++) {
if (!flag && v.get(i) % K == 0 )
v.add(v.get(i) / K);
else {
flag = true ;
}
sum = sum + v.get(i % N);
}
return sum;
}
public static void main (String[] args) {
int arr[] = { 4 , 6 , 8 , 2 };
int K = 2 ;
int N = arr.length;
System.out.println(sum(arr, N, K));
}
}
|
Python3
def Sum (arr, N, K):
sum = 0
v = []
for i in range (N):
v.append(arr[i])
flag = False
i = 0
lenn = len (v)
while (i < lenn):
if ( flag = = False and (v[i] % K = = 0 )):
v.append(v[i] / / K)
else :
flag = True
sum + = v[i % N]
i + = 1
lenn = len (v)
return sum
arr = [ 4 , 6 , 8 , 2 ]
K = 2
N = len (arr)
print ( Sum (arr, N, K))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int sum( int []arr, int N, int K)
{
int sum = 0;
List< int > v = new List< int >();
for ( int i = 0; i < N; i++)
{
v.Add(arr[i]);
}
bool flag = false ;
for ( int i = 0; i < v.Count; i++)
{
if (!flag && v[i] % K == 0)
v.Add(v[i] / K);
else
{
flag = true ;
}
sum = sum + v[i % N];
}
return sum;
}
static void Main()
{
int [] arr = { 4, 6, 8, 2 };
int K = 2;
int N = arr.Length;
Console.WriteLine(sum(arr, N, K));
}
}
|
Javascript
<script>
function sum(arr, N, K)
{
var sum = 0;
var i;
var v = [];
for (i = 0; i < N; i++) {
v.push(arr[i]);
}
var flag = 0;
for (i = 0; i < v.length; i++) {
if (!flag && v[i] % K == 0)
v.push(v[i] / K);
else {
flag = 1;
}
sum = sum + v[i % N];
}
return sum;
}
var arr = [4, 6, 8, 2];
var K = 2;
var N = arr.length;
document.write(sum(arr, N, K));
</script>
|
Time Complexity: O(N * log N)
Auxiliary Space: O(N * log N)
Last Updated :
28 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...