big_aL's blog

By big_aL, 15 months ago, In English

Consider the following graph:

https://i.imgur.com/r9d38v2.png

The vertex 1,2,3,4 belong to the first set and the vertex 5,6,7,8 belong to the second set. We want to check if its possible for each vertex from both sets to be connected to only one vertex from the other set. In other words if there are 8 nodes then there will be 4 different edges each one connecting different pairs of nodes. Is this a well known problem and how to solve it?

Full text and comments »

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

By big_aL, 17 months ago, In English

I don't know if this is a classic problem or not but I can't seem to find a solution for it.

Consider two arrays a = [1, 2, 3, 4, 5] and b = [3, 6, 6]. You are also given two indexes a_ind = 0 and b_ind = 0. In one move you can add 1 to both a_ind and b_ind, if the index for any array goes out of bound then reset that index to 0. The task is to calculate the number of moves it would take such that the value a[a_ind] == b[b_ind] or state that it is impossible.

The brute force method would be to append both arrays to themselves x times. Where x = lcm(lenght(a), lenght(b)) / length(a) for the array a and x = lcm(lenght(a), lenght(b)) / length(b) for the array b, the above arrays would become:

1 2 3 4 5 1 2 3 4 5 1 2 3 4 5

3 6 6 3 6 6 3 6 6 3 6 6 3 6 6

The condition is satisfied at the 12th move. However this method would give TLE on any platform.

What would be an optimal approach for solving it?

Full text and comments »

  • Vote: I like it
  • +32
  • Vote: I do not like it