Gaurav Kumar Dsa File
Gaurav Kumar Dsa File
int main() {
int greatest;
greatest = num2;
else
greatest = num3;
cout << "The greatest number is: " << greatest << endl;
return 0;
#include <iostream>
#include <unordered_set>
using namespace std;
int main() {
const int size1 = 20, size2 = 30;
int arr1[size1], arr2[size2];
// Taking input for the first array
cout << "Enter 20 elements for the first array: ";
for (int i = 0; i < size1; i++) {
cin >> arr1[i];
}
// Taking input for the second array
cout << "Enter 30 elements for the second array: ";
for (int i = 0; i < size2; i++) {
cin >> arr2[i];
}
unordered_set<int> set1(arr1, arr1 + size1);
unordered_set<int> commonElements;
// Finding common elements
for (int i = 0; i < size2; i++) {
if (set1.find(arr2[i]) != set1.end()) {
commonElements.insert(arr2[i]);
}
}
// Displaying common elements
cout << "Common elements are: ";
for (int element : commonElements) {
cout << element << " ";
}
cout << endl;
return 0;
}
7.Given an array, arr, of integers, your task is to return the smallest and
the second smallest elements in the array. If the smallest and the
second smallest elements do not exist, return -1.
#include <iostream>
#include <limits.h>
using namespace std;
void findSmallestAndSecondSmallest(int arr[], int size) {
if (size < 2) {
cout << "-1" << endl;
return;
}
int smallest = INT_MAX, secondSmallest = INT_MAX;
for (int i = 0; i < size; i++) {
if (arr[i] < smallest) {
secondSmallest = smallest;
smallest = arr[i];
} else if (arr[i] > smallest && arr[i] < secondSmallest) {
secondSmallest = arr[i];
}
}
if (secondSmallest == INT_MAX)
cout << "-1" << endl;
else
cout << "Smallest: " << smallest << ", Second Smallest: " << secondSmallest << endl;
}
int main() {
int size;
cout << "Enter the size of the array: ";
cin >> size;
int arr[size];
cout << "Enter " << size << " elements: ";
for (int i = 0; i < size; i++) {
cin >> arr[i];
}
findSmallestAndSecondSmallest(arr, size);
return 0;
}
#include <iostream>
using namespace std;
int main() {
int arr[2][3] = {{2, 3, 4}, {5, 6, 7}};
int *ptr = &arr[0][0]; // Pointer to the first element of the array
cout << "Elements of the 2D array using pointers:" << endl;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
cout << *(ptr + i * 3 + j) << " ";
}
cout << endl;
}
return 0;
}
#include <iostream>
using namespace std;
cout << "Deleted " << temp->data << " from the beginning" <<
endl;
int main() {
Node* head = nullptr; // Initialize empty list
cout << "=== Linked List Operations Demo (Using Struct Only)
===" << endl;
// Initial display
displayList(head);
// Insert at beginning
insertAtBeginning(&head, 10);
displayList(head);
insertAtBeginning(&head, 20);
displayList(head);
// Insert at end
insertAtEnd(&head, 30);
displayList(head);
insertAtEnd(&head, 40);
displayList(head);
// More insertions to demonstrate
insertAtBeginning(&head, 5);
insertAtEnd(&head, 50);
displayList(head);
// More deletions
deleteFromBeginning(&head);
deleteFromEnd(&head);
displayList(head);