C++ Data Structures Coding Questions
Practice 50 most asked C++ Data Structures coding interview questions with solutions - Easy, Medium, and Hard levels.
1. Implement Stack using Array
Easy
Input: Push 10, Push 20, Pop, Push 30, Top
Output: 20
30
#include <iostream>
using namespace std;
class Stack {
int arr[100];
int top;
public:
Stack() { top = -1; }
void push(int x) {
if(top >= 99) return;
arr[++top] = x;
}
int pop() {
if(top < 0) return -1;
return arr[top--];
}
int peek() {
if(top < 0) return -1;
return arr[top];
}
};
int main() {
Stack s;
s.push(10);
s.push(20);
cout << s.pop() << endl;
s.push(30);
cout << s.peek() << endl;
return 0;
}
2. Implement Queue using Array
EasyInput: Enqueue 5, Enqueue 10, Dequeue, Enqueue 15 Output: 5
#include <iostream>
using namespace std;
class Queue {
int arr[100];
int front, rear;
public:
Queue() { front = rear = -1; }
void enqueue(int x) {
if(rear >= 99) return;
if(front == -1) front = 0;
arr[++rear] = x;
}
int dequeue() {
if(front == -1 || front > rear) return -1;
return arr[front++];
}
};
int main() {
Queue q;
q.enqueue(5);
q.enqueue(10);
cout << q.dequeue() << endl;
q.enqueue(15);
return 0;
}
3. Reverse an Array
EasyInput: 1 2 3 4 5 Output: 5 4 3 2 1
#include <iostream>
using namespace std;
void reverseArray(int arr[], int n) {
int start = 0, end = n - 1;
while(start < end) {
swap(arr[start], arr[end]);
start++;
end--;
}
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = 5;
reverseArray(arr, n);
for(int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}
4. Find Maximum Element in Array
EasyInput: 3 7 2 9 1 Output: 9
#include <iostream>
using namespace std;
int findMax(int arr[], int n) {
int maxVal = arr[0];
for(int i = 1; i < n; i++) {
if(arr[i] > maxVal)
maxVal = arr[i];
}
return maxVal;
}
int main() {
int arr[] = {3, 7, 2, 9, 1};
int n = 5;
cout << findMax(arr, n) << endl;
return 0;
}
5. Linear Search in Array
Easy
Input: 10 20 30 40 50
30
Output: Found at index 2
#include <iostream>
using namespace std;
int linearSearch(int arr[], int n, int key) {
for(int i = 0; i < n; i++) {
if(arr[i] == key)
return i;
}
return -1;
}
int main() {
int arr[] = {10, 20, 30, 40, 50};
int n = 5, key = 30;
int result = linearSearch(arr, n, key);
if(result != -1)
cout << "Found at index " << result << endl;
else
cout << "Not Found" << endl;
return 0;
}
6. Binary Search in Sorted Array
Easy
Input: 2 5 8 12 16 23 38 56 72 91
23
Output: Found at index 5
#include <iostream>
using namespace std;
int binarySearch(int arr[], int n, int key) {
int left = 0, right = n - 1;
while(left <= right) {
int mid = left + (right - left) / 2;
if(arr[mid] == key)
return mid;
else if(arr[mid] < key)
left = mid + 1;
else
right = mid - 1;
}
return -1;
}
int main() {
int arr[] = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91};
int n = 10, key = 23;
int result = binarySearch(arr, n, key);
if(result != -1)
cout << "Found at index " << result << endl;
else
cout << "Not Found" << endl;
return 0;
}
7. Insert Element at Beginning of Linked List
EasyInput: Insert 10, Insert 20, Insert 30 Output: 30 20 10
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
void insertAtBeginning(Node** head, int data) {
Node* newNode = new Node();
newNode->data = data;
newNode->next = *head;
*head = newNode;
}
void printList(Node* node) {
while(node != NULL) {
cout << node->data << " ";
node = node->next;
}
}
int main() {
Node* head = NULL;
insertAtBeginning(&head, 10);
insertAtBeginning(&head, 20);
insertAtBeginning(&head, 30);
printList(head);
return 0;
}
8. Insert Element at End of Linked List
EasyInput: Insert 10, Insert 20, Insert 30 Output: 10 20 30
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
void insertAtEnd(Node** head, int data) {
Node* newNode = new Node();
newNode->data = data;
newNode->next = NULL;
if(*head == NULL) {
*head = newNode;
return;
}
Node* temp = *head;
while(temp->next != NULL)
temp = temp->next;
temp->next = newNode;
}
void printList(Node* node) {
while(node != NULL) {
cout << node->data << " ";
node = node->next;
}
}
int main() {
Node* head = NULL;
insertAtEnd(&head, 10);
insertAtEnd(&head, 20);
insertAtEnd(&head, 30);
printList(head);
return 0;
}
9. Delete First Node of Linked List
EasyInput: 10 20 30 (Delete First) Output: 20 30
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
void deleteFirst(Node** head) {
if(*head == NULL) return;
Node* temp = *head;
*head = (*head)->next;
delete temp;
}
void insertAtEnd(Node** head, int data) {
Node* newNode = new Node();
newNode->data = data;
newNode->next = NULL;
if(*head == NULL) {
*head = newNode;
return;
}
Node* temp = *head;
while(temp->next != NULL) temp = temp->next;
temp->next = newNode;
}
void printList(Node* node) {
while(node != NULL) {
cout << node->data << " ";
node = node->next;
}
}
int main() {
Node* head = NULL;
insertAtEnd(&head, 10);
insertAtEnd(&head, 20);
insertAtEnd(&head, 30);
deleteFirst(&head);
printList(head);
return 0;
}
10. Count Nodes in Linked List
EasyInput: 10 20 30 40 Output: 4
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
int countNodes(Node* head) {
int count = 0;
Node* temp = head;
while(temp != NULL) {
count++;
temp = temp->next;
}
return count;
}
void insertAtEnd(Node** head, int data) {
Node* newNode = new Node();
newNode->data = data;
newNode->next = NULL;
if(*head == NULL) {
*head = newNode;
return;
}
Node* temp = *head;
while(temp->next != NULL) temp = temp->next;
temp->next = newNode;
}
int main() {
Node* head = NULL;
insertAtEnd(&head, 10);
insertAtEnd(&head, 20);
insertAtEnd(&head, 30);
insertAtEnd(&head, 40);
cout << countNodes(head) << endl;
return 0;
}
11. Check if Array is Sorted
EasyInput: 1 2 3 4 5 Output: Array is sorted
#include <iostream>
using namespace std;
bool isSorted(int arr[], int n) {
for(int i = 0; i < n - 1; i++) {
if(arr[i] > arr[i + 1])
return false;
}
return true;
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = 5;
if(isSorted(arr, n))
cout << "Array is sorted" << endl;
else
cout << "Array is not sorted" << endl;
return 0;
}
12. Remove Duplicates from Sorted Array
EasyInput: 1 1 2 2 3 4 4 5 Output: 1 2 3 4 5
#include <iostream>
using namespace std;
int removeDuplicates(int arr[], int n) {
if(n == 0 || n == 1) return n;
int j = 0;
for(int i = 0; i < n - 1; i++) {
if(arr[i] != arr[i + 1])
arr[j++] = arr[i];
}
arr[j++] = arr[n - 1];
return j;
}
int main() {
int arr[] = {1, 1, 2, 2, 3, 4, 4, 5};
int n = 8;
n = removeDuplicates(arr, n);
for(int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}
13. Find Sum of Array Elements
EasyInput: 1 2 3 4 5 Output: 15
#include <iostream>
using namespace std;
int arraySum(int arr[], int n) {
int sum = 0;
for(int i = 0; i < n; i++)
sum += arr[i];
return sum;
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = 5;
cout << arraySum(arr, n) << endl;
return 0;
}
14. Check Balanced Parentheses using Stack
Easy
Input: {[()]}
Output: Balanced
#include <iostream>
#include <stack>
using namespace std;
bool isBalanced(string exp) {
stack<char> s;
for(int i = 0; i < exp.length(); i++) {
if(exp[i] == '(' || exp[i] == '{' || exp[i] == '[') {
s.push(exp[i]);
}
else {
if(s.empty()) return false;
char top = s.top();
if((exp[i] == ')' && top == '(') ||
(exp[i] == '}' && top == '{') ||
(exp[i] == ']' && top == '['))
s.pop();
else
return false;
}
}
return s.empty();
}
int main() {
string exp = "{[()]}";
if(isBalanced(exp))
cout << "Balanced" << endl;
else
cout << "Not Balanced" << endl;
return 0;
}
15. Implement Circular Queue
EasyInput: Enqueue 10, Enqueue 20, Dequeue, Enqueue 30 Output: 10
#include <iostream>
using namespace std;
class CircularQueue {
int arr[100];
int front, rear, size;
public:
CircularQueue() {
front = rear = -1;
size = 100;
}
void enqueue(int x) {
if((rear + 1) % size == front) return;
if(front == -1) front = 0;
rear = (rear + 1) % size;
arr[rear] = x;
}
int dequeue() {
if(front == -1) return -1;
int data = arr[front];
if(front == rear)
front = rear = -1;
else
front = (front + 1) % size;
return data;
}
};
int main() {
CircularQueue q;
q.enqueue(10);
q.enqueue(20);
cout << q.dequeue() << endl;
q.enqueue(30);
return 0;
}
16. Find Minimum Element in Array
EasyInput: 8 3 9 1 5 Output: 1
#include <iostream>
using namespace std;
int findMin(int arr[], int n) {
int minVal = arr[0];
for(int i = 1; i < n; i++) {
if(arr[i] < minVal)
minVal = arr[i];
}
return minVal;
}
int main() {
int arr[] = {8, 3, 9, 1, 5};
int n = 5;
cout << findMin(arr, n) << endl;
return 0;
}
17. Search Element in Linked List
Easy
Input: 10 20 30 40 50
30
Output: Found at position 3
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
int search(Node* head, int key) {
Node* temp = head;
int pos = 1;
while(temp != NULL) {
if(temp->data == key)
return pos;
temp = temp->next;
pos++;
}
return -1;
}
void insertAtEnd(Node** head, int data) {
Node* newNode = new Node();
newNode->data = data;
newNode->next = NULL;
if(*head == NULL) {
*head = newNode;
return;
}
Node* temp = *head;
while(temp->next != NULL) temp = temp->next;
temp->next = newNode;
}
int main() {
Node* head = NULL;
insertAtEnd(&head, 10);
insertAtEnd(&head, 20);
insertAtEnd(&head, 30);
insertAtEnd(&head, 40);
insertAtEnd(&head, 50);
int pos = search(head, 30);
if(pos != -1)
cout << "Found at position " << pos << endl;
else
cout << "Not Found" << endl;
return 0;
}
18. Reverse a String using Stack
EasyInput: Hello Output: olleH
#include <iostream>
#include <stack>
using namespace std;
string reverseString(string str) {
stack<char> s;
for(int i = 0; i < str.length(); i++)
s.push(str[i]);
string reversed = "";
while(!s.empty()) {
reversed += s.top();
s.pop();
}
return reversed;
}
int main() {
string str = "Hello";
cout << reverseString(str) << endl;
return 0;
}
19. Rotate Array Left by One Position
EasyInput: 1 2 3 4 5 Output: 2 3 4 5 1
#include <iostream>
using namespace std;
void rotateLeft(int arr[], int n) {
int first = arr[0];
for(int i = 0; i < n - 1; i++)
arr[i] = arr[i + 1];
arr[n - 1] = first;
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = 5;
rotateLeft(arr, n);
for(int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}
20. Find Second Largest Element in Array
EasyInput: 12 35 1 10 34 1 Output: 34
#include <iostream>
#include <climits>
using namespace std;
int secondLargest(int arr[], int n) {
int first = INT_MIN, second = INT_MIN;
for(int i = 0; i < n; i++) {
if(arr[i] > first) {
second = first;
first = arr[i];
}
else if(arr[i] > second && arr[i] != first)
second = arr[i];
}
return second;
}
int main() {
int arr[] = {12, 35, 1, 10, 34, 1};
int n = 6;
cout << secondLargest(arr, n) << endl;
return 0;
}
21. Reverse a Linked List
MediumInput: 10 20 30 40 Output: 40 30 20 10
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
Node* reverseList(Node* head) {
Node* prev = NULL;
Node* current = head;
Node* next = NULL;
while(current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
return prev;
}
void insertAtEnd(Node** head, int data) {
Node* newNode = new Node();
newNode->data = data;
newNode->next = NULL;
if(*head == NULL) {
*head = newNode;
return;
}
Node* temp = *head;
while(temp->next != NULL) temp = temp->next;
temp->next = newNode;
}
void printList(Node* node) {
while(node != NULL) {
cout << node->data << " ";
node = node->next;
}
}
int main() {
Node* head = NULL;
insertAtEnd(&head, 10);
insertAtEnd(&head, 20);
insertAtEnd(&head, 30);
insertAtEnd(&head, 40);
head = reverseList(head);
printList(head);
return 0;
}
22. Detect Loop in Linked List
MediumInput: 10->20->30->40->20 (loop) Output: Loop detected
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
bool detectLoop(Node* head) {
Node* slow = head;
Node* fast = head;
while(fast != NULL && fast->next != NULL) {
slow = slow->next;
fast = fast->next->next;
if(slow == fast)
return true;
}
return false;
}
int main() {
Node* head = new Node();
head->data = 10;
head->next = new Node();
head->next->data = 20;
head->next->next = new Node();
head->next->next->data = 30;
head->next->next->next = new Node();
head->next->next->next->data = 40;
head->next->next->next->next = head->next; // Create loop
if(detectLoop(head))
cout << "Loop detected" << endl;
else
cout << "No loop" << endl;
return 0;
}
23. Find Middle Element of Linked List
MediumInput: 10 20 30 40 50 Output: 30
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
int findMiddle(Node* head) {
Node* slow = head;
Node* fast = head;
while(fast != NULL && fast->next != NULL) {
slow = slow->next;
fast = fast->next->next;
}
return slow->data;
}
void insertAtEnd(Node** head, int data) {
Node* newNode = new Node();
newNode->data = data;
newNode->next = NULL;
if(*head == NULL) {
*head = newNode;
return;
}
Node* temp = *head;
while(temp->next != NULL) temp = temp->next;
temp->next = newNode;
}
int main() {
Node* head = NULL;
insertAtEnd(&head, 10);
insertAtEnd(&head, 20);
insertAtEnd(&head, 30);
insertAtEnd(&head, 40);
insertAtEnd(&head, 50);
cout << findMiddle(head) << endl;
return 0;
}
24. Implement Two Stacks in One Array
MediumInput: Push1 5, Push2 10, Push1 15, Pop1 Output: 15
#include <iostream>
using namespace std;
class TwoStacks {
int arr[100];
int top1, top2;
public:
TwoStacks() {
top1 = -1;
top2 = 100;
}
void push1(int x) {
if(top1 < top2 - 1) {
arr[++top1] = x;
}
}
void push2(int x) {
if(top1 < top2 - 1) {
arr[--top2] = x;
}
}
int pop1() {
if(top1 >= 0)
return arr[top1--];
return -1;
}
int pop2() {
if(top2 < 100)
return arr[top2++];
return -1;
}
};
int main() {
TwoStacks ts;
ts.push1(5);
ts.push2(10);
ts.push1(15);
cout << ts.pop1() << endl;
return 0;
}
25. Implement Queue using Two Stacks
MediumInput: Enqueue 1, Enqueue 2, Dequeue, Enqueue 3 Output: 1
#include <iostream>
#include <stack>
using namespace std;
class QueueUsingStacks {
stack<int> s1, s2;
public:
void enqueue(int x) {
s1.push(x);
}
int dequeue() {
if(s2.empty()) {
while(!s1.empty()) {
s2.push(s1.top());
s1.pop();
}
}
if(s2.empty()) return -1;
int val = s2.top();
s2.pop();
return val;
}
};
int main() {
QueueUsingStacks q;
q.enqueue(1);
q.enqueue(2);
cout << q.dequeue() << endl;
q.enqueue(3);
return 0;
}
26. Implement Stack using Queue
MediumInput: Push 1, Push 2, Pop, Push 3 Output: 2
#include <iostream>
#include <queue>
using namespace std;
class StackUsingQueue {
queue<int> q1, q2;
public:
void push(int x) {
q2.push(x);
while(!q1.empty()) {
q2.push(q1.front());
q1.pop();
}
swap(q1, q2);
}
int pop() {
if(q1.empty()) return -1;
int val = q1.front();
q1.pop();
return val;
}
};
int main() {
StackUsingQueue s;
s.push(1);
s.push(2);
cout << s.pop() << endl;
s.push(3);
return 0;
}
27. Find Nth Node from End of Linked List
Medium
Input: 10 20 30 40 50
n = 2
Output: 40
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
int nthFromEnd(Node* head, int n) {
Node* main_ptr = head;
Node* ref_ptr = head;
for(int i = 0; i < n; i++) {
if(ref_ptr == NULL) return -1;
ref_ptr = ref_ptr->next;
}
while(ref_ptr != NULL) {
main_ptr = main_ptr->next;
ref_ptr = ref_ptr->next;
}
return main_ptr->data;
}
void insertAtEnd(Node** head, int data) {
Node* newNode = new Node();
newNode->data = data;
newNode->next = NULL;
if(*head == NULL) {
*head = newNode;
return;
}
Node* temp = *head;
while(temp->next != NULL) temp = temp->next;
temp->next = newNode;
}
int main() {
Node* head = NULL;
insertAtEnd(&head, 10);
insertAtEnd(&head, 20);
insertAtEnd(&head, 30);
insertAtEnd(&head, 40);
insertAtEnd(&head, 50);
cout << nthFromEnd(head, 2) << endl;
return 0;
}
28. Merge Two Sorted Linked Lists
Medium
Input: List1: 1 3 5
List2: 2 4 6
Output: 1 2 3 4 5 6
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
Node* mergeSorted(Node* a, Node* b) {
if(!a) return b;
if(!b) return a;
Node* result = NULL;
if(a->data <= b->data) {
result = a;
result->next = mergeSorted(a->next, b);
} else {
result = b;
result->next = mergeSorted(a, b->next);
}
return result;
}
void insertAtEnd(Node** head, int data) {
Node* newNode = new Node();
newNode->data = data;
newNode->next = NULL;
if(*head == NULL) {
*head = newNode;
return;
}
Node* temp = *head;
while(temp->next != NULL) temp = temp->next;
temp->next = newNode;
}
void printList(Node* node) {
while(node != NULL) {
cout << node->data << " ";
node = node->next;
}
}
int main() {
Node* head1 = NULL;
Node* head2 = NULL;
insertAtEnd(&head1, 1);
insertAtEnd(&head1, 3);
insertAtEnd(&head1, 5);
insertAtEnd(&head2, 2);
insertAtEnd(&head2, 4);
insertAtEnd(&head2, 6);
Node* merged = mergeSorted(head1, head2);
printList(merged);
return 0;
}
29. Remove Duplicates from Unsorted Linked List
MediumInput: 12 11 12 21 41 43 21 Output: 12 11 21 41 43
#include <iostream>
#include <unordered_set>
using namespace std;
struct Node {
int data;
Node* next;
};
void removeDuplicates(Node* head) {
unordered_set<int> seen;
Node* current = head;
Node* prev = NULL;
while(current != NULL) {
if(seen.find(current->data) != seen.end()) {
prev->next = current->next;
delete current;
} else {
seen.insert(current->data);
prev = current;
}
current = prev->next;
}
}
void insertAtEnd(Node** head, int data) {
Node* newNode = new Node();
newNode->data = data;
newNode->next = NULL;
if(*head == NULL) {
*head = newNode;
return;
}
Node* temp = *head;
while(temp->next != NULL) temp = temp->next;
temp->next = newNode;
}
void printList(Node* node) {
while(node != NULL) {
cout << node->data << " ";
node = node->next;
}
}
int main() {
Node* head = NULL;
insertAtEnd(&head, 12);
insertAtEnd(&head, 11);
insertAtEnd(&head, 12);
insertAtEnd(&head, 21);
insertAtEnd(&head, 41);
insertAtEnd(&head, 43);
insertAtEnd(&head, 21);
removeDuplicates(head);
printList(head);
return 0;
}
30. Evaluate Postfix Expression
MediumInput: 231*+9- Output: -4
#include <iostream>
#include <stack>
using namespace std;
int evaluatePostfix(string exp) {
stack<int> s;
for(int i = 0; i < exp.length(); i++) {
if(isdigit(exp[i]))
s.push(exp[i] - '0');
else {
int val2 = s.top(); s.pop();
int val1 = s.top(); s.pop();
switch(exp[i]) {
case '+': s.push(val1 + val2); break;
case '-': s.push(val1 - val2); break;
case '*': s.push(val1 * val2); break;
case '/': s.push(val1 / val2); break;
}
}
}
return s.top();
}
int main() {
string exp = "231*+9-";
cout << evaluatePostfix(exp) << endl;
return 0;
}
31. Sort a Stack using Recursion
MediumInput: 30 -5 18 14 -3 Output: -5 -3 14 18 30
#include <iostream>
#include <stack>
using namespace std;
void sortedInsert(stack<int>& s, int x) {
if(s.empty() || x > s.top()) {
s.push(x);
return;
}
int temp = s.top();
s.pop();
sortedInsert(s, x);
s.push(temp);
}
void sortStack(stack<int>& s) {
if(!s.empty()) {
int x = s.top();
s.pop();
sortStack(s);
sortedInsert(s, x);
}
}
int main() {
stack<int> s;
s.push(30);
s.push(-5);
s.push(18);
s.push(14);
s.push(-3);
sortStack(s);
while(!s.empty()) {
cout << s.top() << " ";
s.pop();
}
return 0;
}
32. Find Pair with Given Sum in Array
Medium
Input: 1 4 45 6 10 8
16
Output: 6 10
#include <iostream>
#include <unordered_set>
using namespace std;
void findPair(int arr[], int n, int sum) {
unordered_set<int> s;
for(int i = 0; i < n; i++) {
int temp = sum - arr[i];
if(s.find(temp) != s.end()) {
cout << temp << " " << arr[i] << endl;
return;
}
s.insert(arr[i]);
}
cout << "No pair found" << endl;
}
int main() {
int arr[] = {1, 4, 45, 6, 10, 8};
int n = 6;
int sum = 16;
findPair(arr, n, sum);
return 0;
}
33. Rearrange Array in Alternating Positive & Negative
MediumInput: -5 -2 5 2 4 7 1 8 0 -8 Output: -5 5 -2 2 -8 4 7 1 8 0
#include <iostream>
using namespace std;
void rearrange(int arr[], int n) {
int i = 0, j = n - 1;
while(i < j) {
while(i < n && arr[i] < 0) i++;
while(j >= 0 && arr[j] >= 0) j--;
if(i < j) swap(arr[i], arr[j]);
}
if(i == 0 || i == n) return;
int k = 0;
while(k < n && i < n) {
swap(arr[k], arr[i]);
k += 2;
i++;
}
}
int main() {
int arr[] = {-5, -2, 5, 2, 4, 7, 1, 8, 0, -8};
int n = 10;
rearrange(arr, n);
for(int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}
34. Find Intersection of Two Sorted Arrays
Medium
Input: 1 2 4 5 6
2 3 5 7
Output: 2 5
#include <iostream>
using namespace std;
void findIntersection(int arr1[], int n1, int arr2[], int n2) {
int i = 0, j = 0;
while(i < n1 && j < n2) {
if(arr1[i] < arr2[j])
i++;
else if(arr2[j] < arr1[i])
j++;
else {
cout << arr1[i] << " ";
i++;
j++;
}
}
}
int main() {
int arr1[] = {1, 2, 4, 5, 6};
int arr2[] = {2, 3, 5, 7};
int n1 = 5, n2 = 4;
findIntersection(arr1, n1, arr2, n2);
return 0;
}