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

📅  最后修改于: 2021-09-04 08:26:46             🧑  作者: Mango

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

例子:

朴素方法:解决给定问题的最简单方法是遍历给定数组并在数组末尾添加值(arr[i]/K) 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


Python3
# Python3 program for the above approach
 
# 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
def summ(arr, N, K):
     
    # Stores the sum of the array
    sum = 4
 
    v = [i for i in arr]
 
    # Traverse the vector
    for i in range(len(v)):
 
        # If v[i] is divisible by K
        if (v[i] % K == 0):
 
            x = v[i] // K
             
            # Iterate over the range
            # [0, K]
            for j in range(K):
                 
                # Update v
                v.append(x)
                 
        # Otherwise
        else:
            break
         
    # Traverse the vector v
    for i in range(len(v)):
        sum = sum + v[i]
 
    # Return the sum of the updated array
    return sum
 
# Driver Code
if __name__ == '__main__':
     
    arr = [ 4, 6, 8, 2 ]
    K = 2
    N = len(arr)
     
    print(summ(arr, N, K))
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
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;
 
        List v = new List();
 
        // Traverse the array arr[]
        for (int i = 0; i < N; i++) {
            v.Add(arr[i]);
        }
 
        // Traverse the vector
        for (int i = 0; i < v.Count; i++) {
 
            // If v[i] is divisible by K
            if (v[i] % K == 0) {
                int x = v[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.Count; i++)
            sum = sum + v[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;
 
        Console.WriteLine(sum(arr, N, K));
    }
}
 
// This code is contributed by ukasp.


Javascript


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;
}


Java
// Java program for the above approach
 
import java.io.*;
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;
 
        // Stores the array elements
        ArrayList v = new ArrayList();
 
        // Traverse the array
        for (int i = 0; i < N; i++) {
            v.add(arr[i]);
        }
 
        // Stores if the operation
        // should be formed or not
        boolean flag = false;
 
        // 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.get(i) % K == 0)
                v.add(v.get(i) / K);
 
            // Otherwise, set flag as true
            else {
                flag = true;
            }
 
            // Increment the sum by v[i % N]
            sum = sum + v.get(i % N);
        }
 
        // Return the resultant sum
        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 Dharanendra L V.


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
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;
 
    // Stores the array elements
    List v = new List();
 
    // Traverse the array
    for(int i = 0; i < N; i++)
    {
        v.Add(arr[i]);
    }
 
    // Stores if the operation
    // should be formed or not
    bool flag = false;
 
    // Traverse the vector V
    for(int i = 0; i < v.Count; i++)
    {
         
        // If flag is false and if
        // v[i] is divisible by K
        if (!flag && v[i] % K == 0)
            v.Add(v[i] / K);
 
        // Otherwise, set flag as true
        else
        {
            flag = true;
        }
 
        // Increment the sum by v[i % N]
        sum = sum + v[i % N];
    }
 
    // Return the resultant sum
    return sum;
}
 
// Driver Code
static void Main()
{
    int[] arr = { 4, 6, 8, 2 };
    int K = 2;
    int N = arr.Length;
     
    Console.WriteLine(sum(arr, N, K));
}
}
 
// This code is contributed by SoumikMondal


Javascript


输出:
44

时间复杂度: O(N * K * log N)
辅助空间: O(M),M 是数组的最大元素

高效的方法:上述方法也可以基于以下观察进行优化:

  • 如果arr[i]可被K整除,则添加arr[i] / KK次将总和增加arr[i]
  • 因此,这个想法是只在向量的末尾推送arr[i] / K一次。

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

  • 初始化一个变量,比如sum0 ,它存储所有数组元素 array A[]的总和。
  • 初始化一个数组,比如A[]并将所有数组元素arr[] 存储A[] 中
  • 初始化一个变量,比如flag0 ,它存储元素是否要添加到数组的末尾。
  • 遍历数组A[]并执行以下步骤:
    • 如果值标志0并且A[i]可被K整除,则将A[i]推入V的末尾。
    • 否则,将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;
}

Java

// Java program for the above approach
 
import java.io.*;
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;
 
        // Stores the array elements
        ArrayList v = new ArrayList();
 
        // Traverse the array
        for (int i = 0; i < N; i++) {
            v.add(arr[i]);
        }
 
        // Stores if the operation
        // should be formed or not
        boolean flag = false;
 
        // 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.get(i) % K == 0)
                v.add(v.get(i) / K);
 
            // Otherwise, set flag as true
            else {
                flag = true;
            }
 
            // Increment the sum by v[i % N]
            sum = sum + v.get(i % N);
        }
 
        // Return the resultant sum
        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 Dharanendra L V.

C#

// C# program for the above approach
using System;
using System.Collections.Generic;
 
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;
 
    // Stores the array elements
    List v = new List();
 
    // Traverse the array
    for(int i = 0; i < N; i++)
    {
        v.Add(arr[i]);
    }
 
    // Stores if the operation
    // should be formed or not
    bool flag = false;
 
    // Traverse the vector V
    for(int i = 0; i < v.Count; i++)
    {
         
        // If flag is false and if
        // v[i] is divisible by K
        if (!flag && v[i] % K == 0)
            v.Add(v[i] / K);
 
        // Otherwise, set flag as true
        else
        {
            flag = true;
        }
 
        // Increment the sum by v[i % N]
        sum = sum + v[i % N];
    }
 
    // Return the resultant sum
    return sum;
}
 
// Driver Code
static void Main()
{
    int[] arr = { 4, 6, 8, 2 };
    int K = 2;
    int N = arr.Length;
     
    Console.WriteLine(sum(arr, N, K));
}
}
 
// This code is contributed by SoumikMondal

Javascript


输出:
44

时间复杂度: O(N * log N)
辅助空间: O(N * log N)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live