Bojack's blog

By Bojack, history, 7 years ago, In English

Problem link : https://toph.co/p/unbelievable-array How should I approach this problem? Someone help please.

  • Vote: I like it
  • 0
  • Vote: I do not like it

| Write comment?
»
7 years ago, # |
  Vote: I like it 0 Vote: I do not like it

First,hash all the indexes with same values with distinct integers i.e, if the given array is 1 2 3 2 1 4 2 1 , indexes 1,5,8 will be hashed to 1 , indexe(s) 2,4,7 will be hashed to 2, indexe(s) 3 will be hashed to 3 , indexe(s) 6 will be hashed to 4 and also store their corresponding values ! Now if you want to change index x in the original array to some y , go to the hashed index of x and update its value to y and if you want to query the index x in the original array , go to the hashed index of x and print its value ! I have not submitted the solution for this , just an idea ! My first comment on CF ! Please ignore if there are any mistakes !

»
7 years ago, # |
  Vote: I like it +8 Vote: I do not like it

Let's see how can we apply disjoint set here.

  1. while scanning the input array if a value already occurred then we just merge both the index and update the leader of this group with value y for query 1 x y
  2. while performing A[idx] we need to find the leader of idx, and then we get the stored value corresponding to that leader. I made a submission here here. Let me know if you have any questions.
»
7 years ago, # |
  Vote: I like it 0 Vote: I do not like it

First initialize the parents of each number from 1 to 100000.

For type 1 operation set the parent of x as y.

For type 2 operation print the parent of a[idx].