0% found this document useful (0 votes)
32 views3 pages

Answer2 2

ANSWER

Uploaded by

Himanshu Kasera
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)
32 views3 pages

Answer2 2

ANSWER

Uploaded by

Himanshu Kasera
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/ 3

Powered by AI

Copilot

Question: How can you find the length of the longest substring without repeating characters in C++?
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

int lengthOfLongestSubstring(std::string s) {
std::vector<int> dict(256, -1);
int maxLen = 0, start = -1;
for (int i = 0; i != s.length(); i++) {
if (dict[s[i]] > start)
start = dict[s[i]];
dict[s[i]] = i;
maxLen = std::max(maxLen, i - start);
}
return maxLen;
}

int main() {
std::string s = "abcabcbb";
std::cout << "Length of the longest substring without repeating characters: " << lengthOfLongestSubstring(s) << std::endl;
return 0;
}

Question: How can you implement a stack using an array in C++?


#include <iostream>

class Stack {
private:
int top;
int arr[1000]; // Size of Stack

public:
Stack() { top = -1; }
bool push(int x) {
if (top >= (1000 - 1)) {
std::cout << "Stack Overflow";
return false;
}
else {
arr[++top] = x;
std::cout << x << " pushed into stack\n";
return true;
}
}

int pop() {
if (top < 0) {
std::cout << "Stack Underflow";
return 0;
}
else {
int x = arr[top--];
return x;
}
}

int peek() {
if (top < 0) {
std::cout << "Stack is Empty";
return 0;
}
else
return arr[top];
}

bool isEmpty() {
return (top < 0);
}
};

int main() {
Stack s;
s.push(10);
s.push(20);
s.push(30);
std::cout << s.pop() << " Popped from stack\n";
return 0;
}
Question: How can you implement a queue using an array in C++?
#include <iostream>
#include <climits>

class Queue {
private:
int front, rear, size;
unsigned capacity;
int* array;

public:
Queue(unsigned capacity) {
this->capacity = capacity;
front = this->size = 0;
rear = capacity - 1;
array = new int[(this->capacity * sizeof(int))];
}

bool isFull() { return (size == capacity); }


bool isEmpty() { return (size == 0); }
void enqueue(int item) {
if (isFull()) return;
rear = (rear + 1) % capacity;
array[rear] = item;
size = size + 1;
}
int dequeue() {
if (isEmpty()) return INT_MIN;
int item = array[front];
front = (front + 1) % capacity;
size = size - 1;
return item;
}
int front() {
if (isEmpty()) return INT_MIN;
return array[front];
}
int rear() {
if (isEmpty()) return INT_MIN;
return array[rear];
}
};

int main() {
Queue q(1000);
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
q.enqueue(40);
std::cout << q.dequeue() << " dequeued from queue\n";
return 0;
}

Question: How can you find the lowest common ancestor in a binary tree in C++?
#include <iostream>

struct TreeNode {
int val;
TreeNode *left, *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {


if (!root || root == p || root == q) return root;
TreeNode* left = lowestCommonAncestor(root->left, p, q);
TreeNode* right = lowestCommonAncestor(root->right, p, q);
if (left && right) return root;
return left ? left : right;
}

int main() {
TreeNode* root = new TreeNode(3);
root->left = new TreeNode(5);
root->right = new TreeNode(1);
root->left->left = new TreeNode(6);
root->left->right = new TreeNode(2);
root->right->left = new TreeNode(0);
root->right->right = new TreeNode(8);
TreeNode* p = root->left;
TreeNode* q = root->right;
TreeNode* lca = lowestCommonAncestor(root, p, q);
std::cout << "Lowest Common Ancestor: " << lca->val << std::endl;
return 0;
}
Hope this helps! 😄 If you have more questions or need further assistance, feel free to ask!

You might also like