资源下载链接
资源详情介绍
**Answer:**\n\nThe problem is a classic *“maximum subarray”* (Kadane’s algorithm) with a twist: \nthe subarray must contain at least one element that is **not** a multiple of?\\(k\\). \nSo we need to find the maximum sum of a contiguous segment that contains at least one “good” element.\n\n---\n\n### 1. Observations\n\n* If we ignore the “multiple?of?\\(k\\)” restriction, the maximum subarray sum is obtained by Kadane’s algorithm in \\(O(n)\\) time.\n* A subarray that contains only multiples of?\\(k\\) is **invalid**. \n Therefore, we can split the array into blocks separated by “good” elements (elements that are not multiples of?\\(k\\)). \n Inside each block all numbers are multiples of?\\(k\\); the best subarray that uses only this block is simply the maximum subarray of that block (again Kadane). \n However, any valid subarray must include at least one good element, so the maximum over all blocks is not enough – we must also consider subarrays that start or end inside a block and extend across a good element.\n\n---\n\n### 2. Two?pass Kadane\n\nThe following two?pass algorithm gives the answer in linear time:\n\n```text\nmaxSum = -∞\ncurrent = 0\nhasGood = false\n\n# forward pass – keep the best suffix that ends at i\nfor i = 1 … n:\n current += a[i]\n if a[i] % k != 0: # good element\n hasGood = true\n if hasGood:\n maxSum = max(maxSum, current)\n if current \u003c 0:\n current = 0\n hasGood = false\n\n# backward pass – keep the best prefix that starts at i\ncurrent = 0\nhasGood = false\nfor i = n … 1:\n current += a[i]\n if a[i] % k != 0:\n hasGood = true\n if hasGood:\n maxSum = max(maxSum, current)\n if current \u003c 0:\n current = 0\n hasGood = false\n\nreturn maxSum\n```\n\n*The forward pass* keeps the best suffix ending at each position. \nIf that suffix contains a good element (`hasGood == true`) it is a candidate. \nIf the running sum becomes negative we reset it, because a negative prefix can only reduce future sums.\n\n*The backward pass* is symmetric: it keeps the best prefix starting at each position and again updates the answer only when a good element has been seen.\n\nBoth passes are \\(O(n)\\) and use \\(O(1)\\) extra memory. \nThe algorithm works for any integers (positive, negative, or zero) and correctly handles the case where all numbers are multiples of?\\(k\\) (then `maxSum` stays at \\(-∞\\) and you can return “no valid subarray”).\n\n---\n\n### 3. Correctness Sketch\n\n*Lemma 1.* \nDuring the forward pass, `current` always equals the sum of the longest suffix ending at the current index that has a non?negative sum and contains at least one good element.\n\n*Proof.* \nIf `current` becomes negative we reset it to zero and clear `hasGood`. \nThus the next non?negative `current` starts at the next index. \nWhen we add a new element, if the element is good we set `hasGood = true`. \nTherefore `current` represents exactly the suffix described. ?\n\n*Lemma 2.* \nEvery valid subarray is considered either in the forward or in the backward pass.\n\n*Proof.* \nTake any valid subarray `[l … r]`. \nIf its suffix `[l … r]` contains a good element, then during the forward pass when the loop reaches `r` the variable `current` will equal the sum of this suffix (by Lemma?1) and `hasGood` will be true, so the subarray’s sum will be compared with `maxSum`. \nIf the suffix does not contain a good element, then the subarray must contain a good element at its left end. \nIn that case the backward pass, when it reaches `l`, will have `current` equal to the sum of the prefix `[l … r]` and `hasGood` true, so the subarray’s sum will again be considered. ?\n\n*Theorem.* \nAfter both passes `maxSum` equals the maximum sum of any contiguous subarray that contains at least one element not divisible by?\\(k\\).\n\n*Proof.* \nBy Lemma?2 every valid subarray’s sum is compared with `maxSum`. \n`maxSum` is initialized to \\(-∞\\) and updated only with sums of valid subarrays, so after the loops it contains the maximum among them. ?\n\n---\n\n### 4. Complexity\n\n*Time:* \\(O(n)\\) – two linear scans. \n*Space:* \\(O(1)\\) – only a few integer variables.\n\n---\n\n### 5. Example\n\n```\na = [3, 5, 6, 7, 4, 2], k = 3\n```\n\nMultiples of?3 are 3 and 6.\n\n* Forward pass:\n * i=1: current=3, hasGood=false → no update\n * i=2: current=8, hasGood=true → maxSum=8\n * i=3: current=14, hasGood=true → maxSum=14\n * i=4: current=21, hasGood=true → maxSum=21\n * i=5: current=25, hasGood=true → maxSum=25\n * i=6: current=27, hasGood=true → maxSum=27\n\n* Backward pass does not increase the value.\n\nThe answer is **27**, which is the sum of the whole array – the maximum subarray that contains the good elements 5,?7,?4,?2.。