0% found this document useful (0 votes)
49 views46 pages

MCCP Record

The document contains the certification of a student's submission of a report on competitive programming as part of their course. It includes the signatures of faculty and HOD approving the submission. The rest of the document contains problems on different topics in computer science like math logic, prime numbers, arrays, strings, hashing etc. with sample inputs, outputs and programs for each problem.

Uploaded by

Manoj
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)
49 views46 pages

MCCP Record

The document contains the certification of a student's submission of a report on competitive programming as part of their course. It includes the signatures of faculty and HOD approving the submission. The rest of the document contains problems on different topics in computer science like math logic, prime numbers, arrays, strings, hashing etc. with sample inputs, outputs and programs for each problem.

Uploaded by

Manoj
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/ 46

SRI VASAVI ENGINEERING COLLEGE (Autonomous)

Department Of C.S.E[Artificial Intelligence]


Pedatadepalli, Tadepalligudem

This is to certify Mr. Manoj Sai Kumar Pasupuleti bearing Rollno. 21A81A4341 of

CAI Branch of Vsem submitted the report on 02 nd November , 2023 as a part of

Master Coding and Competitive Programming-I [Course code:V20AITJE01] during

academic year 2022-23.

Signature of Faculty Signature of H.O.D.


Dr. V.Venkateswara Rao M.Tech.,Ph.D Dr. G Loshma M.Tech.,Ph.D..
Professor. Professor & HOD.

Signature of External Examiner

1|Page
INDEX

Problems on Math Logic :

Problem Pageno
.
1 Odd or Even no. 06

2 Multiplication Table 07

3 GCD of 2 numbers 08

4 Palindromesx or not 09

5 Nth term in Series 10

Problems on Prime numbers :

Problem Pageno.

1 Prime or not 11

2 Full Prime no. 12

3 Twisted Prime no. 13

4 Prime String 14

5 Sum of digits is Prime or not 15

Problems on Fibonacci series and Series :

Problem Pageno.

1 Nth Fibonacci number 16

2 Modified Fibonacci 17

3 Nth even Fibonacci number 18

4 Composite Series 19

5 Tricky Series 20

2|Page
Problems on Arrays :

Problem Pageno
.

1 Sum of Array 21

2 Smaller and Larger 22

3 Perfect Arrays 23

4 Swap Kth elements 24

5 Display longest name 25

6 Palindromic Array 26

7 Avg. of elements in Array 27

8 Alternate elements of Array 28

Problems on Strings :

Problem Pageno
.

1 String Concatination 29

2 String Length 30

3 String Reversal 31

4 String Comparision 32

5 String Search 33

6 Sub-String Search 34

7 String Case Conversion 35

8 String Replacement 36

3|Page
Problems on Hashmap & Hashset :

Problem Pageno
.

1 Hashmap Basic Usage 37

2 Hashset Basic Usage 38

3 Hashset elements count 39

4 Hashmap for freq. count 40

5 String is isomorphic 41

6 Word Pattern Matching 42

7 Duplicates in an Array 43

8 Common elements in 2Array 44

9 Remove duplicates in String 45

4|Page
Geeks For Geeks Profile

LeetCode Profile

5|Page
PROBLEMS ON MATH LOGIC

01. Find the given no.is odd or even.

Testcase 1:
Input: Enter an Integer: 2
Output: 2 is even

Testcase 2:
Input: Enter an Integer: 1
Output: 1 is odd

Testcase 3:
Input: Enter an Integer: 0
Output: 0 is even

Program :
#include <iostream>

int main() {
int number;

std::cout << "Enter an integer: ";


std::cin >> number;

if (number % 2 == 0) {
std::cout << number << " is even." << std::endl;
} else {
std::cout << number << " is odd." << std::endl;
}

return 0;
}

6|Page
PROBLEMS ON MATH LOGIC

02. Create the multiplication table of a given number N and


return the table as an array.

Testcase :
Input: N = 9
Output: 9 18 27 36 45 54 63 72 81 90

Program :
#include <iostream>
#include <vector>

std::vector<int> generateMultiplicationTable(int N) {
std::vector<int> table;

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


table.push_back(N * i);
}

return table;
}

int main() {
int N;

std::cout << "N = ";


std::cin >> N;

std::vector<int> multiplicationTable = generateMultiplicationTable(N);

std::cout <<std::endl;
for (int i = 0; i < multiplicationTable.size(); i++) {
std::cout << multiplicationTable[i];
if (i < 9) {
std::cout << " ";
}
}
std::cout << std::endl;

return 0;
}
7|Page
PROBLEMS ON MATH LOGIC

03. Given two positive integers A and B, find GCD of A and B.

Testcase 1 :
Input: A = 3, B = 6
Output: 3

