# Find minimum value of K to maximise sum of elements on indices that are multiples of K

Given an array **arr[]** of **N** integers, the task is to find the minimum value of **K** such that the sum of elements on indices that are multiples of **K** is the maximum possible.

**Example:**

Input:arr[] = {-3, 4}Output:2Explanation: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

**Approach: **The given problem can be solved by a similar to that of the Sieve of Eratosthenes. The idea is to calculate the sum for all possible values of **K** in the range **[1, N]** by iterating over each multiple of **K** as done while marking the non-prime elements in the Sieve. The value of **K** giving the maximum sum is the required answer.

Below is the implementation of the above approach:

## C++

`// C++ Program of the above approach` `#include <bits/stdc++.h>` `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

`<script>` ` ` `// JavaScript code for the above approach` ` ` `// Function to find minimum K such that` ` ` `// the sum of elements on indices that` ` ` `// are multiples of K is maximum possible` ` ` `function` `maxSum(arr, N) {` ` ` `// Stores the maximum sum and` ` ` `// respective K value` ` ` `let maxSum = -999999;` ` ` `let ans = -1;` ` ` `// Loop to iterate over all` ` ` `// value of K in range [1, N]` ` ` `for` `(let K = 1; K <= N; K++) {` ` ` `let sum = 0;` ` ` `// Iterating over all` ` ` `// multiples of K` ` ` `for` `(let i = K; i <= N; i += K) {` ` ` `sum = sum + arr[i - 1];` ` ` `}` ` ` `// Update Maximum Sum` ` ` `if` `(sum > maxSum) {` ` ` `maxSum = sum;` ` ` `ans = K;` ` ` `}` ` ` `}` ` ` `// Return Answer` ` ` `return` `ans;` ` ` `}` ` ` `// Driver Code` ` ` `let arr = [-1, -2, -3];` ` ` `let N = arr.length;` ` ` `document.write(maxSum(arr, N));` `// This code is contributed by Potta Lokesh` ` ` `</script>` |

**Output**

2

**Time Complexity:** O(N*log N)**Auxiliary space: **O(1)