0% found this document useful (0 votes)
32 views

Array and String

The document contains 20 questions related to arrays and strings in C/C++. The questions cover a range of topics including swapping elements, finding sums, shifting elements, reversing arrays, encoding words, and more. Users are asked to write programs or functions to solve each problem.

Uploaded by

Ark Singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Array and String

The document contains 20 questions related to arrays and strings in C/C++. The questions cover a range of topics including swapping elements, finding sums, shifting elements, reversing arrays, encoding words, and more. Users are asked to write programs or functions to solve each problem.

Uploaded by

Ark Singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Topics: 1 – D, 2 – D arrays and Strings

Q.1 Write a program to swap the second smallest and second largest element in the array.
Q.2 Given a sorted array of n numbers and another number x, determine whether or not there
exist two elements in the input array whose sum is exactly x. Try to write an iterative and
recursive solution both.
Q.3 Write a program that places kth element of an array at position 1, the (k + 1) th element in
position 2, etc. The original 1st element is placed at (n – k + 1) and so on. The ‘k’ should
be user input.
Q.4 Find all the peak elements in an array of integers. An element in an array is called a peak
element if it is greater than its adjacent neighbours. Assume that the array contains
multiple peak elements. The first and the last element of the array have only one adjacent
neighbour. For example:
A [ ] = {50, 80, 70, 90, 100, 80,50,20}, then peak elements are 80, 100.
A [ ] = {100, 80, 70, 90, 60, 65}, then peak elements are 100, 90, and 65.
Q.5 Write a program that rearranges the elements of an array so that all those originally stored
at odd indices are placed before those at even indices. Consider the array index starts
from 1.
For example, the array
10 20 30 40 50 60 70 80
would be transformed to
10 30 50 70 20 40 60 80

Q.6 Given a 1-D array, write a program that counts the number of even-spaced inversions in
the array. We call a pair (i, j) as an even-spaced inversion when both the following
conditions satisfy:
1. (i, j) is an inversion, which means arr [i] > arr [j] where i < j, and
2. the difference between i and j is even.
Q.7 Given an array A [ ] of size n integers, create an another array output [ ] such that output
[i] is equal to the product of all elements of A except A [i]. For example, A [ ] = {1, 2, 3,
4, 5}, then output [ ] = {120, 60, 40, 30, 24}.
Q.8 Given a sorted array A of integers of size N and two integers viz., left and right. Write a
recursive function to count the number of integers between left and right (inclusive of
both) present in array A. For example, A [ ] = {10, 20, 25, 30, 45, 50, 60, 80, 100}, left =
30, and right = 70, then the output = 4.
Q.9 Consider a 1-D array of integers 0’sand 1’s only. Write a C function takes as input an
array having n elements and then prints the length of the longest sequence of 1’s.
For example, if array is A [ ] = {0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1}, the length of
longest run of 1’s is 4 (underlined).
Q.10 Write a function that counts the number of unique numbers present in a 1-D array. For
example, for an array A [ ] = {1, 2, 3, 4, 5, 4, 3, 2} as input, the output is 5.

Q.11 Write a function compact ( ) which takes as parameters an array of integers and its length.
The function should modify the array so that all consecutive occurrences of the same
integer are replaced by a single occurrence of that integer. The function should return the
length of this new array.
Example: When called with the array {1, 1, 1, 2, 2, 1, 2, 3, 3, 1, 1}, your function should
modify the array to {1, 2, 1, 2, 3, 1} and return 6.
Q.12 Given an unsorted array A of size n that may contain duplicates and a number k < n, write
a function that returns “1” if an array contains at least one duplicate pair within a distance
of k, i.e. there exists i, j ϵ [0, n − 1] such that A [i] = A [j] and |i − j| < k.
Q.13 Write a function that accepts as argument an array A of integers together with its size n
and a non-negative integer k. The function should create another array, which is obtained
by cyclically shifting the input array A by k positions to the right. For example, if array A
= {2, 4, 6, 1, 3, 9, 5} of size n = 7 and k = 3, the function should create another array B =
{3, 9, 5, 2, 4, 6, 1}.
Q.14 Given a matrix of N*N order. Write a program to interchange the diagonals of the matrix.
For example, Input: { {1, 2, 3}, Output: { {3, 2, 1},
{4, 5, 6}, {4, 5, 6},
{7, 8, 9} } {9, 8, 7} }
Q.15 Write a function to reverse the order of the elements in an M*N array of integers, so that
X [0][0] is now at X [M-1][N-1], X [0][1] is now at X [M-1][N-2], and so on.
Input: { {0, 1, 2, 3, 4}, Output: { {19, 18, 17, 16, 15},
{5, 6, 7, 8, 9}, {14, 13, 12, 11, 10},
{10, 11, 12, 13, 14}, {9, 8, 7, 6, 5},
{15, 16, 17, 18, 19} } {4, 3, 2, 1, 0} }

Solve the same problem using loops and recursion both.


Q.16 Write a function that takes an input parameter a string, and returns the first non-repeating
character in it. For example, if the input string is “Malayalam”, then the output should be
‘y’ and if the input string is “Telugu”, then the output should be ‘T’.
Q.17 Write a function strend (s, t), which returns 1 if the string t occurs at the end of the string
s, and zero otherwise.
Q.18 Write a program that encodes English-language word into pig-Latin. To translate an
English word into a pig-Latin word, place the first letter of the word at the end of the
word and add the letters “ay” at the end. For example: word “jump” becomes “umpjay”,
word “the” becomes “hetay” and the word “computer” becomes “omputercay”. Do not
use string.h library functions.
Q.19 Write a program that finds out the initials of the words in a string, where a single blank
space separates two consecutive words in the string. For example, if the input string is
“Sacred Games”, the output will be “S.G.”.
Q.20 Write a function squeeze (S1, S2) that deletes each character in the string S1 that matches
any character in the string S2.

You might also like