Testcase 2 :
Input: A = 1, B = 1
Output: 1

Program :
#include <iostream>
#include <algorithm>

int main() {
int A, B;

std::cout << "A = ";


std::cin >> A;

std::cout << "B = ";


std::cin >> B;

int gcd = std::__gcd(A, B);

std::cout << gcd << std::endl;

return 0;
}

8|Page
PROBLEMS ON MATH LOGIC

04. Given a number N.Find if the digit sum (or sum of digits)
of N is a Palindrome number or not.
Testcase :
Input: N = 56
Output: 1

Program :
#include <iostream>
int reverseNumber(int num) {
int reversedNum = 0;
while (num > 0) {
int digit = num % 10;
reversedNum = reversedNum * 10 + digit;
num /= 10;
}
return reversedNum;
}
bool isPalindrome(int num) {
int reversedNum = reverseNumber(num);
return num == reversedNum;
}
int main() {
int N;
std::cout << "N = ";
std::cin >> N;

int digitSum = 0;
int originalNumber = N;

while (N > 0) {
digitSum += N % 10;
N /= 10;
}

if (isPalindrome(digitSum)) {
std::cout << "1\n";
} else {
std::cout << "0\n";
}

9|Page
return 0;
}

PROBLEMS ON MATH LOGIC

05. Given a number N, find the Nth term in the series 1, 3, 6,


10, 15, 21…
Testcase 1 :
Input: Enter the value of N: 4
Output: The 4th term in the series is: 10

Testcase 2 :
Input: Enter the value of N: 3
Output: The 3th term in the series is: 6

Program :
#include <iostream>

int findNthTerm(int N) {
return N * (N + 1) / 2;
}

int main() {
int N;
std::cout << "Enter the value of N: ";
std::cin >> N;

int result = findNthTerm(N);

std::cout << "The " << N << "th term in the series is: " << result <<
std::endl;

return 0;
}

10 | P a g e
PROBLEMS ON PRIME NUMBERS

01. For a given number N check if it is prime or not.


Testcase 1:
Input: Enter an number: 4
Output: 4 is not a prime number

Testcase 2:
Input: Enter an number: 3
Output: 3 is a prime number

Program :
#include <iostream>
#include <cmath>
bool isPrime(int N) {
if (N <= 1) {
return false;
}
if (N <= 3) {
return true;
}
if (N % 2 == 0 || N % 3 == 0) {
return false;
}

for (int i = 5; i * i <= N; i += 6) {


if (N % i == 0 || N % (i + 2) == 0) {
return false;
}
}
return true;
}
int main() {
int N;
std::cout << "Enter a number: ";
std::cin >> N;
if (isPrime(N)) {
std::cout << N << " is a prime number." << std::endl;
} else {
11 | P a g e
std::cout << N << " is not a prime number." << std::endl;
}
return 0;
}

PROBLEMS ON PRIME NUMBERS

02. Given a number N.Check if it is Full Prime or not.


Note : A full prime number is one in which the number itself
is prime and all its digits are also prime.
Testcase 1:
Input: Enter an number: 37
Output: 37 is a Full Prime number.

Testcase 2:
Input: Enter an number: 31
Output: 31 is not a Full Prime number.
Program :
#include <iostream>
bool isPrime(int num) {
if (num <= 1) return false;
if (num <= 3) return true;
if (num % 2 == 0 || num % 3 == 0) return false;

for (int i = 5; i * i <= num; i += 6) {


if (num % i == 0 || num % (i + 2) == 0) return false;
}
return true;
}
bool isFullPrime(int N) {
while (N > 0) {
int digit = N % 10;
if (!isPrime(digit)) return false;
N /= 10;
}
return true;
}
int main() {
int N;
std::cout << "Enter a number: ";
std::cin >> N;
if (isFullPrime(N)) {

12 | P a g e
std::cout << N << " is a Full Prime number." << std::endl;
} else {
std::cout << N << " is not a Full Prime number." << std::endl;
}
return 0;
}

PROBLEMS ON PRIME NUMBERS

03. Given a number N. Check whether N is a Twisted Prime


number or not. [Note: A number is called Twisted Prime if
it is a prime and its reverse is also a prime. ]
Testcase :
Input: Enter an number: 97
Output: 97 is a Twisted Prime number.
Program :
#include <iostream>
bool isPrime(int num) {
if (num <= 1) return false;
if (num <= 3) return true;
if (num % 2 == 0 || num % 3 == 0) return false;
for (int i = 5; i * i <= num; i += 6) {
if (num % i == 0 || num % (i + 2) == 0) return false;
}
return true;
}
bool isTwistedPrime(int N) {
int reversedN = 0;
int originalN = N;
while (N > 0) {
int digit = N % 10;
if (digit % 2 == 0 || digit == 5) return false;
reversedN = reversedN * 10 + digit;
N /= 10;
}
return isPrime(originalN) && isPrime(reversedN);
}
int main() {
int N;
std::cout << "Enter a number: ";
std::cin >> N;
if (isTwistedPrime(N)) {
std::cout << N << " is a Twisted Prime number." << std::endl;
} else {
std::cout << N << " is not a Twisted Prime number." << std::endl;
13 | P a g e
}
return 0;
}

