3 849
Subscribers
No data24 hours
-297 days
-12730 days
Posts Archive
3 849
#include <iostream>
#include <string>
using namespace std;
int main()
{
int n;
cin >> n;
string arr[n];
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
// initial order
cout<<"Initial order: ";
for (int i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
cout << endl;
// ṣort them alphabetically first letter using insertion sort
for (int i = 1; i < n; i++)
{
string key = arr[i];
int j = i - 1;
cout<<"After Iteration "<<i<<": ";
while (j >= 0 && arr[j] < key)
{
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
for(int k=0;k<n;k++){
cout<<arr[k]<<" ";
}
cout<<endl;
}
cout<<"Sorted order: ";
for (int i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
return 0;
}
3 849
#include <iostream>
#include <string>
using namespace std;
int isFruitPresent(string arr[], int n, string key)
{
int s = 0;
int e = n;
while (s <= e)
{
int mid = (s + e) / 2;
if (arr[mid] == key)
{
return mid;
}
else if (arr[mid] > key)
{
e = mid - 1;
}
else
{
s = mid + 1;
}
}
return -1;
}
int main(){
// take nth array string and find the index of the string which is equal to the given string using binary search
int n;
cin >> n;
string arr[n];
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
string key;
cin>>key;
int res;
res=isFruitPresent(arr,n,key);
if (res>0){
cout<<"The first occurrence of \""<<key<<"\" is at index "<<res<<".";
}
else{
cout<<"\""<<key<<"\" is not found in the array"<<".";
}
return 0;
}
3 849
#include <iostream>
#include <vector>
int findPeakElement(const std::vector<int>& mountain_array) {
int left = 0;
int right = mountain_array.size() - 1;
while (left < right) {
int mid = left + (right - left) / 2;
if (mountain_array[mid] < mountain_array[mid + 1]) {
left = mid + 1;
} else {
right = mid;
}
}
return mountain_array[left];
}
int main() {
int n;
std::vector<int> mountain_array;
// Input
std::cin >> n;
mountain_array.resize(n);
for (int i = 0; i < n; ++i) {
std::cin >> mountain_array[i];
}
// Find the peak element
int peak_element = findPeakElement(mountain_array);
// Output
std::cout << peak_element << std::endl;
return 0;
}
3 849
#include <iostream>
#include <string>
#include <sstream>
int countWords(const std::string& input_string) {
std::stringstream ss(input_string);
std::string word;
int word_count = 0;
while (ss >> word) {
word_count++;
}
return word_count;
}
int main() {
std::string input_string;
// Input
std::getline(std::cin, input_string);
// Count the number of words
int word_count = countWords(input_string);
// Output
std::cout<< word_count << std::endl;
return 0;
}
3 849
Harry Potter is a famous novel of all time that revolves around the story of a young boy who discovers that he is a wizard.
3 849
#include<iostream>
using namespace std;
int main(){
string sentence;
getline(cin,sentence);
string search;
cin>>search;
string word;
string words[100];
int count = 0;
for (int i = 0; i < sentence.length(); i++)
{
if (sentence[i] == ' ' || sentence[i] == ',')
{
words[count] = word;
count++;
word = "";
}
else
{
word += sentence[i];
}
}
words[count] = word;
count++;
int c=0;
for (int i = 0; i < count; i++)
{
if(words[i]==search){
c++;
}
}
if(c>=1){
cout<<"The word '"<<search<<"' is present in the given sentence.";
}
else{
cout<<"The word '"<<search<<"' is not present in the given sentence.";
}
return 0;
}
3 849
#include <iostream>
using namespace std;
int binarySearch(int arr[],int l,int r,int x)
{
if(r<l){
return l;
}
if(r>=l)
{
int mid = l + (r-l)/2;
if(arr[mid]==x)
return mid;
if(arr[mid]>x)
return binarySearch(arr,l,mid-1,x);
return binarySearch(arr,mid+1,r,x);
}
}
int main()
{
int n;
cin >> n;
int arr[n];
for(int i=0;i<n;i++)
cin >> arr[i];
int x;
cin >> x;
cout<<binarySearch(arr,0,n-1,x)<<endl;
return 0;
}
3 849
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
int main() {
int n, m;
std::map<int, int> frequencyMap;
std::vector<int> arr1, arr2;
// Input
std::cin >> n;
arr1.resize(n);
for (int i = 0; i < n; ++i) {
std::cin >> arr1[i];
frequencyMap[arr1[i]]++;
}
std::cin >> m;
arr2.resize(m);
for (int i = 0; i < m; ++i) {
std::cin >> arr2[i];
frequencyMap[arr2[i]]++;
}
// Find the most frequent items
int maxFrequency = 0;
for (const auto &entry : frequencyMap) {
maxFrequency = std::max(maxFrequency, entry.second);
}
// Output the most frequent items
for (const auto &entry : frequencyMap) {
if (entry.second == maxFrequency) {
std::cout << entry.first << " ";
}
}
std::cout << std::endl;
return 0;
}
3 849
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
int size1, size2;
std::vector<std::string> arr1, arr2, merged_and_sorted;
// Input
std::cin >> size1;
arr1.resize(size1);
for (int i = 0; i < size1; ++i) {
std::cin >> arr1[i];
}
std::cin >> size2;
arr2.resize(size2);
for (int i = 0; i < size2; ++i) {
std::cin >> arr2[i];
}
// Merge and sort the arrays
merged_and_sorted.insert(merged_and_sorted.end(), arr1.begin(), arr1.end());
merged_and_sorted.insert(merged_and_sorted.end(), arr2.begin(), arr2.end());
std::sort(merged_and_sorted.begin(), merged_and_sorted.end());
std::cout<<"Merged and sorted array: ";
// Output
for (const std::string &str : merged_and_sorted) {
std::cout << str << " ";
}
std::cout << std::endl;
return 0;
}
