Adaspam
Adaspam
Adaspam
#include <bits/stdc++.h>
using namespace std;
void sieve(int n)
{
vector<bool>isprime(n+1,true);
for(int i=2;i<=n;i++)
{
if(isprime[i])
{
cout<<i<<" ";
for(int j=i*i;j<=n;j+=i)
{
isprime[j]=false;
}
}
}
}
int main() {
int n;
cin>>n;
sieve(n);
return 0;
}
int main() {
int n;
cin >> n;
int a1[n][n], a2[n][n], f[n][n];
return 0;
}
//transpose 0(n^2)
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
int a1[n][n], f[n][n];
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
cin >> a1[i][j];
}
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
f[i][j]=a1[j][i];
}
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
cout<< a1[i][j]<<" ";
}
cout<<endl;
}
cout<<"transpose"<<endl;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
cout << f[i][j] << " ";
}
cout << endl;
}
return 0;
}
//bin and ter o(logn)2 and 3
#include <iostream>
using namespace std;
int main() {
int n, x;
cout << "Enter the size of the array: ";
cin >> n;
int a[n];
cout << "Enter the elements of the array in sorted order:" << endl;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
cout << "Enter the element to search: ";
cin >> x;
cout << "Binary Search: " << binsearch(a, x, n)+1 << endl;
cout << "Ternary Search: " << tersearch(a, x, n)+1 << endl;
return 0;
}
if (q == nullptr) {
k->next = r;
} else {
k->next = q;
}
return head;
}
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
head = mergeSort(head);
return 0;
}
int main() {
vector<int> arr = {10, 7, 8, 9, 1, 5};
int n = arr.size();
quickSort(arr, 0, n - 1);
cout << "Sorted array: \n";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}
//1st
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> arr = {10, 7, 8, 9, 1, 5};
int n = arr.size();
quickSort(arr, 0, n - 1);
cout << "Sorted array: \n";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}
//magic square
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
int a[n][n], f = 0;
int c[n] = {0}, r[n] = {0}, d1 = 0, d2 = 0; // Initialize arrays and variables
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> a[i][j];
if (i == j) // Calculate diagonal sums
d1 += a[i][j];
if (i + j == n - 1) // Calculate other diagonal sum
d2 += a[i][j];
c[i] += a[i][j]; // Calculate column sums
r[j] += a[i][j]; // Calculate row sums
}
}
struct Node {
int data;
Node* left;
Node* right;
int main() {
Node* root = nullptr;
int n;
cout << "Enter the number of nodes in the BST: ";
cin >> n;
cout << "Enter the nodes of the BST: ";
for (int i = 0; i < n; ++i) {
int data;
cin >> data;
root = insert(root, data);
}
return 0;
}
//union intersection
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int arr1[] = {1, 3, 4, 5, 7};
int arr2[] = {2, 3, 5, 6};
int m = sizeof(arr1) / sizeof(arr1[0]);
int n = sizeof(arr2) / sizeof(arr2[0]);
sort(arr1, arr1 + m);
sort(arr2, arr2 + n);
cout << "Union of sorted arrays: ";
findUnion(arr1, m, arr2, n);
cout << endl;
cout << "Intersection of sorted arrays: ";
findIntersection(arr1, m, arr2, n);
cout << endl;
return 0;
}
//merge
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the size of the array: ";
cin >> n;
int a1[n];
cout << "Enter the elements of the array: ";
for (int i = 0; i < n; i++) {
cin >> a1[i];
}
mergeSort(a1, 0, n - 1); // Sort the array
cout << "Sorted array: ";
for (int i = 0; i < n; i++) {
cout << a1[i] << " ";
}
cout << endl;
return 0;
}
//quick sort
#include <iostream>
using namespace std;
}
}
int main() {
int n;
cout << "Enter the size of the array: ";
cin >> n;
int a1[n];
cout << "Enter the elements of the array: ";
for (int i = 0; i < n; i++) {
cin >> a1[i];
}
quicksort(a1, 0, n - 1); // Sort the array
cout << "Sorted array: ";
for (int i = 0; i < n; i++) {
cout << a1[i] << " ";
}
cout << endl;
return 0;
}
//3 median of 3
#include <iostream>
using namespace std;
int medianof3(int a[], int s, int e) {
int mid = (s + e) / 3;
int a1 = a[s], a2 = a[mid], a3 = a[e];
if ((a1 > a2 && a2 > a3) || (a1 < a2 && a2 < a3)) {
return mid;
} else if (a1 < a2 && a2 > a3) {
return s;
} else {
return e;
}
}
int main() {
int n;
cout << "Enter the size of the array: ";
cin >> n;
int a1[n];
cout << "Enter the elements of the array: ";
for (int i = 0; i < n; i++) {
cin >> a1[i];
}
quicksort(a1, 0, n - 1); // Sort the array
cout << "Sorted array: ";
for (int i = 0; i < n; i++) {
cout << a1[i] << " ";
}
cout << endl;
return 0;
}
int main() {
int n, k;
cout << "Enter the size of the array: ";
cin >> n;
int arr[n];
cout << "Enter the elements of the array: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
cout << "Enter the value of k for switching to insertion sort: ";
cin >> k;
quickSort(arr, 0, n - 1, k);
cout << "Sorted array: ";
printArray(arr, n);
return 0;
}
void findMin(int V)
{
sort(denomination, denomination + n);
// Initialize result
vector<int> ans;
// Find denominations
V -= denomination[i];
ans.push_back(denomination[i]);
// Print result
// Driver code
int main()
{
// No of files
int n = 6;
// Total no of computations
// do be done final answer
cout << "Minimum Computations = "
<< minComputation(n, files);
return 0;
}
Time 0nlogn