Count permutations of first N natural numbers having sum of adjacent elements equal to a perfect square
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 = 17
Output: 2
Explanation:
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 = 13
Output: 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++
#include <bits/stdc++.h>
using namespace std;
int countPermutations( int N)
{
vector<vector< int > > adj(105);
int indeg = 0;
for ( int i = 1; i <= N; i++) {
for ( int j = 1; j <= N; j++) {
if (i == j)
continue ;
int sum = i + j;
if ( ceil ( sqrt (sum))
== floor ( sqrt (sum))) {
adj[i].push_back(j);
}
}
if (adj[i].size() == 1)
indeg++;
}
if (indeg == 0)
return N;
else if (indeg <= 2)
return 2;
else
return 0;
}
int main()
{
int N = 17;
cout << countPermutations(N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
import java.lang.*;
class GFG{
static int countPermutations( int N)
{
ArrayList<
ArrayList<Integer>> adj = new ArrayList<
ArrayList<Integer>>( 105 );
for ( int i = 0 ; i < 105 ; i++)
adj.add( new ArrayList<Integer>());
int indeg = 0 ;
for ( int i = 1 ; i <= N; i++)
{
for ( int j = 1 ; j <= N; j++)
{
if (i == j)
continue ;
int sum = i + j;
if (Math.ceil(Math.sqrt(sum)) ==
Math.floor(Math.sqrt(sum)))
{
adj.get(i).add(j);
}
}
if (adj.get(i).size() == 1 )
indeg++;
}
if (indeg == 0 )
return N;
else if (indeg <= 2 )
return 2 ;
else
return 0 ;
}
public static void main(String[] args)
{
int N = 17 ;
System.out.println(countPermutations(N));
}
}
|
Python3
from math import sqrt,floor,ceil
def countPermutations(N):
adj = [[] for i in range ( 105 )]
indeg = 0
for i in range ( 1 , N + 1 ):
for j in range ( 1 , N + 1 ):
if (i = = j):
continue
sum = i + j
if (ceil(sqrt( sum )) = = floor(sqrt( sum ))):
adj[i].append(j)
if ( len (adj[i]) = = 1 ):
indeg + = 1
if (indeg = = 0 ):
return N
elif (indeg < = 2 ):
return 2
else :
return 0
if __name__ = = '__main__' :
N = 17
print (countPermutations(N))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int countPermutations( int N)
{
List<List< int >> adj = new List<List< int >>(105);
for ( int i = 0; i < 105; i++)
adj.Add( new List< int >());
int indeg = 0;
for ( int i = 1; i <= N; i++)
{
for ( int j = 1; j <= N; j++)
{
if (i == j)
continue ;
int sum = i + j;
if (Math.Ceiling(Math.Sqrt(sum)) ==
Math.Floor(Math.Sqrt(sum)))
{
adj[i].Add(j);
}
}
if (adj[i].Count == 1)
indeg++;
}
if (indeg == 0)
return N;
else if (indeg <= 2)
return 2;
else
return 0;
}
public static void Main()
{
int N = 17;
Console.WriteLine(countPermutations(N));
}
}
|
Javascript
<script>
function countPermutations(N)
{
let adj = [];
for (let i = 0; i < 105; i++)
adj.push([]);
let indeg = 0;
for (let i = 1; i <= N; i++)
{
for (let j = 1; j <= N; j++)
{
if (i == j)
continue ;
let sum = i + j;
if (Math.ceil(Math.sqrt(sum)) ==
Math.floor(Math.sqrt(sum)))
{
adj[i].push(j);
}
}
if (adj[i].length == 1)
indeg++;
}
if (indeg == 0)
return N;
else if (indeg <= 2)
return 2;
else
return 0;
}
let N = 17;
document.write(countPermutations(N));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(N2)
Last Updated :
02 Jul, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...