1

I have this coding question, that i was unable to complete

Given an array, find all the subarrays, of length 1 <= k <= len(arr) and find imbalance in those subarrays

Imbalance is when the difference between two neighboring items in a sorted array is more than one

Imbalance is defined as the number of items j who are more than 1 from the item before them, i.e sorted_arr[j] - sorted_arr[j - 1 > 1

for example given array = [4, 1, 3, 2]

The subarrays are:

1. [4]
2. [1]
3. [3]
4. [2]
5. [4, 1]
6. [1, 3]
7. [3, 2]
8. [4, 1, 3]
9. [1, 3, 2]
10. [4, 1, 3, 2]

for each subarray, after sorting them, only 5, 6 and 8 have a case where subarray[i] - subarray[i - 1] > 1, in which case imbalance will be incremented by 1

In the above example, imbalance = 3

heres my code for the probem:

def get_imbalance(arr):
    imbalance = 0
    for i in range(1, len(arr)):
        imbalance += 1 if arr[i] - arr[i - 1] > 1 else 0
    return imbalance

def func(arr):
    imbalance = 0
    if len(arr) <= 1: return 0
    if len(arr) == 2:
        return 1 if abs(arr[0] - arr[1]) > 1 else 0
    for i in range(2, len(arr) + 1):
        for j in range(len(arr) - i + 1):
            imbalance += get_imbalance(sorted(rank[j: j + i]))
    return imbalance

I am using a sliding window to get all the different subarrays of the main array, then sorting the result and returning the imbalance. However, this runs into time limit exceed issues. How can I optimize the algorithm?

Sharhad
  • 11
  • 3
  • how is 10 not imballanced? 4->1 is 3 spaces away ... which is more than 1 .. .ahh i see sorted... – Joran Beasley Oct 01 '22 at 05:21
  • one optimization you could do is `if arr[i] - arr[i-1] > 1:return 1` ... you dont need to keep comparing the rest of the values in that subset but it still might not be fast enough – Joran Beasley Oct 01 '22 at 05:39
  • I think your solution is actually wrong also ... consider `4,1,3,7` which your solution gives 8 ... but i think it is only 6 – Joran Beasley Oct 01 '22 at 05:46
  • It works for all test cases, except for the ones where it times out – Sharhad Oct 01 '22 at 05:48
  • can you run the values i gave you and tell me which 8 are the imbalanced? (maybe the problem statement is wrong, or your answer) the 6 i found were `[1, 4] [1, 3] [3, 7] [1, 3, 4] [1, 3, 7] [1, 3, 4, 7] ` and i think the 2 of length 3 get counted twice in your solution ... if you post a link to the actual problem im curious :P – Joran Beasley Oct 01 '22 at 05:49
  • imbalance is defined as the number of items j who are more than 1 from the item before them, i.e `sorted_arr[j] - sorted_arr[j - 1 > 1` – Sharhad Oct 01 '22 at 05:58
  • ok ... that wasnt clear from problem definition ... in that case neither my optimization nor my suggestion that your solution are wrong will help you :( sorry – Joran Beasley Oct 01 '22 at 05:59
  • Maybe memoize the result (_ie_, dynamic programming)? – Neil Oct 01 '22 at 06:12
  • I thought about that too, but not sure how to go about implementing that – Sharhad Oct 01 '22 at 08:05

1 Answers1

0

Found solution here: Overall time complexity is O(N^2) https://leetcode.com/discuss/interview-question/2461490/Amazon-OA-or-Group-Imbalance-or-2022/1592718

Sharhad
  • 11
  • 3
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 05 '22 at 13:40