Week 2 - Simple C++ Programs
Week 2 - Simple C++ Programs
04-09-2023 (B-Section)
05-09-2023 (A-Section)
Aim
Use the following concepts from C++ to solve the given programming problems.
• Scope and Lifetime: Understanding the scope and lifetime of variables within different parts
of your program.
• Functions: Defining and using functions to break down the program into smaller, reusable
components.
• Recursion: It is a programming technique in which a function calls itself to solve a problem. It
is a concept used to solve problems that can be broken down into smaller, similar
subproblems.
• Arrays: Understanding how to declare and manipulate arrays, which are collections of similar
data types.
• Math Library: Utilizing built-in math functions from the <cmath> library, like sqrt, pow, abs,
etc.
• Strings: Working with string data type and using string manipulation functions.
• String Manipulation: Using functions to manipulate strings, like finding length, concatenation,
substring extraction, etc.
Problem Statements:
1. Write a function in C++ that finds the mode of a sequence of N integers between 0 to 99
where mode is the value which occurs most frequently in an array of elements.
Sample input & output:
Input: a[] = {1, 2, 3, 4, 5, 3, 6, 1, 2, 1, 1, 5, 4, 6}
Output: 1
2. Write a C++ function to delete a specified element from the array and rearrange the
remaining elements.
Sample input & output:
Input: a[] = {1, 2, 3, 4, 5, 6} element to be removed = 3
Output: {1, 2, 4, 5, 6}
3. Write a C++ function that remove duplicates from a sorted array in-place and return the new
length of the array.
Sample input & output:
Input: a[] = {1, 1, 3, 7, 8, 8, 9, 13, 15, 25}
Output: {1, 3, 7, 8, 9, 13, 15, 25}
4. Write a C++ functions that take argument an integer array and find the 2nd largest and 2nd
smallest element in linear time.
Sample input & output:
Input: a[] = [5, 8, 2, 3, 4, 9, 11]
Output: 2nd largest: 9 2nd smallest: 3
5. Given an array, for each element find the value of the nearest element to the right which
is having a frequency greater than as that of the current element. If there does not exist an
answer for a position, then make the value -1. Implement an efficient algorithm for this task
in C++.
Sample input & output:
Input: a[] = [1, 1, 2, 3, 4, 2, 1]
Output: [-1, -1, 1, 2, 2, 1, -1]
Input : a[] = [1, 1, 1, 2, 2, 2, 2, 11, 3, 3]
Output : [2, 2, 2, -1, -1, -1, -1, 3, -1, -1]
6. Take 3 array of integers in sorted order as input. Implement a C++ program to merge these 3
arrays in such a way that the elements in the final merged array should be in sorted order.
Sample input & output:
Input: a1[] = [5, 8, 10] a2 = [1, 6, 9, 15] a3 = [2, 19]
Output: [1, 2, 5, 6, 8, 9, 10, 15, 19]
7. Write a C++ function that takes a string as an argument and modifies the string so as to
remove all consecutive duplicate characters.
Sample input & output:
Input: mississippi
Output: misisipi
9. Given a column title A as appears in an Excel sheet, return its corresponding column
number. Hint: Binary to Decimal conversion
Sample input & output:
A --> 1
B --> 2
C --> 3
...
Z --> 26
AA --> 27
AB --> 28
10. Write a recursive function to find the kth even natural number.
Sample input & output:
Input: k = 4
Output: 6
13. There are N people standing in a circle numbered from 1 to N. Also given an integer K. First,
count the K-th number starting from the first one and delete it. Then K numbers are counted
starting from the next one and the K-th one is removed again, and so on. The process stops
when one number remains. The task is to find the last number.
Sample input and output:
Input: N = 6, K = 2
Output: 5
Algorithm:
14. Towers of Hanoi: Solve the classic Towers of Hanoi problem using recursion to move a tower
of disks from one peg to another.
The Towers of Hanoi is a classic puzzle that involves moving a stack of disks from one peg to
another, subject to the following rules:
• There are three pegs (poles), traditionally named A, B, and C.
• There is a stack of different-sized disks on one peg, arranged in decreasing order of
size, with the largest disk at the bottom and the smallest at the top.
• The goal is to move all the disks from the source peg (say, peg A) to a target peg
(say, peg C), using the third peg (peg B) as an auxiliary peg. The objective is to move
the entire stack to the target peg, obeying the following rules:
o Only one disk can be moved at a time.
o A disk can only be placed on top of a larger disk or an empty peg.
Algorithm:
Hanoi(n_disc, A, C, B) {
if (n_disk == 1) {
move disk from A to C
} else {
Hanoi(n_disc - 1, A, B, C)
move disk from A to C
Hanoi(n_disc - 1, B, C, A)
}
15. The count-and-say sequence is a sequence of digit strings defined by the recursive formula:
countAndSay(1) = "1"
countAndSay(n) is the way you would "say" the digit string from countAndSay(n-1), which is
then converted into a different digit string.
To determine how you "say" a digit string, split it into the minimal number of substrings such
that each substring contains exactly one unique digit. Then for each substring, say the number
of digits, then say the digit. Finally, concatenate every said digit.
For example, the saying and conversion for digit string "3322251" → 23321511
Sample input & output:
Input: n = 4
Output: "1211"
Explanation:
countAndSay(1) = "1"
countAndSay(2) = say "1" = one 1 = "11"
countAndSay(3) = say "11" = two 1's = "21"
countAndSay(4) = say "21" = one 2 + one 1 = "12" + "11" = "1211"