Avoid Squares Please CodeChef Solution

Problem – Avoid Squares Please CodeChef Solution

You are given an integer N.

Print a permutation PP of [1,2,…,N] such that the following condition holds:

  • For any index i (1≤i<N), Pi​×Pi+1​ is not a perfect square.

If there are multiple correct answers, you may print any of them.

Note: A permutation of [1,2,…,N] is a rearrangement of those numbers.

Input Format

  • The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
  • The first line of each test case contains an integer N – the length of the required permutation.

Output Format

For each test case, print N space-separated integers representing the valid permutation on a single line.

If there are multiple correct answers, you may print any of them.

Constraints

  • 1≤T≤500
  • 2≤N≤500

Sample 1:

Input: 
3
2
3
4
Output: 
2 1
3 1 2
4 2 3 1

Explanation:

Test case 1: 2×1=2 is not a perfect square. Note that [1,2] is also a valid answer for this case, and will be accepted.

Test case 2: 3×1=3 and 1×2=2 are not perfect squares.

Test case 3: 4×2=8,2×3=6,3×1=3 are all not perfect squares.

Avoid Squares Please CodeChef Solution in Python3

# cook your dish here
t=int(input())
for i in range(t):
    n=int(input())
    l=set()
    for i in range(1,n+1):
        print(i,end=" ")
    print()

Avoid Squares Please CodeChef Solution in C++17

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ff first
#define ss second
#define mod 1000000007
#define N 100005
#define dbg(x)  cerr<<#x<<"="<<x<<'\n'

void solve()
{
    int n;
    cin>>n;
    for (int i = 1; i <= n; i++)
        cout<<i<<" ";
    cout<<"\n"; 
}

int main() {
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    int t;
    cin>>t;
    while(t--)
    {
        solve();
    }
    return 0;
}

Avoid Squares Please CodeChef Solution in Java

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		
		Scanner sc =  new Scanner(System.in);
		int T  =  sc.nextInt();
		while(T!= 0) {
		    
		    int N  =  sc.nextInt();
		    while(N!=0) {
		        
		        System.out.println(N + "\t");
		        N--;
		        
		    }
		    System.out.println();
		    T--;
		}
		sc.close();
	}
}
Avoid Squares Please CodeChef Solution Review:

In our experience, we suggest you solve this Avoid Squares Please CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Avoid Squares Please CodeChef Solution

Find on CodeChef

Conclusion:

I hope this Avoid Squares Please CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published. Required fields are marked *