Approach:

On a chess board, every adjacent pair of cell is painted in different color.

For each cell (i, j), check whether the adjacent cells i.e (i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1) is painted with different color than (i, j) or not.

` ````
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int tt;
cin >> tt;
while (tt--) {
int n;
cin >> n;
int top;
cin >> top;
int ok = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i + j == 0) continue;
int cur;
cin >> cur;
ok &= (cur == (top + i + j) % 2);
}
}
cout << (ok ? "Yes" : "No") << '\n';
}
return 0;
}
```

Approach:

In this question it is clearly given that we have to find maximum value such that it become common factor. From this we can conclude that we have to find the GCD of the given number. And if the GCD will be perfect square then print the number otherwise print "Increase".

` ````
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while(t--)
{
int a, b;
cin >> a >> b;
int res = __gcd(a, b);
double result = sqrt(res);
if (result == (int)result)
{
cout << result * result << endl;
}
else
cout << "Increase" << endl;
}
return 0;
}
```

Approach:

For 2 bottles: Taking one rat (R1). If the rat R1 drinks the bottle 1 and dies, then bottle 1 is poisonous. Else the bottle 2 is poisonous. Hence 1 rat is enough to identify

For 3 bottles: Taking two rats (R1) and (R2). If the rat R1 drinks the bottle 1 and bottle 3 and dies, then bottle 1 or bottle 3 is poisonous. So the rat R2 drinks the bottle 1 then. If it dies, then the bottle 1 is poisonous, Else the bottle 3 is poisonous. Now if the rat R1 do not die after drinking from bottle 1 and bottle 3, then bottle 2 is poisonous. Hence 2 rats is enough to identify.

For 4 bottles: Taking two rats (R1) and (R2). If the rat R1 drinks the bottle 1 and bottle 3 and dies, then bottle 1 or bottle 3 is poisonous. So the rat R2 drinks the bottle 1 then. If it dies, then the bottle 1 is poisonous, Else the bottle 3 is poisonous. Now if the rat R1 do not die after drinking from bottle 1 and bottle 3, then bottle 2 or bottle 4 is poisonous. So the rat R1 drinks the bottle 2 then. If it dies, then the bottle 2 is poisonous, Else the bottle 4 is poisonous. Hence 2 rats is enough to identify.

For N bottles: Minimum number of rats required are = ceil(log2 N))

` ````
#include <bits/stdc++.h>
using namespace std;
int minRats(int n)
{
return ceil(log2(n));
}
int main()
{
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
cout << minRats(n) << endl;
}
}
```

Approach:

First, we have to find the first element of the kth group.

For example:

In general, the kth group's first element is the nth odd number, where n = (1 + 2 + 3 + ... + (k - 1)) + 1

Now, as we know, the nth odd number is 2n - 1. This gives us the first element of the kth group. We also know that there are k elements in this group, so we can find their sum by simply counting upwards from 2n - 1, by two, k times and adding them all. This gives us a linear-time solution.

To optimize, note that the expression for n can be simplified as k(k - 1) / 2, hence 2n - 1 = k(k - 1) + 1. It's also possible to compute the sum of an arithmetic progression with a formula. This gives a fast, constant-time solution.

On observation we can conclude that all the groups become equal to the cube of the size of the group on summation.

` ````
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
int k;
cin >> k;
ll res = ll(k) * k * k;
cout << res << endl;
return 0;
}
```

Approach:

From graph we can consider that friends will be shorter and intelligent only when node have zero inorder degree.

Simply see that if we know v is taller then we can never have that v in set(since information is consistent). so simply ignore all v and print all others number from 1 to N.

` ````
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,m,u,v;
cin>>n>>m;
bool NoInEdge[n+1];
for(int i=1;i<=n;i++)
NoInEdge[i]=true;
for(int i=1;i<=m;i++)
{
cin>>u>>v;
NoInEdge[v]=false;
}
for(int i=1;i<=n;i++)
if(NoInEdge[i])
cout << i << " ";
}
```

Approach:

Just output the last and first character alternatively for each string inside the double quotes separated by space.

` ````
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
String[] st=null;
if(s.length()!=0)
st=(s.substring(1,s.length()-1)).split(" ");
System.out.print("\"");
for(int i=0;i
```0){
if(i%2==0)
System.out.print(st[i].charAt(st[i].length()-1));
else
System.out.print(st[i].charAt(0));
}
}
System.out.print("\"");
}
}

