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:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode *fast = head, *slow = head;
while(n--) fast = fast -> next;
if(!fast) return head -> next;
while(fast -> next)
fast = fast -> next, slow = slow -> next;
slow -> next = slow -> next -> next;
return head;
}
};
class Solution{
public:
// The main function that returns the arrangement with the largest value as
// string.
// The function accepts a vector of strings
static bool cmp(string a,string b){
return a+b>b+a;
}
string printLargest(int n, vector &arr) {
// code here
sort(arr.begin(),arr.end(),cmp);
string ans = "";
for(int i = 0;i
class Solution {
public:
vector sortedSquares(vector& nums) {
for(auto &x : nums){
x = x*x;
}
sort(nums.begin(),nums.end());
return nums;
}
};
class Solution{
public:
int firstElementKTime(int n, int k, int a[])
{
std::unordered_map map;
for(int i=0; i
class Solution {
public:
string maximumOddBinaryNumber(string s) {
int i=s.length()-1;
while(s[i]!='1')
{
i--;
}
swap(s[i],s[s.length()-1]);
int l=0,r=i-1;
while(l=0)
{
while(l<=r && r>=0 && s[r]=='1')
{
swap(s[l],s[r]);
l++;
}
r--;
}
return s;
}
};
class Solution
{
public:
int peakElement(int arr[], int n)
{
int ans=0;
for(int i=0;iarr[i+1]){
ans=i;
break;
}
else if(arr[i]>arr[i-1]){
ans=i;
}
}
return ans;
}
};
class Solution {
public:
bool isEvenOddTree(TreeNode* root) {
if(!root)
return false;
queueq;
q.push(root);
int lev=0;
while(!q.empty()){
int n=q.size();
TreeNode* prev=NULL;
while(n--)
{
TreeNode* cur=q.front();
q.pop();
if(lev%2==0){
if(cur->val % 2 == 0)
return false;
if(prev && prev->val>=cur->val)
return false;
}else{
if(cur->val % 2 == 1)
return false;
if(prev && prev->val<=cur->val)
return false;
}
prev=cur;
if(cur->left)
q.push(cur->left);
if(cur->right)
q.push(cur->right);
}
lev++;
}
return true;
}
};
class Solution{
public:
long long sumBitDifferences(int arr[], int n) {
long long ans = 0;
for(int k=0;k<32;k++){
int ct1 = 0;
int ct0 = 0;
for(int i=0;i>k)&1);
if(t==1){
ct1++;
}
else{
ct0++;
}
}
ans+=(ct1*(1ll)*ct0);
}
return ans*2;
}
};
class Solution {
public:
int findBottomLeftValue(TreeNode* root) {
queue q;
q.push(root);
int ans = 0;
while(!q.empty()){
int sz = q.size();
for(int i =0; i val;
if(node->right) q.push(node -> right);
if(node->left) q.push(node -> left);
}
}
return ans;
}
};
