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?
#include<bits/stdc++.h>
int main() {
std::vector<int> arr = {35, 45, 70, 80, 90, 28, 66, 49, 53, 52, 57, 78};
std::map<int, std::string> categoryMap;
for (int value : arr) {
if (value > 65) {
categoryMap[value] = "Obesity";
} else if (value >= 45 && value <= 65) {
categoryMap[value] = "Healthy";
} else {
categoryMap[value] = "Ricket";
}
}
std::cout << "Value -> Category:\n";
for (const auto &pair : categoryMap) {
std::cout << pair.first << " -> " << pair.second << '\n';
}
return 0;
}
-----------------------------------------------------------------------------------
---------------------
2. vector [12,21,32,23,56,65,76,67]
[[12,21],[32,23],[65,56]] group them like this using map.
#include<bits/stdc++.h>
using namespace std;
int main() {
vector<int> vec = {12, 21, 32, 23, 56, 65, 76, 67};
map<int, int> group_map;
for (size_t i = 0; i < vec.size(); i += 2) {
if (i + 1 < vec.size()) {
group_map[vec[i]] = vec[i + 1];
}
}
for (const auto &pair : group_map) {
cout << "[" << pair.first << ", " << pair.second << "]" << 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"]
]
#include<bits/stdc++.h>
using namespace std;
int main() {
vector<string> words = {"listen", "silent", "enlist", "inlets", "rat", "tar",
"art", "cat", "act", "tac"};
map<string,vector<string>> anagram_groups;
for (const string &word : words) {
string sorted_word = word;
sort(sorted_word.begin(), sorted_word.end());
anagram_groups[sorted_word].push_back(word);
}
cout << "[\n";
for (const auto &group : anagram_groups) {
cout << " [";
for (const string &word : group.second) {
cout << "\"" << word << "\" ";
}
cout << "],\n";
}
cout << "]\n";
return 0;
}
-----------------------------------------------------------------------------------
-----------------------
4.Given 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.
#include <bits/stdc++.h>
using namespace std;
unordered_map<int, int> countFrequencies(const vector<int>& arr) {
unordered_map<int, int> frequencyMap;
for (int num : arr) {
frequencyMap[num]++;
}
return frequencyMap;
}
int main() {
vector<int> arr = {1, 2, 2, 3, 4, 4, 4, 5, 5, 6};
unordered_map<int, int> freqMap = countFrequencies(arr);
cout << "Frequency of each integer:\n";
for (const auto& pair : freqMap) {
cout << "Number " << pair.first << ": " << pair.second << " 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.
#include <bits/stdc++.h>
using namespace std;
int findMostFrequentElement(const vector<int>& arr) {
unordered_map<int, int> frequencyMap;
for (int num : arr) {
frequencyMap[num]++;
}
int mostFrequentElement = arr[0];
int maxFrequency = frequencyMap[mostFrequentElement];
for (const auto& entry : frequencyMap) {
if (entry.second > maxFrequency) {
mostFrequentElement = entry.first;
maxFrequency = entry.second;
}
}
return mostFrequentElement;
}
int main() {
vector<int> arr = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4,5,5,5};
int result = findMostFrequentElement(arr);
cout << "The element that appears most frequently is: " << result << endl;
return 0;
}
===================================================================================
========================
Given an array of integers and a threshold value, write a function that finds and
returns all elements whose frequency is above the given threshold.
#include <bits/stdc++.h>
using namespace std;
vector<int> findElementsAboveThreshold(const vector<int>& arr, int threshold) {
unordered_map<int, int> frequencyMap;
for (int num : arr) {
frequencyMap[num]++;
}
vector<int> result;
for (const auto& entry : frequencyMap) {
if (entry.second > threshold) {
result.push_back(entry.first);
}
}
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;
===================================================================================
========================================
Given an array of integers and a threshold value, write a function that finds and
returns all elements whose frequency is above the given threshold.
#include<bits/stdc++.h>
std::vector<int> findElementsAboveThreshold(const std::vector<int>& arr, int
threshold) {
std::map<int, int> frequency_map;
std::vector<int> result;
for (int num : arr) {
frequency_map[num]++;
}
for (const auto& pair : frequency_map) {
if (pair.second > threshold) {
result.push_back(pair.first);
}
}
return result;
}
int main() {
std::vector<int> arr = {1, 3, 2, 3, 4, 2, 3, 2, 2, 4, 4, 4,5,5,5,5,5,5,5};
int threshold = 2;
std::vector<int> elements_above_threshold = findElementsAboveThreshold(arr,
threshold);
std::cout << "Elements with frequency above " << threshold << ": ";
for (int num : elements_above_threshold) {
std::cout << num << " ";
}
std::cout << std::endl;
return 0;
}
===================================================================================
============================================
Write 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.
#include<bits/stdc++.h>
std::vector<int> findElementsAboveThreshold(const std::vector<int>& arr, int
threshold) {
std::map<int, int> frequency_map;
std::vector<int> result;
for (int num : arr) {
frequency_map[num]++;
}
for (const auto& pair : frequency_map) {
if (pair.second > threshold) {
result.push_back(pair.first);
}
}
return result;
}
int main() {
std::vector<int> arr = {1, 3, 2, 3, 4, 2, 3, 2, 2, 4, 4, 4,5,5,5,5,5,5,5};
int threshold = 2;
std::vector<int> elements_above_threshold = findElementsAboveThreshold(arr,
threshold);
std::cout << "Elements with frequency above " << threshold << ": ";
for (int num : elements_above_threshold) {
std::cout << num << " ";
}
std::cout << std::endl;
return 0;
}
===================================================================================
================================
Given 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.
#include <bits/stdc++.h>
using namespace std;
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 < arr.size(); ++i) {
int complement = target_sum - arr[i];
if (index_map.find(complement) != index_map.end()) {
return {index_map[complement], i};
}
index_map[arr[i]] = i;
}
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 (result.first != -1 && result.second != -1) {
cout << "Indices of elements that add up to " << target_sum << " are: "
<< result.first << " and " << result.second << endl;
} else {
cout << "No two elements add up to the target sum." << endl;
}
return 0;
}
===================================================================================
=======================
Write 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.
#include <bits/stdc++.h>
using namespace std;
int main() {
set<int> mySet;
mySet.insert(5);
mySet.insert(10);
mySet.insert(15);
mySet.insert(20);
mySet.insert(25);
cout << "Elements in the set in ascending order:" << endl;
for (const int& element : mySet) {
cout << element << " ";
}
cout << endl;
return 0;
}
===================================================================================
======================
Write 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.
#include <bits/stdc++.h>
using namespace std;
int main() {
set<int> mySet;
mySet.insert(10);
mySet.insert(20);
mySet.insert(20);
mySet.insert(30);
mySet.insert(40);
mySet.insert(40);
mySet.insert(50);
cout << "Elements in the set (duplicates not to be included):" << endl;
for (const int& element : mySet) {
cout << element << " ";
}
cout << endl;
return 0;
}
===================================================================================
==========================
Write 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.
#include <bits/stdc++.h>
using namespace std;
int main() {
set<int> mySet = {1, 3, 5, 7, 9};
set<int>::iterator it;
cout << "Elements in the set are:" << endl;
for (it = mySet.begin(); it != mySet.end(); it++) {
cout << *it << " ";
}
cout << endl;
return 0;
}
===================================================================================
================
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.
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> vec1 = {1, 2, 3, 4, 5};
vector<int> vec2 = {4, 5, 6, 7, 8};
cout << "Original Vector 1: ";
for (int num : vec1) cout << num << " ";
cout << endl;
cout << "Original Vector 2: ";
for (int num : vec2) cout << num << " ";
cout << endl;
set<int> set1(vec1.begin(), vec1.end());
set<int> set2(vec2.begin(), vec2.end());
vector<int> commonElements;
set_intersection(set1.begin(), set1.end(),
set2.begin(), set2.end(),
back_inserter(commonElements));
vec1.erase(remove_if(vec1.begin(), vec1.end(),
[&commonElements](int x) {
return find(commonElements.begin(), commonElements.end(), x) !=
commonElements.end();
}),
vec1.end());
vec2.erase(remove_if(vec2.begin(), vec2.end(),
[&commonElements](int x) {
return find(commonElements.begin(),
commonElements.end(), x) != commonElements.end();
}),
vec2.end());
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;
}
===================================================================================
==========================
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).
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> vec1 = {1, 2, 3, 4, 5};
vector<int> vec2 = {4, 5, 6, 7, 8};
set<int> set1(vec1.begin(), vec1.end());
set<int> set2(vec2.begin(), vec2.end());
set<int> uniqueToVec1;
set_difference(set1.begin(), set1.end(),
set2.begin(), set2.end(),
inserter(uniqueToVec1, uniqueToVec1.begin()));
set<int> uniqueToVec2;
set_difference(set2.begin(), set2.end(),
set1.begin(), set1.end(),
inserter(uniqueToVec2, uniqueToVec2.begin()));
cout << "Elements unique to vec1:" << endl;
for (const int& element : uniqueToVec1) {
cout << element << " ";
}
cout << endl;
cout << "Elements unique to vec2:" << endl;
for (const int& element : uniqueToVec2) {
cout << element << " ";
}
cout << endl;
return 0;
}
===================================================================================
========================
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.
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> vec1 = {1, 2, 3, 4, 5};
vector<int> vec2 = {4, 5, 6, 7, 8};
set<int> set1(vec1.begin(), vec1.end());
set<int> set2(vec2.begin(), vec2.end());
set<int> uniqueToVec1;
set_difference(set1.begin(), set1.end(),
set2.begin(), set2.end(),
inserter(uniqueToVec1, uniqueToVec1.begin()));
set<int> uniqueToVec2;
set_difference(set2.begin(), set2.end(),
set1.begin(), set1.end(),
inserter(uniqueToVec2, uniqueToVec2.begin()));
cout << "Elements unique to vec1:" << endl;
for (const int& element : uniqueToVec1) {
cout << element << " ";
}
cout << endl;
cout << "Elements unique to vec2:" << endl;
for (const int& element : uniqueToVec2) {
cout << element << " ";
}
cout << endl;
return 0;
}
===================================================================================
======================
Remove Duplicates from Four Vectors
Write a program that takes four vectors as input and removes any duplicate
elements from each vector.
#include <bits/stdc++.h>
using namespace std;
vector<int> removeDuplicates(const vector<int>& vec) {
set<int> uniqueElements(vec.begin(), vec.end());
vector<int> result(uniqueElements.begin(), uniqueElements.end());
return result;
}
int main() {
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};
vector<int> uniqueVec1 = removeDuplicates(vec1);
vector<int> uniqueVec2 = removeDuplicates(vec2);
vector<int> uniqueVec3 = removeDuplicates(vec3);
vector<int> uniqueVec4 = removeDuplicates(vec4);
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;
}
===================================================================================
=======================================
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.
#include <bits/stdc++.h>
using namespace std;
int main() {
set<int> mySet = {10, 20, 30, 40, 50};
int n;
cout << "Enter the number of elements to check: ";
cin >> n;
vector<int> elementsToCheck(n);
cout << "Enter the elements to check: ";
for (int i = 0; i < n; ++i) {
cin >> elementsToCheck[i];
}
for (const int& element : elementsToCheck) {
if (mySet.find(element) != mySet.end()) {
cout << element << " is present in the set." << endl;
} else {
cout << element << " is not present in the set." << endl;
}
}
return 0;
}
===================================================================================
====================
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).\\
#include <bits/stdc++.h>
using namespace std;
set<int> symmetricDifference(const set<int>& set1, const set<int>& set2, const
set<int>& set3) {
set<int> intersection123;
set_intersection(set1.begin(), set1.end(),
set2.begin(), set2.end(),
inserter(intersection123, intersection123.begin()));
set<int> intersection23;
set_intersection(set2.begin(), set2.end(),
set3.begin(), set3.end(),
inserter(intersection23, intersection23.begin()));
set<int> intersection13;
set_intersection(set1.begin(), set1.end(),
set3.begin(), set3.end(),
inserter(intersection13, intersection13.begin()));
set<int> intersectionAll;
set_union(intersection23.begin(), intersection23.end(),
intersection13.begin(), intersection13.end(),
inserter(intersectionAll, intersectionAll.begin()));
set<int> unionAll;
set_union(set1.begin(), set1.end(),
set2.begin(), set2.end(),
inserter(unionAll, unionAll.begin()));
set<int> unionAll3;
set_union(unionAll.begin(), unionAll.end(),
set3.begin(), set3.end(),
inserter(unionAll3, unionAll3.begin()));
set<int> symDifference;
set_difference(unionAll3.begin(), unionAll3.end(),
intersectionAll.begin(), intersectionAll.end(),
inserter(symDifference, symDifference.begin()));
return symDifference;
}
int main() {
set<int> set1 = {1, 2, 3, 4, 5};
set<int> set2 = {4, 5, 6, 7, 8};
set<int> set3 = {8, 9, 10, 1, 2};
set<int> result = symmetricDifference(set1, set2, set3);
cout << "Symmetric difference of the three sets: ";
for (const int& element : result) {
cout << element << " ";
}
cout << endl;
return 0;
}
===================================================================================
========================
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.
#include <bits/stdc++.h>
using namespace std;
bool isSubset(const set<int>& set1, const set<int>& set2) {
return includes(set1.begin(), set1.end(), set2.begin(), set2.end());
}
int main() {
set<int> originalSet = {10, 20, 23, 40, 50, 60, 89};
set<int> subset = {50, 60, 89};
bool result = isSubset(originalSet, subset);
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;
}
===================================================================================
=========================