Cogni 3-8-2021
Cogni 3-8-2021
Format:
Input:
The first line of input contains an integer T, denoting the test cases. Every test case contains two lines, the first line is N (size of an array) and the
second line is space-separated elements of an array.
Output:
Space-separated elements of sorted arrays. There should be a new line between the output of every test case.
Constraints:
1 < = T <= 100
1 <= N <= 106
0 <= A[i] <= 1
Example:
Input:
2
5
10110
10
1011111000
Output:
00111
0000111111
Test Case 1
Input
2 5 1 0 1 1 0 10 1 0 1 1 1 1 1 0 0 0
Output
00111
0000111111
Test Case 2
Input
2 10 1 0 0 0 1 0 1 1 0 1 7 1 1 1 1 0 0 0
Output
0000011111
0001111
Abhi is a spy. Its quite natural for him to go on a secret mission. Once You and Abhi were in the same secret mission, when Abhi felt an
urgent need to communicate with Dr. Sabitabatra, who appointed both of you for this mission. The only way to communicate with him was
by sending letters. As both of you were on a secret mission, Abhi will write the letter with some encrypted words which Dr. Sabitabatra was
well aware of . As per the encryption is concerned, the encrypted word will start with the middle character of the string and will be formed
henceforth with the middle characters of the left and right substrings (of the middle character of the word) and so on. Take a look at the
explanation of the sample test case for better comprehension. As the letter was going to be quite long, Abhi wants your help to encrypt
some critical words for him so that he can quickly finish the letter and sent it to Dr. Sabitabatra.
INPUT :
The first line contains an integer T denoting the number of TEST CASES. Each TEST CASE consists of 2 lines. The first line contains an
integer N denoting the length of the word that needs to be encrypted. The second line contains the word that needs to be encrypted.
OUTPUT : N lines where each line will contain the encrypted words
CONSTRAINTS :
1 <= T <= 10
1 <= Length of the string <= 10000
Sample Input:
2
3
abc
4
abcd
Sample Output:
bac
bacd
Test Case 1
Input
2 3 abc 4 abcd
Output
bac
bacd
Test Case 2
Input
1 50 rscdrregojwcpsxpfjlxbksvulbsugewwxaresuhkokbpslskx
Output
ucrcrsdroegjwjxpspfblxskvseslbugxwwraebkuhoklpsksx
Given an array of positive integers. Your task is to find the leaders in the array.
Note: An element of array is leader if it is greater than or equal to all the elements to its right side. Also, the rightmost element is always a leader.
Input:
The first line of input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains a single integer N denoting the size of array.
The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of the array.
Output:
Print all the leaders.
Constraints:
1 <= T <= 100
1 <= N <= 107
0 <= Ai <= 107
Example:
Input:
3
6
16 17 4 3 5 2
5
12340
5
74573
Output:
17 5 2
40
773
Explanation:
Testcase 3: All elements on the right of 7 (at index 0) are smaller than or equal to 7. Also, all the elements of right side of 7 (at index 3) are smaller than
7. And, the last element 3 is itself a leader since no elements are on its right.
Test Case 1
Input
3 6 16 17 4 3 5 2 5 1 2 3 4 0 5 7 4 5 7 3
Output
17 5 2
40
773
Test Case 2
Input
1 15 12 45 85 96 96 85 324 422 10 14 721 741 852 65 453
Output
852 453
Swap two numbers [C Programming]
Write a program to swap the 2 numbers using logical operator.
The given program compiles successfully but fails to return the desired result for some test cases due to logical errors.
Your task is to fix the code so that it passes all the test cases.
The code has been commented. Uncomment the code and debug it.
#include <stdio.h>
int main()
{
/*
int i = 65;
int k = 120;
Case 1
printf("%d %d\n", i, k); Input (stdin)
65 120
i = i ~ k; Output (stdout)
k = k ^ i;
printf("%d %d", i, k); 65 120
120 65
return 0;
*/
}
Binary palindrome
Write a program to check whether it’s a binary palindrome or not.
Input format:
The input consist of an integer
Output format:
Print "Yes" or "No"
Sample Input:
9
Sample Output:
Yes
Case 1 Case 2
Input (stdin) Input (stdin)
9 8
Output (stdout) Output (stdout)
Yes No
Dirty Characters
Write a function that takes two strings as arguments and removes the characters from first string which are present in
second string. Note: Treat lowercase and uppercase characters differently. And, space ‘ ‘ is also considered as a
character. Function: removeDirtyChars: function(str, mask_str) This function should return the string after removing
those characters from string str which are present in mask_str.
Input Format
Input is being fetched from the command line arguments. The two arguments are the two strings.
Output Format
Output will be the new string after masking. Case 1 Case 2
Constraints Input (stdin) Input (stdin)
Length of string str>=length of string mask_str.
Length of string str is within range [1,1000]. "Dennis" "Face"
Length of string mask_str is within range [1,100]. "RPD" "focus"
Refer the sample output for formatting Output (stdout) Output (stdout)
Sample Input:
ennis Fae
“Dennis Ritchie”
“RPD”
(Both the input strings should be in between double quotes).
Sample Output:
ennis itchie
Traverse M nodes and delete N nodes
Given head to the singly linear linked list, traversal M nodes and delete N node repeatedly
until you reach the end.
Hint:
Traverse the list M nodes and delete N nodes,
Connect the Mth node and N+1th node directly.
Consider the edge case that the list terminates.
Sample Input:
10
32 Case 1 Case 2
1 2 3 4 5 6 7 8 9 1 Input (stdin) Input (stdin)
Sample Output: 10 5
123678 32 21
1234567891 12345
Output (stdout) Output (stdout)
123678 1245
Make a palindrome
Given a string find the minimum number of appends to make it a palindrome. Rearranging of
the string is not permitted.
Input format
The input consists of string.
Output format
The output prints the minimum number of appends needed
Sample Input:
abcec
Sample Output:
2
Case 1 Case 2
Input (stdin) Input (stdin)
abcec adda
Output (stdout) Output (stdout)
2 0
Check if a given string is a substring of another.
Write a program to check if a given string is a substring of another.
Input Format:
The first input corresponds to the integer.
The remaining consist of string.
Output Format:
Check whether sub string is there or not. If it is print "Yes" or "No".
Sample Input:
Case 1
2
Input (stdin)
Madam mad
2
Madam Mad
Madam mad
Sample Output:
Madam Mad
No
Output (stdout)
Yes
No
Yes
Maximum Exponent
You are given a function,
Int MaxExponents (int a , int b);
You have to find and return the number between ‘a’ and ‘b’ ( range inclusive on both ends)
which has the maximum exponent of 2.
The algorithm to find the number with maximum exponent of 2 between the given range is
Loop between ‘a’ and ‘b’. Let the looping variable be ‘i’.
Find the exponent (power) of 2 for each ‘i’ and store the number with maximum exponent of 2
, let say ‘max’. Set ‘max’ to ‘i’ only if ‘i’ has more exponent of 2 than ‘max’.
Return ‘max’.
Assumption: a <b
Note: If two or more numbers in the range have the same exponents of 2 , return the small
number.
Example
Input:
7
12
Output:
8