PROBLEMS ON PRIME NUMBERS

04. Provided a String of length N, your task is to find out


whether or not the given string is a prime string. A prime
string is a string in which the sum of the ASCII value of
all the characters is prime.
Testcase :
Input: Enter a String: manojpasupuleti
Output: The string is not a prime string.
Program :
#include <iostream>
#include <string>
#include <cmath>
bool isPrime(int num) {
if (num <= 1) return false;
if (num <= 3) return true;
if (num % 2 == 0 || num % 3 == 0) return false;

for (int i = 5; i * i <= num; i += 6) {


if (num % i == 0 || num % (i + 2) == 0) return false;
}
return true;
}
bool isPrimeString(const std::string& str) {
int sum = 0;
for (char c : str) {
sum += static_cast<int>(c);
}
return isPrime(sum);
}
int main() {
std::string input;
std::cout << "Enter a string: ";
std::cin >> input;

if (isPrimeString(input)) {
std::cout << "The string is a prime string." << std::endl;
14 | P a g e
} else {
std::cout << "The string is not a prime string." << std::endl;
}

return 0;
}

PROBLEMS ON PRIME NUMBERS

05. Given a number N, you need to write a program that finds


the sum of digits of the number till the number becomes a
single digit and then check whether the number is
Prime/Non-Prime.
Testcase :
Input: N=5602
Output: 0
Explanation: 1st step=5+6+0+2=13
2nd step=1+3=4
Since 4 is not prime, so answer is 0.

#include <iostream>
#include <cmath>
bool isPrime(int num) {
if (num <= 1) return false;
if (num <= 3) return true;
if (num % 2 == 0 || num % 3 == 0) return false;

for (int i = 5; i * i <= num; i += 6) {


if (num % i == 0 || num % (i + 2) == 0) return false;
}
return true;
}
int getSingleDigitSum(int N) {
return (N % 9 == 0 && N != 0) ? 9 : N % 9;
}
int main() {
int N;
std::cout << "Enter a number: ";
std::cin >> N;
int singleDigitSum = getSingleDigitSum(N);

if (isPrime(singleDigitSum)) {
std::cout << singleDigitSum << " is a prime number." << std::endl;
15 | P a g e
} else {
std::cout << singleDigitSum << " is not a prime number." << std::endl;
}
return 0;
}

PROBLEMS ON FIBONACCI SERIES & SERIES

01. Find the Nth Fibonacci Number


Testcase 1 :
Input: Enter the value of n: 3
Output: The 3th Fibonacci number is: 2

Testcase 2 :
Input: Enter the value of n: 13
Output: The 3th Fibonacci number is: 233

Program :

#include <iostream>
int fibonacci(int n) {
if (n <= 1)
return n;
int prev = 0;
int current = 1;
int next;
for (int i = 2; i <= n; i++) {
next = prev + current;
prev = current;
current = next;
}
return current;
}
int main() {
int n;
std::cout << "Enter the value of n: ";
std::cin >> n;

if (n < 0) {
std::cout << "Invalid input. Please enter a non-negative integer." <<
std::endl;
} else {

16 | P a g e
int result = fibonacci(n);
std::cout << "The " << n << "th Fibonacci number is: " << result <<
std::endl;
}

return 0;
}

PROBLEMS ON FIBONACCI SERIES & SERIES

02. Given 4 integers A, B, C and N, find the value of F(N)


such that
F(1) = A + B
F(2) = B + C
F(N) = F(N-1) - F(N-2), for N > 2.
Testcase :
Input: Enter A, B, C, and N: 2 3 4 2
Output: F(2)=7

Program :

#include <iostream>
int calculateF(int A, int B, int C, int N) {
if (N < 1) return -1;
if (N == 1) return A + B;
if (N == 2) return B + C;
int FnMinus1 = B + C;
int FnMinus2 = A + B;
int Fn;
for (int i = 3; i <= N; i++) {
Fn = FnMinus1 - FnMinus2;
FnMinus2 = FnMinus1;
FnMinus1 = Fn;
}
return Fn;
}

int main() {
int A, B, C, N;
std::cout << "Enter A, B, C, and N: ";
std::cin >> A >> B >> C >> N;
int result = calculateF(A, B, C, N);
if (result != -1) {

17 | P a g e
std::cout << "F(" << N << ") = " << result << std::endl;
} else {
std::cout << "Invalid input. N must be a positive integer." << std::endl;
}
return 0;
}

