Skip to main content

Posts

Showing posts from 2021

Samsung R&D Coding Question

 Q). => Coding: #include<iostream> using namespace std; int subsetSum(int a[],int len){ int sum=0,maxSum=0; for(int i=1;i<=len;i++){     for(int j=0;j<=len-i;j++){        for(int k=0;k<i;k++){          sum=sum+a[j+k];        }         if(sum>maxSum){           maxSum=sum;         }         sum=0;      } } return maxSum; } int main(){ int n; cout<<"Enter the size of the array\n"; cin>>n; int a[n]; cout<<"Enter Array size : \n"; for(int i=0;i<n;i++){     cin>>a[i]; } int r=subsetSum(a,n); cout<<"result "<<r<<endl; } => Output:

Blume Global Inc Coding Question in c++

 =>Bitwise Queries => Coding  #include<iostream> #include<vector> using namespace std; vector<int> myQuery(int N,vector<int> A,vector<vector<int>> queries){ vector<int> my; int fVal,X,Y,counter=0; for(int i=0;i<queries.size();i++){           X=queries[i][0];           for(int k=0;k<N;k++){             fVal=2*(A[k]|X)-(A[k]^X);             Y=queries[i][1];             if(fVal>=Y){                 counter++;             }           }           my.push_back(counter);           counter=0; } cout<<"\n**Result***\n\n"; return my; } int main(){     int n,q,val,qVal;     cout<<"Enter the size of the array :";     cin>>n;     vector<int> arr;     for(int i=0;i<n;i++){         cin>>val;         arr.push_back(val);     }     cout<<"Enter the size of the query :";     cin>>q;     vector<vector<int>> query;     for(int i=0;i<q;i++){         vector<int

Two Sum Problem

  Question 1 Given an array of integers   nums  and an integer   target , return   indices of the two numbers such that they add up to  target . You may assume that each input would have  exactly  one solution , and you may not use the  same  element twice. You can return the answer in any order.   Example 1: Input: nums = [2,7,11,15], target = 9 Output: [0,1] Output: Because nums[0] + nums[1] == 9, we return [0, 1]. Example 2: Input: nums = [3,2,4], target = 6 Output: [1,2] Example 3: Input: nums = [3,3], target = 6 Output: [0,1]   Constraints: 2 <= nums.length <= 10 4 -10 9  <= nums[i] <= 10 9 -10 9  <= target <= 10 9 Only one valid answer exists.   Follow-up:  Can you come up with an algorithm that is less than  O(n 2 )  time complexity? ******************************** Time Complexity (O(n^2)) ******************************** => Coding  vector<int> twoSum(vector<int>& nums, int target) {               vector<int> twoSum;           

Capgemini Coding Q1

 Q). => Solution : #include<iostream> #include<vector> #include<algorithm> using namespace std; vector<int> removeDupMerge(int a1[],int a2[],int m,int n){ vector<int> mVec; for(int i=0;i<m-1;i++){      if(a1[i]!=a1[i+1])         mVec.push_back(a1[i]); } mVec.push_back(a1[m-1]); for(int i=0;i<n-1;i++){      if(a2[i]!=a2[i+1])         mVec.push_back(a2[i]); } mVec.push_back(a2[n-1]); sort(mVec.begin(),mVec.end()); return mVec; } int main(){ int m,n; vector<int> myVec; cin>>m; int a1[m]; for(int i=0;i<m;i++)     cin>>a1[i]; cin>>n; int a2[n]; for(int i=0;i<n;i++)     cin>>a2[i]; sort(a1,a1+m); sort(a2,a2+n); cout<<"\n\n*******| OUTPUT |*******\n\n"; myVec=removeDupMerge(a1,a2,m,n); //find median int median,sVec=myVec.size(); if(sVec%2==0){     median=(sVec-1)/2 + sVec/2; }else{     median=(sVec-1)/2; } cout<<myVec[median]<<endl; } =>Output :

New Year Chaos(HackerRank Question)

 Question) *********************************************************************************** =>Solution :

Hexadecimal Number To Decimal Number Using C++ ( With Condition Check)

 => Program : #include<iostream> #include<math.h> using namespace std; string low2up(string passStr){ string changeStr=passStr; for(int i=0;i<changeStr.size();i++){      if(passStr[i]>=97 && passStr[i]<=122)       { changeStr[i]=changeStr[i]-32;       } } return changeStr; } bool validHexVal(string checkSr){ bool checkValid; int countNum=0; for(int i=0;i<checkSr.size();i++){      if(checkSr[i]>=65 && checkSr[i]<=70)       { countNum++;       }       if(checkSr[i]>=48 && checkSr[i]<=57){         countNum++;       } } if(checkSr.size()==countNum){     return true; }else{     return false; } } int main(){ int decNum=0,val; string hexNum,upperHexNum; cout<<"Enter the hex decimal number "<<endl; cin>>hexNum; upperHexNum=low2up(hexNum); int n=upperHexNum.size(); if(validHexVal(upperHexNum)){  for(int i=n-1;i>=0;i--){     if(upperHexNum[i]>=65 && upperHexNum[i]<=70)       { val

