LeetCode, GeeksForGeeks Problem of the day solution
Kanalga Telegram’da o‘tish
Complete daily challenges from LeetCode, GeeksForGeeks and redeem their rewards Channel link : https://t.me/leetcode_gfg_potd
Ko'proq ko'rsatish1 250
Obunachilar
+224 soatlar
+147 kunlar
+2930 kunlar
Postlar arxiv
class Solution {
public:
int findSwapValues(int a[], int n, int b[], int m) {
// Your code goes here
int sumA=0,sumB=0;
unordered_map<int,int>mp;
for(int i=0;i<n;i++){
sumA+=a[i];
mp[a[i]]++;
}
for(int j=0;j<m;j++){
sumB+=b[j];
}
if(sumA==sumB) return 1;
if((sumA+sumB)&1) return -1;
int diff=max(sumA,sumB)-(sumA+sumB)/2;
for(int j=0;j<m;j++){
if(sumA>sumB){
if(mp.find(diff+b[j])!=mp.end()) return 1;
}
else if(sumA<sumB){
if(mp.find(b[j]-diff)!=mp.end()) return 1;
}
}
return -1;
}
};
class Solution {
public:
int longestPalindrome(string s) {
unordered_map mp;
for(auto c:s){
mp[c]++;
}
int count=0;
bool oddExists = false;
for(auto i:mp){
if(i.second %2==0){
count += i.second;
}
else{
count += i.second-1;
oddExists = true;
}
}
if(oddExists==true){
count++;
}
return count;
}
};
class Solution {
public:
string binaryNextNumber(string s) {
int n = s.size();
int carry = 1;
for(int i = n - 1; i >= 0; i--)
{
if(s[i] == '0' && carry == 1)
{
s[i] = '1';
carry = 0;
}
else if(s[i] == '1')
{
if(carry == 1)
{
s[i] = '0';
}
else s[i] = '1';
}
}
int i = 0;
for(i = 0; i < n; i++)
{
if(s[i] == '1') break;
}
if(i != n)
s = s.substr(i);
if(carry == 1)
s = "1" + s;
return s;
}
};
class Solution {
public:
int appendCharacters(string s, string t) {
int n = s.size(), m = t.size();
int j=0, i=0;
while(i=n) {
return m-i;
}
if(s[j]==t[i]) i++, j++;
else {
j++;
}
}
return 0;
}
};
class Solution {
public:
int mod = 1e9 + 7;
public:
int numberOfConsecutiveOnes(int n) {
vector count1({1,0,1,0}), count2(4);
for(int i = 1; i < n; i++) {
count2[0] = (count1[0] + count1[2]) % mod;
count2[1] = (count1[1] + count1[3]) % mod;
count2[2] = count1[0];
count2[3] = (count1[1] + count1[2] + count1[3]) % mod;
count1 = count2;
}
return (count1[1] + count1[3]) % mod;
}
};
class Solution {
public:
void reverseString(vector& s) {
int start = 0;
int end = s.size()-1;
while(start< end){
char ch = s[start];
s[start] = s[end];
s[end] = ch;
start++;
end--;
}
}
};
class Solution {
public:
vector constructList( int N,vector> Q) {
vector v;
int x=0;
for(int i=N-1;i>=0;i--)
{
if(Q[i][0]==0)
v.push_back(Q[i][1]^x);
else
x^=Q[i][1];
}
v.push_back(0^x);
sort(v.begin(),v.end());
return v;
}
};
class Solution {
public:
int scoreOfString(string s) {
map mp;
int ans=0;
for(auto str: s)
{
mp[str]=int(str);
}
for(int i=0;i
class Solution {
public:
string oddEven(string s) {
int x = 0,y = 0;
unordered_map mp;
for(int i=0;i
class Solution {
public:
vector singleNumber(vector& nums) {
long long xored=0;
for(auto &i:nums){
xored^=i;
}
long long rightSetBit=(xored&(xored-1))^xored;
int a=0,b=0;
for(auto &i:nums){
if(rightSetBit&i) a^=i;
else b^=i;
}
return {a,b};
}
};
