0

I'm looking for a solution for this problem.

I have an array which defines the rule of the order of elements like below.

let rule = [A,B,C,D,E,F,G,H,I,J,K]

I then have another array whose element can be removed or added back. So for example, I have a list like this:

var list = [A,D,E,I,J,K]

Now If I want to add element 'B' to 'list' the list should be

var list = [A,B,D,E,I,J,K]

because 'B' comes after 'A' and before 'D' in the rule array. So the insertion index would be 1 in this case.

The item in the array are not comparable each other (Let's say a developer can change the order of rule list at any time if that make sense). And there needs no duplicates in the array.

I'm not sure if I explained the problem clearly, but I'd like to know a good approach that finds an insertion index.

6
  • 1
    you can assign value to the letters? and then use binary search to find the insertion point? Commented Jan 14, 2022 at 0:46
  • Can there be duplicates, or are the items unique? I take it the items aren't actually comparable to each other? e.g. you don't know B comes after A without having to refer to the rule array
    – Dijkgraaf
    Commented Jan 14, 2022 at 0:54
  • No duplicates and elements are not comparable.
    – BokyobT
    Commented Jan 14, 2022 at 7:26
  • What do you mean by "the elements in the array are not comparable"? Doesn't the question define a comparison between them -- namely "after or before in the list." But are they hashable? Commented Jan 14, 2022 at 8:36
  • Elements "must" be comparable. Otherwise " 'B' comes after 'A' and before 'D' " wouldn't make sense. Basically, the comparison here is "who comes earlier in the rule list"? Commented Jan 15, 2022 at 0:41

3 Answers 3

1

Explained the Python code in comments. Basically, find the right place to insert the new element using binary search. The order of elements is decided using rank. The below code assumes that if elements is non-empty then the rule is followed by items in the elements.

rule = ['A','B','C','D','E','F','G','H','I','J','K']
rank = dict()
for i in range(len(rule)):
    rank[rule[i]] = i

elements = ['A','D','E','I','J','K'] #list in which we wish to add elements
target = 'B'    #element to be inserted

#Binary search to find the right place to insert the target in elements
left, right = 0, len(elements)
while left < right:
    mid = left + (right - left) // 2
    if rank[elements[mid]] >= rank[target]:
        right = mid
    else:
        left = mid + 1

elements.insert(left, target) #left is the insertion index
print(elements)

Time complexity of add: O(log(len(elements)))
Space complexity: O(1)

2
  • Define a dict for rule areay and after that when want to insert new item to list check keys of dictionaries and check item key with rule dict to define order. After that find pre and post item in list and insert it to that. Commented Jan 14, 2022 at 7:36
  • 1
    @Hamed_gibago: You got it!! Fun problem, isn't it? Commented Jan 15, 2022 at 0:37
0

If the items are unique (only can occur once), and are not comparable to each other (don't know that B comes after A), then.

  1. Iterate through the rules and find the items position in the rule array.
  2. Check if it is the first item in rules, if so insert at the first position and skip the other steps.
  3. Check to see if it is the last item in rules, if so insert at the end and skip the other steps.
  4. Select the value of the item 1 before into a variable A.
  5. Select the value of the item 1 after into a variable B.
  6. Iterate through the list, if you encounter the value in parameter A insert it after that value, if you encounter the value B, add the value before that.
  7. If you come to the end without finding either value A or B, then you need to repeat but with values 2 before and 2 after the item in the rules (again checking to see if you hit the start or end of the rules list).

You will probably want to make 6 & 7 a function that calls itself recursively.

0

A simple approach is, we can use one iteration of Insertion sort.

So, we start from right side of array compare our input x with array elements a go from right to left side. if we arrive an index i of array that let[i]<=x then let[i+1] is correct location that x can be insert. This approach that has time complexity O(n), follow from correctness of Insertion sort.

Note that the lower of your problem is O(n) because your data structure is array so you need after each insertion shift whole elements.

Not the answer you're looking for? Browse other questions tagged or ask your own question.