I am analyzing a solution to the counting exercise from Codility (Frog_river_one).
Most of the solutions I have seen are based on looping through the positions and values of the given array. One of the solutions I came across seems to tackle this problem differently.
I recognize that the "bitwise or of x and y" was used there however I don't understand the logic of the solution. Especially the part checker=checker| 1<<(A[x]-1)
Can anybody explain please?
The problem
A small frog wants to get to the other side of a river. The frog is currently located at position 0, and wants to get to position X. Leaves fall from a tree onto the surface of the river. You are given a non-empty zero-indexed array A consisting of N integers representing the falling leaves. A[K] represents the position where one leaf falls at time K, measured in minutes. The goal is to find the earliest time when the frog can jump to the other side of the river. The frog can cross only when leaves appear at every position across the river from 1 to X.
A= [1,3,1,4,2,3,5,4]
def solution(X, A):
checker=0
final_val=pow(2,5)-1
for x in range(len(A)):
checker=checker| 1<<(A[x]-1)
if(checker==final_val):
return x
return -1