Skip to main content

Posts

Showing posts from July, 2021

Median of Two Sorted Arrays in C++ (LeetCode)

 Q). Find Median using class and STL in C++ => Solution : #include<iostream> #include<vector> #include <algorithm> using namespace std; class Solution { public:     double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {         double mediumResult;         int vectSize;         nums1.insert(nums1.end(), nums2.begin(), nums2.end());         sort(nums1.begin(),nums1.end());         vectSize=nums1.size();             if(vectSize%2==0){          mediumResult=double((nums1[vectSize/2-1]+nums1[vectSize/2]))/2;         }else{           mediumResult=(nums1[vectSize/2]);         }         return mediumResult;     } }; int main(){ vector<int> vect1 = {1,2}; vector<int> vect2 = {3,4}; double medium; Solution sol; medium=sol.findMedianSortedArrays(vect1,vect2); cout<<"Medium : "<<medium<<endl; }

Pattern 6 C++ Program

 Q) Print K letter Pattern using C++ Program => Program : #include<iostream> using namespace std; int main(){ int n; cin>>n; cout<<"\n\n*****************************\n\n\n"; for(int i=1; i<=n;i++){     cout<<"* ";     for(int j=i; j<=n-1; j++){         if(j==n-1){             cout<<"* ";         }else{             cout<<"  ";         }     }     cout<<endl; } for(int i=1; i<=n-1;i++){     cout<<"* ";     for(int j=2; j<=i; j++){       cout<<"  ";     }     cout<<"* ";     cout<<endl; } }

Pattern 5 C++ Program

 Q). Write Program for the below pattern? => Program: #include<iostream> using namespace std; int main(){ int n; cout<<"Enter the number : "; cin>>n; for(int i=1;i<=n;i++){     for(int j=1; j<=n; j++){         if(i==1 || i==n){            cout<<"* ";         }else{             if(j==1 || j==n){                  cout<<"* ";             }else{                  cout<<"  ";             }         }     }     cout<<endl; } }

Maximum possible number of monsters you can defeat?

 Q).Maximum possible number of monsters you can defeat?  =>Introduction: While playing an RPG game, you were assigned to complete one of the hardest quests in this game. There are n monsters you'll need to defeat in this quest. Each monster i is described with two integer numbers - poweri and bonusi. To defeat this monster, you'll need at least poweri experience points. If you try fighting this monster without having enough experience points, you lose immediately. You will also gain bonusi experience points if you defeat this monster. You can defeat monsters in any order. The quest turned out to be very hard - you try to defeat the monsters but keep losing repeatedly. Your friend told you that this quest is impossible to complete. Knowing that, you're interested, what is the maximum possible number of monsters you can defeat? (Question difficulty level: Hardest) => Input: The first line contains an integer, n, denoting the number of monsters. The next line contains an