
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to count number of permutations where sum of adjacent pairs are perfect square in Python
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 −
Example
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))
Input
[2, 9, 7]
Output
2
- Related Articles
- 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 count number of fraction pairs whose sum is 1 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 find max number of K-sum pairs in Python
- Program to form smallest number where no two adjacent digits are same in Python
- Program to count maximum number of distinct pairs whose differences are larger than target in Python
- 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 count indices pairs for which elements sum is power of 2 in Python
- Program to count number of square submatrices in given binary matrix in Python
- Count all pairs of adjacent nodes whose XOR is an odd number in C++
- 8086 program to find the square root of a perfect square root number
- Program to count number of paths whose sum is k in python