PROBLEMS ON FIBONACCI SERIES & SERIES

03. Given a positive integer N, find the Nth Even


Fibonaccinumber. Since the answer can be very large,
return the answer modulo 1000000007..
Testcase :
Input: Enter a positive integer N: 1
Output: The 1th even Fibonacci number modulo
1000000007 is: 2

Program :

#include <iostream>
const int MOD = 1000000007;
int findNthEvenFibonacci(int N) {
if (N <= 0) return 0;
long long a = 0;
long long b = 1;
int evenFib = 0;
int count = 0;
while (count < N) {
long long next = a + b;
if (next % 2 == 0) {
evenFib = next % MOD;
count++;
}
a = b;
b = next % MOD;
}
return evenFib;
}
int main() {
int N;
std::cout << "Enter a positive integer N: ";
18 | P a g e
std::cin >> N;
int result = findNthEvenFibonacci(N);
std::cout << "The " << N << "th even Fibonacci number modulo 1000000007 is: "
<< result << std::endl;
return 0;
}

PROBLEMS ON FIBONACCI SERIES & SERIES

04. Given a number N, print all the composite numbers less than or equal to N.
The number should be printed in ascending order.

Testcase :
Input: Enter a integer N: 10
Output: 4 6 8 9 10

Program :

#include <iostream>

bool isComposite(int num) {


if (num <= 1) return false;
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) return true;
}
return false;
}
void printComposites(int N) {
if (N < 4) {
std::cout << "No composite numbers less than or equal to " << N <<
std::endl;
return;
}
for (int i = 4; i <= N; i++) {
if (isComposite(i)) std::cout << i << ' ';
}
std::cout << std::endl;
}
int main() {
int N;
std::cout << "Enter a integer N: ";
std::cin >> N;
19 | P a g e
printComposites(N);
return 0;
}

PROBLEMS ON FIBONACCI SERIES & SERIES

05. Given a series with starting 6 members of the series. Given


an integer n find the nth term of this series modulo 109+7.
Series: 7, 15, 32, 67, 138, 281, ............
Testcase :
Input: Enter the value of n: 2
Output: The 2th term of the series is: 15

Program :

#include <iostream>
using namespace std;

const int MOD = 1000000007;

long long nthTerm(int n) {


long long a = 281, b = 281;
if (n <= 6) return (int[]){7, 15, 32, 67, 138, 281}[n - 1];
for (int i = 6; i < n; i++) a = (b = (2 * a + 1) % MOD);
return b;
}

int main() {
int n;
cout << "Enter the value of n: ";
cin >> n;
cout << "The " << n << "th term of the series is: " << nthTerm(n) << endl;
return 0;
}

20 | P a g e
PROBLEMS ON ARRAY

01. Program to find sum of the elements in the array.


Testcase :
Input: Enter the number of elements in the array: 5
Enter 5 elements: 12 6 7 3 0
Output:Sum of elements in the array: 28

Program :

#include <iostream>

int main() {
int n;
std::cout << "Enter the number of elements in the array: ";
std::cin >> n;

if (n <= 0) {
std::cout << "Invalid array size. Please enter a positive integer." <<
std::endl;
return 1;
}

int* arr = new int[n];

std::cout << "Enter " << n << " elements: ";


for (int i = 0; i < n; i++) {
std::cin >> arr[i];
}

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

21 | P a g e
sum += arr[i];
}

std::cout << "Sum of elements in the array: " << sum << std::endl;

delete[] arr;

return 0;
}

PROBLEMS ON ARRAY

02. Program to find smallest and largest elements in the array.


Testcase :
Input: Enter the number of elements in the array: 5
Enter 5 elements: 12 6 7 3 0
Output: Smallest element: 0
Largest element: 12
Program :
#include <iostream>
int main() {
int n;
std::cout << "Enter the number of elements in the array: ";
std::cin >> n;
if (n <= 0) {
std::cout << "Invalid array size. Please enter a positive integer." <<
std::endl;
return 1;
}
int* arr = new int[n];
std::cout << "Enter " << n << " elements: ";
for (int i = 0; i < n; i++) {
std::cin >> arr[i];
}
if (n == 0) {
std::cout << "The array is empty." << std::endl;
delete[] arr;
return 1;
}
int minElement = arr[0];
int maxElement = arr[0];
22 | P a g e
for (int i = 1; i < n; i++) {
if (arr[i] < minElement) {
minElement = arr[i];
}
if (arr[i] > maxElement) {
maxElement = arr[i];
}
}
std::cout << "Smallest element: " << minElement << std::endl;
std::cout << "Largest element: " << maxElement << std::endl;
delete[] arr;
return 0;
}

