Questions - For - Smvec - Set 2
Questions - For - Smvec - Set 2
In the digital realm of Cyberville, an ancient numeric cipher has been discovered by the
intrepid coder, Alex. This cipher, embedded within a mysterious string, possesses the power
to reverse the order of its alphabetic characters while leaving the numeric codes untouched.
As the chosen codebreaker, you are bestowed with the task of crafting a program to unravel
this enigma. The cipher, known as "AlphaChronos," has the ability to encode messages with
its unique reversal technique.
Embark on your coding quest, delve into the secrets of Cyberville, and create a C program to
decrypt the AlphaChronos cipher. Can you unlock the hidden messages encoded within
strings, where only the alphabet dances in reverse while the numeric codes stand steadfast?
Input Format:
Output Format:
Output shows the reversed string without change the position of numbers in it.
Sample Input:
A3r7un
Sample Output:
n3u7rA
Solution :
#include <stdio.h>
#include <ctype.h>
int main() {
char input[100];
char temp;
int i, j;
scanf("%s", input);
// Find the length of the input
int length = 0;
length++;
i = 0;
j = length - 1;
while (i < j) {
i++;
j--;
if (i < j) {
temp = input[i];
input[i] = input[j];
input[j] = temp;
i++;
j--;
}
return 0;
Test cases:
case=t1
input=A3r7un
output=n3u7rA
case=t2
input=3wr5o8k
output=3ko5r8w
case=t3
input=a7b8c5d
output=d7c8b5a
case=t4
input=B12a34t56
output=t12a34B56
case=t5
input=12h3a4i2i
output=12i3i4a2h
Question 2:
Input Format:
Output Format:
Sample Input:
10
20
47
Sample Output:
Solution:
#include <stdio.h>
int main() {
int n, i, j, max = 0;
scanf("%d", &n);
scanf("%d", &arr[i]);
lis[i] = 1;
lis[i] = lis[j] + 1;
}
// Find the maximum value in lis[]
max = lis[i];
printf("%d\n", max);
return 0;
Test Cases:
case=t1
input=5
10
20
47
output=3
case=t2
input=10
80
20
25
35
90
36
40
output=8
case=t3
input=7
28
38
29
30
57
34
40
output=5
case=t4
input=7
10
20
output=3
case=t5
input=4
10
output=2
Question 3:
Imagine a librarian arranging books on a shelf. Initially, the books are scattered randomly.
As the librarian starts sorting them, she picks up one book at a time and carefully places it in
its correct position among the already sorted books. Eventually, all the books are neatly
arranged in ascending order. Can you write a program that mimics the librarian's sorting
process, sorting an array of numbers using a similar approach?
Input Format:
Output Format:
It shows the sorted array.
Sample Input:
90 4 78 65 6
Sample output:
4 6 65 78 90
solution:
#include <stdio.h>
int main() {
int n, i, j, key;
scanf("%d", &n);
int arr[n];
scanf("%d", &arr[i]);
}
// Insertion sort algorithm
key = arr[i];
j = i - 1;
arr[j + 1] = arr[j];
j = j - 1;
arr[j + 1] = key;
return 0;
Test cases:
case=1
input=5
90 4 78 65 6
output=4 6 65 78 90
case=2
input=7
7 98 4 66 90 8 10
output=4 7 8 10 66 90 98
case=3
input=10
88 79 43 65 78 95 34 23 89 69
output=23 34 43 65 69 78 79 88 89 95
case=4
input=15
99 67 78 56 65 45 88 90 68 87 98 89 13 96 15
output=13 15 45 56 65 67 68 78 87 88 89 90 96 98 99
case=5
input=6
695482
output=2 4 5 6 8 9
Question 4:
Picture a bustling marketplace where vendors arrange their goods for display. Initially, the
items are randomly scattered on the stalls. As the market opens, vendors start comparing
adjacent items, swapping them if they are out of order. Gradually, the marketplace becomes
organized, with items sorted in ascending order. Can you write a program that simulates this
process, sorting an array of numbers using a similar bubble-like movement?
Input Format:
Output Format:
Sample Input:
bfadc
Sample Output:
abcdf
Solution:
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
char arr[n];
// Input the elements of the array
// Sorting logic
arr[j + 1] = temp;
printf("Sorted array:\n");
return 0;
}
Test cases:
case=1
input=5
bfadc
output=a b c d f
case=2
input=7
qwertyb
output=b e q r t w y
case=3
input=10
hgimbnfsou
output=b f g h i m n o u s
case=4
input=15
zxcvbnmasdfghjk
output=a b c d f g h j k m n s v x z
case=5
input=6
poiuyt
output=i o p t u y
Question 5:
You are given an array of n unique integers where the integers range from 1 to n. One number in
the range is missing. Write a program in C to find the missing number efficiently using the
formula for the sum of the first n natural numbers.
Input Format:
1. The first line contains an integer n, the total number of elements that should be in the
array.
2. Enter the elements of array
Output Format:
Output a single line containing the missing number.
Sample Input:
12356
Sample Output:
Solution:
#include <stdio.h>
int main() {
scanf("%d", &n);
int arr[n];
scanf("%d", &arr[i]);
actualSum += arr[i];
expectedSum = (n + 1) * (n + 2) / 2;
return 0;
Test cases:
case=t1
input=5
12356
output=4
case=t2
input=4
1425
output=3
case=t3
input=5
53146
output=2
case=t4
input=5
4253
output=1
case=t5
input=3
output=3
Question 6 :
Given an array of integers, find the majority element, which appears more than n / 2 times
where n is the size of the array. If no majority element exists, return -1.
Input:
sample Testcase:
Input: 7
2233222
Output : 2
Constraints:
Solution:
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
int arr[n];
scanf("%d", &arr[i]);
}
// Variables to store the majority element and its count
int count = 0;
if (count == 0) {
majorityElement = arr[i];
count = 1;
count++;
} else {
count--;
if (arr[i] == majorityElement) {
count++;
}
// Output the majority element if it occurs more than n / 2 times, otherwise -1
if (count > n / 2) {
printf("%d\n", majorityElement);
} else {
printf("-1\n");
return 0;
Testcases:
case=t1
Input=7
2233222
Output=2
case=t2
Input=6
323313
Output=3
case=t3
Input=10
2434242448
Output=4
case=t4
Input=10
2535252448
Output=5
case=t5
Input=10
2636262468
Output=6
Question 7:
Given an array of integers, find the maximum difference between any two elements in the
array such that the larger element appears after the smaller element.
Input:
Output:
The maximum difference between any two elements such that the larger element
appears after the smaller element. If no such pair exists, return -1.
sample testcase:
Input:
arr = {2, 3, 10, 6, 4, 8, 1}
Output:
8
Explanation:
The maximum difference is between 10 and 2.
Solution:
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
int arr[n];
scanf("%d", &arr[i]);
max_diff = diff;
min_element = arr[i];
printf("%d\n", max_diff);
return 0;
}
Testcases:
case=t1
input=7
2 3 10 6 4 8 1
output=8
case=t2
input=6
795632
output=2
case=t3
input=5
12345
output=4
Question 8:
Write a program to obtain a matrix and print the sum of boundary elements.
Input format :
The first line of the input consists of the number of rows and columns.
The next input is the matrix.
Output format :
The output prints the boundary sum of boundary elements.
Sum of boundary is 40
solution:
#include <stdio.h>
int main() {
int matrix[rows][cols];
int sum = 0;
scanf("%d", &matrix[i][j]);
if (i == 0 || i == rows - 1 || j == 0 || j == cols - 1) {
sum += matrix[i][j];
}
return 0;
Testcases:
case=t1
input=3 3
123
456
789
output=Sum of boundary is 40
case=t2
input=4 4
1234
5678
9 10 11 12
13 14 15 16
input=2 2
12
34
Output=Sum of boundary is 10
case=t4
input= 3 4
1234
5678
9 10 11 12
Output=Sum of boundary is 56
case=t4
input= 5 3
123
456
789
10 11 12
13 14 15
Output=Sum of boundary is 84
Question 9:
Arun loves to multitask. He wants to design a new type of device which does multitask like
him. The device should perform 4 tasks based on the code entered by the user. Task details
and corresponding code details are given below. Users should enter a number and task code
as shown in the sample input.
A (or) a to calculate the sum of the first and last digit of the given number
B (or) b to check number is palindrome or not
C (or) c to count the number of digits in the given number
D (or) d to print it into words
Input format :
Number in the first line
Task code in second line
Output format :
Display the result as per task code.
Refer all sample inputs for exact statement and format.
Code constraints :
Choice code is A/B/C/D or a/b/c/d
Solution:
#include <stdio.h>
int main() {
char task_code;
scanf("%d", &number);
temp = number;
temp /= 10;
digit_count++;
temp = number;
temp /= 10;
if (reverse == number) {
printf("Palindrome: Yes\n");
} else {
printf("Palindrome: No\n");
temp = number;
temp /= 10;
switch (digit) {
printf("\n");
} else {
return 0;
Testcases:
case=t1
input=5232
Output=7
case=t2
input=5232
output=Not palindrome
case=t3
input=52325
output=Palindrome
case=t4
input=5232
output= 4
case=t5
input=5232
Question 10:
Given a sorted array of positive integers, rearrange the array alternately i.e first element
should be the maximum value, the second minimum value, the third-second max, the
fourth-second min, and so on.
Input format :
Output format :
1234567
7162534
Solution:
#include <stdio.h>
int main() {
int n;
int arr[n];
int temp[n];
if (index % 2 == 0) {
temp[index++] = arr[right--];
} else {
temp[index++] = arr[left++];
return 0;
Testcases:
case=t1
input=7
1234567
output=7 1 6 2 5 3 4
case=t2
input=12
2 4 6 8 10 12 14 16 18 20 22 24
output=24 2 22 4 20 6 18 8 16 10 14 12
case=t3
input= 5
5 15 25 35 45
output=45 5 35 15 25
case=t4
input= 8
2 4 6 8 10 12 14 16
output= 16 2 14 4 12 6 10 8
case=t5
input= 6
10 20 30 40 50 60
output= 60 10 50 20 40 30