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
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){
if(t.second % res)
return "-1";
}
string p="";
for(auto t:hm){
int pre=t.second/res;
while(pre--)
p+=t.first;
}
string ans="";
while(res--)
ans+=p;
return ans;
}
};
Input:
s = "abba"
K = 2
Output:
abab
Input:
s = "abbbbbb"
K = 4
Output:
-1
Comments
Post a Comment