es
Feedback
inactive

inactive

Canal cerrado
3 849
Suscriptores
Sin datos24 horas
-297 días
-12730 días
Archivo de publicaciones
#include <iostream> #include <vector> void merge(std::vector<char>& arr, int left, int middle, int right) { int n1 = middle - left + 1; int n2 = right - middle; std::vector<char> leftArr(n1); std::vector<char> rightArr(n2); for (int i = 0; i < n1; ++i) leftArr[i] = arr[left + i]; for (int j = 0; j < n2; ++j) rightArr[j] = arr[middle + 1 + j]; int i = 0, j = 0, k = left; while (i < n1 && j < n2) { if (leftArr[i] <= rightArr[j]) { arr[k++] = leftArr[i++]; } else { arr[k++] = rightArr[j++]; } } while (i < n1) { arr[k++] = leftArr[i++]; } while (j < n2) { arr[k++] = rightArr[j++]; } } void mergeSort(std::vector<char>& arr, int left, int right) { if (left < right) { int middle = left + (right - left) / 2; mergeSort(arr, left, middle); mergeSort(arr, middle + 1, right); merge(arr, left, middle, right); } } int main() { int n; // std::cout << "Enter the number of characters: "; std::cin >> n; std::vector<char> characters(n); // std::cout << "Enter the characters: "; for (int i = 0; i < n; i++) { std::cin >> characters[i]; } //awasthi // Perform merge sort to sort characters mergeSort(characters, 0, n - 1); std::cout << "Sorted Characters: "; for (char c : characters) { std::cout << c << " "; } std::cout << std::endl; return 0; }

You have a dictionary with a collection of characters. Implement a program that sorts the characters in lexicographic order using the recursive Merge Sort algorithm. Call the function mergeSort to sort the array of characters. The program should take the unsorted list of characters as input and output the sorted list of characters. Write a logic for merge sorting and a recursive function. Note: The lexicographical order of characters will be 'A', 'B', 'C', …, 'Y', 'Z', 'a', 'b', 'c', …, 'y', 'z'. Input format : The first line of input consists of the characters n, representing the number of characters in the dictionary. The second line of input consists of n characters, separated by spaces. Output format : The output displays a sorted list of characters in lexicographic order, with each character separated by a space.

#include <iostream> #include <vector> void merge(std::vector<int>& arr, int left, int middle, int right) { int n1 = middle - left + 1; int n2 = right - middle; // Create temporary arrays to hold even and odd numbers std::vector<int> evens, odds; // Fill the temporary arrays with even and odd numbers for (int i = left; i <= right; i++) { if (arr[i] % 2 == 0) evens.push_back(arr[i]); else odds.push_back(arr[i]); } int i = 0, j = 0, k = left; // Merge the even numbers while (i < evens.size()) { arr[k++] = evens[i++]; } // Merge the odd numbers while (j < odds.size()) { arr[k++] = odds[j++]; } } void mergeSort(std::vector<int>& arr, int left, int right) { if (left < right) { int middle = left + (right - left) / 2; mergeSort(arr, left, middle); mergeSort(arr, middle + 1, right); merge(arr, left, middle, right); } } int main() { int n; // std::cout << "Enter the number of integers: "; std::cin >> n; std::vector<int> numbers(n); // std::cout << "Enter the integers: "; for (int i = 0; i < n; i++) { std::cin >> numbers[i]; } // Perform merge sort to arrange even and odd numbers mergeSort(numbers, 0, n - 1); //awasthi // std::cout << "Sorted integers: "; for (int num : numbers) { std::cout << num << " "; } std::cout << std::endl; return 0; }

Alex is a young computer science enthusiast who loves solving coding problems. One day, Alex stumbled upon a unique challenge related to sorting numbers. The challenge involves sorting a list of integers with a unique twist: Alex aims to place the even numbers in their original input order first, followed by the odd numbers in the same input order. Your task is to help Alex implement a logic of merge sort and a recursive function to arrange the even and odd numbers separately. Input format : The first line contains an integer n, the number of integers in the list. The second line contains n space-separated integers, a[i], representing the elements of the list. Output format : The output displays a single line containing n space-separated integers, representing the sorted list, with even numbers appearing first in the given input order, followed by odd numbers in the given input order.

#include <iostream> #include <vector> bool isVowel(char c) { // Check if the character is a vowel (both uppercase and lowercase) return (c == 'a' c == 'e' c == 'i' c == 'o' c == 'u' c == 'A' c == 'E' c == 'I' c == 'O' || c == 'U'); } void merge(std::vector<char>& arr, int left, int middle, int right) { int n1 = middle - left + 1; int n2 = right - middle; // Create temporary arrays to hold vowels and consonants std::vector<char> vowels, consonants; // Fill the temporary arrays with vowels and consonants for (int i = left; i <= right; i++) { if (isVowel(arr[i])) vowels.push_back(arr[i]); else consonants.push_back(arr[i]); } int i = 0, j = 0, k = left; // Merge the vowel characters while (i < vowels.size()) { arr[k++] = vowels[i++]; } // Merge the consonant characters while (j < consonants.size()) { arr[k++] = consonants[j++]; } } void mergeSort(std::vector<char>& arr, int left, int right) { if (left < right) { int middle = left + (right - left) / 2; mergeSort(arr, left, middle); mergeSort(arr, middle + 1, right); merge(arr, left, middle, right); } } int main() { int n; // std::cout << "Enter the number of characters: "; std::cin >> n; std::vector<char> characters(n); //std::cout << "Enter the characters: "; for (int i = 0; i < n; i++) { std::cin >> characters[i]; } // Perform merge sort based on vowels and consonants mergeSort(characters, 0, n - 1); //std::cout << "Sorted characters: "; for (char c : characters) { std::cout << c << " "; } std::cout << std::endl; //awasthi return 0; }

