给定一个由N个整数和整数K组成的数组arr [] ,任务是通过遍历该数组并在数组末尾添加k次arr [i] / K来找到可能的数组元素之和,如果arr [i]可被K整除。否则,停止遍历。
例子:
Input: arr[] = {4, 6, 8, 2}, K = 2
Output: 44
Explanation:
Following operations are performed:
- For arr[0](= 4): arr[0](= 4) is divisible by 2, therefore append 4/2 = 2, 2 number 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 element 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
天真的方法:最简单的方法是解决给定的问题,即遍历给定的数组并在数组末尾添加K次(arr [i] / K)值。完成上述步骤后,打印数组元素的总和。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to calculate sum of array
// elements after adding arr[i] / K
// to the end of the array if arr[i]
// is divisible by K
int sum(int arr[], int N, int K)
{
// Stores the sum of the array
int sum = 0;
vector v;
// Traverse the array arr[]
for (int i = 0; i < N; i++) {
v.push_back(arr[i]);
}
// Traverse the vector
for (int i = 0;
i < v.size(); i++) {
// If v[i] is divisible by K
if (v[i] % K == 0) {
long long x = v[i] / K;
// Iterate over the range
// [0, K]
for (int j = 0; j < K; j++) {
// Update v
v.push_back(x);
}
}
// Otherwise
else
break;
}
// Traverse the vector v
for (int i = 0; i < v.size(); i++)
sum = sum + v[i];
// Return the sum of the updated array
return sum;
}
// Driver Code
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
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
// Function to calculate sum of array
// elements after adding arr[i] / K
// to the end of the array if arr[i]
// is divisible by K
static int sum(int arr[], int N, int K)
{
// Stores the sum of the array
int sum = 0;
ArrayList v = new ArrayList<>();
// Traverse the array arr[]
for(int i = 0; i < N; i++)
{
v.add(arr[i]);
}
// Traverse the vector
for(int i = 0; i < v.size(); i++)
{
// If v[i] is divisible by K
if (v.get(i) % K == 0)
{
int x = v.get(i) / K;
// Iterate over the range
// [0, K]
for(int j = 0; j < K; j++)
{
// Update v
v.add(x);
}
}
// Otherwise
else
break;
}
// Traverse the vector v
for(int i = 0; i < v.size(); i++)
sum = sum + v.get(i);
// Return the sum of the updated array
return sum;
}
// Driver Code
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));
}
}
// This code is contributed by Kingash
C++
// C++ program for the above approach
#include
using namespace std;
// Function to calculate sum of array
// elements after adding arr[i] / K
// to the end of the array if arr[i]
// is divisible by K
int sum(int arr[], int N, int K)
{
// Stores the sum of the array
int sum = 0;
// Stores the array elements
vector v;
// Traverse the array
for (int i = 0; i < N; i++) {
v.push_back(arr[i]);
}
// Stores if the operation
// should be formed or not
bool flag = 0;
// Traverse the vector V
for (int i = 0; i < v.size(); i++) {
// If flag is false and if
// v[i] is divisible by K
if (!flag && v[i] % K == 0)
v.push_back(v[i] / K);
// Otherwise, set flag as true
else {
flag = 1;
}
// Increment the sum by v[i % N]
sum = sum + v[i % N];
}
// Return the resultant sum
return sum;
}
// Driver Code
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;
}
输出:
44
时间复杂度: O(N * K * log N)
辅助空间: O(M),M是数组的最大元素。
高效方法:还可以基于以下观察来优化上述方法:
- 如果arr [i]可被K整除,则将arr [i] / K相加, K次将总和增加arr [i] 。
- 因此,想法是在向量的末尾仅将arr [i] / K推送一次。
请按照以下步骤解决问题:
- 初始化一个变量,例如sum为0 ,该变量存储所有数组元素array A []的和。
- 初始化一个数组,比如说A [] ,并将所有数组元素arr []存储在A []中。
- 初始化一个变量,将其标记为0 ,以存储是否要在数组末尾添加元素。
- 遍历数组A []并执行以下步骤:
- 如果值标志为0并且A [i]可被K整除,则在V的末尾按下A [i] 。
- 否则,将flag的值更新为1 。
- 将总和的值增加V [i%N] 。
- 完成上述步骤后,打印总和的值作为结果总和。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to calculate sum of array
// elements after adding arr[i] / K
// to the end of the array if arr[i]
// is divisible by K
int sum(int arr[], int N, int K)
{
// Stores the sum of the array
int sum = 0;
// Stores the array elements
vector v;
// Traverse the array
for (int i = 0; i < N; i++) {
v.push_back(arr[i]);
}
// Stores if the operation
// should be formed or not
bool flag = 0;
// Traverse the vector V
for (int i = 0; i < v.size(); i++) {
// If flag is false and if
// v[i] is divisible by K
if (!flag && v[i] % K == 0)
v.push_back(v[i] / K);
// Otherwise, set flag as true
else {
flag = 1;
}
// Increment the sum by v[i % N]
sum = sum + v[i % N];
}
// Return the resultant sum
return sum;
}
// Driver Code
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;
}
输出:
44
时间复杂度: O(N * log N)
辅助空间: O(N * log N)