0% found this document useful (0 votes)
22 views4 pages

1 Find The Largest Element

coding

Uploaded by

sk02480ak
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)
22 views4 pages

1 Find The Largest Element

coding

Uploaded by

sk02480ak
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

1.

Find the Largest Element in an Array

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

int findLargest(const vector<int>& arr) {


int maxElem = arr[0];
for (int num : arr) {
if (num > maxElem) maxElem = num;
}
return maxElem;
}

int main() {
vector<int> arr = {1, 5, 3, 9, 2};
cout << "Largest element: " << findLargest(arr) << endl;
return 0;
}

2. Reverse a String

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

string reverseString(string str) {


int left = 0, right = str.size() - 1;
while (left < right) {
swap(str[left++], str[right--]);
}
return str;
}

int main() {
string str = "hello";
cout << "Reversed string: " << reverseString(str) << endl;
return 0;
}

3. Check if a Number is Prime

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

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;
}

int main() {
int num = 29;
cout << (isPrime(num) ? "Prime" : "Not Prime") << endl;
return 0;
}

4. Find the Fibonacci Number

#include <iostream>
using namespace std;

int fibonacci(int n) {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}

int main() {
int n = 5;
cout << "Fibonacci number: " << fibonacci(n) << endl;
return 0;
}

5. Merge Two Sorted Arrays

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

vector<int> mergeSortedArrays(const vector<int>& a, const vector<int>& b) {


vector<int> merged;
int i = 0, j = 0;
while (i < a.size() && j < b.size()) {
if (a[i] < b[j]) merged.push_back(a[i++]);
else merged.push_back(b[j++]);
}
while (i < a.size()) merged.push_back(a[i++]);
while (j < b.size()) merged.push_back(b[j++]);
return merged;
}

int main() {
vector<int> a = {1, 3, 5}, b = {2, 4, 6};
vector<int> result = mergeSortedArrays(a, b);
for (int num : result) cout << num << " ";
return 0;
}

6. Binary Search

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

int binarySearch(const vector<int>& arr, int target) {


int left = 0, right = arr.size() - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target) return mid;
if (arr[mid] < target) left = mid + 1;
else right = mid - 1;
}
return -1;
}

int main() {
vector<int> arr = {1, 3, 5, 7, 9};
int target = 7;
int index = binarySearch(arr, target);
cout << (index != -1 ? "Found at index " + to_string(index) : "Not found") << endl;
return 0;
}

7. Find the Missing Number in an Array

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

int findMissingNumber(const vector<int>& arr, int n) {


int total = n * (n + 1) / 2;
int sum = 0;
for (int num : arr) sum += num;
return total - sum;
}

int main() {
vector<int> arr = {3, 0, 1};
cout << "Missing number: " << findMissingNumber(arr, 3) << endl;
return 0;
}

8. Find All Pairs with a Given Sum

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

void findPairsWithSum(const vector<int>& arr, int sum) {


unordered_map<int, int> map;
for (int num : arr) {
int complement = sum - num;
if (map[complement]) {
cout << "(" << complement << ", " << num << ")" << endl;
}
map[num]++;
}
}

int main() {
vector<int> arr = {1, 5, 7, -1};
int sum = 6;
findPairsWithSum(arr, sum);
return 0;
}

9. Find the First Non-Repeating Character in a String


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

char firstNonRepeatingChar(const string& str) {


unordered_map<char, int> count;
for (char c : str) count[c]++;
for (char c : str) {
if (count[c] == 1) return c;
}
return '\0';
}

int main() {
string str = "swiss";
char result = firstNonRepeatingChar(str);
cout << "First non-repeating character: " << (result ? result : ' ') << endl;
return 0;
}

10. Implement a Stack with Get-Min Function

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

class MinStack {
stack<int> mainStack, minStack;
public:
void push(int x) {
mainStack.push(x);
if (minStack.empty() || x <= minStack.top()) minStack.push(x);
}
void pop() {
if (mainStack.top() == minStack.top()) minStack.pop();
mainStack.pop();
}
int top() { return mainStack.top(); }
int getMin() { return minStack.top(); }
};

int main() {
MinStack s;
s.push(1);
s.push(2);
s.push(-1);
s.push(4);
cout << "Minimum element: " << s.getMin() << endl;
return 0;
}

You might also like