PROBLEMS ON ARRAY

03.Program to find weather the array is perfect array or not.


Testcase :
Input: Enter the number of elements in the array: 5
Enter 5 elements: 1 2 3 2 1
Output: The given array is a perfect array.
Program :

#include <iostream>
#include <vector>
#include <cmath>
bool isPerfectArray(const std::vector<int>& arr) {
if (arr.size() <= 1) {
return true;
}
int commonDifference = std::abs(arr[1] - arr[0]);
for (size_t i = 1; i < arr.size() - 1; i++) {
int diff = std::abs(arr[i + 1] - arr[i]);
if (diff != commonDifference) {
return false;}
} return true;
}
int main() {
std::vector<int> arr;
int n;
std::cout << "Enter the number of elements in the array: ";
std::cin >> n;
if (n <= 0) {
std::cout << "Invalid array size. Please enter a positive integer." <<
std::endl;
23 | P a g e
return 1;}
std::cout << "Enter " << n << " elements: ";
for (int i = 0; i < n; i++) {
int element;
std::cin >> element;
arr.push_back(element);
}
if (isPerfectArray(arr)) {
std::cout << "The given array is a perfect array." << std::endl;
} else {
std::cout << "The given array is not a perfect array." << std::endl;
}
return 0;
}

PROBLEMS ON ARRAY

04.Program to Swap Kth element in the array with Integer.


Testcase :
Input: Enter the number of elements in the array: 5
Enter 5 elements: 12 5 8 9 0
Enter the index (k) to swap (0-based): 0 3
Output: Enter the new value: Array after swapping
the 0-th element with 3: 3 5 8 9 0

Program :

#include <iostream>
#include <vector>
void swapKthElement(std::vector<int>& arr, int k, int newValue) {
if (k >= 0 && k < arr.size()) {
arr[k] = newValue; }
}
int main() {
int n;
std::cout << "Enter the number of elements in the array: ";
std::cin >> n;
std::vector<int> arr(n);
std::cout << "Enter " << n << " elements: ";
for (int i = 0; i < n; i++) {
std::cin >> arr[i];
}
int k, newValue;
std::cout << "Enter the index (k) to swap (0-based): ";
std::cin >> k;
24 | P a g e
if (k < 0 || k >= n) {
std::cout << "Invalid index. Please enter a valid index." << std::endl;
return 1;
}
std::cout << "Enter the new value: ";
std::cin >> newValue;
swapKthElement(arr, k, newValue);
std::cout << "Array after swapping the " << k << "-th element with " <<
newValue << ": ";
for (int value : arr) {
std::cout << value << " ";
}
return 0;
}

PROBLEMS ON ARRAY

05.Program to find longest string in array of Strings.


Testcase :
Input: Enter the number of strings in the array: 5
Enter 5 strings: Manoj Pasupuleti Pujith
Paida CAI_AIM_Dept_SVEC
Output: CAI_AIM_Dept_SVEC

Program :

#include <iostream>
#include <vector>
#include <string>
std::string findLongestString(const std::vector<std::string>& arr) {
if (arr.empty()) {
return "";
}
std::string longest = arr[0];
for (const std::string& str : arr) {
if (str.length() > longest.length()) {
longest = str;
}
}
return longest;
}
int main() {
int n;
std::cout << "Enter the number of strings in the array: ";
std::cin >> n;

25 | P a g e
std::vector<std::string> arr(n);
std::cout << "Enter " << n << " strings: ";
for (int i = 0; i < n; i++) {
std::cin >> arr[i];
}
std::string longestString = findLongestString(arr);
if (longestString.empty()) {
std::cout << "The array is empty." << std::endl;
} else {
std::cout << longestString << std::endl;
}
return 0;
}

PROBLEMS ON ARRAY

06.Program to find weather the array is Palindromic Array.


Testcase :
Input: Enter the number of elements in the array: 5
Enter 5 elements: 12 6 7 3 0
Output:The array is not palindromic

Program :

#include <iostream>
using namespace std;

bool isPalindromicArray(int arr[], int n) {


for (int i = 0, j = n - 1; i < j; ++i, --j) {
if (arr[i] != arr[j]) {
return false;
}
}
return true;
}

int main() {
int n;
cout << "Enter the size of the array: ";
cin >> n;

int arr[n];
cout << "Enter the elements of the array: ";
for (int i = 0; i < n; ++i) {
26 | P a g e
cin >> arr[i];
}

if (isPalindromicArray(arr, n)) {
cout << "The array is palindromic." << endl;
} else {
cout << "The array is not palindromic." << endl;
}

return 0;
}

PROBLEMS ON ARRAY

07.Program to find Average of the elements in the array.


