Problem-Solving in C(Part-2): Loop, Array and String Operations(Final)

Mohammed Muzahidul Islam
4 min readFeb 4, 2024

--

Tiger Vs Pathan

Problem Statement

Tiger and Pathan are bored with movies and want to play a game. They will play the game in N rounds. Each time, both of them will fight with each other, and one of them can win in a round. After N rounds, print the name of the winner who has won more rounds than the other. If they have won an equal number of rounds, then there will be a tie.

Input Format

  • First line will contain T, the number of test cases.
  • First line of each test case will contain N, the number of rounds.
  • Last line of each test case will contain a string S of size N. Each character of that string refers to a round. If the character is ‘T’ then Tiger has won. If the character is ‘P’ then Pathan has won.

Constraints

  1. 1 <= T <= 100
  2. 1 <= N <= 100000

Output Format

  • Output “Tiger” if Tiger wins, and “Pathaan” if Pathan wins. If it is a draw, print “Draw” without the quotation marks.

Sample Input 0

3
5
TPTPP
3
TTP
4
TPPT

Sample Output 0

Pathaan
Tiger
Draw

Try on your own.

Photo by Boitumelo on Unsplash

Photo by Nate Grant on Unsplash

Answer:

//Answer
#include<stdio.h>
#include<limits.h>
#include<string.h>
int main(){
int t;
scanf("%d", &t);
while (t--) {
int n;
scanf("%d", &n);
char a[n];
scanf("%s", a);
int tiger=0;
int pathan=0;
for(int i=0; i<n; i++){
if(a[i]=='T'){
tiger++;
}
if(a[i]=='P'){
pathan++;
}
}
if(tiger==pathan){
printf("Draw\n");
}
else if(tiger>pathan){
printf("Tiger\n");
}
else{
printf("Pathaan\n");
}
}
return 0;
}

Find the Missing Number

Problem Statement

Given the multiplication of four numbers and three of those numbers, find the missing number.

Note: If the missing number could not be found for the given input, print -1. All numbers are non-negative integers.

Input Format

  • The first line will contain T, number of test cases.
  • For every test case, the input will contain one integer M (the multiplication of the four numbers), A, B, C (three of those four numbers).

Constraints

  1. 1 < T <= 100000
  2. 0 <= M <= 1⁰¹⁸
  3. 1 <= A, B, C <= 1⁰⁶

Output Format

  • Print the missing number. Don’t forget to print a new line after it.

Sample Input 0

4
20 1 2 2
10 2 2 1
1 1 2 3
0 3 10 15

Sample Output 0

5
-1
-1
0

Sample Input 1

3
20 5 1 1
18 2 3 3
0 10 20 30

Sample Output 1

4
1

Try on your own.

Photo by Fotis Fotopoulos on Unsplash

Answer:

#include<stdio.h>
#include<limits.h>
#include<string.h>
int main(){
int test;
scanf("%d", &test);
for(int i=0; i<test; i++){
long long int d;
int a, b, c;
scanf("%lld %d %d %d", &d, &a, &b, &c);
long long int e= (a*b*c);
if(e==0){
printf("-1\n");
}
else{
long long int f=d/e;
if(d%e!=0){
printf("-1\n");
}
else{
printf("%lld\n", f);
}
}
}
return 0;
}

Difference Array

Problem Statement

You will be given an array A and the size of that array N. You need to make a copy of this array named B. Then sort the array B in ascending order.

After that, you need to make another array C, where each index i (0 <= i < N) of array C is the absolute difference between array A[i] and B[i].

That means,

C[0] = |A[0] - B[0]|
C[1] = |A[1] - B[1]|
C[2] = |A[2] - B[2]|
.....
.....
C[N-1] = |A[N-1] - B[N-1]|

Here, || means the absolute value. After making the array C, print that array.

Input Format

  • First line will contain T, the number of test cases.
  • First line of each test case will contain N.
  • Second line of each test case will contain the array A.

Constraints

  1. 1 <= T <= 100
  2. 1 <= N <= 1000
  3. 0 <= A[i] <= 1000

Output Format

  • Output the array C.

Sample Input 0

2
5
5 4 3 2 1
6
2 4 5 6 5 1

Sample Output 0

4 2 0 2 4 
1 2 1 1 0 5

Explanation 0

In the first test case, A=[5,4,3,2,1], B=[1,2,3,4,5], so array C=[|5-1|,|4-2|,|3-3|,|2-4|,|1-5|]
In the second test case, A=[2,4,5,6,5,1], B=[1,2,4,5,5,6], so array C=[|2-1|,|4-2|,|5-4|,|6-5|,|5-5|,|1-6|]

Try Yourself.

Photo by Christopher Gower on Unsplash

//Answer
#include<stdio.h>
#include<limits.h>
#include<string.h>
int main(){
int test;
scanf("%d", &test);
for(int i=0; i<test; i++){
int n;
scanf("%d", &n);
int ar[n];
for(int i=0;i<n;i++){
scanf("%d", &ar[i]);
}
int br[n];
for(int i=0; i<n; i++){
br[i]=ar[i];
}
for(int i=0; i<n-1; i++){
for(int j=i+1; j<n;j++){
if(br[i]>br[j]){
int temp=br[i];
br[i]=br[j];
br[j]=temp;
}
}
}
// int cr[n];
int c;
for(int i=0; i<n; i++){
c=ar[i]-br[i];
int diff;
if(c<0){
diff=-c;
}
else{
diff=c;
}
printf("%d ", diff);
}
printf("\n");
}
return 0;
}

Please leave a clap and comment on my work. Your feedback will motivate me to create and share more content. Thank you!

--

--