Competitive Programming New
Competitive Programming New
Competitive Programming New
Task
This challenge requires you to print on a single line, and then print the already provided
input string . If you are not familiar with C, you may want to read about the printf()
command.
Example
char s[100];
scanf("%[^\n]%*c", s);
printf("Hello, World!\n");
printf("%s", s);
return 0;
}
Q-2
This challenge will help you to learn how to take a character, a string and a sentence
as input in C.
To take a single character as input, you can use scanf("%c", &ch ); and printf("%c",
You can take a string as input in C using scanf(“%s”, s). But, it accepts string only
character. ^\n stands for taking input until a newline isn't encountered. Then, with
this %*c, it reads the newline character and here, the used * indicates that this newline
character is discarded.
Note: The statement: scanf("%[^\n]%*c", s); will not work because the last statement
will read a newline character, \n, from the previous line. This can be handled in a
variety of ways. One way is to use scanf("\n"); before the last statement.
Task
You have to print the character, , in the first line. Then print in next line. In the last line
Input Format
Constraints
Strings for and will have fewer than 100 characters, including the newline.
Output Format
Print three lines of output. The first line prints the character, .
Sample Input 0
C
Language
Welcome To C!!
Sample Output 0
C
Language
Welcome To C!!
int main() {
char ch;
char s[20], sen[100];
scanf("%c", &ch);
scanf("%s", s);
scanf("\n");
scanf("%[^\n]%*c", sen);
printf("%c\n", ch);
printf("%s\n", s);
printf("%s\n", sen);
return 0;
}
Q-3
Objective
The fundamental data types in c are int, float and char. Today, we're discussing int
The printf() function prints the given statement to the console. The syntax
reads integer number from the console and stores the given value in variable .
To input two integers separated by a space on a single line, the command
is scanf("%d %d", &n, &m), where and are the two integers.
Task
Your task is to take two numbers of int data type, two numbers of float data type as input and
Read lines of input from stdin (according to the sequence given in the 'Input Format' section
Print the sum and difference of two int variable on a new line.
Print the sum and difference of two float variable rounded to one decimal place on a new
line.
Input Format
The first line contains two integers.
The second line contains two floating point numbers.
Constraints
integer variables
float variables
Output Format
Print the sum and difference of both integers separated by a space on the
first line, and the sum and difference of both float (scaled to decimal place)
separated by a space on the second line.
Sample Input
10 4
4.0 2.0
Sample Output
14 6
6.0 2.0
Explanation
When we sum the integers and , we get the integer . When we subtract the second
When we sum the floating-point numbers and , we get . When we subtract the second
return 0;
}
Q-4
Objective
In this challenge, you will learn simple usage of functions in C. Functions are a
bunch of statements grouped together. A function is provided with zero or more
arguments, and it executes the statements on it. Based on the return type, it either
returns nothing (void) or something.
Task
Write a function int max_of_four(int a, int b, int c, int d) which reads four arguments
and returns the greatest of them.
Note
There is not built in max function in C. Code that will be reused is often put in a
separate function, e.g. int max(x, y) that returns the greater of the two values.
Input Format
Input will contain four integers - , one on each line.
Output Format
Print the greatest of the four integers.
Note: I/O will be automatically handled.
Sample Input
3
4
6
5
Sample Output
6
int max(int a, int b) {
return a > b ? a : b;
}
int max_of_four(int a, int b, int c, int d) {
return max(a, max(b, max(c, d)));
}
int main() {
int a, b, c, d;
scanf("%d %d %d %d", &a, &b, &c, &d);
int ans = max_of_four(a, b, c, d);
printf("%d", ans);
return 0;
}
Q-5
Task
Complete the function void update(int *a,int *b). It receives two integer pointers,
int* a and int* b. Set the value of to their sum, and to their absolute difference. There
is no return value, and no return statement is needed.
Input Format
The input will contain two integers, and , separated by a newline.
Output Format
Modify the two values in place and the code stub main() will print their values.
Note: Input/ouput will be automatically handled. You only have to complete the
function described in the 'task' section.
Sample Input
4
5
Sample Output
9
1
void update(int *a,int *b) {
int temp = *a;
*a = *a + *b;
*b = temp - *b;
}
int main() {
int a, b;
int *pa = &a, *pb = &b;
return 0;
}
Q-6
Objective
In this challenge, you will learn the usage of the for loop, which is a programming
language statement which allows code to be executed until a terminal condition is met.
They can even repeat forever if the terminal condition is never met.
The syntax for the for loop is:
for ( <expression_1> ; <expression_2> ; <expression_3> )
<statement>
expression_1 is used for intializing variables which are generally used for
controlling the terminating flag for the loop.
expression_2 is used to check for the terminating condition. If this evaluates to
false, then the loop is terminated.
expression_3 is generally used to update the flags/variables.
The following loop initializes to 0, tests that is less than 10, and increments at
every iteration. It will execute 10 times.
Task
For each integer in the interval (given as input) :
If , then print the English representation of it in lowercase. That is "one" for , "two"
for , and so on.
Else if and it is an even number, then print "even".
Else if and it is an odd number, then print "odd".
Input Format
Note:
Sample Input
8
11
Sample Output
eight
nine
even
odd
int main() {
int a, b,i;
char* represent[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight",
"nine"};
scanf("%d\n%d", &a, &b);
for( i = a; i <= b; i++)
{
if(i > 9) {
if(i % 2 == 0)
printf("even\n");
else printf("odd\n");
}
else {
printf("%s\n", represent[i]);
}
}
return 0;
}
Q-7
Program to replace all 0's with 1 in a given integer is discussed here. Given an integer as an input, all the
0's in the number has to be replaced with 1.
For example, consider the following number
Input: 102405
Output: 112415
Input: 56004
Output: 56114
Task
int n;
scanf("%d", &n);
if(n == 1) {
printf("one");
}
else if(n == 2) {
printf("two");
}
else if(n == 3) {
printf("three");
}
else if(n == 4) {
printf("four");
} else if(n == 5) {
printf("five");
} else if(n == 6) {
printf("six");
} else if(n == 7) {
printf("seven");
} else if(n == 8) {
printf("eight");
}
else if(n == 9) {
printf("nine");
} else {
printf("Greater than 9");
} return 0; }
Q-9
The modulo operator, %, returns the remainder of a division. For example, 4
% 3 = 1 and 12 % 10 = 2. The ordinary division operator, /, returns a truncated
integer value when performed on integers. For example, 5 / 3 = 1. To get the
last digit of a number in base 10, use as the modulo divisor.
Task
Given a five digit integer, print the sum of its digits.
Input Format
The input contains a single five digit number, .
Output Format
Print the sum of the digits of the five digit number.
Sample Input 0
10564
Sample Output 0
16
int main() {
int n;
scanf("%d", &n);
//Complete the code to calculate the sum of the five digits on n.
int sum = 0;
do {
sum += (n % 10);
n /= 10;
}
while(n != 0);
printf("%d", sum);
return 0;
}
Q-10
In this challenge, you will use logical bitwise operators. All data is stored in its
binary representation. The logical operators, and C language, use to represent true
and to represent false. The logical operators compare bits in two numbers and return
true or false, or , for each bit compared.
Bitwise AND operator & The output of bitwise AND is 1 if the corresponding bits
of two operands is 1. If either bit of an operand is 0, the result of corresponding bit
is evaluated to 0. It is denoted by &.
Bitwise OR operator | The output of bitwise OR is 1 if at least one corresponding
bit of two operands is 1. It is denoted by |.
Bitwise XOR (exclusive OR) operator ^ The result of bitwise XOR operator is 1 if
the corresponding bits of two operands are opposite. It is denoted by .
For example, for integers 3 and 5,
3 = 00000011 (In Binary)
5 = 00000101 (In Binary)
return 0;
}
Q-11
In this challenge, create an array of size n dynamically, and read the values from
stdin. Iterate the array calculating the sum of all elements. Print the sum and free
the memory where the array is stored.
Input Format
The first line contains an integer, n.
The next line contains n space-separated integers.
Constraints
1 <= n <=1000
1 <= a[i] <=1000
Output Format
Print the sum of the integers in the array.
Sample Input 0
6
16 13 7 2 1 12
Sample Output 0
51
Sample Input 1
7
1 13 15 20 12 13 2
Sample Output 1
76
int main()
{ int n, *arr, i, sum = 0;
scanf("%d", &n);
arr = (int*) malloc(n * sizeof(int));
for(i = 0; i < n; i++) {
scanf("%d", arr + i);
}
for(i = 0; i < n; i++) {
sum += *(arr + i);
}
printf("%d\n", sum);
return 0;
}
Q-12
In this challenge, create an array of size n dynamically, and read the
values from stdin.
Reverse the given array.
1 140 82
2 89 134
3 90 110
4 112 106
5 88 90
Q-13
The total scores of both players, the leader and the lead after each round
for this game is given below:
1 140 82 Player 1 58
Output
Your output must consist of a single line containing two integers W
and L, where W is 1 or 2 and indicates the winner and L is the
maximum lead attained by the winner.
Q-13
Input:
5
140 82
89 134
90 110
112 106
88 90
Output:
1 58
Q-15
An English text needs to be encrypted using the following encryption
scheme. First, the spaces are removed from the text. Let L be the
length of this text. Then, characters are written into a grid, whose
rows and columns have the following constraints:
For example:
return 4->5->1->2->3->NULL.
Q-18
Raj is a boy. He loves Sanjeev’s recipes very much.
Raj like a positive integer p, and now he wants to get a receipt of
Sanjeev’s restaurant whose total price is exactly p. The current menus
of Sanjeev’s restaurant are shown the following table.
Q-18
Note that the i-th menu has the price 2i-1 (1 ≤ i ≤ 12).
Input
The first line contains an integer T, the number of test cases. Then T
test cases follow. Each test case contains an integer p.
Output
For each test case, print the minimum number of menus whose total
price is exactly p.
Q-18
Constraints
1≤T≤5
1 ≤ p ≤ 100000 (105)
There exists combinations of menus whose total price is exactly p.
Sample Input
4
10
256
255
4096
Sample Output
2
1
8
2
Q-18
Explanations
In the first sample, examples of the menus whose total price is 10 are
the following:
1+1+1+1+1+1+1+1+1+1 = 10 (10 menus)
1+1+1+1+1+1+1+1+2 = 10 (9 menus)
2+2+2+2+2 = 10 (5 menus)
2+4+4 = 10 (3 menus)
2+8 = 10 (2 menus)
Here the minimum number of menus is 2.
Problem Constraints
1 <= |A| <= 105
Input Format
First argument is an string A.
Output Format
Return 1 if parantheses in string are balanced else return 0.
Q-19
Example Input
Input 1:
A = "(()())"
Input 2:
A = "(()"
Example Output
Output 1:
1
Explanation 1:
Given string is balanced so we return 1
Output 2 :
0
Explanation 2:
Given string is not balanced so we return 0
Q-20
#include <stdio.h>
/* Function to get sum of digits */
int getSum(int n)
{
int sum;
/* Single line that calculates sum */
for (sum = 0; n > 0; sum += n % 10, n /= 10);
return sum;
}
int main()
{
int n = 687;
printf(" %d ", getSum(n));
return 0;
}
Q-20
/ C program to compute sum of digits in number using recursion.
#include <stdio.h>
using namespace std;
int sumDigits(int no)
{
return no == 0 ? 0 : no % 10 + sumDigits(no / 10);
}
int main(void)
{
printf("%d", sumDigits(687));
return 0;
}
Q-21
Lucky numbers are some special integer numbers. From basic
numbers, some special numbers are eliminated by their position.
Instead of their value, for their position, the numbers are eliminated.
The numbers which are not deleted, they are the lucky numbers.
Example 2:
Input:
N = 19
Output: 1
Explanation: 19 is a lucky number
Q-21
#include <stdio.h>
/* Returns 1 if n is a lucky no. otherwise returns 0*/
int isLucky(int n ,int counter)
{
int next_position = n;
if(counter > n)
return 1;
if(n%counter == 0)
return 0;
}
Q-22
Input:
12345
Output:
54321
Q-23
Given a stack, the task is to sort it such that the top of the stack has
the greatest element.
Example 1:
Input:
Stack: 3 2 1
Output: 3 2 1
Example 2:
Input:
Stack: 11 2 32 3 41
Output: 41 32 11 3 2
Q-23
Example 1:
Note that it is the kth largest element in the sorted order, not the kth
distinct element.
Example 1:
Each seat costs equal to the number of vacant seats in the row it
belongs to. The task is to maximize the profit by selling the tickets
to B people.
Input Format
First argument is the array A.
Output Format
Return one integer, the answer to the problem.
Q-27
Example Input
Input 1:
A = [2, 3]
B=3
Input 2:
A = [1, 4]
B=2
Example Output
Output 1:
7
Output 2:
7
Q-12
Given a sentence, , print each word of the sentence in a new line.
Input Format
The first and only line contains a sentence, .
Constraints
Output Format
Print each word of the sentence in a new line.
Sample Input 0
This is C
Sample Output 0
This
is
C
Explanation 0
In the given string, there are three words ["This", "is", "C"]. We have to print
each of these words in a new line.
Sample Input 1
Learning C is fun
Sample Output 1
Learning
C
is
fun
Sample Input 2
How is that
Sample Output 2
How
Is
that
int main()
{ char *s;
int i;
s =(char*)malloc(1024 * sizeof(char));
scanf("%[^\n]", s);
s = realloc(s, strlen(s) + 1);
int len = strlen(s);
for( i = 0; i < len; i++) {
if(s[i] == ' ') {
printf("\n"); }
else {
printf("%c", s[i]);
} }
free(s); return 0;
}
Q-13
Given a string, , consisting of alphabets and digits, find the frequency of each
digit in the given string.
Input Format
The first line contains a string, which is the given number.
Constraints
All the elements of num are made of english alphabets and digits.
Output Format
Print ten space-separated integers in a single line denoting the frequency of
each digit from to .
Sample Input 0
a11472o5t6
Sample Output 0
0210111100
Explanation 0
In the given string:
occurs two times.
and occur one time each.
The remaining digits and don't occur at all.
Sample Input 1
lw4n88j12n1
Sample Output 1
0 2 1 0 1 0 0 0 2 0
Sample Input 2
1v88886l256338ar0ekk
Sample Output 2
1 1 1 2 0 1 2 0 5 0
int main()
{
char *s;
s = (char*)malloc(1024 * sizeof(char));
scanf("%s", s);
s = realloc(s, strlen(s) + 1);
int len = strlen(s), i;
int arr[10];
for(i = 0; i < 10; i++)
arr[i] = 0;
Input format:
5
2
4
1
3
5
Sample output:
Mixed
int main() {
//fill the code;
int n;
scanf("%d",&n);
int arr[n];
int i;
int odd = 0, even = 0;
for(i = 0; i < n; i++) {
scanf("%d",&arr[i]); }
for(i = 0; i < n; i++) {
if(arr[i] % 2 == 1)
odd++;
if(arr[i] % 2 == 0)
even++;
}
if(odd == n)
printf("Odd");
else if(even == n)
printf("Even");
else
printf("Mixed");
return 0;
}
Q-16
Program to find the sum of perfect square elements in an array is discussed
here. An array is given as input and the sum of all the perfect square
elements present in the array is produced as output.
fVar=sqrt((double)number);
iVar=fVar;
if(iVar==fVar)
return number;
else
return 0;
}
int main() {
int n;
scanf("%d",&n);
int arr[n];
int i;
for(i = 0; i < n; i++){
scanf("%d",&arr[i]);}
int sum = 0;
for(i = 0; i < n; i++)
{
sum = sum + isPerfectSquare(arr[i]);
}
printf("%d",sum);
return 0;
}
Q-17
Program to sort a string in alphabetical order is discussed here. Given a string,
the task is to sort the string in alphabetical order and display it as output.
Input: face
Output: acef
int main ()
{
char string[100];
printf("\n\t Enter the string : ");
scanf("%s",string);
char temp;
int i, j;
int n = strlen(string);
for (i = 0; i < n-1; i++) {
for (j = i+1; j < n; j++) {
if (string[i] > string[j]) {
temp = string[i];
string[i] = string[j];
string[j] = temp; } } }
N friends are planning to go to a movie. One among them suggested few movies and
all others started to discuss and finally they selected a movie. One among them quickly
booked their tickets online, to their surprise they are unable to select their seats. All of
them got confused. Then anyhow, decided to go to the movie. They rushed to reach the
theater on time. Again they are surprised that no one was there in the theater. They are
the only people about to watch the movie. There is 'r' number of seats in which, 'n'
number persons should sit. In how many ways they can sit inside the theater?
Given the number of people 'n' and the number of seats 'r' as input. The task is to find
the different number of ways in which 'n' number of people can be seated in those 'r'
number of seats.
Input:
Number of people: 5
Number of Rows: 3
Output:
The total number of ways in which 'n' people can be seated in 'r' seats = 60.
Calculation:
P(n,r) = n! /(n-r)!
#include<stdio.h>
int fact(long int x)
{
long int f=1,i;
for(i=1;i<=x;i++)
{
f=f*i;
}
return f;
}
int main()
{
long int n,r,p,temp;
long int num,den;
// Enter the number of seats
printf("Enter the number of seats available : ");
scanf("%ld",&r);
// Enter the number of people
printf("nEnter the number of persons : ");
scanf("%ld",&n);
// Base condition
// Swap n and r
if(n < r)
{
temp=n;
n=r;
r=temp;
}
num=fact(n);
den=fact(n-r);
p=num/den;
printf("nNumber of ways people can be seated : ");
printf("%ld",p);
}
Q-19
Program to toggle each character in a string is discussed here. A string is
given as input in which all the lower case letters are converted to upper case
and the upper case letters are converted to lower case.
For example, consider the following string
Input: FacePrep
Output: fACEpREP
Input: FocucAcadeMy
Output: fOCUSaCADEmy
#include <stdio.h>
#define MAX_SIZE 100 // Maximum string size
void toggleCase(char * str);
int main()
{
char str[MAX_SIZE];
printf("Enter any string: ");
gets(str);
Sample Input:
pro$#&gra7m
Sample Output:
program
int main()
{
char str[150];
int i, j;
printf("\nEnter a string : ");
gets(str);
for(i = 0; str[i] != '\0'; ++i)
{
while (!( (str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z') || str[i] ==
'\0') )
{
for(j = i; str[j] != '\0'; ++j)
{
str[j] = str[j+1];
}
str[j] = '\0';
}
}
printf("\nResultant String : ");
puts(str);
return 0;
}
Q-21
Printing an array into Zigzag fashion is discussed here. Suppose you were
given an array of integers, and you are told to sort the integers in a zig-zag
pattern. In general, in a zig-zag pattern, the first integer is less than the second
integer, which is greater than the third integer, which is less than the fourth
integer, and so on.
Hence, the converted array should be in the form of
e1 < e2 > e3 < e4 > e5 < e6.
Input 1:
7
4 3 7 8 6 2 1
Output 1:
3 7 4 8 2 6 1
Input 2:
4
1 4 3 2
Output 2:
1 4 2 3
void swap(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}
void zig_zag_fashion(int arr[], int n)
{
int flag = 1;
int i;
for (i=0; i<=n-2; i++)
{
if (flag)
{
if (arr[i] > arr[i+1])
swap(&arr[i], &arr[i+1]);
}
else
{
if (arr[i] < arr[i+1])
swap(&arr[i], &arr[i+1]);
}
flag = !flag;
}
}
int main()
{
int n,i;
printf("\nEnter the number of elements : " );
scanf("%d", &n);
int arr[n];
printf("\nInput the array elements : ");
for(i=0; i<n; i++)
scanf("%d", &arr[i]);
printf("\nZigzag pattern of the array : ");
zig_zag_fashion(arr, n);
for (i=0; i<n; i++)
printf("%d ", arr[i]);
printf("\n");
return 0;
}
Q-22
Program to print Pascal's triangle is discussed here. Pascals triangle is a
triangular array of the binomial coefficients.
The numbers outside Pascal's triangle are all "0". These "0s" are very
important for the triangular pattern to work to form a triangular array. The
triangle starts with a number "1" above, and any new number added below the
upper number "1" is just the sum of the two numbers above, except for the
edge, which is all "1".
row 0 =1
row 1 = (0+1), (1+0) = 1, 1
row 2 = (0+1), (1+1), (1+0) = 1, 2, 1
row 3 = (0+1), (1+2), (2+1), (1+0) = 1, 3, 3, 1
row 4 = (0+1), (1+3), (3+3), (3+1), (1+0) = 1, 4, 6, 4, 1
row 5 = (0+1), (1+4), (4+6), (6+4), (4+1),(1+0) = 1, 5, 10, 10, 5, 1
row 6 = (0+1), (1+5), (5+10), (10+10), (10+5), (5+1), (1+0) = 1, 6, 15, 20, 15,
6, 1
int main()
{
int rows, coef = 1, space, i, j;
printf("\nEnter the number of rows : ");
scanf("%d",&rows);
printf("\n");
printf("%4d", coef);
}
printf("\n\n");
}
return 0;
}
Q-23
Input:
First line contains an integer, the number of test cases ‘T’. Each test case
should be an integer. Size of the array ‘N’ in the second line. In the third line,
input the integer elements of the array in a single line separated by space.
Element X should be inputted in the fourth line, i.e., after entering the
elements of array. Repeat the above steps second line onwards for multiple
test cases.
Output:
Print the output in a separate line returning the index of the element X. If the
element is not present, then print -1.
Constraints:
1 <= T <= 100
1 <= N <= 100
1 <= Arr[i] <= 100
Input:
2
4
1234
3
5
10 90 20 30 40
40
Output:
2
4
Explanation:
There are 2 test cases (Note 2 at the beginning of input)
Test Case 1: Input: arr[] = {1, 2, 3, 4},
Element to be searched = 3.
Output: 2
Explanation: 3 is present at index 2.
Output:
Write a single integer to output , denoting how many integers t’i are divisible
by k.
Input:-
7 3
1
51
966369
7
9
999996
11
Output:-
4
void main()
{
int n,k,i,t;
int tot=0;
printf("Enter Two Value\n");
scanf("%d%d",&n,&k);
for(i=0; i<n;i++)
{
scanf("%d",&t);
if(t%k==0)
{
tot++;
}
}
printf("Value is :%d\n",tot);
}
Q-25
You are given an integer N, Write a program to calculate the sum of the digits
of N.
Input:-
The first line contains an integer T, total number of testcases. Then follow T
lines, each line contains an integer N.
Output:- Calculate the sum of digits of N.
Constraints:-
1 <= T <=1000
1 <= N <=100000
Input:-
3
12345
31203
2123
Output:-
15
9
8
void main()
{
int T;
printf("Enter Number of TestCase\n");
scanf("%d",&T);
while(T--)
{
int N;
int rem,sum=0;
printf("Enter Number\n");
scanf("%d",&N);
while(N!=0)
{
rem = N%10;
sum = sum + rem;
N = N/10;
}
printf("%d\n",sum);
}
}
Q-26
You are given four integers a,b,c and d. Determine if there is a rectangle such
that the lengths of its sides are a,b,c and d(in any order).
Input:-
The first line of the input contains a single integer T denoting the number of
test cases. The description of T test cases follows.
The first and only of each test case contains four space-separated integer
a,b,c and d.
Output:-
For each test case , print a single line containing one string “YES” or “NO”
Constraints:
1 <= t <=1000
1<= a,b,c,d <=10000
Input:
3
1 1 2 2
3 2 2 3
4 2 2 2
Output:-
YES
YES
NO
void main()
{
int T;
printf("Enter Number of TestCase\n");
scanf("%d",&T);
while(T--)
{
int a,b,c,d;
printf("\nEnter Four Side\n");
scanf("%d%d%d%d",&a,&b,&c,&d);
if((a==c && b==d) || (a==b && c==d) || (a==d && c==b))
{
printf("\nYES");
}
else
{
printf("NO");
}
}
}
Q-27
Chef is a really and respectful person, in sharp contrast to his little brother,
Who is a very nice and disrespectful person. Chef always sends messages to his
friends in all small letters, whereas the little brother sends messages in all capital
letters.
You just received a message given by a string s. You don’t know whether this
message is sent by Chef or his brother. Also the communication channel through
which you received the message is erroneous and hence can flip a letter from
uppercase to lowercase or vice versa. However you know that this channel can make
at most k such flips.
Determine whether the messages could have been sent only by Chef , only by the
little brother , the message could have been sent only by Chef , only by the little
brother, by both or by none.
Input:-
The first line of the 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 cases contains two space-separated integer N and K
denoting the length of the string S and the maximum number of flips that the
erroneous channel can make.
The second line contains a single string s denoting the messages you received.
Output:-
For each test case , output a single line containing one string- ‘chef’ , ‘brother’
, ‘both’ , ‘none’.
Constraints:-
1 <= t <= 1000
1 <= N <= 100
0 <= K <=N
S consists only of (lowercase and uppercase)
Input:-
4
5 1
frauD
6 1
FRAUD
7 4
Life
10 4
sTRAWBerry
Output:-
Chef
brother
both
none
void main()
{
int T;
printf("Enter Number of TestCase\n");
scanf("%d",&T);
while(T--)
{
int n,k,i;
int cap=0;
int small=0;
char ch[20];
if(cap==small)
{
if(k>=cap)
{
printf("both\n");
}
else if(k<cap)
{
printf("none\n");
}
}
else if(small > cap)
{
if(k>=small)
{
printf("both\n");
}
else if(k<cap)
{
printf("none\n");
}
else
{
printf("chef\n");
}
}
else if(cap > small)
{
if(k>=cap)
{
printf("both\n");
}
else if(k<small)
{
printf("none\n");
}
else
{
printf("brother\n");
}
}
}
}
Q-28
He knows some subset of the letter of lattin alphabet. In order to help Jeff to
study, Chef gave him a book with the text consisting of N words . Jeff can read
a word if it consists only of the letters he knows.
Now Chef is curious about which words his brother will be able to read and
which are not. Please help him!.
Input:-
The first line of the input contains a lowercase Latin letter string S. consisting
of the letters Jeff can read. Every letter will appear in S no more than once.
The second line of the input contains an integer N denoting the number of
words in the book.
Output:-
For each of the words output “Yes” in case Jeff can read it. And “No”
otherwise.
Constraints
1<= ISI <=26
1<= N <=1000
1<=|wi|<=12
Each letter will appear in S no more than once
S,W consist only of lowercase Lattin letters.
Input:-
act
2
cat
dog
Output:-
Yes
No
void main()
{
char s[100];
int a[26]= {0}; // we put the value 0 in all the blocks
int len,len1,i,j;
a[p]=1;
}
int n;
printf("Enter N\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
int count=0; // no of character that not present in string s
char k[10];
printf("Enter String\n");
scanf("%s",&k); // k ---> words
len1= strlen(k);
}
Q-29
What is the maximum number of squares of size 2x2 that can be fit in a right
angled isosceles triangle of base B.
One side of the square must be parallel to the base of the isosceles triangle.
Base is the shortest side of the triangle
Input:-
First line contains T, the number of test cases.
Each of the following T lines contains 1 integer B.
Output:-
Output exactly T lines, each line containing the required answer.
Constraints:-
1 ≤ T ≤ 103
1 ≤ B ≤ 104
Sample Input
11
1
2
3
4
5
6
7
8
9
10
11
Sample Output
0
0
0
1
1
3
3
6
6
10
10
#include<stdio.h>
int main()
{
int T;
printf("Enter Test Case\n");
scanf("%d",&T);
while(T--)
{
int b;
printf("Enter Side of Triangle\n");
scanf("%d",&b);
if(b==1 || b==2 || b==3)
{
printf("0\n");
}
else
{
if(b%2==0) // for even
{
int area =0.5*b*b;
int res = (area -b)/4;
printf("%d \n",res);
}
else // for odd
{
b = b-1;
int area =0.5*b*b;
int res = (area -b)/4;
printf("%d \n",res);
}
}
}
}
Q-30
Kostya likes the number 4 much. Of course! This number has such a lot of properties,
like:
• Four is the smallest composite number;
• It is also the smallest Smith number;
• The smallest non-cyclic group has four elements;
• Four is the maximal degree of the equation that can be solved in radicals;
• There is four-color theorem that states that any map can be colored in no
more than four colors in such a way that no two adjacent regions are colored in the
same color;
• Lagrange's four-square theorem states that every positive integer can be
written as the sum of at most four square numbers;
• Four is the maximum number of dimensions of a real division algebra;
• In bases 6 and 12, 4 is a 1-automorphic number;
• And there are a lot more cool stuff about this number!
Impressed by the power of this number, Kostya has begun to look for occurrences of
four anywhere. He has a list of T integers, for each of them he wants to calculate the
number of occurrences of the digit 4 in the decimal representation. He is too busy
now, so please help him.
Input:-
The first line of input consists of a single integer T, denoting the number of integers in
Kostya's list.
Then, there are T lines, each of them contain a single integer from the list.
Output:-
Output T lines. Each of these lines should contain the number of occurences of the
digit 4 in the respective integer from Kostya's list.
Constraints:-
• 1 ≤ T ≤ 105
• (Subtask 1): 0 ≤ Numbers from the list ≤ 9 - 33 points.
• (Subtask 2): 0 ≤ Numbers from the list ≤ 109 - 67 points.
Input:
5
447474
228
6664
40
81
Output:
4
0
1
1
0
#include<stdio.h>
int main()
{
int T;
printf("Enter Test Case\n");
scanf("%d",&T);
while(T--)
{
int n,i;
char ch[100];
scanf("%d",&n);
sprintf(ch, "%d", n);
int count=0;
int len;
len = strlen(ch);
for(i=0; i<len; i++)
{
if(ch[i]=='4')
{
count++;
}
else
{
count=count;
}
}
printf("%d \n",count);
}
}
Q-31
Chef's daily routine is very simple. He starts his day with cooking food, then he eats
the food and finally proceeds for sleeping thus ending his day. Chef carries a robot as
his personal assistant whose job is to log the activities of Chef at various instants
during the day. Today it recorded activities that Chef was doing at N different instants.
These instances are recorded in chronological order (in increasing order of time). This
log is provided to you in form of a string s of length N, consisting of characters 'C', 'E'
and 'S'. If s[i] = 'C', then it means that at the i-th instant Chef was cooking, 'E' denoting
he was eating and 'S' means he was sleeping.
You have to tell whether the record log made by the robot could possibly be correct or
not.
Input:-
The first line of the input contains an integer T denoting the number of test cases. The
description of T test cases follows.
The only line of each test case contains string s.
Output:-
For each test case, output a single line containing "yes" or "no" (without quotes)
accordingly.
Constraints
• 1 ≤ T ≤ 20
• 1 ≤ N ≤ 105
Input:
5
CES
CS
CCC
SC
ECCC
Output:
yes
yes
yes
no
no
#include<stdio.h>
int main()
{
int T;
printf("Enter Number of Test Case\n");
scanf("%d",&T);
while(T--)
{
int len,i,count=0;
char ch[10];
printf("Enter A String\n");
scanf("%s",&ch);
len = strlen(ch);
for(i=0; i<len-1; i++)
{
if(ch[i]=='C')
{
if(ch[i+1]=='E' || ch[i+1]=='S' || ch[i+1]=='C')
{
count++;
}
}
else if(ch[i]=='E')
{
if(ch[i+1]=='S' || ch[i+1]=='E')
{
count++;
}
}
else if(ch[i]=='S')
{
if(ch[i+1]=='S')
{
count++;
}
}
}
if(count==len-1)
{
printf("yes\n");
}
else
{
printf("no\n");
}
}
}
Q-32
Chef Two and Chef Ten are playing a game with a number XX. In one turn, they can
multiply XX by 22. The goal of the game is to make XX divisible by 1010.
Help the Chefs find the smallest number of turns necessary to win the game (it may be
possible to win in zero turns) or determine that it is impossible.
Input
• The first line of the input contains a single integer TT denoting the number
of test cases. The description of TT test cases follows.
• The first and only line of each test case contains a single integer denoting the
initial value of XX.
Output
For each test case, print a single line containing one integer — the minimum required
number of turns or −1−1 if there is no way to win the game.
Constraints
• 1≤T≤10001≤T≤1000
• 0≤X≤1090≤X≤109
Example Input
3
10
25
1
Example Output
0
1
-1
#include<stdio.h>
int main()
{
int T;
printf("Enter Number of Test Case\n");
scanf("%d",&T);
while(T--)
{
int n;
printf("Enter A Number\n");
scanf("%d",&n);
if(n%10==0)
{
printf("0\n");
}
else if(n%5==0)
{
printf("1\n");
}
else
{
printf("-1\n");
}
}
}
Q-33
Print a pattern of numbers from to as shown below. Each of the numbers is
separated by a single space.
4444444
4333334
4322234
4321234
4322234
4333334
4444444
Input Format
The input will contain a single integer .
Constraints
Sample Input 0
2
Sample Output 0
222
212
222
Sample Input 1
5
Sample Output 1
555555555
544444445
543333345
543222345
543212345
543222345
543333345
544444445
555555555
#include<stdio.h>
void print2Dsequence(int n){
int i,j,k,l;
int s = 2 * n - 1;
for (i = 0; i < (s / 2) + 1; i++) {
int m = n;
for ( j = 0; j < i; j++) {
printf("%d ",m);
m--;
}
for ( k = 0; k < s - 2 * i; k++) {
printf("%d ",n-i);
}
m = n - i + 1;
for ( l = 0; l < i; l++) {
printf("%d ",m);
m++;
}
printf("\n");
}
for (i = s / 2 - 1; i >= 0; i--) {
int m = n;
for (j = 0; j < i; j++) {
printf("%d ",m);
m--;
}
for (k = 0; k < s - 2 * i; k++) {
printf("%d ",n-i);
}
m = n - i + 1;
for ( l = 0; l < i; l++) {
printf("%d ",m);
m++;
}
printf("\n");
}
}
int main(){
int n = 4;
Example
Return '12:01:00'.
Return '00:01:00'.
Function Description
Complete the timeConversion function in the editor below. It should return a new string
char s[20];
scanf("%s",s);
if(s[strlen(s)-2]=='P' || s[strlen(s)-2]=='p')
{
if(!(s[0]=='1' && s[1]=='2'))
{
s[0]=s[0]+1;
s[1]=s[1]+2;
}
}
else if(s[strlen(s)-2]=='A' || s[strlen(s)-2]=='a')
{
if(s[0]=='1' && s[1]=='2')
{
s[0]='0';
s[1]='0';
}
}
s[strlen(s)-2]='\0';
printf("%s",s);
return 0;
}
Q-35
You are in charge of the cake for a child's birthday. You have decided the cake will
have one candle for each year of their total age. They will only be able to blow out the
Example
The maximum height candles are units high. There are of them, so return .
Function Description
have one candle for each year of their total age. They will only be able to blow out the
Example
The maximum height candles are units high. There are of them, so return .
Function Description
The minimum sum is and the maximum sum is . The function prints
16 24
Function Description
Complete the miniMaxSum function in the editor below.
miniMaxSum has the following parameter(s):
• arr: an array of integers
Print
Print two space-separated integers on one line: the minimum sum and the maximum sum
of of elements.
Input Format
Constraints
Output Format
Print two space-separated long integers denoting the respective minimum and maximum values
that can be calculated by summing exactly four of the five integers. (The output can be greater