Testcase :
Input: Enter the number of elements in the array: 3
Enter 5 elements: 1 2 3
Output:The Averege is: 2

Program :

#include <iostream>
using namespace std;

double calculateAverage(int arr[], int n) {


if (n == 0) {
return 0.0;
}

int sum = 0;
for (int i = 0; i < n; ++i) {
sum += arr[i];
}

return static_cast<double>(sum) / n;
}

int main() {
int n;
cout << "Enter the size of the array: ";

27 | P a g e
cin >> n;

int arr[n];
cout << "Enter the elements of the array: ";
for (int i = 0; i < n; ++i) {
cin >> arr[i];
}

double average = calculateAverage(arr, n);

cout << "The average is: " << average << endl;

return 0;
}

PROBLEMS ON ARRAY

08.Program to find Alternate elements in the array.


Testcase :
Input: Enter the number of elements in the array: 5
Enter 5 elements: 12 6 7 3 0
Output:Alternate elements in the array: 12 7 0

Program :

#include <iostream>
using namespace std;

void printAlternateElements(int arr[], int n) {


cout << "Alternate elements in the array are: ";
for (int i = 0; i < n; i += 2) {
cout << arr[i] << " ";
}
cout << endl;
}

int main() {
int n;
cout << "Enter the size of the array: ";
cin >> n;

int arr[n];
cout << "Enter the elements of the array: ";
for (int i = 0; i < n; ++i) {
28 | P a g e
cin >> arr[i];
}

printAlternateElements(arr, n);

return 0;
}

PROBLEMS ON STRINGS

01.Program for String Concatination.


Testcase :
Input: Enter the first string: I’m a
Enter the second string: Student
Output:Concatinated String: I’m a Student

Program :

#include <iostream>
#include <string>
using namespace std;

string concatenateStrings(const string& str1, const string& str2) {


return str1 + str2;
}

int main() {
cout << "Enter the first string: ";
string firstString;
getline(cin, firstString);

cout << "Enter the second string: ";


string secondString;
getline(cin, secondString);

string concatenatedString = concatenateStrings(firstString, secondString);

29 | P a g e
cout << "Concatenated String: " << concatenatedString << endl;

return 0;
}

PROBLEMS ON STRINGS

02.Program for Finding String Length.


Testcase :
Input: Enter a string: SVECCAIAIMDEPT
Output: Length of the string: 14
Program :

#include <iostream>
#include <string>
using namespace std;

int main() {
cout << "Enter a string: ";
string userInput;
getline(cin, userInput);

cout << "Length of the string: " << userInput.length() << endl;

return 0;
}

30 | P a g e
PROBLEMS ON STRINGS

03.Program for Finding String Reversal.


Testcase :
Input: Enter a string: ABCD
Output: Reversed string: DCBA
Program :

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main() {
cout << "Enter a string: ";
string userInput;
getline(cin, userInput);

string reversedString = userInput;


reverse(reversedString.begin(), reversedString.end());

cout << "Reversed string: " << reversedString << endl;

return 0;
}

31 | P a g e
PROBLEMS ON STRINGS

04.Program for String Comparision.


Testcase :
Input: Enter the first string: ABCD
Enter the second string: ABCD
Output: Strings are equal.
Program :

#include <iostream>
#include <string>
using namespace std;

int main() {
cout << "Enter the first string: ";
string firstString;
getline(cin, firstString);

cout << "Enter the second string: ";


string secondString;
getline(cin, secondString);

if (firstString == secondString) {
cout << "Strings are equal." << endl;
} else {
cout << "Strings are not equal." << endl;
}

32 | P a g e
return 0;
}

PROBLEMS ON STRINGS

05.Program for String Search.


Testcase :
Input: Enter a sentence: Im a Student
Enter a word to search: a
Output: Word found in the sentence.
Program :

#include <iostream>
#include <string>
using namespace std;

int main() {
cout << "Enter a sentence: ";
string sentence;
getline(cin, sentence);

cout << "Enter a word to search: ";


string searchWord;
cin >> searchWord;

if (sentence.find(searchWord) != string::npos) {
cout << "Word found in the sentence." << endl;
} else {
cout << "Word not found in the sentence." << endl;
33 | P a g e
}

return 0;
}

PROBLEMS ON STRINGS

06.Program for Finding Sub-String in a Sentence.


Testcase :
Input: Enter a sentence: Im a Student
Enter a substring to check: dent
Output: Word found in the sentence.
Program :

#include <iostream>
#include <string>
using namespace std;

int main() {
cout << "Enter a sentence: ";
string sentence;
getline(cin, sentence);

cout << "Enter a substring to check: ";


string substring;
getline(cin, substring);

if (sentence.find(substring) != string::npos) {
cout << "Substring found in the sentence." << endl;
} else {
34 | P a g e
cout << "Substring not found in the sentence." << endl;
}

return 0;
}

