Count permutations of first N natural numbers having sum of adjacent elements equal to a perfect square Last Updated : 02 Jul, 2021 Comments Improve Suggest changes Like Article Like Report Given a positive integer N, the task is to find the number of unique permutations of first N natural numbers having sum of the adjacent elements equal to a perfect square. Examples: Input: N = 17Output: 2Explanation:Following permutations have sum of adjacent elements equal to a perfect square: {17, 8, 1, 15, 10, 6, 3, 13, 12, 4, 5, 11, 14, 2, 7, 9, 16}{16, 9, 7, 2, 14, 11, 5, 4, 12, 13, 3, 6, 10, 15, 1, 8, 17} Therefore, count of such permutations is 2. Input: N = 13Output: 0 Approach: The given problem can be solved by using the concepts of Graph. Follow the steps below to solve the problem: List all the perfect square numbers up to (2*N - 1) that can be obtained by adding any two positive integers.Represent the graph as the adjacency list representation such that if the sum of two numbers X and Y is a perfect square, then add an edge from node X to node Y.Count the number of nodes in the graph whose in-degree is 1 and store it in a variable X.Now, the number of permutation can be calculated as per the following conditions:If the value of X is 0, then a total of N permutations are possible. Hence, print N as the result.If the value of X is 1 or 2, then a total of 2 permutations are possible. Hence, print 2 as the result.Otherwise, no such permutations exist satisfying the given criteria. Hence, print 0 as the result. Below is the implementation of the above approach: C++ // C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to count total number of // permutation of the first N natural // number having the sum of adjacent // elements as perfect square int countPermutations(int N) { // Create an adjacency matrix vector<vector<int> > adj(105); // Count elements whose indegree // is 1 int indeg = 0; // Generate adjacency matrix for (int i = 1; i <= N; i++) { for (int j = 1; j <= N; j++) { if (i == j) continue; // Find the sum of i and j int sum = i + j; // If sum is perfect square. // then move from i to j if (ceil(sqrt(sum)) == floor(sqrt(sum))) { // Add it in adjacency // list of i adj[i].push_back(j); } } // If any list is of size 1, // then the indegree is 1 if (adj[i].size() == 1) indeg++; } // If there is no element whose // indegree is 1, then N such // permutations are possible if (indeg == 0) return N; // If there is 1 or 2 elements // whose indegree is 1, then 2 // permutations are possible else if (indeg <= 2) return 2; // If there are more than 2 // elements whose indegree is // 1, then return 0 else return 0; } // Driver Code int main() { int N = 17; cout << countPermutations(N); return 0; } Java // Java program for the above approach import java.io.*; import java.util.*; import java.lang.*; class GFG{ // Function to count total number of // permutation of the first N natural // number having the sum of adjacent // elements as perfect square static int countPermutations(int N) { // Create an adjacency matrix ArrayList< ArrayList<Integer>> adj = new ArrayList< ArrayList<Integer>>(105); for(int i = 0; i < 105; i++) adj.add(new ArrayList<Integer>()); // Count elements whose indegree // is 1 int indeg = 0; // Generate adjacency matrix for(int i = 1; i <= N; i++) { for(int j = 1; j <= N; j++) { if (i == j) continue; // Find the sum of i and j int sum = i + j; // If sum is perfect square. // then move from i to j if (Math.ceil(Math.sqrt(sum)) == Math.floor(Math.sqrt(sum))) { // Add it in adjacency // list of i adj.get(i).add(j); } } // If any list is of size 1, // then the indegree is 1 if (adj.get(i).size() == 1) indeg++; } // If there is no element whose // indegree is 1, then N such // permutations are possible if (indeg == 0) return N; // If there is 1 or 2 elements // whose indegree is 1, then 2 // permutations are possible else if (indeg <= 2) return 2; // If there are more than 2 // elements whose indegree is // 1, then return 0 else return 0; } // Driver Code public static void main(String[] args) { int N = 17; System.out.println(countPermutations(N)); } } // This code is contributed by Dharanendra L V. Python3 # python program for the above approach from math import sqrt,floor,ceil # Function to count total number of # permutation of the first N natural # number having the sum of adjacent # elements as perfect square def countPermutations(N): # Create an adjacency matrix adj = [[] for i in range(105)] # bCount elements whose indegree # bis 1 indeg = 0 # bGenerate adjacency matrix for i in range(1, N + 1): for j in range(1, N + 1): if (i == j): continue # Find the sum of i and j sum = i + j # If sum is perfect square. # then move from i to j if (ceil(sqrt(sum)) == floor(sqrt(sum))): # Add it in adjacency # list of i adj[i].append(j) # If any list is of size 1, # then the indegree is 1 if (len(adj[i]) == 1): indeg += 1 # If there is no element whose # indegree is 1, then N such # permutations are possible if (indeg == 0): return N # If there is 1 or 2 elements # whose indegree is 1, then 2 # permutations are possible elif (indeg <= 2): return 2 # If there are more than 2 # elements whose indegree is # 1, then return 0 else: return 0 # Driver Code if __name__ == '__main__': N = 17 print (countPermutations(N)) # This code is contributed by mohit kumar 29. C# // C# program for the above approach using System; using System.Collections.Generic; class GFG{ // Function to count total number of // permutation of the first N natural // number having the sum of adjacent // elements as perfect square static int countPermutations(int N) { // Create an adjacency matrix List<List<int>> adj = new List<List<int>>(105); for(int i = 0; i < 105; i++) adj.Add(new List<int>()); // Count elements whose indegree // is 1 int indeg = 0; // Generate adjacency matrix for(int i = 1; i <= N; i++) { for(int j = 1; j <= N; j++) { if (i == j) continue; // Find the sum of i and j int sum = i + j; // If sum is perfect square. // then move from i to j if (Math.Ceiling(Math.Sqrt(sum)) == Math.Floor(Math.Sqrt(sum))) { // Add it in adjacency // list of i adj[i].Add(j); } } // If any list is of size 1, // then the indegree is 1 if (adj[i].Count == 1) indeg++; } // If there is no element whose // indegree is 1, then N such // permutations are possible if (indeg == 0) return N; // If there is 1 or 2 elements // whose indegree is 1, then 2 // permutations are possible else if (indeg <= 2) return 2; // If there are more than 2 // elements whose indegree is // 1, then return 0 else return 0; } // Driver Code public static void Main() { int N = 17; Console.WriteLine(countPermutations(N)); } } // This code is contributed by SoumikMondal JavaScript <script> // JavaScript program for the above approach // Function to count total number of // permutation of the first N natural // number having the sum of adjacent // elements as perfect square function countPermutations(N) { // Create an adjacency matrix let adj = []; for(let i = 0; i < 105; i++) adj.push([]); // Count elements whose indegree // is 1 let indeg = 0; // Generate adjacency matrix for(let i = 1; i <= N; i++) { for(let j = 1; j <= N; j++) { if (i == j) continue; // Find the sum of i and j let sum = i + j; // If sum is perfect square. // then move from i to j if (Math.ceil(Math.sqrt(sum)) == Math.floor(Math.sqrt(sum))) { // Add it in adjacency // list of i adj[i].push(j); } } // If any list is of size 1, // then the indegree is 1 if (adj[i].length == 1) indeg++; } // If there is no element whose // indegree is 1, then N such // permutations are possible if (indeg == 0) return N; // If there is 1 or 2 elements // whose indegree is 1, then 2 // permutations are possible else if (indeg <= 2) return 2; // If there are more than 2 // elements whose indegree is // 1, then return 0 else return 0; } // Driver Code let N = 17; document.write(countPermutations(N)); // This code is contributed by patel2127 </script> Output: 2 Time Complexity: O(N2)Auxiliary Space: O(N2) Comment More infoAdvertise with us Next Article Count permutations of first N natural numbers having sum of adjacent elements equal to a perfect square ajaykr00kj Follow Improve Article Tags : Graph Greedy Mathematical DSA permutation graph-connectivity Graph Traversals +3 More Practice Tags : GraphGreedyMathematicalpermutation Similar Reads Permutation of numbers such that sum of two consecutive numbers is a perfect square 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". Examples: Input : 17 Output : [16, 9, 7, 2, 15+ min read Generate a matrix having sum of secondary diagonal equal to a perfect square Given an integer N, the task is to generate a matrix of dimensions N x N using positive integers from the range [1, N] such that the sum of the secondary diagonal is a perfect square. Examples: Input: N = 3Output:1 2 32 3 13 2 1Explanation:The sum of secondary diagonal = 3 + 3 + 3 = 9(= 32). Input: 6 min read Difference between sum of the squares of first n natural numbers and square of sum Given an integer n, find the absolute difference between sum of the squares of first n natural numbers and square of sum of first n natural numbers.Examples : Input : n = 3Output : 22Sum of first three numbers is 3 + 2 + 1 = 6Square of the sum = 36Sum of squares of first three is 9 + 4 + 1 = 14Absol 7 min read Sum of alternating sign Squares of first N natural numbers Given a number N, the task is to find the sum of alternating sign squares of first N natural numbers, i.e., 12 - 22 + 32 - 42 + 52 - 62 + .... Examples: Input: N = 2 Output: 5 Explanation: Required sum = 12 - 22 = -1 Input: N = 8 Output: 36 Explanation: Required sum = 12 - 22 + 32 - 42 + 52 - 62 + 7 8 min read Permutation of first N elements with absolute adjacent difference in increasing order Given a positive integer N, the task is to construct a permutation from 1 to N such that the absolute difference of elements is in strictly increasing order. Note: N cannot be 0 or 1. Examples: Input: N = 10Output: 6 5 7 4 8 3 9 2 10 1Explanation: abs(6 - 5) i.e., 1 < abs(5 - 7) i.e., 2 < abs( 5 min read Count of primes in a given range that can be expressed as sum of perfect squares Given two integers L and R, the task is to find the number of prime numbers in the range [L, R] that can be represented by the sum of two squares of two numbers.Examples: Input: L = 1, R = 5 Output: 1 Explanation: Only prime number that can be expressed as sum of two perfect squares in the given ran 7 min read Sum of all perfect square divisors of numbers from 1 to N Given a number N, the task is to find the sum of all the perfect square divisors of numbers from 1 to N. Examples: Input: N = 5 Output: 9 Explanation: N = 5 Perfect square divisors of 1 = 1. Similarly, perfect square divisors of 2, 3 = 1. Perfect square divisors of 4 = 1, 4. Perfect square divisors 12 min read Count of numbers in given range [L, R] that is perfect square and digits are in wave form Given two integers L and R, the task is to count the integers in the range [L, R] such that they satisfy the following two properties: The number must be a perfect square of any integer.In digits of the integer must be in the wave form i.e, let d1, d2, d3, d4, d5 be the digits in the current integer 11 min read Smallest N digit number whose sum of square of digits is a Perfect Square Given an integer N, find the smallest N digit number such that the sum of the square of digits (in decimal representation) of the number is also a perfect square. If no such number exists, print -1.Examples: Input : N = 2 Output : 34 Explanation: The smallest possible 2 digit number whose sum of squ 15+ min read Construct an Array of size N whose sum of cube of all elements is a perfect square Given an integer N, the tasks is to construct a sorted array arr[] of size N, such that the sum of cube of all elements is a perfect square, i.e. \sum_{}A_i^3=X^2 , where X is an integer. Examples: Input: N = 5 Output: 1 2 3 4 5 Explanation Sum of cube of all elements = 1 + 8 + 27 + 64 + 125 = 225 w 3 min read Like