给定n 个盒子,里面装着一些排成一排的巧克力。有k个学生。问题是通过从给定批次中选择一系列连续的盒子,在k 个学生之间平均分配最大数量的巧克力。考虑这些盒子从左到右排列成一行,数字从 1 到 n。我们必须选择一组连续排列的盒子,这些盒子可以为所有k 个学生平均提供最大数量的巧克力。数组arr[]表示盒子的行排列,arr[i] 表示该盒子中位于“i”位置的巧克力数量。
例子:
Input : arr[] = {2, 7, 6, 1, 4, 5}, k = 3
Output : 6
The subarray is {7, 6, 1, 4} with sum 18.
Equal distribution of 18 chocolates among
3 students is 6.
Note that the selected boxes are in consecutive order
with indexes {1, 2, 3, 4}.
资料来源:在亚马逊上询问。
问题是找到可被k整除的最大和子数组,然后返回(sum/k)。
方法 1(朴素方法):考虑所有子数组的总和。选择最大总和。让它成为maxSum 。返回(maxSum / k) 。时间复杂度为 O(n 2 )。
方法 2(高效方法):创建一个数组sum[] ,其中sum[i]存储sum(arr[0]+..arr[i]) 。创建一个包含元组为(ele, idx)的哈希表,其中 ele 表示(sum[i] % k)的元素, idx表示从左到右遍历数组sum[]时第一次出现的元素索引。现在遍历sum[]从 i = 0 到 n 并按照下面给出的步骤进行操作。
- 计算当前余数为curr_rem = sum[i] % k。
- 如果 curr_rem == 0,则检查是否 maxSum < sum[i],更新maxSum = sum[i]。
- 否则,如果哈希表中不存在curr_rem,则在哈希表中创建元组(curr_rem, i) 。
- 否则,在哈希表中获取与curr_rem关联的值。让它成为idx 。现在,如果 maxSum < (sum[i] – sum[idx]) 然后更新maxSum = sum[i] – sum[idx]。
最后,返回(maxSum / k) 。
解释:
如果 (sum[i] % k) == (sum[j] % k),其中 sum[i] = sum(arr[0]+..+arr[i]) 和 sum[j] = sum(arr [0]+..+arr[j]) 且 ‘i’ 小于 ‘j’,则 sum(arr[i+1]+..+arr[j]) 必须能被 ‘k’ 整除。
C++
// C++ implementation to find the maximum number
// of chocolates to be distributed equally among
// k students
#include
using namespace std;
// function to find the maximum number of chocolates
// to be distributed equally among k students
int maxNumOfChocolates(int arr[], int n, int k)
{
// unordered_map 'um' implemented as
// hash table
unordered_map um;
// 'sum[]' to store cumulative sum, where
// sum[i] = sum(arr[0]+..arr[i])
int sum[n], curr_rem;
// to store sum of sub-array having maximum sum
int maxSum = 0;
// building up 'sum[]'
sum[0] = arr[0];
for (int i = 1; i < n; i++)
sum[i] = sum[i - 1] + arr[i];
// traversing 'sum[]'
for (int i = 0; i < n; i++) {
// finding current remainder
curr_rem = sum[i] % k;
// if true then sum(0..i) is divisible
// by k
if (curr_rem == 0) {
// update 'maxSum'
if (maxSum < sum[i])
maxSum = sum[i];
}
// if value 'curr_rem' not present in 'um'
// then store it in 'um' with index of its
// first occurrence
else if (um.find(curr_rem) == um.end())
um[curr_rem] = i;
else
// if true, then update 'max'
if (maxSum < (sum[i] - sum[um[curr_rem]]))
maxSum = sum[i] - sum[um[curr_rem]];
}
// required maximum number of chocolates to be
// distributed equally among 'k' students
return (maxSum / k);
}
// Driver program to test above
int main()
{
int arr[] = { 2, 7, 6, 1, 4, 5 };
int n = sizeof(arr) / sizeof(arr[0]);
int k = 3;
cout << "Maximum number of chocolates: "
<< maxNumOfChocolates(arr, n, k);
return 0;
}
Java
// Java implementation to find the maximum number
// of chocolates to be distributed equally among
// k students
import java.io.*;
import java.util.*;
class GFG {
// Function to find the maximum number of chocolates
// to be distributed equally among k students
static int maxNumOfChocolates(int arr[], int n, int k)
{
// Hash table
HashMap um = new HashMap();
// 'sum[]' to store cumulative sum, where
// sum[i] = sum(arr[0]+..arr[i])
int[] sum=new int[n];
int curr_rem;
// To store sum of sub-array having maximum sum
int maxSum = 0;
// Building up 'sum[]'
sum[0] = arr[0];
for (int i = 1; i < n; i++)
sum[i] = sum[i - 1] + arr[i];
// Traversing 'sum[]'
for (int i = 0; i < n; i++) {
// Finding current remainder
curr_rem = sum[i] % k;
// If true then sum(0..i) is divisible
// by k
if (curr_rem == 0) {
// update 'maxSum'
if (maxSum < sum[i])
maxSum = sum[i];
}
// If value 'curr_rem' not present in 'um'
// then store it in 'um' with index of its
// first occurrence
else if (!um.containsKey(curr_rem) )
um.put(curr_rem , i);
else
// If true, then update 'max'
if (maxSum < (sum[i] - sum[um.get(curr_rem)]))
maxSum = sum[i] - sum[um.get(curr_rem)];
}
// Required maximum number of chocolates to be
// distributed equally among 'k' students
return (maxSum / k);
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 2, 7, 6, 1, 4, 5 };
int n = arr.length;
int k = 3;
System.out.println("Maximum number of chocolates: "
+ maxNumOfChocolates(arr, n, k));
}
}
// This code is contributed by 'Gitanjali'.
Python3
# Python3 implementation to
# find the maximum number
# of chocolates to be
# distributed equally
# among k students
# function to find the
# maximum number of chocolates
# to be distributed equally
# among k students
def maxNumOfChocolates(arr, n, k):
um, curr_rem, maxSum = {}, 0, 0
# 'sm[]' to store cumulative sm,
# where sm[i] = sm(arr[0]+..arr[i])
sm = [0]*n
sm[0] = arr[0]
# building up 'sm[]'
for i in range(1, n):
sm[i] = sm[i - 1] + arr[i]
# traversing 'sm[]'
for i in range(n):
# finding current remainder
curr_rem = sm[i] % k
if (not curr_rem and maxSum < sm[i]) :
maxSum = sm[i]
elif (not curr_rem in um) :
um[curr_rem] = i
elif (maxSum < (sm[i] - sm[um[curr_rem]])):
maxSum = sm[i] - sm[um[curr_rem]]
return maxSum//k
# Driver program to test above
arr = [ 2, 7, 6, 1, 4, 5 ]
n, k = len(arr), 3
print("Maximum number of chocolates: " +
str(maxNumOfChocolates(arr, n, k)))
# This code is contributed by Ansu Kumari
C#
// C# implementation to find
// the maximum number of
// chocolates to be distributed
// equally among k students
using System;
using System.Collections.Generic;
class GFG
{
// Function to find the
// maximum number of
// chocolates to be distributed
// equally among k students
static int maxNumOfChocolates(int []arr,
int n, int k)
{
// Hash table
Dictionary um =
new Dictionary();
// 'sum[]' to store cumulative
// sum, where sum[i] =
// sum(arr[0]+..arr[i])
int[] sum = new int[n];
int curr_rem;
// To store sum of sub-array
// having maximum sum
int maxSum = 0;
// Building up 'sum[]'
sum[0] = arr[0];
for (int i = 1; i < n; i++)
sum[i] = sum[i - 1] + arr[i];
// Traversing 'sum[]'
for (int i = 0; i < n; i++)
{
// Finding current
// remainder
curr_rem = sum[i] % k;
// If true then sum(0..i)
// is divisible by k
if (curr_rem == 0)
{
// update 'maxSum'
if (maxSum < sum[i])
maxSum = sum[i];
}
// If value 'curr_rem' not
// present in 'um' then store
// it in 'um' with index of
// its first occurrence
else if (!um.ContainsKey(curr_rem))
um.Add(curr_rem , i);
else
// If true, then
// update 'max'
if (maxSum < (sum[i] -
sum[um[curr_rem]]))
maxSum = sum[i] -
sum[um[curr_rem]];
}
// Required maximum number
// of chocolates to be
// distributed equally
// among 'k' students
return (maxSum / k);
}
// Driver Code
static void Main()
{
int []arr = new int[]{ 2, 7, 6, 1, 4, 5 };
int n = arr.Length;
int k = 3;
Console.Write("Maximum number of chocolates: " +
maxNumOfChocolates(arr, n, k));
}
}
// This code is contributed by
// Manish Shaw(manishshaw1)
Javascript
输出 :
Maximum number of chocolates: 6
时间复杂度: O(n)。
辅助空间: O(n)。
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。