We are given an infinite two dimensional plane made of hexagons connected together. We can visualize this plane as a honeycomb. Element X is present on one of the cells / hexagon.
We are given N steps, the task is to calculate number of such hexagonal paths possible in which element X has to perform a walk of N steps and return back to the original hexagon, where
Input : 1 Output : Number of walks possible is/are 0 Explanation : 0 because using just one step we can move to any of the adjacent cells but we cannot trace back to the original hexagon. Input : 2 Output : Number of walks possible is/are 6 Input : 4 Output : Number of walks possible is/are 90
- A hexagonal walk can be defined as walking through adjacent hexagons and returning to the original cell. We know the fact that a hexagon contains six sides i.e. a hexagon is surrounded by six hexagons. Now, we have to count number of ways we take N steps and come back to the original hexagon.
- Now, let us suppose the original hexagon (where element X was initially present) to be the origin. We need all possible ways we can take (N-k) steps such that we have some steps which would trace back to our original hexagon. We can visualize this hexagon and it’s related coordinate system from the picture below.
- Now, let’s assume, our element X was present at 0:0 of the given picture. Thus, we can travel in six possible directions from a hexagon. Now, using the directions above we memorize all possible movements such that we trace back to the original 0:0 index. For memorizing we use a 3D array and we preprocess our answer for a given number of steps and then query accordingly.
Below is the implementation of above approach :
Number of walks possible is/are 90
The time complexity of the above code is and the space complexity is also similar due to the 3D array used.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count all possible walks from a source to a destination with exactly k edges
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Count of numbers whose difference with Fibonacci count upto them is atleast K
- Count of all values of N in [L, R] such that count of primes upto N is also prime
- Count of binary strings of length N having equal count of 0's and 1's
- Count of binary strings of length N with even set bit count and at most K consecutive 1s
- Maximum sub-matrix area having count of 1's one more than count of 0's
- Longest subarray having count of 1s one more than count of 0s
- Count of cells in a matrix which give a Fibonacci number when the count of adjacent cells is added
- Count subarrays such that remainder after dividing sum of elements by K gives count of elements
- Count of subarrays with sum at least K
- Count pairs with Odd XOR
- Count of quadruplets with given sum | Set 3
- Count of Subsets containing only the given value K
- Count of quadruplets with given Sum | Set 2
- Count pairs with given sum | Set 2
- Count pairs with given sum
- Count all pairs with given XOR
- Count of quadruplets with given Sum
- Count the triplets such that A[i] < B[j] < C[k]
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.