Skip to main content

Posts

Showing posts with the label Geeks for Geeks

GEEKS FOR GEEKS WEEKLY CODING CONTEST 103 SOLUTIONS

GEEKS FOR GEEKS WEEKLY CODING CONTEST 103 SOLUTIONS Refueling Code Answer C++ Refueling First GFG Weekly Coding Contest 103 class Solution{     public:     long long refueling(int X)     {         long long a, b;         for(int i=0; i<X; i++){             a = pow(2,i);             if(a == X)return a;             else if(a>X){                 b = pow(2,i-1);                 if(abs(X-a) == abs(X-b))return a;                 if(abs(X-a) < abs(X-b))return a;                 else return b;             }         }         return -1;     } }; Input: X = 3,  Output: 4 Input:  X = 2,  Output: 2 K-Periodic Circular String Code Solution  C++ K-Periodic Circular String Second GFG Weekly Coding Contest 103   class Solution{     public:     string kPeriodic(string s, int K){         // code here         map<char,int> hm;         for(int i=0;i<s.size();i++){           hm[s[i]]++;         }         int n= s.size();         int res= n/__gcd(n,K);              for(auto t:hm

Bus Conductor Coding Solution POTD Solution 21 May 2023

Bus Conductor  Coding Solution Geeks for Geeks Problem of the Day 21 May 2023 You are conductor of a bus ...  Return the minimum number of moves required GFG POTD Solution Today   Gfg problem of the day solution with explanation C++ class Solution {   public:     int findMoves(int n, vector<int> chairs, vector<int> passengers) {      sort(chairs.begin(), chairs.end());      sort(passengers.begin(), passengers.end());            int ans=0;      for(int i=0;i<n;i++){          ans=ans+abs(passengers[i]-chairs[i]);      }      return ans;     } }; Input:  chairs = [3,1,5]  passengers = [2,7,4]  Output: 4 Input:  chairs = [2,2,6,6]  passengers = [1,3,2,6]  Output: 4 GFG POTD Answer in C++   Gfg problem of the day in python  Gfg problem of the day in java

Hands of Straights Coding Solution POTD Solution 20 May 2023

Hands of Straights Coding Solution Geeks for Geeks Problem of the Day 20 May 2023 GFG POTD Solution Today  Gfg problem of the day solution with explanation C++ class Solution { public: bool isStraightHand(int n, int k, vector &v) { // k= group size if(n%k>0){ return false; } map mp; for(auto it:v){ mp[it]++; } // put all the size() with its frquency in the (min)p_queue priority_queue , vector >, greater > > pq; for(auto it:mp){ pq.push({it.first,it.second}); } int cnt=0; int last=-1; queue > q; while(pq.size()>0){ int t=pq.top().first; int val=pq.top().second; pq.pop(); // take top ele of the heap and check if the last element and // the top element are coincidnt if not return false // if yes then put the last ele to curr op eleme