John is a linguistic enthusiast who is fascinated by the arrangement of letters based on their characteristics. He is particularly interested in sorting characters based on whether they are vowels or consonants. Write a program to sort a given set of characters. The characters should be sorted in such a way that vowels appear first, followed by consonants. Within the vowels and consonants, maintain the original input order of characters. Your task is to assist John in implementing the logic for merge sort and a recursive function to achieve the goal stated in the program. Input format : The first line consists of an integer n, representing the number of characters in the list. The next line consists of n space-separated characters, containing alphabetic characters (both uppercase and lowercase). Output format : The output displays the characters sorted first by vowels, maintaining the original order of input, followed by consonants in the same input order.

// You are using GCC #include <iostream> #include <vector> int maximumLoot(const std::vector<int>& houses, int index) { if (index < 0) return 0; // Rob the current house or skip it return std::max(houses[index] + maximumLoot(houses, index - 2), maximumLoot(houses, index - 1)); } int main() { int n; // std::cout << "Enter the number of houses: "; std::cin >> n; std::vector<int> houses(n); // std::cout << "Enter the amount in each house: "; for (int i = 0; i < n; ++i) { std::cin >> houses[i]; } //awasthi // Calculate the maximum loot int maxLoot = maximumLoot(houses, n - 1); std::cout << maxLoot << std::endl; return 0; }

MONEY HEIST! You are the Professor of a professional robbery group. And now you are planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected, and they will automatically contact the police if two adjacent houses are broken into on the same night. Given an integer array representing the amount of money in each house. As the professor, plan smart to get the maximum amount of money you can rob tonight without alerting the police. Write a recursive function, maximumLoot(), that returns the maximum amount that can be robbed. Example 1 Input: 5 2 7 9 3 1 Output: 12 Explanation: Rob house 1 (money = 2), rob house 3 (money = 9), and rob house 5 (money = 1). Total amount you can rob = 2 + 9 + 1 = 12.

// You are using GCC #include <iostream> #include <cmath> int towerOfHanoi(int n) { // Base case: If there are no disks, no moves are needed if (n == 0) { return 0; } else { // Recursive formula for Tower of Hanoi: T(n) = 2 * T(n-1) + 1 return 2 * towerOfHanoi(n - 1) + 1; } } int main() { int n; //std::cout << "Enter the number of disks: "; std::cin >> n; //awasthi // Calculate the minimum number of moves using Tower of Hanoi formula int minMoves = towerOfHanoi(n); std::cout<< minMoves << std::endl; return 0; } //awasthi

In this mystical kingdom, there were three enchanted pegs: the Source Peg, the Destination Peg, and the Helper Peg. Each peg had unique magical properties essential for saving the kingdom from an evil sorceress's destructive curse. To counter this spell and protect the kingdom, they had to transfer magical disks of varying sizes, represented by positive integers, from the Source Peg to the Destination Peg using the Helper Peg. Notably, each disk was smaller than the one beneath it. According to the ancient legends, the Sorceress' curse could be broken only if the disks were transferred from the Source Peg to the Destination Peg following these rules: Only one disk could be moved at a time. A larger disk could never be placed on top of a smaller disk. The Helper Peg could be used temporarily for disk movements. The kingdom's wise sage had devised a solution using the Tower of Hanoi algorithm, a mysterious ritual that required the minimum number of moves to transfer all the magical disks from the Source Peg to the Destination Peg. Write a program to calculate the minimum number of moves required to solve the Tower of Hanoi problem for n magical disks, thus breaking the Sorceress' curse and saving the kingdom from impending doom. Note: This kind of question will be helpful in clearing Capgemini recruitment.

Unit-4 👇

🔥

Dm @awasthi108 for tickets 🎟️

Be ready to laugh out loud coz Comedy queen - Gurleen is coming 💥
Be ready to laugh out loud coz Comedy queen - Gurleen is coming 💥

Dear all, start studying DSA & CPP because this time cheating may can lead to UMC case in CA2. Dates has been extended to giv
Dear all, start studying DSA & CPP because this time cheating may can lead to UMC case in CA2. Dates has been extended to give time for practice only  👨‍💻

photo content

#include <iostream> using namespace std; // Node structure for the linked list struct Node { int data; Node* next; Node(int value) : data(value), next(nullptr) {} }; class Deque { private: Node* front; Node* rear; public: Deque() : front(nullptr), rear(nullptr) {} // Check if the deque is empty bool isEmpty() { return front == nullptr; } // Insert an element at the rear of the deque void insertRear(int value) { Node* newNode = new Node(value); if (isEmpty()) { front = rear = newNode; } else { rear->next = newNode; rear = newNode; } } // Display even elements in the deque void displayEven() { Node* current = front; cout << "Even elements: "; while (current != nullptr) { if (current->data % 2 == 0) { cout << current->data << " "; } current = current->next; } cout << endl; } // Display odd elements in the deque void displayOdd() { Node* current = front; cout << "Odd elements: "; while (current != nullptr) { if (current->data % 2 != 0) { cout << current->data << " "; } current = current->next; } cout << endl; } }; int main() { Deque deque; int num; while (true) { cin >> num; if (num == -1) { break; // Terminate input when -1 is encountered } deque.insertRear(num); } deque.displayEven(); deque.displayOdd(); return 0; }