PROBLEMS ON STRINGS

07.Program for String Case conversion.


Testcase :
Input: Enter a string: sTRING
Output: Uppercase: STRING
Lowercase: string
Program :

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main() {
cout << "Enter a string: ";
string userInput;
getline(cin, userInput);

// Convert to uppercase
transform(userInput.begin(), userInput.end(), userInput.begin(), ::toupper);
cout << "Uppercase: " << userInput << endl;

// Convert to lowercase
transform(userInput.begin(), userInput.end(), userInput.begin(), ::tolower);
35 | P a g e
cout << "Lowercase: " << userInput << endl;

return 0;
}

PROBLEMS ON STRINGS

08.Program for String Replacement.


Testcase :
Input: Enter a sentence: Im a Student
Enter the word to replace: a
Enter the replacement word: the
Output: Updated sentence: Im the Student
Program:
#include <iostream>
#include <string>
using namespace std;

int main() {
cout << "Enter a sentence: ";
string sentence;
getline(cin, sentence);

cout << "Enter the word to replace: ";


string toReplace;
getline(cin, toReplace);

cout << "Enter the replacement word: ";


string replacement;
36 | P a g e
getline(cin, replacement);

size_t pos = sentence.find(toReplace);


if (pos != string::npos) {
sentence.replace(pos, toReplace.length(), replacement);
cout << "Updated sentence: " << sentence << endl;
} else {
cout << "Word not found in the sentence." << endl;
}

return 0;
}

PROBLEMS ON HASHMAP & HASHSET

01.Program for Hashmap Basic Usage.


Testcase :
Input: Enter key: 5
Enter value: 1
Output: Value for key 5: 1

Program:

#include <iostream>
#include <unordered_map>
using namespace std;

int main() {
unordered_map<string, int> myMap;

string key;
int value;

cout << "Enter key: ";


cin >> key;

cout << "Enter value: ";


37 | P a g e
cin >> value;

myMap[key] = value;

cout << "Value for key " << key << ": " << myMap[key] << endl;

return 0;
}

PROBLEMS ON HASHMAP & HASHSET

02.Program for Hashset Basic Usage.


Testcase :
Input: Enter an element to insert into the set: 3
Output: Size of set: 1

Program:

#include <iostream>
#include <unordered_set>
using namespace std;

int main() {
unordered_set<int> mySet;

int element;
cout << "Enter an element to insert into the set: ";
cin >> element;

mySet.insert(element);

cout << "Size of set: " << mySet.size() << endl;

return 0;
38 | P a g e
}

PROBLEMS ON HASHMAP & HASHSET

03.Program to find count of distinct elements using Hashset.


Testcase :
Input: Enter the size of the vector: 5
Enter elements of the vector: 1 2 3 4 5
Output: Distinct elements count: 5

Program:

#include <iostream>
#include <unordered_set>
#include <vector>
using namespace std;

int countDistinct(const vector<int>& nums) {


unordered_set<int> uniqueSet(nums.begin(), nums.end());
return uniqueSet.size();
}

int main() {
int size;
cout << "Enter the size of the vector: ";
cin >> size;
39 | P a g e
vector<int> numbers(size);

cout << "Enter elements of the vector: ";


for (int i = 0; i < size; ++i) {
cin >> numbers[i];
}

cout << "Distinct elements count: " << countDistinct(numbers) << endl;

return 0;
}

PROBLEMS ON HASHMAP & HASHSET

04.Program to find frequency using Hashmap.


Testcase :
Input: Enter the size of the vector: 5
Enter elements of the vector: 1 2 3 4 5
Output: Frequency of elements:
5: 1 times
4: 1 times
3: 1 times
2: 1 times
1: 1 times
Program:
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
void countFrequency(const vector<int>& nums) {
unordered_map<int, int> freqMap;
for (int num : nums) {
freqMap[num]++;

40 | P a g e
}
cout << "Frequency of elements:\n";
for (const auto& pair : freqMap) {
cout << pair.first << ": " << pair.second << " times\n";
}
}
int main() {
int size;
cout << "Enter the size of the vector: ";
cin >> size;
vector<int> numbers(size);
cout << "Enter elements of the vector: ";
for (int i = 0; i < size; ++i) {
cin >> numbers[i];
}
countFrequency(numbers);
return 0;
}

PROBLEMS ON HASHMAP & HASHSET

05.Program to find the String Is Isomorphic using Hashmap.


Testcase :
Input: Enter the first string: ABC
Enter the second string: XYZ
Output: The strings are isomorphic.
Program:
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;

