找到 K 的最小值以最大化 K 倍数的索引上的元素总和
给定一个包含N个整数的数组arr[] ,任务是找到K的最小值,使得索引上的元素总和是K的倍数是可能的最大值。
例子:
Input: arr[] = {-3, 4}
Output: 2
Explanation: For the given array, it the value of K = 1, then the multiples of K are {1, 2} which sums up to arr[1] + arr[2] = -3 + 4 = 1. For K = 2, the valid multiple of K is 2 and hence the sum is arr[2] = 4, which is the maximum possible. Hence, K = 2 is a valid answer.
Input: arr[] = {-1, -2, -3}
Output: 2
方法:给定的问题可以通过类似于埃拉托色尼筛法的方法来解决。这个想法是通过迭代K的每个倍数来计算范围[1, N]内所有可能值的总和,同时在筛子中标记非素数元素。给出最大和的K值是所需的答案。
下面是上述方法的实现:
C++
// C++ Program of the above approach
#include
using namespace std;
// Function to find minimum K such that
// the sum of elements on indices that
// are multiples of K is maximum possible
int maxSum(int arr[], int N)
{
// Stores the maximum sum and
// respective K value
int maxSum = INT_MIN, ans = -1;
// Loop to iterate over all
// value of K in range [1, N]
for (int K = 1; K <= N; K++) {
int sum = 0;
// Iterating over all
// multiples of K
for (int i = K; i <= N; i += K) {
sum += arr[i - 1];
}
// Update Maximum Sum
if (sum > maxSum) {
maxSum = sum;
ans = K;
}
}
// Return Answer
return ans;
}
// Driver Code
int main()
{
int arr[] = { -1, -2, -3 };
int N = sizeof(arr) / sizeof(int);
cout << maxSum(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find minimum K such that
// the sum of elements on indices that
// are multiples of K is maximum possible
static int maxSum(int arr[], int N)
{
// Stores the maximum sum and
// respective K value
int maxSum = Integer.MIN_VALUE, ans = -1;
// Loop to iterate over all
// value of K in range [1, N]
for(int K = 1; K <= N; K++)
{
int sum = 0;
// Iterating over all
// multiples of K
for(int i = K; i <= N; i += K)
{
sum += arr[i - 1];
}
// Update Maximum Sum
if (sum > maxSum)
{
maxSum = sum;
ans = K;
}
}
// Return Answer
return ans;
}
// Driver Code
public static void main(String args[])
{
int arr[] = { -1, -2, -3 };
int N = arr.length;
System.out.println(maxSum(arr, N));
}
}
// This code is contributed by Samim Hossain Mondal.
Python3
# Python program for the above approach
import sys
# Function to find minimum K such that
# the sum of elements on indices that
# are multiples of K is maximum possible
def maxSum(arr, N):
# Stores the maximum sum and
# respective K value
maxSum = -sys.maxsize;
ans = -1;
# Loop to iterate over all
# value of K in range [1, N]
for K in range(1,N+1):
sum = 0;
# Iterating over all
# multiples of K
for i in range(K, N + 1,K):
sum += arr[i - 1];
# Update Maximum Sum
if (sum > maxSum):
maxSum = sum;
ans = K;
# Return Answer
return ans;
# Driver Code
if __name__ == '__main__':
arr = [ -1, -2, -3 ];
N = len(arr);
print(maxSum(arr, N));
# This code is contributed by gauravrajput1
C#
// C# program for the above approach
using System;
public class GFG{
// Function to find minimum K such that
// the sum of elements on indices that
// are multiples of K is maximum possible
static int maxSum(int []arr, int N)
{
// Stores the maximum sum and
// respective K value
int maxSum = int.MinValue, ans = -1;
// Loop to iterate over all
// value of K in range [1, N]
for(int K = 1; K <= N; K++)
{
int sum = 0;
// Iterating over all
// multiples of K
for(int i = K; i <= N; i += K)
{
sum += arr[i - 1];
}
// Update Maximum Sum
if (sum > maxSum)
{
maxSum = sum;
ans = K;
}
}
// Return Answer
return ans;
}
// Driver Code
public static void Main(String []args)
{
int []arr = { -1, -2, -3 };
int N = arr.Length;
Console.WriteLine(maxSum(arr, N));
}
}
// This code is contributed by 29AjayKumar
Javascript
输出
2
时间复杂度: O(N*log N)
辅助空间: O(1)