Prerequisite: Hamiltonian Cycle
Given an integer n(>=2), find a permutation of numbers from 1 to n such that the sum of two consecutive numbers of that permutation is a perfect square. If that kind of permutation is not possible to print “No Solution”.
Input : 17 Output : [16, 9, 7, 2, 14, 11, 5, 4, 12, 13, 3, 6, 10, 15, 1, 8, 17] Explanation : 16+9 = 25 = 5*5, 9+7 = 16 = 4*4, 7+2 = 9 = 3*3 and so on. Input: 20 Output: No Solution Input : 25 Output : [2, 23, 13, 12, 24, 25, 11, 14, 22, 3, 1, 8, 17, 19, 6, 10, 15, 21, 4, 5, 20, 16, 9, 7, 18]
We can represent a graph, where numbers from 1 to n are the nodes of the graph and there is an edge between ith and jth node if (i+j) is a perfect square. Then we can search if there is any Hamiltonian Path in the graph. If there is at least one path then we print a path otherwise we print “No Solution”.
1. First list up all the perfect square numbers which we can get by adding two numbers. We can get at max (2*n-1). so we will take only the squares up to (2*n-1). 2. Take an adjacency matrix to represent the graph. 3. For each number from 1 to n find out numbers with which it can add upto a perfect square number. Fill respective cells of the adjacency matrix by 1. 4. Now find if there is any Hamiltonian path in the graph using backtracking as discussed earlier.
17 -> [16, 9, 7, 2, 14, 11, 5, 4, 12, 13, 3, 6, 10, 15, 1, 8, 17] 20 -> No Solution 25 -> [2, 23, 13, 12, 24, 25, 11, 14, 22, 3, 1, 8, 17, 19, 6, 10, 15, 21, 4, 5, 20, 16, 9, 7, 18]
This backtracking algorithm takes exponential time to find Hamiltonian Path. Hence the time complexity of this algorithm is exponential.
In the last part of the hampath(n) function if we just print the path rather returning it then it will print all possible Hamiltonian Path i.e. all possible representations.
Actually we will first get a representation like this for n = 15. For n<15 there is no representation. For n = 18, 19, 20, 21, 22, 24 there is also no Hamiltonian Path. For rest of the numbers it works well.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Count numbers upto N which are both perfect square and perfect cube
- Sum of all perfect square divisors of numbers from 1 to N
- Print n numbers such that their sum is a perfect square
- Count of subarrays having exactly K perfect square numbers
- Check if product of array containing prime numbers is a perfect square
- Count square and non-square numbers before n
- Number of perfect squares between two given numbers
- Sum of all perfect numbers present in an array
- Check if a number is perfect square without finding square root
- Find a permutation of 2N numbers such that the result of given expression is exactly 2K
- Sum of square of first n even numbers
- Sum of square of first n odd numbers
- Numbers less than N that are perfect cubes and the sum of their digits reduced to a single digit is 1
- Sum of Hamming difference of consecutive numbers from 0 to N | Set 2
- Fibbinary Numbers (No consecutive 1s in binary)
- Expressing factorial n as sum of consecutive numbers
- Program to print non square numbers
- Perfect Square String
- Count of subarrays whose sum is a perfect square
- Check if given number is perfect square
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.