0% found this document useful (0 votes)
340 views

Mansour

The document contains C++ code examples demonstrating the use of common data structures and algorithms including vectors, maps, sets, queues, stacks, sorting, binary search, two sum, frequency counting, prefix/suffix sums, and max/min heaps. It also includes functions to check if a number is prime or a palindrome.

Uploaded by

enkaz7yat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
340 views

Mansour

The document contains C++ code examples demonstrating the use of common data structures and algorithms including vectors, maps, sets, queues, stacks, sorting, binary search, two sum, frequency counting, prefix/suffix sums, and max/min heaps. It also includes functions to check if a number is prime or a palindrome.

Uploaded by

enkaz7yat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <iostream>

#include <vector>

using namespace std;

int binarySearch(vector<int>& nums, int target) {


int left = 0, right = nums.size() - 1;

while (left <= right) {


int mid = left + (right - left) / 2;

if (nums[mid] == target)
return mid;
else if (nums[mid] < target)
left = mid + 1;
else
right = mid - 1;
}

return -1; // Target not found


}
#include <iostream>
#include <vector>

using namespace std;

bool twoSum(vector<int>& nums, int target) {


int left = 0, right = nums.size() - 1;

while (left < right) {


int sum = nums[left] + nums[right];
if (sum == target)
return true;
else if (sum < target)
left++;
else
right--;
}

return false;
}
#include <iostream>
#include <vector>

using namespace std;

void countFrequencies(vector<int>& nums) {


map<int, int> freq;

for (int num : nums)


freq[num]++;

for (auto& p : freq)


cout << "Number: " << p.first << ", Frequency: " << p.second << endl;
}
#include <iostream>
#include <vector>

using namespace std;

void prefixSum(vector<int>& nums) {


int n = nums.size();

for (int i = 1; i < n; i++)


nums[i] += nums[i - 1];
}

void suffixSum(vector<int>& nums) {


int n = nums.size();

for (int i = n - 2; i >= 0; i--)


nums[i] += nums[i + 1];
}
#include <iostream>
#include <vector>

using namespace std;

void partialSum(vector<int>& nums) {


int n = nums.size();

for (int i = 1; i < n; i++)


nums[i] += nums[i - 1];
}

#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>

using namespace std;

int main() {
// Example of vector
vector<int> v = {1, 2, 3, 4, 5};

// Example of sorting a vector


sort(v.begin(), v.end());

// Example of map
map<string, int> m;
m["apple"] = 3;
m["banana"] = 2;

// Example of set
set<int> s = {5, 2, 8, 1};
// Example of queue
queue<int> q;
q.push(1);
q.push(2);

// Example of stack
stack<int> st;
st.push(1);
st.push(2);

return 0;
}

#include <queue>
priority_queue<int> pq; // Max heap
priority_queue<int, vector<int>, greater<int>> minHeap; // Min heap

#include <deque>
deque<int> dq;
dq.push_back(1);
dq.push_front(2);

###############
int position = upper_bound(a.begin(), a.end(), b[i]) - a.begin();
Here’s what each part does:

upper_bound(a.begin(), a.end(), b[i]): This function searches for the first element in the range [a.begin(), a
.end()) that is greater than b[i]. It returns an iterator pointing to that position.

a.begin(): This is the iterator pointing to the beginning of vector a.


####
bool isPrime(int n) {
if (n <= 1) {
return false;
}
for (int i = 2; i <= sqrt(n); ++i) {
if (n % i == 0) {
return false;
}
}
return true;
}
####
bool isPalindromeExtension(int num) {
if (num < 0) {
return false;
} else if (num % 10 == 0) {
while (num % 10 == 0) {
num = num / 10;
}
}

int reversed_num = 0;
while (num > reversed_num) {
reversed_num = reversed_num * 10 + num % 10;
num = num / 10;
}

if (num == reversed_num || num == reversed_num / 10) {


return true;
}

return false;
}

You might also like