3 849
Підписники
Немає даних24 години
-297 днів
-12730 днів
Архів дописів
3 849
// You are using GCC
#include <iostream>
using namespace std;
int main () {
int i,n,iteam,j;
cin>>n;
int arr1[n];
for(i=0;i<n;i++) {
cin>>arr1[i];
}
for (i=0;i<n-1;i++) {
int min =i;
for(j=i+1;j<n;j++){
if(arr1[j]>arr1[min]) {
min=j;
}
}
if(min!=i) {
swap(arr1[i],arr1[min]);
}
}
cout<<"Sorted array: ";
for(i=0;i<n;i++) {
cout<<arr1[i]<<" ";
}
cout<<endl;
cout<<"The second largest element is ";
cout<<arr1[1];
//awasthi
}
3 849
// You are using GCC
#include <iostream>
using namespace std;
int main () {
int i,n,j,k,temp;
cin>>n;
int arr[n];
for(i=0;i<n;i++) {
cin>>arr[i];
}
cout<<"Original array:"<<endl;
for(i=0;i<n;i++) {
cout<<arr[i]<<" ";
}
cout<<endl;
for(i=0;i<n-1;i++) {
for(j=0;j<n-1;j++) {
if(arr[j]>arr[j+1]) {
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
cout<<"Iteration "<<i+1<<": ";
for(k=0;k<n;k++) {
cout<<arr[k]<<" ";
}
}
cout<<endl;
cout<<"Sorted array: ";
for(i=0;i<n;i++) {
cout<<arr[i]<<" ";
}
}
3 849
Input 1 :
5
89 72 95 68 82
Output 1 :
Original array:
89 72 95 68 82
Iteration 1: 72 89 68 82 95
Iteration 2: 72 68 82 89 95
Iteration 3: 68 72 82 89 95
Iteration 4: 68 72 82 89 95
Sorted array: 68 72 82 89 95
3 849
// You are using GCC
#include <iostream>
using namespace std;
int main () {
int n,i,data;
cin>>n;
int arr[n];
for(i=0;i<n;i++) {
cin>>arr[i];
}
cin>>data;
for (i=0;i<n;i++) {
if(arr[i]==data) {
cout<<"Target found at index: "<<i;
break;
}
}
if (arr[i]!=data) {
cout<<"-1";
}
}
3 849
I have uploaded answers of 3-4 different sets here only. Plz do not ask any code personally.
3 849
// You are using GCC
#include <iostream>
#include <vector>
long long mergeAndCount(std::vector<int>& arr, int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;
std::vector<int> leftArr(n1);
std::vector<int> rightArr(n2);
for (int i = 0; i < n1; ++i) {
leftArr[i] = arr[left + i];
}
for (int j = 0; j < n2; ++j) {
rightArr[j] = arr[mid + 1 + j];
}
long long invCount = 0;
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++];
invCount += n1 - i;
}
}
while (i < n1) {
arr[k++] = leftArr[i++];
}
while (j < n2) {
arr[k++] = rightArr[j++];
}
return invCount;
}
long long mergeSortAndCount(std::vector<int>& arr, int left, int right) {
long long invCount = 0;
if (left < right) {
int mid = left + (right - left) / 2;
invCount += mergeSortAndCount(arr, left, mid);
invCount += mergeSortAndCount(arr, mid + 1, right);
invCount += mergeAndCount(arr, left, mid, right);
}
return invCount;
}
int main() {
int n;
std::cin >> n;
std::vector<int> arr(n);
for (int i = 0; i < n; ++i) {
std::cin >> arr[i];
}
long long inversionCount = mergeSortAndCount(arr, 0, n - 1);
// Print the inversion count
std::cout << inversionCount << std::endl;
return 0;
}
//awasthi
3 849
// You are using GCC
#include <iostream>
int main() {
int n;
std::cin >> n;
int arr[n];
for (int i = 0; i < n; ++i) {
std::cin >> arr[i];
}
int target;
std::cin >> target;
int index = -1;
for (int i = 0; i < n; ++i) {
if (arr[i] == target) {
index = i;
break;
}
}
if (index != -1) {
std::cout << "Element " << target << " is present at index " << index << std::endl;
} else {
std::cout << "Element " << target << " is not present" << std::endl;
}
return 0;
}
3 849
5
40 20 50 30 10
80
In a large online retail platform, customers often face difficulties in locating specific products within the
extensive catalog. The current search feature primarily relies on a linear search algorithm, resulting in slow
response times and suboptimal user experience. As a result, customers become frustrated when trying to
find specific items, leading to decreased conversion rates and customer satisfaction.
The organization seeks a solution to improve the efficiency and speed of the linear search algorithm, ensuring
that customers can quickly locate their desired products with minimal effort. The goal is to enhance the
overall usability of the platform, increase customer engagement, and boost sales by optimizing the linear
search algorithm for large-scale product searches.
3 849
#include <iostream>
#include <vector>
int main() {
int n, m;
std::cin >> n;
std::vector<int> arrA(n);
for (int i = 0; i < n; ++i) {
std::cin >> arrA[i];
}
std::cin >> m;
std::vector<int> arrB(m);
for (int i = 0; i < m; ++i) {
std::cin >> arrB[i];
}
std::vector<int> mergedArray;
// Merge arrays while maintaining the sorting order
int i = 0, j = 0;
while (i < n && j < m) {
if (arrA[i] <= arrB[j]) {
mergedArray.push_back(arrA[i]);
++i;
} else {
mergedArray.push_back(arrB[j]);
++j;
}
}
while (i < n) {
mergedArray.push_back(arrA[i]);
++i;
}
while (j < m) {
mergedArray.push_back(arrB[j]);
++j;
}
// Output the merged sorted array
for (int k = 0; k < mergedArray.size(); ++k) {
std::cout << mergedArray[k];
if (k != mergedArray.size() - 1) {
std::cout << " ";
}
}
return 0;
}
3 849
3
1 2 3
2
4 5
You are developing a program that merges two sorted arrays of student IDs. The first array contains the
student IDs of students who have successfully completed Course A, while the second array contains the
student IDs of students who have successfully completed Course B.
Your task is to merge these two arrays into a single sorted array that will be used for further analysis.
3 849
#include <iostream>
#include <vector>
int main() {
int n;
std::cin >> n;
std::vector<int> arrA(n);
for (int i = 0; i < n; ++i) {
std::cin >> arrA[i];
}
int m;
std::cin >> m;
std::vector<int> arrB(m);
for (int i = 0; i < m; ++i) {
std::cin >> arrB[i];
}
// Output the second array (arrB) followed by the first array (arrA)
for (int i = 0; i < m; ++i) {
std::cout << arrB[i] << " ";
}
for (int i = 0; i < n; ++i) {
std::cout << arrA[i];
if (i != n - 1) {
std::cout << " ";
}
}
return 0;
}
3 849
3
1 2 3
2
4 5
You are developing a program that merges two sorted arrays of student IDs.
The first array contains the student IDs of students who have successfully completed Course A, while the
second array contains the student IDs of students who have successfully completed Course B. Your task is to
merge these two arrays into a single array that will be used for further analysis
3 849
// You are using GCC
#include <iostream>
#include <string>
// Function to check if a word is present in a sentence
//awasthi
bool isWordPresent(const std::string& sentence, const std::string& targetWord) {
size_t found = sentence.find(targetWord); // Search for the target word in the sentence
if (found != std::string::npos) {
return true; // Target word found in the sentence
} else {
return false; // Target word not found in the sentence
}
}
int main() {
std::string sentence;
std::string targetWord;
std::getline(std::cin, sentence);
std::cin >> targetWord;
if (isWordPresent(sentence, targetWord)) {
std::cout << "The word \'" << targetWord << "\' is present in the given sentence." << std::endl;
} else {
std::cout << "The word \'" << targetWord << "\' is not present in the given sentence." << std::endl;
}
return 0;
}
