📌  相关文章
📜  对于可被K整除的数组元素,可以通过将arr [i] / K附加到数组末尾K次来实现数组元素的总和

📅  最后修改于: 2021-04-29 04:46:05             🧑  作者: Mango

给定一个由N个整数和整数K组成的数组arr [] ,任务是通过遍历该数组并在数组末尾添加karr [i] / K来找到可能的数组元素之和,如果arr [i]可被K整除。否则,停止遍历。

例子:

天真的方法:最简单的方法是解决给定的问题,即遍历给定的数组并在数组末尾添加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推送一次。

请按照以下步骤解决问题:

  • 初始化一个变量,例如sum0 ,该变量存储所有数组元素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)