void countFrequency(const vector<int>& nums) {


unordered_map<int, int> freqMap;

for (int num : nums) {


freqMap[num]++;
}

cout << "Frequency of elements:\n";


for (const auto& pair : freqMap) {
41 | P a g e
cout << pair.first << ": " << pair.second << " times\n";
}
}

int main() {
int size;
cout << "Enter the size of the vector: ";
cin >> size;

vector<int> numbers(size);

cout << "Enter elements of the vector: ";


for (int i = 0; i < size; ++i) {
cin >> numbers[i];
}

countFrequency(numbers);

return 0;
}

PROBLEMS ON HASHMAP & HASHSET

06.Program to Word pattern matching.


Testcase :
Input: Enter the pattern: ABC
Enter the string: XYZ
Output: The pattern and the String doesn’t match.
Program:
#include <iostream>
#include <unordered_map>
#include <sstream>
using namespace std;
bool wordPattern(const string& pattern, const string& str) {
unordered_map<char, string> charToString;
unordered_map<string, char> stringToChar;
istringstream iss(str);string word;
int i = 0;
while (iss >> word) {
if (i == pattern.length()) {
return false;}
if (charToString.find(pattern[i]) == charToString.end() &&
stringToChar.find(word) == stringToChar.end()) {
42 | P a g e
charToString[pattern[i]] = word;
stringToChar[word] = pattern[i];
} else {
if (charToString[pattern[i]] != word || stringToChar[word] !=
pattern[i]) {
return false;}}
i++; }
return i == pattern.length();}
int main() {
string pattern, str;
cout << "Enter the pattern: ";
cin >> pattern;
cout << "Enter the string: ";
cin.ignore();
getline(cin, str);
if (wordPattern(pattern, str)) {
cout << "The pattern and string match.\n";
} else {
cout << "The pattern and string do not match.\n";
}

return 0;
}

PROBLEMS ON HASHMAP & HASHSET

07.Program to find weather there are duplicate elemnts or not.


Testcase :
Input: Enter the size of the array: 5
Enter elements of the array: 1 2 3 3 4
Output: The array contains duplicates.
Program:
#include <iostream>
#include <unordered_set>
#include <vector>
using namespace std;

int main() {
int n;
cout << "Enter the size of the array: ";
cin >> n;

vector<int> nums(n);
cout << "Enter elements of the array: ";
for (int i = 0; i < n; ++i) {
cin >> nums[i];
43 | P a g e
}

unordered_set<int> numSet;

bool hasDuplicates = false;


for (int num : nums) {
if (numSet.count(num) > 0) {
hasDuplicates = true;
break;
}
numSet.insert(num);
}

if (hasDuplicates) {
cout << "The array contains duplicates.\n";
} else {
cout << "The array does not contain duplicates.\n";
}

return 0;
}

PROBLEMS ON HASHMAP & HASHSET

08.Program to find the common elements in array.


Testcase :
Input: Enter the first array elements: 1 2 3 4 5
Enter the second array elements: 4 5 6 7 8
Output: Common elements are 4 5
Program:
#include <iostream>
#include <unordered_set>
#include <vector>
using namespace std;
int main() {
int size1, size2;
cout << "Enter the size of the first array: ";
cin >> size1;
vector<int> nums1(size1);
cout << "Enter elements of the first array: ";
for (int i = 0; i < size1; ++i) {
cin >> nums1[i];
}

44 | P a g e
cout << "Enter the size of the second array: ";
cin >> size2;
vector<int> nums2(size2);
cout << "Enter elements of the second array: ";
for (int i = 0; i < size2; ++i) {
cin >> nums2[i];
}
unordered_set<int> set1(nums1.begin(), nums1.end());
unordered_set<int> commonElements;
for (int num : nums2) {
if (set1.count(num) > 0) {
commonElements.insert(num);
}
}
cout << "Common elements: ";
for (int num : commonElements) {
cout << num << " ";
}
cout << endl;
return 0;
}

PROBLEMS ON HASHMAP & HASHSET

09.Program to find the String Is Isomorphic using Hashmap.


Testcase :
Input: Enter the first string: ABC
Enter the second string: XYZ
Output: The strings are isomorphic.
Program:
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;

void countFrequency(const vector<int>& nums) {


unordered_map<int, int> freqMap;

for (int num : nums) {


freqMap[num]++;
}

45 | P a g e
cout << "Frequency of elements:\n";
for (const auto& pair : freqMap) {
cout << pair.first << ": " << pair.second << " times\n";
}
}

int main() {
int size;
cout << "Enter the size of the vector: ";
cin >> size;

vector<int> numbers(size);

cout << "Enter elements of the vector: ";


for (int i = 0; i < size; ++i) {
cin >> numbers[i];
}

countFrequency(numbers);

return 0;
}

46 | P a g e

You might also like