Decimal Number To Hexadecimal Number Using C++

  => Program : #include<iostream> #include<math.h> using namespace std; int main(){ int decNum,rem,i=0; cout<<"Enter the Decimal Number : "<<endl; cin>>decNum; char hexVal[100]; while(decNum){    rem=decNum%16;    if(rem<10){      hexVal[i++] = 48 + rem;    }else{      hexVal[i++] = 55 + rem;    }   decNum=decNum/16; } cout<<"HEXA DECIMAL NUMBER : "; for(int j=i-1;j>=0;j--){     cout<<hexVal[j]; } cout<<endl; } => Output :

Count Frequency of Character in Any Sentence using C++

 Q)Write the C++ program for character frequency count for any sentence => Program : #include<iostream> #include<string> #include<iomanip> using namespace std; int main(){ string mystr; cout<<"Enter the Any Sentence  for character frequency count \n"; getline(cin,mystr); int charFreq[26]={0}; for(int i=0;i<mystr.length();i++){     int index=mystr[i]-'a';     charFreq[index]++; } cout<<"\n****************************************\n"; cout<<"\nSentence : "<<mystr<<endl<<endl; cout<<setw(12)<<"Character"<<setw(12)<<"Frequency"<<endl<<endl; for(int i=0;i<26;i++){     if(charFreq[i]!=0){         cout<<setw(6)<<char(i+'a')<<setw(10)<<charFreq[i]<<" times \n";     } } } =>Output :

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

Queue using C++(Struct)

 Q). Implement Queue using struct in c++ => Prog : #include<iostream> #include<iomanip> using namespace std; #define MAX 5 typedef struct queue_type{   int arr[MAX]={0,0,0,0,0};   int frontSide;   int rearSide; }node; void insertVal(int item,node * ptr); int deleteVal(node *ptr); void display(node *ptr); int main(){ char ch; int choice,item; node queueNode; queueNode.frontSide=-1; queueNode.rearSide=-1; do{     cout<<"\n************* please press below key for that option *************\n\n";     cout<<"1.Insert Value in Queue \n";     cout<<"2.Delete Value in Queue \n";     cout<<"3.Display \n";     cout<<"choice : ";     cin>>choice;     switch(choice){      case 1: cout<<"Enter the data : ";              cin>>item;              insertVal(item,&queueNode);              break;      case 2: item=deleteVal(&queueNode);              cout<<"Pop eleme

Stack using C++(Struct)

 Q). Implement Stack push and pop function using struct in C++ =>Program : #include<iostream> #include<iomanip> using namespace std; #define MAX 5 typedef struct stack_node{   int arr[MAX];   int top; }node; void push(int item,node *ptr); int pop(node *ptr); void display(node *ptr); int main(){ int item,choice; char ch; node myStack; myStack.top=-1; do{     cout<<"\n************* please press below key for that option *************\n\n";     cout<<"1.Push Value in stack \n";     cout<<"2.Pop Value in stack \n";     cout<<"3.Display \n";     cin>>choice;     switch(choice){      case 1: cout<<"Enter the data : ";              cin>>item;              push(item,&myStack);              break;      case 2: item=pop(&myStack);              cout<<"Pop element : "<<item<<endl;              break;       case 3: display(&myStack);               break;

Direct Method of Mean (C++ Program)

 Q). Write the C++ Program for direct method of Mean => Prog : #include<iostream> #include <iomanip> using namespace std; int main(){ int n; double sumF=0,sumFX=0; cout<<"Enter the number input  : "; cin>>n; double number[n]; double freq[n]; for(int i=0; i<n; i++){     cout<<"Number["<<i+1<<"] : ";     cin>>number[i];     cout<<"frequency["<<i+1<<"] : ";     cin>>freq[i]; } cout<<"\n\n"<<setw(20)<<"Number"<<setw(10)<<"|"<<setw(20)<<"Frequency"<<endl<<endl; for(int i=0; i<n; i++){     cout<<setw(20)<<number[i]<<setw(10)<<"|"<<setw(20)<<freq[i]<<endl;;     sumF=sumF+freq[i]; } cout<<"\n****************      After Calculation      ********************\n"; cout<<"\n\n"<<setw(20)&l

Arrays: Left Rotation

  A   left rotation   operation on an array shifts each of the array's elements     unit to the left. For example, if     left rotations are performed on array   , then the array would become   . Note that the lowest index item moves to the highest index in a rotation. This is called a   circular array . Given an array   of   integers and a number,  , perform   left rotations on the array. Return the updated array to be printed as a single line of space-separated integers. Function Description Complete the function  rotLeft  in the editor below. rotLeft has the following parameter(s): int a[n]:  the array to rotate int d:  the number of rotations Returns int a'[n]:  the rotated array Input Format The first line contains two space-separated integers   and  , the size of   and the number of left rotations. The second line contains   space-separated integers, each an  . Constraints Sample Input 5 4 1 2 3 4 5 Sample Output 5 1 2 3 4 Explanation When we perform   left rotations, the