0% found this document useful (0 votes)
10 views33 pages

Questions - For - Smvec - Set 2

The document outlines several programming tasks in C, including decrypting a numeric cipher, finding the longest increasing subsequence, sorting arrays using insertion and bubble sort, identifying a missing number in a sequence, finding a majority element, calculating maximum differences in arrays, and summing boundary elements of a matrix. Each task includes input and output formats, sample inputs and outputs, and solutions in C code. The tasks are designed to enhance coding skills and problem-solving abilities.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views33 pages

Questions - For - Smvec - Set 2

The document outlines several programming tasks in C, including decrypting a numeric cipher, finding the longest increasing subsequence, sorting arrays using insertion and bubble sort, identifying a missing number in a sequence, finding a majority element, calculating maximum differences in arrays, and summing boundary elements of a matrix. Each task includes input and output formats, sample inputs and outputs, and solutions in C code. The tasks are designed to enhance coding skills and problem-solving abilities.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Description:

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:

Enter a word with integer numbers.

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;

// Read the input string

printf("Enter the string: ");

scanf("%s", input);
// Find the length of the input

int length = 0;

while (input[length] != '\0') {

length++;

// Reverse only the alphabetic characters

i = 0;

j = length - 1;

while (i < j) {

// Skip numeric characters from the front

while (i < j && isdigit(input[i])) {

i++;

// Skip numeric characters from the back

while (i < j && isdigit(input[j])) {

j--;

// Swap alphabetic characters

if (i < j) {

temp = input[i];

input[i] = input[j];

input[j] = temp;

i++;

j--;
}

// Print the output

printf("Reversed string: %s\n", input);

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:

Amelia, a young mathematician, discovers a mysterious sequence etched on an ancient


parchment. Legend says it holds the key to unlocking hidden powers. Can you help Amelia
by writing a C program to find the length of the Longest Increasing Subsequence within this
mystical sequence and reveal the secrets it guards? The destiny of unlocking ancient powers
lies in the code you create.

Input Format:

1st line of the input contains the size of the array.

Follows by the elements of the array.

Output Format:

Single integer shows the length LIS of the given array.

Sample Input:

10

20

47

Sample Output:

Solution:

#include <stdio.h>

int main() {
int n, i, j, max = 0;

// Read the size of the array

scanf("%d", &n);

int arr[n], lis[n];

// Read the elements of the array

for (i = 0; i < n; i++) {

scanf("%d", &arr[i]);

// Initialize LIS values for all indexes as 1

for (i = 0; i < n; i++) {

lis[i] = 1;

// Compute LIS values in a bottom-up manner

for (i = 1; i < n; i++) {

for (j = 0; j < i; j++) {

if (arr[i] > arr[j] && lis[i] < lis[j] + 1) {

lis[i] = lis[j] + 1;

}
// Find the maximum value in lis[]

for (i = 0; i < n; i++) {

if (lis[i] > max) {

max = lis[i];

// Output the length of LIS

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:

First line consists of the number of elements in the array

Followed by the elements of the array.

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;

// Read the number of elements in the array

printf("Enter the number of elements: ");

scanf("%d", &n);

int arr[n];

// Read the elements of the array

printf("Enter the elements: \n");

for (i = 0; i < n; i++) {

scanf("%d", &arr[i]);

}
// Insertion sort algorithm

for (i = 1; i < n; i++) {

key = arr[i];

j = i - 1;

// Move elements of arr[0..i-1], that are greater than key,

// to one position ahead of their current position

while (j >= 0 && arr[j] > key) {

arr[j + 1] = arr[j];

j = j - 1;

arr[j + 1] = key;

// Print the sorted array

printf("Sorted array: ");

for (i = 0; i < n; i++) {

printf("%d ", arr[i]);

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:

Enter the number of elements in the array

Enter the elements of array(in character)

Output Format:

Shows the sorted array.

Sample Input:

bfadc

Sample Output:

abcdf

Solution:

#include <stdio.h>

int main() {

int n;

// Input the number of elements

printf("Enter the number of elements in the array:\n");

scanf("%d", &n);

char arr[n];
// Input the elements of the array

printf("Enter the elements of the array (separated by spaces):\n");

for (int i = 0; i < n; i++) {

scanf(" %c", &arr[i]);

// Sorting logic

for (int i = 0; i < n - 1; i++) {

for (int j = 0; j < n - i - 1; j++) {

// Compare and swap adjacent elements if out of order

if (arr[j] > arr[j + 1]) {

char temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

// Output the sorted array

printf("Sorted array:\n");

for (int i = 0; i < n; i++) {

printf("%c ", arr[i]);

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.

Your program should:

1. Accept the total number of elements that should be in the array, n.


2. Accept the array elements (1 to n, with one number missing).
3. Calculate and output the missing number.

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() {

int n, i, expectedSum = 0, actualSum = 0, missingNumber;

scanf("%d", &n);
int arr[n];

for (i = 0; i < n; i++) {

scanf("%d", &arr[i]);

actualSum += arr[i];

expectedSum = (n + 1) * (n + 2) / 2;

missingNumber = expectedSum - actualSum;

printf("The missing number is: %d\n", missingNumber);

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:

An integer array arr of size n where 1 <= n <= 100.

Each element arr[i] satisfies -100 <= arr[i] <= 100.


Output:

The majority element if it exists, otherwise -1.

sample Testcase:
Input: 7
2233222
Output : 2

Explanation: The majority element is 2, which appears 5 times out of 7 elements.

Constraints:

The array may contain duplicates.

The array may be empty.

Solution:

#include <stdio.h>

int main() {

int n;

// Read the size of the array

scanf("%d", &n);

// Declare the array

int arr[n];

// Read the elements of the array

for (int i = 0; i < n; i++) {

scanf("%d", &arr[i]);

}
// Variables to store the majority element and its count

int majorityElement = -1;

int count = 0;

// Loop through the array to find the majority element

for (int i = 0; i < n; i++) {

// If count is 0, set the current element as the majority candidate

if (count == 0) {

majorityElement = arr[i];

count = 1;

} else if (arr[i] == majorityElement) {

count++;

} else {

count--;

// Check if the majority element appears more than n / 2 times

count = 0; // Reset the count for validation

for (int i = 0; i < n; i++) {

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:

 An integer array arr of size n where 1 <= n <= 100.


 Each element arr[i] satisfies -100 <= arr[i] <= 100.

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];

for(int i = 0; i < n; i++) {

scanf("%d", &arr[i]);

int max_diff = -1;

int min_element = arr[0];

for(int i = 1; i < n; i++) {

if(arr[i] > min_element) {

int diff = arr[i] - min_element;

if(diff > max_diff) {

max_diff = diff;

if(arr[i] < min_element) {

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.

Refer sample output for exact space between elements.

Sample test cases :


3 3
1 2 3
4 5 6
7 8 9

Sum of boundary is 40

solution:

#include <stdio.h>

int main() {

int rows, cols;

scanf("%d %d", &rows, &cols);

int matrix[rows][cols];

int sum = 0;

// Reading the matrix

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

scanf("%d", &matrix[i][j]);

// Summing boundary elements

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

// Check if the element is on the boundary

if (i == 0 || i == rows - 1 || j == 0 || j == cols - 1) {

sum += matrix[i][j];
}

// Printing the sum of boundary elements

printf("Sum of boundary is %d\n", sum);

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

Output=Sum of boundary is 102


case=t3

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

Sample test cases :


5232
A
Sum of first and last digit: 7

Solution:

#include <stdio.h>

int main() {

int number, first_digit, last_digit, temp, digit_count = 0, reverse = 0;

char task_code;

// Input number and task code

printf("Enter a number: ");

scanf("%d", &number);

printf("Enter task code: ");

scanf(" %c", &task_code);

temp = number;

last_digit = number % 10;

// Find first digit


while (temp > 0) {

first_digit = temp % 10;

temp /= 10;

digit_count++;

if (task_code == 'A' || task_code == 'a') {

printf("Sum of first and last digit: %d\n", first_digit + last_digit);

} else if (task_code == 'B' || task_code == 'b') {

temp = number;

while (temp > 0) {

reverse = reverse * 10 + temp % 10;

temp /= 10;

if (reverse == number) {

printf("Palindrome: Yes\n");

} else {

printf("Palindrome: No\n");

} else if (task_code == 'C' || task_code == 'c') {

printf("Number of digits: %d\n", digit_count);

} else if (task_code == 'D' || task_code == 'd') {

temp = number;

printf("Number in words: ");

while (temp > 0) {


int digit = temp % 10;

temp /= 10;

switch (digit) {

case 0: printf("Zero "); break;

case 1: printf("One "); break;

case 2: printf("Two "); break;

case 3: printf("Three "); break;

case 4: printf("Four "); break;

case 5: printf("Five "); break;

case 6: printf("Six "); break;

case 7: printf("Seven "); break;

case 8: printf("Eight "); break;

case 9: printf("Nine "); break;

printf("\n");

} else {

printf("Invalid task code!\n");

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

output=five two three two

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 :

Number of array elements


Array elements separated by space

Output format :

Refer sample output

Sample test cases :

1234567

7162534

Solution:

#include <stdio.h>

int main() {

int n;

scanf("%d", &n); // Input the number of elements

int arr[n];

for (int i = 0; i < n; i++) {

scanf("%d", &arr[i]); // Input the array elements

int temp[n];

int left = 0, right = n - 1, index = 0;


// Rearrange alternately: max, min, second max, second min, and so on

while (left <= right) {

if (index % 2 == 0) {

temp[index++] = arr[right--];

} else {

temp[index++] = arr[left++];

// Print the rearranged array

for (int i = 0; i < n; i++) {

printf("%d ", temp[i]);

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

You might also like