Approach:

All substring with length 1 consists of a single character thus will always be a palindrome.

Now, Let consider substrings of S with length 3. We can see that for all substring to be palindromic following conditions should be true

- s[0] = s[2] = s[4] = ..

- s[1] = s[3] = s[5] = ..

i.e, all characters at odd positions should be equal to each other similiarly all character at odd position should be equal to each other.

` ````
t=int(input())
while(t>0):
t-=1
s=input()
for i in range(len(s)):
if(i-2>=0 and s[i]!=s[i-2]):
print("NO")
break
else:
print("YES")
```

Approach:

Just use if-else or switch statement and check the given conditions.

` ````
import java.io.; import java.util.;
public class Solution {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int count=0;
for(int i=0;i<7;i++){
switch(i){
case 0: //monday
if(sc.next().equals("W")) count++;
break;
case 1: //tuesday
if(sc.next().equals("W")) count++;
break;
case 2: //wednesday
sc.next(); count++;
break;
case 3: //thursday
if(sc.next().equals("W")) count++;
break;
case 4: //friday
sc.next(); count++;
break;
case 5: //saturday
sc.next(); count++;
break;
case 6: //sunday
if(sc.next().equals("W")) count++;
break;
}
}
System.out.println(count);
}
}
```

Approach:

Even + Odd = Odd. Odd + Even = Odd.

Use the fact that Number of even numbers from 1 to N are (N / 2) and number of odd numbers from 1 to N are (N / 2).

` ````
t = int(input())
for i in range(0, t):
n, m = [int(z) for z in input().split()]
n = n * m
if n & 1:
print(n >> 1,'/', n, sep = '')
else:
print("1/2")
```

Approach:

First we find the distance between Bob and his friends using distance formula. If it is equal or more than the square of radius then it will possible for him to meet his friend in 0 second. If it is less then we find the time by using formula of time.(radius-distance)/velocity.

` ````
#include<bits/stdc++.h>
using namespace std;
long long T,r,x,y,xx,yy,v,len;
int main()
{
ios_base::sync_with_stdio;
scanf("%lld",&T);
while(T--)
{
scanf("%lld%lld%lld%lld%lld%lld",&r,&x,&y,&xx,&yy,&v);
len=(xx-x)*(xx-x)+(yy-y)*(yy-y);
if(len>=r*r){puts("0.00000000");continue;}
printf("%.8lf\n",(r-sqrt(len))/(double)v);
}
}
```

Approach:

` ````
#include
```
using namespace std;
typedef long long ll;
bool queen(ll qx, ll qy, ll ox, ll oy)
{
// If queen and the opponent are in the same row
if (qx == ox)
return true;
// If queen and the opponent are in the same column
if (qy == oy)
return true;
// If queen can attack diagonally
if (abs(qy - oy) == abs(qx - ox))
return true;
// Opponent is safe
return false;
}
int main()
{
ll t;
cin >> t;
for (int i = 0; i < t; ++i)
{
ll qx, qy, ox, oy;
cin >> qx >> qy >> ox >> oy;
if (queen(qx, qy, ox, oy))
cout << "Yes" << endl;
else
cout << "No" << endl;
}
cerr <<"Time Elapsed "<< (double)clock()/CLOCKS_PER_SEC <<" s"<

```
```

```
```

```
```

```
```

```
Click here for Question
```

Prequisutes: Implementation, Simple Maths
Approach:

Simple approach is to use two pointers approach.Take two variable i = 0 and j = n-1 and increment i upto n/2 and decrement j. Then check if s[i] is not equal s[j] and if this condition is true then only check whether s[i] is not equal to '?' and s[j] is not equal to '?'. If both condition true then print "NO" otherwise print "YES".

```
for i in range(int(input())):
n=int(input())
l=list(str(input()))
l1=[]
p=0
for j in range(n):
l1.append(l[n-1-j])
for k in range(n):
if(l[k]==l1[k]) or (l[k]=='?') or (l1[k]=='?'):
p+=1
if p==n:
print('YES')
else :
print('NO')
```

Click here for Question

Prequisutes: Implementation
Approach:

Check that if s[i] != s[i-2] or not if this condition is true print "NO" otherwise print "YES"

```
t=int(input())
while(t>0):
t-=1
s=input()
for i in range(len(s)):
if(i-2>=0 and s[i]!=s[i-2]):
print("NO")
break
else:
print("YES")
```

```
```