Posts

Reverse Array In optimal time

 Hello Everyone Hopes all will be fine Welcome to my another blog. In this blog We are learning that how to reverse an array in optimal time. CODE IS HERE... #include <iostream> #include<vector> using namespace std; vector<int> reverse( vector <int> v ){     int s =0;     int e =v.size()-1;     while(s<=e){          swap(v[s], v[e]);         s++;         e--;     }     return v; } void print(vector<int> v){     for(int i=0;i<v.size();i++){         cout<<v[i]<<" ";     }     cout<<endl; } int main() {      vector<int> v;     v.push_back(41);     v.push_back(7);     v.push_back(23);     v.push_back(3);     v.push_back(4);      vector<int> ans = reverse(v);      print(ans); } KEEP PRACTICING || HAPPY CODING ........

Standard Templete Library in CPP

Hello everyone  Welcome to our another blog. In this blog i code for some Standard Templete Library(STL) which will help you to coding practice. All the best !! Keep Practicing!! #include <iostream> #include<algorithm> #include<vector> using namespace std; int main() {      vector<int> v; //Defining vector v      v.push_back(1);     v.push_back(5); //Inserting element in vector     v.push_back(8);     v.push_back(9);     cout<<"Finding 5  -> "<< binary_search(v.begin(), v.end(), 5) <<endl; //Applyin binary search for 5     cout<<"lower bound -> "<< lower_bound(v.begin(),v.end(),5)-v.begin() <<endl;      //finding lower bond for 5 cout<<"uper bound -> "<< upper_bound(v.begin(), v.end(),5)-v.begin() <<endl; //finding upper bond for 5     int a =4;     int b = 8;     cout<<&q

Allocate Book Problem Solution || Code Studio

Image
ALLOCATE BOOK PROBLEM SOLUTION PROBLEM STATEMENT:- Recommendation: First you have to try then go for solution. For practice  Click here  . Given an array ‘arr’ of integer numbers . where ‘arr[i]’ represents the number of pages in the ‘i-th’ book. There are ‘m’ number of students and the task is to allocate all the books to their students. Allocate books in such a way that: 1. Each student gets at least one book. 2. Each book should be allocated to a student. 3. Book allocation should be in a contiguous manner. You have to allocate the book to ‘m’ students such that the maximum number of pages assigned to a student is minimum. Example: Let’s consider ‘n=4’ (number of books ) and ‘m=2’ (number of students). ‘arr = { 10, 20, 30, 40 }’. All possible way to allocate the ‘4’ books in ‘2’ number of students is - 10 | 20, 30, 40 - sum of all the pages of books which allocated to student-1 is ‘10’, and student-2 is ‘20+ 30+ 40 = 90’ so maximum is ‘max(10, 90)= 90’. 10, 20 | 30, 40 - sum of all

Peak Index in a Mountain Array || Binary Search practice example || Leet Code poblem #852

 || Peak Index in a Mountain Array  || Binary Search practice example  || Leet Code poblem #852 Given an integer array arr we have to return the index of peak element.   Array first increase and then decrease.  Format  a[1] < a[2] <  a[3] <........a[n].......> a[2]  > a[1]  We have to return the index of a[n] Input: arr = [0,1,0] Output: 1 Input: arr = [0,2,1,0] Output: 1 Input: arr = [0,10,5,2] Output: 1 ***************CODE**************** //TIME COMPLEXITY IS O(LOG N)  int peakIndexInMountainArray(vector<int>& arr) {         int s=0;         int e=arr.size()-1;         int mid= s + (e-s)/2;         while (s<e){                          if(arr[mid] < arr[mid+1]){                 s = mid +1;             }             else                 e= mid;                          mid= s + (e-s)/2;         }         return s;     } 

First and Last position of an element in sorted array | Binary Search Practice Example | Code Studio Interview Question

 Here I am writting only function solution not main part... ||It is based on Binary Search Example Problem is there is given an array and you have to find a particular key of first and last index of that arry. Example:- Input format 2     // number of test Cases 6  3        // size of array      &&   the key element which have to find there first and last index in array 0  5  5  6  6  6 8  2 0  0  1  1  2  2  2  2   Output -1   -1     ///output is -1 because 3 is not present in the array 4    7    /// output is 4(first occurance) and 7 ( the last Occurance) ***************************************CODE************************************** int firstocc(vector<int>& arr, int n, int key){     int s=0;     int e=n-1;     int mid = s+(e-s)/2;     int ans =-1;     while(s<=e){         if(arr[mid] == key){             ans = mid;             e = mid -1;                     }         else if(key> arr[mid]){   //right me jana hai             s = mid +1;         }         e

Bitwise operator in c program. Hacker rank problem.

  #include   < stdio.h > #include <string.h> #include <math.h> #include <stdlib.h> //Complete the following function. void calculate_the_maximum(int n, int k) {     int maxand =0;     int maxor =0;     int maxxor =0;     for(int i=1; i<=n; i++){         for (int j= i+1; j<=n; j++){             if(((i & j)> maxand) && ((i&j)<k)){                 maxand= i&j;             }                         if(((i | j)> maxor) && ((i|j)<k)){                 maxor= i|j;             }                         if(((i ^ j)> maxxor) && ((i^j)<k)){                 maxxor= i^j;             }         }     }     printf("%d\n%d\n%d", maxand,maxor,maxxor); } int main() {     int n, k;        scanf("%d %d", &n, &k);     calculate_the_maximum(n, k);       return 0; }

For Loop Problem Solution in Hacker rank

  int   main ()   { int   a , b ;      int   n = 0 ;      string   intMap [ 9 ]=   { "one" ,   "two" , "three" , "four" , "five" , "six" , "seven" , "eight" , "nine" };           cin >> a >> b ;           if   (( a <= 9 )&&( b <= 9 )){           for ( n = a ; n <= b ; n ++){              cout   <<   intMap [ n - 1 ]<< endl ;            }   }      else   if   (( a <= 9 )&&( b > 9 )){               for ( n = a ; n <= 9 ; n ++){              cout << intMap [ n - 1 ]<< endl ;          }               for ( n = 10 ; n <= b ; n ++){                   if   ( n % 2 == 0 ){                  cout << "even" << endl ;              }               else   {                   cout << "odd" << endl ;               }           }    }           else   {          for ( n = a ; n <= b ; n ++){