Maximum Subarray Sum Excluding Certain Elements | Java Program to find max subarray sum excluding some elements ; 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 ; set max_so_far to INT_MIN ; if the element is present in B , set current max to 0 and move to the next element ; Proceed as in Kadane 's Algorithm ; Wrapper for findMaxSubarraySumUtil ( ) ; sort array B to apply Binary Search ; This case will occour when all elements of A are present in B , thus no subarray can be formed ; Driver Code ; Function call
import java . util . * ; class GFG { static int findMaxSubarraySumUtil ( int A [ ] , int B [ ] , int n , int m ) { int max_so_far = Integer . MIN_VALUE , curr_max = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( Arrays . binarySearch ( B , A [ i ] ) >= 0 ) { curr_max = 0 ; continue ; } curr_max = Math . max ( A [ i ] , curr_max + A [ i ] ) ; max_so_far = Math . max ( max_so_far , curr_max ) ; } return max_so_far ; } static void findMaxSubarraySum ( int A [ ] , int B [ ] , int n , int m ) { Arrays . sort ( B ) ; int maxSubarraySum = findMaxSubarraySumUtil ( A , B , n , m ) ; if ( maxSubarraySum == Integer . MIN_VALUE ) { System . out . println ( " Maximum ▁ subarray ▁ sum ▁ cant ▁ be ▁ found " ) ; } else { System . out . println ( " The ▁ Maximum ▁ subarray ▁ sum ▁ = ▁ " + maxSubarraySum ) ; } } 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 ; findMaxSubarraySum ( A , B , n , m ) ; } }