Lab2 24 - 07 - 2024
Lab2 24 - 07 - 2024
Lab2 24 - 07 - 2024
Date:24-07-2024
1. The symbols a, e, i, o, u are called as vowels and the remaining symbols of English
alphabet are called consonants. A word of length 6 (a word with 6 symbols) w1 is
said to be vowel − greater than another word w1 of lenght 6 if the number of distinct
vowels is greater than or equal to the number of distinct vowels in w2. If two words
w1, w2 have the same number of vowels, the word which comes later in an
alphabetical order, is said to be vowel−greater than the other. The number of distinct
vowels in abbaae is 2. The word aaeoub is said to be vowel − greater than the word
abedfg since the number of distinct vowels in the first word is 4 and the number of
distinct vowels in the second word is 2. Similarly, the word deoudd is vowel − greater
than cedodu. Here, both the words have the same number of vowels and the first
word deoudd comes later in the alphabetical order when compared with the second
word cedodu. Given the three words dedodu, aeiouu, befghs, they are arranged in an
ascending order with respect to the operator vowel − greater, as befghs, dedodu,
aeiouu. Consider an array of n distinct words of same length (ie., no two words are
same), design an algorithm and c++ code to sort the array in an ascending order with
respect to the relational operator vowel − greater. Analyse the algorithm with all the
required steps.
2. Consider a set S of positive integers, does there exist two disjoint subsets S1, S2 of S
such that the sum of the integers in S1 equals the sum of the integers of S2. For the
input S = {3, 4, 2, 5}, the solution to the problem is ‘yes’ since there exist two subsets
S1 = {3, 4}, S2 = {2, 5} such that the sum of the elements of S1 equals the sum of the
elements of S2 and both subsets are of equal size. The solution to the problem is ‘no’
if no such subsets exist where the sum of the elements in S1 equals the sum of the
elements in S2 and both subsets are of equal size. Design a pseudocode and c++ code
to solve this problem. Analyse the algorithm with all the required steps.
3. Let A( 1...n) be a sorted array of n integers. It is observed that elements in this array
occur one or more times. The problem is to find the frequency of the elements of A.
Write an algorithm and c++ code to output the frequency of each element in A.
Answers
1)
Algorithm:
1)Count Distinct Vowels:
2)Comparison Function:
Create a comparison function that compares two words based on the number of
distinct vowels.
If two words have the same number of distinct vowels, compare them
lexicographically (alphabetical order).
Use the std::sort function with the custom comparison function to sort the
array.
4)Analysis:
The time complexity for counting distinct vowels in a word is O(1) since the word
length is fixed (6).
The time complexity for comparing two words is O(1) for counting vowels and O(6)
for lexicographic comparison.
The overall time complexity for sorting is O(n log n), where n is the number of words.
Code:
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <string>
set<char> vowels;
return vowels.size();
if (vowelsW1 != vowelsW2) {
} else {
int main() {
sortWords(words);
}
cout << endl;
return 0;
output:
2)
Algorithm:
1.Initial Check:
Code:
#include <iostream>
#include <vector>
#include <numeric>
int n = S.size();
int halfSize = n / 2;
dp[0][0][0] = true;
}
}
return dp[n][halfSum][halfSize];
int main() {
if (canPartitionEqualSize(S)) {
} else {
return 0;
Output:
PseudoCode:
function canPartitionEqualSize(S):
totalSum = sum(S)
n = length(S)
if totalSum % 2 != 0:
return "no"
halfSum = totalSum / 2
halfSize = n / 2
dp[0][0][0] = True
for i from 1 to n:
dp[i][j][k] = dp[i-1][j][k]
3)
Algorithm:
After the loop, output the last element and its count.
PsuedoCode:
function findFrequencies(A):
n = length(A)
if n == 0:
return
currentElement = A[0]
count = 1
for i from 1 to n:
if A[i] == currentElement:
count += 1
else:
output(currentElement, count)
currentElement = A[i]
count = 1
output(currentElement, count)
Code:
#include <iostream>
#include <vector>
int n = A.size();
if (n == 0) return;
int currentElement = A[0];
int count = 1;
if (A[i] == currentElement) {
count += 1;
} else {
cout << currentElement << " occurs " << count << " times" << endl;
currentElement = A[i];
count = 1;
cout << currentElement << " occurs " << count << " times" << endl;
int main() {
findFrequencies(A);
return 0;
Output: