[SOLUTION] Switch Game solution codechef 2021 – Code Cracker codechef

Switch Game solution codechef 2021

Switch Game solution codechef 2021

Chef has NN switches which are currently OFF. Chef is sleeping. There are NN kids denoted by kkii (1iN1≤i≤N) i.e. kk11kk22 and so on. kkii kid comes and changes the state of switches of every iith switch. For e.g., if kk11 kid comes, he turns on all the switches, and then if kk22 kid comes, he turns off every second switch as all the switches are ON now.

After kkNN kid changes the state of every nth lock, Chef wakes up. Chef wants you to output all the positions of the switch that is currently ON.

Input: Switch Game solution codechef 2021

  • First-line will contain TT, the number of test cases. Then the test cases follow.
  • Each test case contains a single integer NN.

Output: Switch Game solution codechef 2021

  • For each test case, output in a single line the position of the switch that is currently ON.

Constraints Switch Game solution codechef 2021

  • 1T51≤T≤5
  • 1N1091≤N≤109

Sample Input: Switch Game solution codechef 2021

1
2

Sample Output:

1

EXPLANATION:

There are 2 switches. The first kid turns on both switches and the second kid turns off the second switch. So, now the first switch is ON.

Problem Code :

C++

#include<bits/stdc++.h>
#include <string> 
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
long long int q;
cin>>q;
long long int j= sqrt(q);
for(long long int i=1;i<=j;i++)
{
cout<<i*i<<" ";
}
cout<<endl; 
}
}
Python 

import sys
from random import *
from bisect import *
from itertools import permutations as prm,product
from heapq import *
#from collections import deque
pl=1
from math import gcd,sqrt,ceil,floor
from copy import *
sys.setrecursionlimit(10**5)
if pl:
input=sys.stdin.readline
else: 
sys.stdin=open('input.txt', 'r')
sys.stdout=open('outpt.txt','w')

def li():
return [int(xxx) for xxx in input().split()]
def fi():
return int(input())
def si():
return list(input().rstrip()) 
def mi():
return map(int,input().split())


t=fi()



while t>0:
t-=1
n=fi()
i=1
while i*i<=n:
print(i*i,end=" ")
i+=1
print()
JAVA



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


class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner sc = new Scanner(System.in);
int test,n,i,j;
test=sc.nextInt();
for(i=0;i<test;i++)
{
n=sc.nextInt();
int[] arr=new int[n+1];
for(i=1;i<=n;i++)
cal(arr,i);
for(j=1;j<=n;j++)
if(arr[j]==1)
System.out.print(j+" ");
}
}
public static void cal(int[] arr,int k)
{
int i;
int l=arr.length;
for(i=k;i<l;i+=k)
{
if(arr[i]==0)
arr[i]=1;
else
arr[i]=0;
}
}
}

 

 

Switch Game solution codechef 2021

Also read : Flower Sequence solution codechef 2021

Switch Game solution codechef 2021

Leave a Comment