给定一个由 n 个整数组成的数组 A 和一个由 m 个整数组成的数组 B,找到数组 A 的最大连续子数组和,使得数组 B 的任何元素都不存在于该子数组中。
例子 :
Input : A = {1, 7, -10, 6, 2}, B = {5, 6, 7, 1}
Output : 2
Explanation Since the Maximum Sum Subarray of A is not allowed to have any element that is present in array B.
The Maximum Sum Subarray satisfying this is {2} as the only allowed subarrays are:{-10} and {2}. The Maximum Sum Subarray being {2} which sums to 2
Input : A = {3, 4, 5, -4, 6}, B = {1, 8, 5}
Output : 7
Explanation
The Maximum Sum Subarray satisfying this is {3, 4} as the only allowed subarrays are {3}, {4}, {3, 4}, {-4}, {6}, {-4, 6}. The Maximum Sum subarray being {3, 4} which sums to 7
方法 1(O(n*m) 方法):
我们可以使用 Kadane 算法解决这个问题。由于我们不希望数组 B 的任何元素成为 A 的任何子数组的一部分,因此我们需要稍微修改经典的 Kadane 算法。
每当我们考虑 Kadane 算法中的一个元素时,我们要么扩展当前的子数组,要么开始一个新的子数组。
curr_max = max(a[i], curr_max+a[i]);
if (curr_max < 0)
curr_max = 0
现在,在这个问题中,当我们考虑任何元素时,我们通过在数组 B 中线性搜索来检查,如果该元素存在于 B 中,那么我们将 curr_max 设置为零,这意味着在该索引处我们考虑到该点的所有子数组都将结束并且不能进一步扩展,因为不能形成进一步的连续数组,即
If Ai is present in B, all subarrays in A from 0 to (i – 1) cannot be extended further as, the ith element can never be included in any subarray
如果数组 A 的当前元素不是数组 B 的一部分,我们继续使用 Kadane 算法并跟踪 max_so_far。
C++
// C++ Program to find max subarray
// sum excluding some elements
#include
using namespace std;
// Function to check the element
// present in array B
bool isPresent(int B[], int m, int x)
{
for (int i = 0; i < m; i++)
if (B[i] == x)
return true;
return false;
}
// Utility function for findMaxSubarraySum()
// with the following parameters
// A => Array A,
// B => Array B,
// n => Number of elements in Array A,
// m => Number of elements in Array B
int findMaxSubarraySumUtil(int A[], int B[], int n, int m)
{
// set max_so_far to INT_MIN
int max_so_far = INT_MIN, curr_max = 0;
for (int i = 0; i < n; i++)
{
// if the element is present in B,
// set current max to 0 and move to
// the next element */
if (isPresent(B, m, A[i]))
{
curr_max = 0;
continue;
}
// Proceed as in Kadane's Algorithm
curr_max = max(A[i], curr_max + A[i]);
max_so_far = max(max_so_far, curr_max);
}
return max_so_far;
}
// Wrapper for findMaxSubarraySumUtil()
void findMaxSubarraySum(int A[], int B[], int n, int m)
{
int maxSubarraySum = findMaxSubarraySumUtil(A, B, n, m);
// This case will occur when all elements
// of A are present in B, thus
// no subarray can be formed
if (maxSubarraySum == INT_MIN)
{
cout << "Maximum Subarray Sum cant be found"
<< endl;
}
else
{
cout << "The Maximum Subarray Sum = "
<< maxSubarraySum << endl;
}
}
// Driver Code
int main()
{
int A[] = { 3, 4, 5, -4, 6 };
int B[] = { 1, 8, 5 };
int n = sizeof(A) / sizeof(A[0]);
int m = sizeof(B) / sizeof(B[0]);
// Function call
findMaxSubarraySum(A, B, n, m);
return 0;
}
Java
// Java Program to find max subarray
// sum excluding some elements
import java.io.*;
class GFG {
// Function to check the element
// present in array B
static boolean isPresent(int B[], int m, int x)
{
for (int i = 0; i < m; i++)
if (B[i] == x)
return true;
return false;
}
// Utility function for findMaxSubarraySum()
// with the following parameters
// A => Array A,
// B => Array B,
// n => Number of elements in Array A,
// m => Number of elements in Array B
static int findMaxSubarraySumUtil(int A[], int B[],
int n, int m)
{
// set max_so_far to INT_MIN
int max_so_far = -2147483648, curr_max = 0;
for (int i = 0; i < n; i++)
{
// if the element is present in B,
// set current max to 0 and move to
// the next element
if (isPresent(B, m, A[i]))
{
curr_max = 0;
continue;
}
// Proceed as in Kadane's Algorithm
curr_max = Math.max(A[i], curr_max + A[i]);
max_so_far = Math.max(max_so_far, curr_max);
}
return max_so_far;
}
// Wrapper for findMaxSubarraySumUtil()
static void findMaxSubarraySum(int A[], int B[], int n,
int m)
{
int maxSubarraySum
= findMaxSubarraySumUtil(A, B, n, m);
// This case will occur when all
// elements of A are present
// in B, thus no subarray can be formed
if (maxSubarraySum == -2147483648)
{
System.out.println("Maximum Subarray Sum"
+ " "
+ "can't be found");
}
else {
System.out.println("The Maximum Subarray Sum = "
+ maxSubarraySum);
}
}
// Driver code
public static void main(String[] args)
{
int A[] = { 3, 4, 5, -4, 6 };
int B[] = { 1, 8, 5 };
int n = A.length;
int m = B.length;
// Function call
findMaxSubarraySum(A, B, n, m);
}
}
// This code is contributed by Ajit.
Python3
# Python Program to find max
# subarray sum excluding some
# elements
# Function to check the element
# present in array B
INT_MIN = -2147483648
def isPresent(B, m, x):
for i in range(0, m):
if B[i] == x:
return True
return False
# Utility function for findMaxSubarraySum()
# with the following parameters
# A => Array A,
# B => Array B,
# n => Number of elements in Array A,
# m => Number of elements in Array B
def findMaxSubarraySumUtil(A, B, n, m):
# set max_so_far to INT_MIN
max_so_far = INT_MIN
curr_max = 0
for i in range(0, n):
if isPresent(B, m, A[i]) == True:
curr_max = 0
continue
# Proceed as in Kadane's Algorithm
curr_max = max(A[i], curr_max + A[i])
max_so_far = max(max_so_far, curr_max)
return max_so_far
# Wrapper for findMaxSubarraySumUtil()
def findMaxSubarraySum(A, B, n, m):
maxSubarraySum = findMaxSubarraySumUtil(A, B, n, m)
# This case will occur when all
# elements of A are present
# in B, thus no subarray can be
# formed
if maxSubarraySum == INT_MIN:
print('Maximum Subarray Sum cant be found')
else:
print('The Maximum Subarray Sum =',
maxSubarraySum)
# Driver code
A = [3, 4, 5, -4, 6]
B = [1, 8, 5]
n = len(A)
m = len(B)
# Function call
findMaxSubarraySum(A, B, n, m)
# This code is contributed
# by sahil shelangia
C#
// C# Program to find max subarray
// sum excluding some elements
using System;
class GFG {
// Function to check the element
// present in array B
static bool isPresent(int[] B, int m, int x)
{
for (int i = 0; i < m; i++)
if (B[i] == x)
return true;
return false;
}
// Utility function for findMaxSubarraySum()
// with the following parameters
// A => Array A,
// B => Array B,
// n => Number of elements in Array A,
// m => Number of elements in Array B
static int findMaxSubarraySumUtil(int[] A, int[] B,
int n, int m)
{
// set max_so_far to INT_MIN
int max_so_far = -2147483648, curr_max = 0;
for (int i = 0; i < n; i++)
{
// if the element is present in B,
// set current max to 0 and move to
// the next element
if (isPresent(B, m, A[i]))
{
curr_max = 0;
continue;
}
// Proceed as in Kadane's Algorithm
curr_max = Math.Max(A[i], curr_max + A[i]);
max_so_far = Math.Max(max_so_far, curr_max);
}
return max_so_far;
}
// Wrapper for findMaxSubarraySumUtil()
static void findMaxSubarraySum(int[] A, int[] B, int n,
int m)
{
int maxSubarraySum
= findMaxSubarraySumUtil(A, B, n, m);
// This case will occur when all
// elements of A are present
// in B, thus no subarray can be formed
if (maxSubarraySum == -2147483648)
{
Console.Write("Maximum Subarray Sum"
+ " "
+ "can't be found");
}
else {
Console.Write("The Maximum Subarray Sum = "
+ maxSubarraySum);
}
}
// Driver Code
static public void Main()
{
int[] A = { 3, 4, 5, -4, 6 };
int[] B = { 1, 8, 5 };
int n = A.Length;
int m = B.Length;
// Function call
findMaxSubarraySum(A, B, n, m);
}
}
// This code is contributed by Shrikant13.
PHP
Array A,
// B => Array B,
// n => Number of elements
// in Array A,
// m => Number of elements
// in Array B
function findMaxSubarraySumUtil($A, $B,
$n, $m)
{
// set max_so_far
// to INT_MIN
$max_so_far = PHP_INT_MIN;
$curr_max = 0;
for ($i = 0; $i < $n; $i++)
{
// if the element is present
// in B, set current max to
// 0 and move to the next
// element
if (isPresent($B, $m, $A[$i]))
{
$curr_max = 0;
continue;
}
// Proceed as in
// Kadane's Algorithm
$curr_max = max($A[$i],
$curr_max + $A[$i]);
$max_so_far = max($max_so_far,
$curr_max);
}
return $max_so_far;
}
// Wrapper for
// findMaxSubarraySumUtil()
function findMaxSubarraySum($A, $B,
$n, $m)
{
$maxSubarraySum = findMaxSubarraySumUtil($A, $B,
$n, $m);
// This case will occur
// when all elements of
// A are present in
// B, thus no subarray
// can be formed
if ($maxSubarraySum == PHP_INT_MIN)
{
echo ("Maximum Subarray " .
"Sum cant be found\n");
}
else
{
echo ("The Maximum Subarray Sum = ".
$maxSubarraySum. "\n");
}
}
// Driver Code
$A = array(3, 4, 5, -4, 6);
$B = array(1, 8, 5);
$n = count($A);
$m = count($B);
// Function call
findMaxSubarraySum($A, $B, $n, $m);
// This code is contributed by
// Manish Shaw(manishshaw1)
?>
Javascript
C++
// C++ Program to find max subarray
// sum excluding some elements
#include
using namespace std;
// Utility function for findMaxSubarraySum()
// with the following parameters
// A => Array A,
// B => Array B,
// n => Number of elements in Array A,
// m => Number of elements in Array B
int findMaxSubarraySumUtil(int A[], int B[], int n, int m)
{
// set max_so_far to INT_MIN
int max_so_far = INT_MIN, curr_max = 0;
for (int i = 0; i < n; i++) {
// if the element is present in B,
// set current max to 0 and move to
// the next element
if (binary_search(B, B + m, A[i])) {
curr_max = 0;
continue;
}
// Proceed as in Kadane's Algorithm
curr_max = max(A[i], curr_max + A[i]);
max_so_far = max(max_so_far, curr_max);
}
return max_so_far;
}
// Wrapper for findMaxSubarraySumUtil()
void findMaxSubarraySum(int A[], int B[], int n, int m)
{
// sort array B to apply Binary Search
sort(B, B + m);
int maxSubarraySum = findMaxSubarraySumUtil(A, B, n, m);
// This case will occur when all elements
// of A are present in B, thus no subarray
// can be formed
if (maxSubarraySum == INT_MIN) {
cout << "Maximum subarray sum cant be found"
<< endl;
}
else {
cout << "The Maximum subarray sum = "
<< maxSubarraySum << endl;
}
}
// Driver Code
int main()
{
int A[] = { 3, 4, 5, -4, 6 };
int B[] = { 1, 8, 5 };
int n = sizeof(A) / sizeof(A[0]);
int m = sizeof(B) / sizeof(B[0]);
// Function call
findMaxSubarraySum(A, B, n, m);
return 0;
}
Java
// Java Program to find max subarray
// sum excluding some elements
import java.util.*;
class GFG {
// Utility function for findMaxSubarraySum()
// with the following parameters
// A => Array A,
// B => Array B,
// n => Number of elements in Array A,
// m => Number of elements in Array B
static int findMaxSubarraySumUtil(int A[], int B[],
int n, int m)
{
// set max_so_far to INT_MIN
int max_so_far = Integer.MIN_VALUE, curr_max = 0;
for (int i = 0; i < n; i++)
{
// if the element is present in B,
// set current max to 0 and move to
// the next element
if (Arrays.binarySearch(B, A[i]) >= 0)
{
curr_max = 0;
continue;
}
// Proceed as in Kadane's Algorithm
curr_max = Math.max(A[i], curr_max + A[i]);
max_so_far = Math.max(max_so_far, curr_max);
}
return max_so_far;
}
// Wrapper for findMaxSubarraySumUtil()
static void findMaxSubarraySum(int A[], int B[], int n,
int m)
{
// sort array B to apply Binary Search
Arrays.sort(B);
int maxSubarraySum
= findMaxSubarraySumUtil(A, B, n, m);
// This case will occur when all elements
// of A are present in B, thus no subarray
// can be formed
if (maxSubarraySum == Integer.MIN_VALUE)
{
System.out.println(
"Maximum subarray sum cant be found");
}
else
{
System.out.println("The Maximum subarray sum = "
+ maxSubarraySum);
}
}
// Driver Code
public static void main(String[] args)
{
int A[] = { 3, 4, 5, -4, 6 };
int B[] = { 1, 8, 5 };
int n = A.length;
int m = B.length;
// Function call
findMaxSubarraySum(A, B, n, m);
}
}
// This code has been contributed by 29AjayKumar
C#
// C# Program to find max subarray
// sum excluding some elements
using System;
class GFG {
// Utility function for findMaxSubarraySum()
// with the following parameters
// A => Array A,
// B => Array B,
// n => Number of elements in Array A,
// m => Number of elements in Array B
static int findMaxSubarraySumUtil(int []A, int []B,
int n, int m)
{
// set max_so_far to INT_MIN
int max_so_far = int.MinValue, curr_max = 0;
for (int i = 0; i < n; i++)
{
// if the element is present in B,
// set current max to 0 and move to
// the next element
if (Array.BinarySearch(B, A[i]) >= 0)
{
curr_max = 0;
continue;
}
// Proceed as in Kadane's Algorithm
curr_max = Math.Max(A[i], curr_max + A[i]);
max_so_far = Math.Max(max_so_far, curr_max);
}
return max_so_far;
}
// Wrapper for findMaxSubarraySumUtil()
static void findMaxSubarraySum(int []A, int []B, int n,
int m)
{
// sort array B to apply Binary Search
Array.Sort(B);
int maxSubarraySum
= findMaxSubarraySumUtil(A, B, n, m);
// This case will occur when all elements
// of A are present in B, thus no subarray
// can be formed
if (maxSubarraySum == int.MinValue)
{
Console.WriteLine(
"Maximum subarray sum cant be found");
}
else
{
Console.WriteLine("The Maximum subarray sum = "
+ maxSubarraySum);
}
}
// Driver Code
public static void Main(params string[] args)
{
int []A = { 3, 4, 5, -4, 6 };
int []B = { 1, 8, 5 };
int n = A.Length;
int m = B.Length;
// Function call
findMaxSubarraySum(A, B, n, m);
}
}
// This code is contributed by pratham76.
C++
// C++ Program implementation of the
// above idea
#include
using namespace std;
// Function to calculate the max sum of contiguous
// subarray of B whose elements are not present in A
int findMaxSubarraySum(vector A,vector B)
{
unordered_map m;
// mark all the elements present in B
for(int i=0;i a = { 3, 4, 5, -4, 6 };
vector b = { 1, 8, 5 };
// Function call
cout << findMaxSubarraySum(a,b);
return 0;
}
//This code is contributed by G.Vivek
Java
// Java Program implementation of the
// above idea
import java.util.*;
class GFG
{
// Function to calculate the max sum of contiguous
// subarray of B whose elements are not present in A
static int findMaxSubarraySum(int A[], int B[])
{
HashMap m = new HashMap<>();
// mark all the elements present in B
for(int i = 0; i < B.length; i++)
{
m.put(B[i], 1);
}
// initialize max_so_far with INT_MIN
int max_so_far = Integer.MIN_VALUE;
int currmax = 0;
// traverse the array A
for(int i = 0; i < A.length; i++)
{
if(currmax < 0 || (m.containsKey(A[i]) && m.get(A[i]) == 1))
{
currmax = 0;
continue;
}
currmax = Math.max(A[i], A[i] + currmax);
// if current max is greater than
// max so far then update max so far
if(max_so_far < currmax)
{
max_so_far = currmax;
}
}
return max_so_far;
}
// Driver code
public static void main(String[] args)
{
int a[] = { 3, 4, 5, -4, 6 };
int b[] = { 1, 8, 5 };
// Function call
System.out.println(findMaxSubarraySum(a, b));
}
}
// This code is contributed by divyesh072019.
Python3
# Python3 program implementation of the
# above idea
import sys
# Function to calculate the max sum of
# contiguous subarray of B whose elements
# are not present in A
def findMaxSubarraySum(A, B):
m = dict()
# Mark all the elements present in B
for i in range(len(B)):
if B[i] not in m:
m[B[i]] = 0
m[B[i]] = 1
# Initialize max_so_far with INT_MIN
max_so_far = -sys.maxsize - 1
currmax = 0
# Traverse the array A
for i in range(len(A)):
if (currmax < 0 or (A[i] in m and m[A[i]] == 1)):
currmax = 0
continue
currmax = max(A[i], A[i] + currmax)
# If current max is greater than
# max so far then update max so far
if (max_so_far
C#
// C# Program implementation of the
// above idea
using System;
using System.Collections.Generic;
class GFG {
// Function to calculate the max sum of contiguous
// subarray of B whose elements are not present in A
static int findMaxSubarraySum(int[] A, int[] B)
{
Dictionary m = new Dictionary();
// mark all the elements present in B
for(int i = 0; i < B.Length; i++)
{
m[B[i]] = 1;
}
// initialize max_so_far with INT_MIN
int max_so_far = Int32.MinValue;
int currmax = 0;
// traverse the array A
for(int i = 0; i < A.Length; i++)
{
if(currmax<0 || (m.ContainsKey(A[i]) && m[A[i]] == 1))
{
currmax = 0;
continue;
}
currmax = Math.Max(A[i],A[i]+currmax);
// if current max is greater than
// max so far then update max so far
if(max_so_far
The Maximum Subarray Sum = 7
时间复杂度: O(n*m)
方法 2(O((n+m)*log(m)) 方法)
这种方法背后的主要思想与方法 1 完全相同。这种方法只是通过使用二进制搜索更快地搜索数组 A 中的数组 B 中的元素
注意:我们需要对数组 B 进行排序以对其应用二分搜索。
C++
// C++ Program to find max subarray
// sum excluding some elements
#include
using namespace std;
// Utility function for findMaxSubarraySum()
// with the following parameters
// A => Array A,
// B => Array B,
// n => Number of elements in Array A,
// m => Number of elements in Array B
int findMaxSubarraySumUtil(int A[], int B[], int n, int m)
{
// set max_so_far to INT_MIN
int max_so_far = INT_MIN, curr_max = 0;
for (int i = 0; i < n; i++) {
// if the element is present in B,
// set current max to 0 and move to
// the next element
if (binary_search(B, B + m, A[i])) {
curr_max = 0;
continue;
}
// Proceed as in Kadane's Algorithm
curr_max = max(A[i], curr_max + A[i]);
max_so_far = max(max_so_far, curr_max);
}
return max_so_far;
}
// Wrapper for findMaxSubarraySumUtil()
void findMaxSubarraySum(int A[], int B[], int n, int m)
{
// sort array B to apply Binary Search
sort(B, B + m);
int maxSubarraySum = findMaxSubarraySumUtil(A, B, n, m);
// This case will occur when all elements
// of A are present in B, thus no subarray
// can be formed
if (maxSubarraySum == INT_MIN) {
cout << "Maximum subarray sum cant be found"
<< endl;
}
else {
cout << "The Maximum subarray sum = "
<< maxSubarraySum << endl;
}
}
// Driver Code
int main()
{
int A[] = { 3, 4, 5, -4, 6 };
int B[] = { 1, 8, 5 };
int n = sizeof(A) / sizeof(A[0]);
int m = sizeof(B) / sizeof(B[0]);
// Function call
findMaxSubarraySum(A, B, n, m);
return 0;
}
Java
// Java Program to find max subarray
// sum excluding some elements
import java.util.*;
class GFG {
// Utility function for findMaxSubarraySum()
// with the following parameters
// A => Array A,
// B => Array B,
// n => Number of elements in Array A,
// m => Number of elements in Array B
static int findMaxSubarraySumUtil(int A[], int B[],
int n, int m)
{
// set max_so_far to INT_MIN
int max_so_far = Integer.MIN_VALUE, curr_max = 0;
for (int i = 0; i < n; i++)
{
// if the element is present in B,
// set current max to 0 and move to
// the next element
if (Arrays.binarySearch(B, A[i]) >= 0)
{
curr_max = 0;
continue;
}
// Proceed as in Kadane's Algorithm
curr_max = Math.max(A[i], curr_max + A[i]);
max_so_far = Math.max(max_so_far, curr_max);
}
return max_so_far;
}
// Wrapper for findMaxSubarraySumUtil()
static void findMaxSubarraySum(int A[], int B[], int n,
int m)
{
// sort array B to apply Binary Search
Arrays.sort(B);
int maxSubarraySum
= findMaxSubarraySumUtil(A, B, n, m);
// This case will occur when all elements
// of A are present in B, thus no subarray
// can be formed
if (maxSubarraySum == Integer.MIN_VALUE)
{
System.out.println(
"Maximum subarray sum cant be found");
}
else
{
System.out.println("The Maximum subarray sum = "
+ maxSubarraySum);
}
}
// Driver Code
public static void main(String[] args)
{
int A[] = { 3, 4, 5, -4, 6 };
int B[] = { 1, 8, 5 };
int n = A.length;
int m = B.length;
// Function call
findMaxSubarraySum(A, B, n, m);
}
}
// This code has been contributed by 29AjayKumar
C#
// C# Program to find max subarray
// sum excluding some elements
using System;
class GFG {
// Utility function for findMaxSubarraySum()
// with the following parameters
// A => Array A,
// B => Array B,
// n => Number of elements in Array A,
// m => Number of elements in Array B
static int findMaxSubarraySumUtil(int []A, int []B,
int n, int m)
{
// set max_so_far to INT_MIN
int max_so_far = int.MinValue, curr_max = 0;
for (int i = 0; i < n; i++)
{
// if the element is present in B,
// set current max to 0 and move to
// the next element
if (Array.BinarySearch(B, A[i]) >= 0)
{
curr_max = 0;
continue;
}
// Proceed as in Kadane's Algorithm
curr_max = Math.Max(A[i], curr_max + A[i]);
max_so_far = Math.Max(max_so_far, curr_max);
}
return max_so_far;
}
// Wrapper for findMaxSubarraySumUtil()
static void findMaxSubarraySum(int []A, int []B, int n,
int m)
{
// sort array B to apply Binary Search
Array.Sort(B);
int maxSubarraySum
= findMaxSubarraySumUtil(A, B, n, m);
// This case will occur when all elements
// of A are present in B, thus no subarray
// can be formed
if (maxSubarraySum == int.MinValue)
{
Console.WriteLine(
"Maximum subarray sum cant be found");
}
else
{
Console.WriteLine("The Maximum subarray sum = "
+ maxSubarraySum);
}
}
// Driver Code
public static void Main(params string[] args)
{
int []A = { 3, 4, 5, -4, 6 };
int []B = { 1, 8, 5 };
int n = A.Length;
int m = B.Length;
// Function call
findMaxSubarraySum(A, B, n, m);
}
}
// This code is contributed by pratham76.
The Maximum subarray sum = 7
时间复杂度: O(nlog(m) + mlog(m)) 或 O((n + m)log(m))。
注意:mlog(m) 因子是由于对数组 B 进行排序。
方法 3: O(max(n,m)) 方法)
这种方法背后的主要思想是将 B 数组保存在映射中,这将帮助您检查 A[i] 是否存在于 B 中。
下面是上述方法的实现:
C++
// C++ Program implementation of the
// above idea
#include
using namespace std;
// Function to calculate the max sum of contiguous
// subarray of B whose elements are not present in A
int findMaxSubarraySum(vector A,vector B)
{
unordered_map m;
// mark all the elements present in B
for(int i=0;i a = { 3, 4, 5, -4, 6 };
vector b = { 1, 8, 5 };
// Function call
cout << findMaxSubarraySum(a,b);
return 0;
}
//This code is contributed by G.Vivek
Java
// Java Program implementation of the
// above idea
import java.util.*;
class GFG
{
// Function to calculate the max sum of contiguous
// subarray of B whose elements are not present in A
static int findMaxSubarraySum(int A[], int B[])
{
HashMap m = new HashMap<>();
// mark all the elements present in B
for(int i = 0; i < B.length; i++)
{
m.put(B[i], 1);
}
// initialize max_so_far with INT_MIN
int max_so_far = Integer.MIN_VALUE;
int currmax = 0;
// traverse the array A
for(int i = 0; i < A.length; i++)
{
if(currmax < 0 || (m.containsKey(A[i]) && m.get(A[i]) == 1))
{
currmax = 0;
continue;
}
currmax = Math.max(A[i], A[i] + currmax);
// if current max is greater than
// max so far then update max so far
if(max_so_far < currmax)
{
max_so_far = currmax;
}
}
return max_so_far;
}
// Driver code
public static void main(String[] args)
{
int a[] = { 3, 4, 5, -4, 6 };
int b[] = { 1, 8, 5 };
// Function call
System.out.println(findMaxSubarraySum(a, b));
}
}
// This code is contributed by divyesh072019.
蟒蛇3
# Python3 program implementation of the
# above idea
import sys
# Function to calculate the max sum of
# contiguous subarray of B whose elements
# are not present in A
def findMaxSubarraySum(A, B):
m = dict()
# Mark all the elements present in B
for i in range(len(B)):
if B[i] not in m:
m[B[i]] = 0
m[B[i]] = 1
# Initialize max_so_far with INT_MIN
max_so_far = -sys.maxsize - 1
currmax = 0
# Traverse the array A
for i in range(len(A)):
if (currmax < 0 or (A[i] in m and m[A[i]] == 1)):
currmax = 0
continue
currmax = max(A[i], A[i] + currmax)
# If current max is greater than
# max so far then update max so far
if (max_so_far
C#
// C# Program implementation of the
// above idea
using System;
using System.Collections.Generic;
class GFG {
// Function to calculate the max sum of contiguous
// subarray of B whose elements are not present in A
static int findMaxSubarraySum(int[] A, int[] B)
{
Dictionary m = new Dictionary();
// mark all the elements present in B
for(int i = 0; i < B.Length; i++)
{
m[B[i]] = 1;
}
// initialize max_so_far with INT_MIN
int max_so_far = Int32.MinValue;
int currmax = 0;
// traverse the array A
for(int i = 0; i < A.Length; i++)
{
if(currmax<0 || (m.ContainsKey(A[i]) && m[A[i]] == 1))
{
currmax = 0;
continue;
}
currmax = Math.Max(A[i],A[i]+currmax);
// if current max is greater than
// max so far then update max so far
if(max_so_far
7
时间复杂度: O(max(n,m))
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。