0% found this document useful (0 votes)
6 views11 pages

TCS Dsa

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views11 pages

TCS Dsa

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Prime numbers with from 2 to limit

// Online C++ compiler to run C++ program online

#include <iostream>

#include<bits/stdc++.h>

using namespace std;

bool isp(int num){

for(int i=2;i*i<=num;i++){

if(num%i==0){

return false;
int main() {
}
int n=5;
}
int a[n]={2,5,1,3,19};
return true;
int mini=INT_MAX;
}
for(int i=0;i<n;i++){
int main() {
if(a[i]<mini){
int limit=100;
mini=a[i];
for(int i=2;i<limit;i++){
}
if(isp(i)){
}

cout<<mini;
cout<<i<<" ";
}
}

}
Largest element:

int main() {
return 0;
int n=5;
}
int a[n]={2,5,1,3,19};

int maxi=INT_MIN;

for(int i=0;i<n;i++){

if(a[i]>maxi){

maxi=a[i];

}
cout<<maxi; smaxi=a[i];

return 0; }

} }

cout<<smaxi<<endl<<smini;

Second smallest and largest without duplicates return 0;

In an array }

int main() {

int n=6;

int a[n]={1,2,4,7,8,5};

sort(a,a+n);

cout<<a[1]<<endl<<a[n-2];

return 0;

Second smallest and second largest int main() {


int main() { int n=6;
int n=6; int a[n]={10,5,10,15,10,5};
int a[n]={1,2,4,7,8,5}; map<int,int>mp;
int fmini=INT_MAX,smini=INT_MAX; for(int i=0;i<n;i++){
int fmaxi=INT_MIN,smaxi=INT_MIN; mp[a[i]]++;
for(int i=0;i<n;i++){ }
fmini=min(fmini,a[i]); for(auto it:mp){
fmaxi=max(fmaxi,a[i]); cout<<it.first<<" :"<<it.second<<endl;
} }

return 0;
for(int i=0;i<n;i++){ }
if(a[i]<smini && a[i]!=fmini){

smini=a[i];

if(a[i]>smaxi && a[i]!=fmaxi){


#include <iostream>

#include<bits/stdc++.h>

using namespace std;

int main() {

int n=7;
int n=6;
int a[n]={1,2,3,4,5,6,7};
int a[n]={8,7,1,6,5,9};
// int b[n];
sort(a,a+n);
int k=3;
for(int i=0;i<n/2;i++){
k=k%n;
cout<<a[i]<<" ";
reverse(a, a + n);
}

// reverse(a+n/2,a+n);
// Step 2: Reverse the first k elements
for(int i=n-1;i>=n/2;i--){
reverse(a, a + k);
cout<<a[i]<<" ";

}
// Step 3: Reverse the remaining n-k elements
}
reverse(a + k, a + n);

Sum of elements in an array


for(int i=0;i<n;i++){
int main() {
cout<<a[i]<<" ";
int n=5;
}
int a[n]={1,2,3,4,5};

int sum=0;
return 0;
for(int i=0;i<n;i++){
avg of an array
sum+=a[i];

cout<<sum;
int main() {

int n=6;

int a[n]={1,2,1,1,5,1};

float sum=0;

for(int i=0;i<n;i++){

sum+=a[i];

cout<<setprecision(2)<<endl<<sum/n;

return 0;

Median

int main() { int removeDuplicates(vector<int>& nums) {


int n=4; int j = 1;
int a[n]={2,5,1,7}; for(int i = 1; i < nums.size(); i++){
sort(a,a+n); if(nums[i] != nums[i - 1]){
if(n%2!=0) nums[j] = nums[i];
cout<<a[n/2]; j++;
else{ }
int i=(n/2)-1; }
int j=(n/2); return j;
double sum=(a[i]+a[j])/2.0; }

cout<<sum;

return 0;

void duplicate(int arr[], int n) {


map<int, int>mp ; a[n]=value;

for (int i = 0; i < n; i++) { }

if (mp.find(arr[i]) == mp.end()) { void insertatpos(int a[],int& n,int value,int pos){

cout << arr[i] << " "; for(int i=n;i>=pos;i--){

mp[arr[i]]++; a[i]=a[i-1];

} }

} a[pos-1]=value;

int main() {

int n=5;

int a[8]={1,2,3,4,5};

insertatbeg(a,n,0);

for(int i=0;i<=n;i++){

cout<<a[i]<<" ";

cout<<endl;

// Online C++ compiler to run C++ program online insertatend(a,n,7);

#include <iostream> for(int i=0;i<=n;i++){

#include<bits/stdc++.h> cout<<a[i]<<" ";

using namespace std; }

void insertatbeg(int a[],int &n,int value){ cout<<endl;

for(int i=n-1;i>=0;i--){ insertatpos(a,n,9,3);

a[i+1]=a[i]; for(int i=0;i<=n;i++){

} cout<<a[i]<<" ";

a[0]=value; }

} return 0;

void insertatend(int a[],int& n,int value){ }


int a[n]={1,1,2,3,4,4,5,2};

map<int,int>mp;

for(int i=0;i<n;i++){

mp[a[i]]++;

for(auto it:mp){

if(it.second<=1){

cout<<it.first<<" ";
int main() {
}
int n=8;
}
int a[n]={1,1,2,3,4,4,5,2};

map<int,int>mp;
return 0;s
for(int i=0;i<n;i++){

mp[a[i]]++;

for(auto it:mp){

if(it.second>1){

cout<<it.first<<" ";

int main() {

int n = 5;

int arr[][2] = {{1, 2}, {2, 1}, {3, 4}, {4, 5}, {5, 4}};

cout << "The Symmetric Pairs are: " << endl;

for (int i = 0; i < n; i++) {

for (int j = i + 1; j < n; j++) {

if (arr[j][0] == arr[i][1] && arr[j][1] == arr[i][0]) {

int main() { cout << "(" << arr[i][1] << " " << arr[i][0] << ")" << " ";

int n=8; break;


}

Approach 2:

int main()

{
int main() {
int n = 5;
int n=6;
int arr[5][2] = {{1, 2}, {2, 1}, {3, 4}, {4, 5}, {5, 4}};
int a[n]={1,2,-3,0,-4,-5};
unordered_map<int, int>mp;
int r=a[0];
cout<<"The Symmetric Pairs are: "<<endl;
for(int i=0;i<n-1;i++){
for (int i = 0; i < n; i++) {
int p=a[i];
int first = arr[i][0];
for(int j=i+1;j<n;j++){
int second = arr[i][1];
r=max(r,p);

p*=a[j];
//if {second,first} existed previously,print them.
}
if (mp.find(second) != mp.end() && mp[second]
r=max(r,p);
== first) {
}
cout << "(" << first << " " << second <<
")" << " "; cout<<r;
}

//else store them in the map return 0;


else { }
mp[first] = second;

} Approach 2:
} int maxProductSubArray(vector<int> &arr) {

int n = arr.size(); //size of array.

int pre = 1, suff = 1;

int ans = INT_MIN;


for (int i = 0; i < n; i++) {

if (pre == 0) pre = 1; }

if (suff == 0) suff = 1;

pre *= arr[i]; for(int i=0;i<n;i++){

suff *= arr[n - i - 1];

ans = max(ans, max(pre, suff)); cout<<mp[a[i]]<<" ";

} }

return ans;

int main() {

int n=8;
int n=6;
int a[n]={1,2,3,2,4,3,1,2};
int a[n]={20,15,26,2,98,6};
map<int,int>mp;
int b[n];
for(int i=0;i<n;i++){
for(int i=0;i<n;i++){
mp[a[i]]++;
b[i]=a[i];
}
}
while (!mp.empty()) {
sort(b,b+n);
auto maxIt = mp.begin();
map<int,int>mp;
for (auto it = mp.begin(); it != mp.end(); ++it) {
int temp=1;
if (it->second > maxIt->second) {
for(int i=0;i<n;i++){
maxIt = it;
if(mp[b[i]]==0){
}
mp[b[i]]=temp;
}
temp++;
for (int i = 0; i < maxIt->second; i++) {
}
cout << maxIt->first << " "; }

} return 0;

mp.erase(maxIt); }

}}

Approach 2:

#include <iostream>

#include <unordered_map>

using namespace std;

bool isSubset(int a[], int n, int b[], int m) {

// Create a frequency map for array b

unordered_map<int, int> freqMap;

int n=5; for (int i = 0; i < m; i++) {


int a[n]={1,3,4,5,2}; freqMap[b[i]]++;
int m=7; }
int b[m]={2,4,3,1,7,5,15};

sort(a,a+n); // Check each element in array a


sort(b,b+m); for (int i = 0; i < n; i++) {
int count=0; if (freqMap[a[i]] == 0) {
for(int i=0;i<n;i++){ // If the element in a is not found in b
if(a[i]==b[i]){ return false;

}
count++; // Decrease the count for the element
} freqMap[a[i]]--;
} }
cout<<count;

if(count==n){ // If all elements in a are found in b


cout<<"true"; return true;
}else{ }
cout<<"false";
int main() { ans=false;

int n = 5; }

int a[n] = {1, 3, 4, 5, 2}; }

int m = 7; if(ans){

int b[m] = {2, 4, 3, 1, 7, 5, 15}; cout<<b;

}else{

if (isSubset(a, n, b, m)) { cout<<"no";

cout << "Array A is a subset of Array B." << endl; }

} else { return 0;

cout << "Array A is not a subset of Array B." << }endl;

return 0;

int
findEquilibriumIdx(int nums[], int n) {

int totalSum = 0;

} for (int i = 0; i < n; i++) {

int n=5; totalSum += nums[i];

int a[n]={1,3,4,5,2}; }

int k=30; int leftSum = 0, rightSum = totalSum;

bool ans; for (int i = 0; i < n; i++) {

int b; rightSum -= nums[i];

for(int i=0;i<n;i++){ if (leftSum == rightSum) {

if(a[i]==k){ return i;

b=i; }

ans=true; leftSum += nums[i];

break; }

}else{ return -1;


}

You might also like