1.
Given the following array: [35, 45, 70, 80, 90, 28, 66, 49, 53, 52, 57, 78],
convert it into a map where:
Values greater than 65 are categorized as "Obesity."
Values between 45 and 65 (inclusive) are categorized as "Healthy."
Values less than 45 are categorized as "Ricket."
So, how would you create a map that assigns these categories to each value in the
array?
ans1 #include <bits/stdc++.h>
using namespace std;
int main() {
// Initialize the array
vector<int> arr = {35, 45, 70, 80, 90, 28, 66, 49, 53, 52, 57, 78};
// Create a map to hold the categories for each value
map<int, string> categorizedMap;
// Iterate over each value in the array and categorize it
for (int value : arr) {
if (value > 65) {
categorizedMap[value] = "Obesity";
} else if (value >= 45 && value <= 65) {
categorizedMap[value] = "Healthy";
} else {
categorizedMap[value] = "Ricket";
}
}
// Output the categorized map
for (const auto& entry : categorizedMap) {
cout << "Value: " << [Link] << " -> Category: " << [Link] <<
endl;
}
return 0;
}
2. vector [12,21,32,23,56,65,76,67]
[[12,21],[32,23],[65,56]] group them like this using map
ans 2#include <bits/stdc++.h>
using namespace std;
int main() {
// Initialize the vector
vector<int> vec = {12, 21, 32, 23, 56, 65, 76, 67};
// Create a map to hold the groups
map<int, pair<int, int>> groupedMap;
// Ensure the vector has an even number of elements
if ([Link]() % 2 != 0) {
cout << "The vector does not have an even number of elements." << endl;
return 1;
}
// Group the elements in pairs and add to the map
int index = 0;
for (size_t i = 0; i < [Link](); i += 2) {
// Use index/2 as the key to identify the pair
groupedMap[index++] = make_pair(vec[i], vec[i+1]);
}
// Output the grouped map
for (const auto& entry : groupedMap) {
cout << "[" << [Link] << ", " << [Link] << "]" <<
endl;
}
return 0;
}
3. Given a list of words, write a program to group all anagrams together. Each
group should contain words that, when rearranged, can form the same word
words = ["listen", "silent", "enlist", "inlets", "rat", "tar", "art", "cat", "act",
"tac"]
output:
[
["listen", "silent", "enlist", "inlets"],
["rat", "tar", "art"],
["cat", "act", "tac"]
]
ans3 #include <bits/stdc++.h>
using namespace std;
int main() {
// Initialize the list of words
vector<string> words = {"listen", "silent", "enlist", "inlets", "rat", "tar",
"art", "cat", "act", "tac"};
// Create a map to group words by their sorted character sequence
map<string, vector<string>> anagramGroups;
// Process each word
for (const string& word : words) {
// Create a copy of the word and sort its characters
string sortedWord = word;
sort([Link](), [Link]());
// Use the sorted word as the key and append the original word to the map
anagramGroups[sortedWord].push_back(word);
}
// Extract the groups into a vector of vectors
vector<vector<string>> result;
for (const auto& entry : anagramGroups) {
result.push_back([Link]);
}
// Output the result
cout << "[\n";
for (const auto& group : result) {
cout << " [ ";
for (const auto& word : group) {
cout << word << " ";
}
cout << "]\n";
}
cout << "]\n";
return 0;
}
[Link] an array of integers, write a function that creates and returns a frequency
array (or dictionary) that counts the occurrences of each integer in the array.
ans4 #include <bits/stdc++.h>
using namespace std;
unordered_map<int, int> countFrequencies(const vector<int>& arr) {
unordered_map<int, int> frequencyMap;
// Iterate through the array and count the frequency of each integer
for (int num : arr) {
frequencyMap[num]++;
}
return frequencyMap;
}
int main() {
// Example array of integers
vector<int> arr = {1, 2, 2, 3, 4, 4, 4, 5, 5, 6};
// Get the frequency map
unordered_map<int, int> freqMap = countFrequencies(arr);
// Output the frequency map
cout << "Frequency of each integer:\n";
for (const auto& pair : freqMap) {
cout << "Number " << [Link] << ": " << [Link] << " times\n";
}
return 0;
}
5. Write a function that takes an array of integers as input and returns the
element that appears most frequently in the array. In the case of a tie, the
function should return any of the most frequent elements.
ans5 #include <bits/stdc++.h>
using namespace std;
int findMostFrequentElement(const vector<int>& arr) {
unordered_map<int, int> frequencyMap;
// Count the frequency of each element
for (int num : arr) {
frequencyMap[num]++;
}
// Find the element with the maximum frequency
int mostFrequentElement = arr[0];
int maxFrequency = frequencyMap[mostFrequentElement];
for (const auto& entry : frequencyMap) {
if ([Link] > maxFrequency) {
mostFrequentElement = [Link];
maxFrequency = [Link];
}
}
return mostFrequentElement;
}
int main() {
vector<int> arr = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4};
int result = findMostFrequentElement(arr);
cout << "The element that appears most frequently is: " << result << endl;
return 0;
}
[Link] an array of integers and a threshold value, write a function that finds and
returns all elements whose frequency is above the given threshold.
ans6 #include <bits/stdc++.h>
using namespace std;
vector<int> findElementsAboveThreshold(const vector<int>& arr, int threshold) {
unordered_map<int, int> frequencyMap;
// Count the frequency of each element
for (int num : arr) {
frequencyMap[num]++;
}
vector<int> result;
// Find elements with frequency above the threshold
for (const auto& entry : frequencyMap) {
if ([Link] > threshold) {
result.push_back([Link]);
}
}
return result;
}
int main() {
vector<int> arr = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4};
int result = findMostFrequentElement(arr);
cout << "The element that appears most frequently is: " << result << endl;
return 0;
[Link] a function that takes a sentence as input and returns a frequency array (or
dictionary) that counts the occurrences of each word in the sentence.
ans7 #include <bits/stdc++.h>
using namespace std;
// Function to count word frequencies in a sentence
unordered_map<string, int> wordFrequency(const string& sentence) {
unordered_map<string, int> frequencyMap;
stringstream ss(sentence);
string word;
// Read each word from the sentence
while (ss >> word) {
// Increment the count of the current word
frequencyMap[word]++;
}
return frequencyMap;
}
int main() {
string sentence = "this is a test this is only a test";
unordered_map<string, int> frequencies = wordFrequency(sentence);
cout << "Word frequencies:\n";
for (const auto& entry : frequencies) {
cout << [Link] << ": " << [Link] << endl;
}
return 0;
}
[Link] an array of integers and a target sum, write a function that finds two
numbers in the array that add up to the target sum and returns their indices using
a map (or dictionary). If no such pair exists, the function should return null or
None.
ans8 #include <bits/stdc++.h>
using namespace std;
// Function to find indices of two numbers that add up to the target sum
pair<int, int> findTwoSumIndices(const vector<int>& arr, int target_sum) {
unordered_map<int, int> index_map; // To store element and its index
for (int i = 0; i < [Link](); ++i) {
int complement = target_sum - arr[i];
// Check if the complement exists in the map
if (index_map.find(complement) != index_map.end()) {
return {index_map[complement], i};
}
// Store the index of the current element
index_map[arr[i]] = i;
}
// Return a pair indicating no valid indices found
return {-1, -1};
}
int main() {
vector<int> arr = {2, 7, 11, 15};
int target_sum = 9;
pair<int, int> result = findTwoSumIndices(arr, target_sum);
if ([Link] != -1 && [Link] != -1) {
cout << "Indices of elements that add up to " << target_sum << " are: "
<< [Link] << " and " << [Link] << endl;
} else {
cout << "No two elements add up to the target sum." << endl;
}
return 0;
}
[Link] a C++ program that creates a set of integers. Insert the elements 5, 10,
15, 20, and 25 into the set, and then display all the elements in ascending order.
ans9 #include <bits/stdc++.h>
using namespace std;
int main() {
// Create a set of integers
set<int> mySet;
// Insert elements into the set
[Link](5);
[Link](10);
[Link](15);
[Link](20);
[Link](25);
// Display elements of the set in ascending order
cout << "Elements in the set in ascending order:" << endl;
for (const int& element : mySet) {
cout << element << " ";
}
cout << endl;
return 0;
}
[Link] a C++ program to demonstrate that a set does not allow duplicate elements.
Insert the elements 10, 20, 20, 30, 40, 40, and 50 into a set. Display the elements
of the set and verify that duplicates are not included.
ans10 #include <bits/stdc++.h>
using namespace std;
int main() {
// Create a set of integers
set<int> mySet;
// Insert elements into the set
[Link](10);
[Link](20);
[Link](20); // Duplicate element
[Link](30);
[Link](40);
[Link](40); // Duplicate element
[Link](50);
// Display elements of the set
cout << "Elements in the set (duplicates should not be included):" << endl;
for (const int& element : mySet) {
cout << element << " ";
}
cout << endl;
return 0;
}
[Link] a C++ program that creates a set with the elements {2, 4, 6, 8, 10}. Check
if the element 6 is present in the set. If it is present, erase it. Display the
elements of the set before and after performing the erase operation.
ans11 #include <bits/stdc++.h>
using namespace std;
// Function to display elements of the set
void displaySet(const set<int>& s) {
for (const int& element : s) {
cout << element << " ";
}
cout << endl;
}
int main() {
// Create and initialize the set with elements {2, 4, 6, 8, 10}
set<int> mySet = {2, 4, 6, 8, 10};
// Display elements of the set before erasing
cout << "Set elements before erasing 6:" << endl;
displaySet(mySet);
// Check if the element 6 is present in the set
if ([Link](6) != [Link]()) {
cout << "Element 6 is present in the set. Erasing it..." << endl;
[Link](6); // Erase the element 6
} else {
cout << "Element 6 is not present in the set." << endl;
}
// Displa
[Link] a C++ program that creates a set with the elements {1, 3, 5, 7, 9}. Use an
iterator to traverse the set and print each element.
ans12 #include <bits/stdc++.h>
using namespace std;
int main() {
// Create and initialize the set with elements {1, 3, 5, 7, 9}
set<int> mySet = {1, 3, 5, 7, 9};
// Create an iterator for the set
set<int>::iterator it;
// Traverse the set using the iterator and print each element
cout << "Elements in the set are:" << endl;
for (it = [Link](); it != [Link](); ++it) {
cout << *it << " ";
}
cout << endl;
return 0;
}
[Link] a C++ program that creates two sets of integers: set1 with elements {1, 2,
3, 4, 5} and set2 with elements {4, 5, 6, 7, 8}. Find the union and intersection of
these sets and display the resulting elements for both operations.
ans13 #include <bits/stdc++.h>
using namespace std;
int main() {
// Create and initialize the sets
set<int> set1 = {1, 2, 3, 4, 5};
set<int> set2 = {4, 5, 6, 7, 8};
// Create sets to store the results of union and intersection
set<int> unionSet;
set<int> intersectionSet;
// Compute the union of set1 and set2
set_union([Link](), [Link](),
[Link](), [Link](),
inserter(unionSet, [Link]()));
// Compute the intersection of set1 and set2
set_intersection([Link](), [Link](),
[Link](), [Link](),
inserter(intersecti
14. Remove Common Elements from Two Vectors
Write a program that takes two vectors as input. Remove any elements that are
common between the two vectors.
ans14 #include <bits/stdc++.h>
using namespace std;
int main() {
// Input vectors
vector<int> vec1 = {1, 2, 3, 4, 5};
vector<int> vec2 = {4, 5, 6, 7, 8};
// Display original vectors
cout << "Original Vector 1: ";
for (int num : vec1) cout << num << " ";
cout << endl;
cout << "Original Vector 2: ";
for (int num : vec2) cout << num << " ";
cout << endl;
// Use sets to find common elements and remove them
set<int> set1([Link](), [Link]());
set<int> set2([Link](), [Link]());
// Find common elements
vector<int> commonElements;
set_intersection([Link](), [Link](),
[Link](), [Link](),
back_inserter(commonElements));
// Remove common elements from vec1
[Link](remove_if([Link](), [Link](),
[&commonElements](int x) {
return find([Link](),
[Link](), x) != [Link]();
}),
[Link]());
// Remove common elements from vec2
[Link](remove_if([Link](), [Link](),
[&commonElements](int x) {
return find([Link](),
[Link](), x) != [Link]();
}),
[Link]());
// Display updated vectors
cout << "Updated Vector 1: ";
for (int num : vec1) cout << num << " ";
cout << endl;
cout << "Updated Vector 2: ";
for (int num : vec2) cout << num << " ";
cout << endl;
return 0;
}
15. Print Uncommon Elements in Two Vectors
Write a program that takes two vectors as input and prints the elements that are
unique to each vector (i.e., elements that are not shared between the two vectors).
ans15 #include <bits/stdc++.h>
using namespace std;
int main() {
// Initialize vectors (you can modify or input these vectors as needed)
vector<int> vec1 = {1, 2, 3, 4, 5};
vector<int> vec2 = {4, 5, 6, 7, 8};
// Convert vectors to sets for easier operations
set<int> set1([Link](), [Link]());
set<int> set2([Link](), [Link]());
// Find elements unique to vec1 (not in vec2)
set<int> uniqueToVec1;
set_difference([Link](), [Link](),
[Link](), [Link](),
inserter(uniqueToVec1, [Link]()));
// Find elements unique to vec2 (not in vec1)
set<int> uniqueToVec2;
set_difference([Link](), [Link](),
[Link](), [Link](),
inserter(uniqueToVec2, [Link]()));
// Print elements unique to vec1
cout << "Elements unique to vec1:" << endl;
for (const int& element : uniqueToVec1) {
cout << element << " ";
}
cout << endl;
// Print elements unique to vec2
cout << "Elements unique to vec2:" << endl;
for (const int& element : uniqueToVec2) {
cout << element << " ";
}
cout << endl;
return 0;
}
16. Print Intersection Elements of Three Vectors
Write a program that takes three vectors as input and prints the elements that
are common to all three vectors.
ans16 #include <bits/stdc++.h>
using namespace std;
int main() {
// Initialize vectors (you can modify or input these vectors as needed)
vector<int> vec1 = {1, 2, 3, 4, 5};
vector<int> vec2 = {4, 5, 6, 7, 8};
// Convert vectors to sets for easier operations
set<int> set1([Link](), [Link]());
set<int> set2([Link](), [Link]());
// Find elements unique to vec1 (not in vec2)
set<int> uniqueToVec1;
set_difference([Link](), [Link](),
[Link](), [Link](),
inserter(uniqueToVec1, [Link]()));
// Find elements unique to vec2 (not in vec1)
set<int> uniqueToVec2;
set_difference([Link](), [Link](),
[Link](), [Link](),
inserter(uniqueToVec2, [Link]()));
// Print elements unique to vec1
cout << "Elements unique to vec1:" << endl;
for (const int& element : uniqueToVec1) {
cout << element << " ";
}
cout << endl;
// Print elements unique to vec2
cout << "Elements unique to vec2:" << endl;
for (const int& element : uniqueToVec2) {
cout << element << " ";
}
cout << endl;
return 0;
}
17. Remove Duplicates from Four Vectors
Write a program that takes four vectors as input and removes any duplicate
elements from each vector.
ans17 #include <bits/stdc++.h>
using namespace std;
// Function to remove duplicates from a vector
vector<int> removeDuplicates(const vector<int>& vec) {
// Convert vector to set to remove duplicates
set<int> uniqueElements([Link](), [Link]());
// Convert the set back to vector
vector<int> result([Link](), [Link]());
return result;
}
int main() {
// Initialize four vectors with example values
vector<int> vec1 = {1, 2, 2, 3, 4, 4, 5};
vector<int> vec2 = {6, 7, 7, 8, 9, 9, 9};
vector<int> vec3 = {10, 10, 11, 12, 12, 13};
vector<int> vec4 = {14, 15, 15, 16, 16, 17};
// Remove duplicates from each vector
vector<int> uniqueVec1 = removeDuplicates(vec1);
vector<int> uniqueVec2 = removeDuplicates(vec2);
vector<int> uniqueVec3 = removeDuplicates(vec3);
vector<int> uniqueVec4 = removeDuplicates(vec4);
// Print the vectors after removing duplicates
cout << "Vector 1 after removing duplicates: ";
for (int num : uniqueVec1) cout << num << " ";
cout << endl;
cout << "Vector 2 after removing duplicates: ";
for (int num : uniqueVec2) cout << num << " ";
cout << endl;
cout << "Vector 3 after removing duplicates: ";
for (int num : uniqueVec3) cout << num << " ";
cout << endl;
cout << "Vector 4 after removing duplicates: ";
for (int num : uniqueVec4) cout << num << " ";
cout << endl;
return 0;
}
18. Check Element Presence in a Set
Write a program that takes a set and a number of elements as input. Check if
each element is present in the set or not.
ans18 #include <bits/stdc++.h>
using namespace std;
int main() {
// Initialize the set with some example values
set<int> mySet = {10, 20, 30, 40, 50};
// Number of elements to check
int n;
cout << "Enter the number of elements to check: ";
cin >> n;
// Vector to store the elements to check
vector<int> elementsToCheck(n);
// Input the elements to check
cout << "Enter the elements to check: ";
for (int i = 0; i < n; ++i) {
cin >> elementsToCheck[i];
}
// Check presence of each element in the set
for (const int& element : elementsToCheck) {
if ([Link](element) != [Link]()) {
cout << element << " is present in the set." << endl;
} else {
cout << element << " is not present in the set." << endl;
}
}
return 0;
}
19. Find Symmetric Difference of Three Sets
Write a program that takes three sets as input and finds the symmetric
difference between them (i.e., elements that are in any of the sets but not in
their intersection).
ans 19 #include <bits/stdc++.h>
using namespace std;
// Function to compute the symmetric difference of three sets
set<int> symmetricDifference(const set<int>& set1, const set<int>& set2, const
set<int>& set3) {
// Compute the intersection of all three sets
set<int> intersection123;
set_intersection([Link](), [Link](),
[Link](), [Link](),
inserter(intersection123, [Link]()));
set<int> intersection23;
set_intersection([Link](), [Link](),
[Link](), [Link](),
inserter(intersection23, [Link]()));
set<int> intersection13;
set_intersection([Link](), [Link](),
[Link](), [Link](),
inserter(intersection13, [Link]()));
// Compute the union of the intersections
set<int> intersectionAll;
set_union([Link](), [Link](),
[Link](), [Link](),
inserter(intersectionAll, [Link]()));
// Compute the union of all sets
set<int> unionAll;
set_union([Link](), [Link](),
[Link](), [Link](),
inserter(unionAll, [Link]()));
set<int> unionAll3;
set_union([Link](), [Link](),
[Link](), [Link](),
inserter(unionAll3, [Link]()));
// Compute the symmetric difference (elements in the union but not in the
intersection of all sets)
set<int> symDifference;
set_difference([Link](), [Link](),
[Link](), [Link](),
inserter(symDifference, [Link]()));
return symDifference;
}
int main() {
// Initialize three sets (modify or input these sets as needed)
set<int> set1 = {1, 2, 3, 4, 5};
set<int> set2 = {4, 5, 6, 7, 8};
set<int> set3 = {8, 9, 10, 1, 2};
// Compute symmetric difference
set<int> result = symmetricDifference(set1, set2, set3);
// Print the result
cout << "Symmetric difference of the three sets: ";
for (const int& element : result) {
cout << element << " ";
}
cout << endl;
return 0;
}
20. Check Subset Relationship Between Two Sets
Write a program that takes a vector of `n` numbers and checks if one set is a
subset of another. For example, given the original set `{10, 20, 23, 40, 50, 60,
89}` and a subset `{50, 60, 89}`, check if the subset is indeed a subset of the
original set.
ans20 #include <bits/stdc++.h>
using namespace std;
// Function to check if set2 is a subset of set1
bool isSubset(const set<int>& set1, const set<int>& set2) {
// Use std::includes to check if all elements of set2 are in set1
return includes([Link](), [Link](), [Link](), [Link]());
}
int main() {
// Initialize the original set and the subset (modify or input these sets as
needed)
set<int> originalSet = {10, 20, 23, 40, 50, 60, 89};
set<int> subset = {50, 60, 89};
// Check if subset is a subset of originalSet
bool result = isSubset(originalSet, subset);
// Print the result
if (result) {
cout << "The subset is indeed a subset of the original set." << endl;
} else {
cout << "The subset is not a subset of the original set." << endl;
}
return 0;
}