Sunday, July 31, 2022
HomeWordPress DevelopmentMost doable distinction between two Subarrays after eradicating N components from Array

Most doable distinction between two Subarrays after eradicating N components from Array


import copy

import heapq

from collections import Counter

  

  

def best_remove(left, proper):

  

    temp = []

    heapq.heapify(left)

    heapq.heapify(proper)

  

    if len(left) == n and len(proper) > n:

        

        temp.lengthen(heapq.nlargest(n, proper))

    elif len(proper) == n and len(left) > n:

        

        temp.lengthen(heapq.nsmallest(n, left))

    else:

        x = len(left) - n

        temp.lengthen(heapq.nsmallest(x, left)+heapq.nlargest(n-x, proper))

    return temp

  

  

def remove_elements(guardian, little one):

  

    f_s = Counter(little one)

      

    

    

    r_h = []  

  

    for i in guardian:

        if i in f_s and f_s[i] > 0:

            f_s[i] -= 1

        else:

            r_h.append(i)

  

    

    return r_h

  

    

    

    

    

    

    

  

    

  

  

def max_diff(arr): 

    

  

    mid = len(arr)//2

  

    left = sum(arr[:mid])

    proper = sum(arr[mid:])

    return left-proper

  

  

arr = [7, 9, 5, 8, 1, 3]

n = len(arr)//3

  

final_max = -float("inf")

  

for i in vary(n, 2 * n + 1):

  

    left = arr[:i] 

    proper = arr[i:] 

  

    

  

    

    best_remove_elements = best_remove(left, proper)

    

  

    dup = []

    

    dup = copy.deepcopy(arr)

  

    

    remove_element = remove_elements(dup, best_remove_elements)

    

    

  

    curr_max = max_diff(remove_element) 

    final_max = max(final_max, curr_max)

  

print("The utmost diffrence between S1 and S2 is", final_max)

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments