- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have a list of numbers called nums. We have to find the number of permutations of nums such that sum of every pair of adjacent values is a perfect square. Two permutations A and B are unique when there is some index i where A[i] is not same as B[i].

So, if the input is like nums = [2, 9, 7], then the output will be 2, as we have [2, 7, 9] and [9, 7, 2]

To solve this, we will follow these steps −

res := 0

Define a function util() . This will take i

if i + 1 is same as size of nums , then

res := res + 1

return

visited := a new empty set

for j in range i + 1 to size of nums, do

s := nums[i] + nums[j]

if s is not visited and (square root of s)^2 is s, then

mark s as visited

swap nums[i + 1] and nums[j]

util(i + 1)

swap nums[i + 1] and nums[j]

From the main method do the following −

visited := a new set

for i in range 0 to size of nums, do

swap nums[i] and nums[0]

if nums[0] is not visited, then

util(0)

mark nums[0] as visited

swap nums[i] and nums[0]

return res

Let us see the following implementation to get better understanding −

from math import sqrt class Solution: def solve(self, nums): self.res = 0 def util(i): if i + 1 == len(nums): self.res += 1 return visited = set() for j in range(i + 1, len(nums)): s = nums[i] + nums[j] if s not in visited and int(sqrt(s)) ** 2 == s: visited.add(s) nums[i + 1], nums[j] = nums[j], nums[i + 1] util(i + 1) nums[i + 1], nums[j] = nums[j], nums[i + 1] visited = set() for i in range(len(nums)): nums[i], nums[0] = nums[0], nums[i] if nums[0] not in visited: util(0) visited.add(nums[0]) nums[i], nums[0] = nums[0], nums[i] return self.res ob = Solution() nums = [2, 9, 7] print(ob.solve(nums))

[2, 9, 7]

2

- Related Questions & Answers
- Count of pairs in an array whose sum is a perfect square in C++
- Program to count number of valid pairs from a list of numbers, where pair sum is odd in Python
- Program to find two pairs of numbers where difference between sum of these pairs are minimized in python
- Program to find number of pairs where elements square is within the given range in Python
- Program to count number of fraction pairs whose sum is 1 in python
- 8086 program to find the square root of a perfect square root number
- Count of strings where adjacent characters are of difference one in C++
- Program to count number of perfect squares are added up to form a number in C++
- Program to find max number of K-sum pairs in Python
- Program to form smallest number where no two adjacent digits are same in Python
- C Program to find sum of perfect square elements in an array using pointers.
- Count numbers upto N which are both perfect square and perfect cube in C++
- Program to find number of ways where square of number is equal to product of two numbers in Python
- Program to count maximum number of distinct pairs whose differences are larger than target in Python
- Program to count number of square submatrices with all ones using Python

Advertisements