LeetCode, GeeksForGeeks Problem of the day solution
Відкрити в Telegram
Complete daily challenges from LeetCode, GeeksForGeeks and redeem their rewards Channel link : https://t.me/leetcode_gfg_potd
Показати більше1 250
Підписники
+224 години
+147 днів
+2930 день
Архів дописів
class Solution {
public:
vector getSumAbsoluteDifferences(vector& nums) {
int total_sum=0;
int n=nums.size();
for(int i=0; i ans;
int curr_sum=0;
for(int i=0; i
class Solution{
public:
void shuffleArray(int arr[],int n)
{
int maxi=1e5;
int i=0;
int j=n/2;
while(j<n)
{
arr[i]=(arr[j]*maxi)+arr[i];
i++;
j++;
}
i=(n/2)-1;
j=n-1;
while(i>=0)
{
arr[j]=arr[i]/maxi;
arr[j-1]=arr[i]%maxi;
j-=2;
i--;
}
}
};
class Solution {
int findSecondMaxi(int i, int j, int k, int maxi){
if(maxi == i && k < j) return j;
else if(maxi == j && i < k) return k;
else return i;
}
public:
int maxCoins(vector& piles) {
sort(piles.begin(),piles.end());
int i = 0;
int j = piles.size()-1;
int k = piles.size()-2;
int res = 0;
while(i < j && i < k && k < j){
int maxi = max(piles[i], max(piles[j],piles[k]));
int secondMaxi = findSecondMaxi(piles[i],piles[j], piles[k], maxi);
res += secondMaxi;
i++;
j -= 2;
k -= 2;
}
return res;
}
};
class Solution{
public:
vector nthRowOfPascalTriangle(int n) {
int mod = 1000000007;
vector> pT(n);
for(int i=0;i
class Solution {
public:
bool isvalid(int start,int end,vectornums){
vectortemp;
for(int i=start;i<=end;i++){
temp.push_back(nums[i]);
}
sort(temp.begin(),temp.end());
int Size=temp.size();
int curr=temp[1]-temp[0];
for(int i=1;i checkArithmeticSubarrays(vector& nums, vector& l, vector& r) {
int lSize=l.size();
int rSize=r.size();
int i=0; // l
int j=0; // r
vectorans;
while(i
class Solution{
public:
int height(Node* N){
if(N==NULL){
return 0;
}
return N->height;
}
int getBalance(Node* N){
if(N==NULL){
return 0;
}
return height(N->left)-height(N->right);
}
Node* leftRotation(Node* x){
Node* y = x->right;
Node* T2 = y->left;
y->left = x;
x->right = T2;
x->height = 1+max(height(x->left),height(x->right));
y->height = 1+max(height(y->left),height(y->right));
return y;
}
Node* rightRotation(Node* x){
Node* y = x->left;
Node* T2 = y->right;
y->right = x;
x->left = T2;
x->height = 1+max(height(x->left),height(x->right));
y->height = 1+max(height(y->left),height(y->right));
return y;
}
/*You are required to complete this method */
Node* insertToAVL(Node* node, int data)
{
if(node==NULL){
return new Node(data);
}else if(data > node->data){
node->right = insertToAVL(node->right,data);
}else if(data < node->data){
node->left = insertToAVL(node->left,data);
}else{
return node;
}
node->height = 1+max(height(node->left),height(node->right));
int balance = getBalance(node);
if(balance>1 && data < node->left->data){
return rightRotation(node);
}
if(balance<-1 && data>node->right->data){
return leftRotation(node);
}
if(balance>1 && data>node->left->data){
node->left = leftRotation(node->left);
return rightRotation(node);
}
if(balance<-1 && dataright->data){
node->right = rightRotation(node->right);
return leftRotation(node);
}
return node;
}
};
class Solution {
public:
vector findDiagonalOrder(vector>& nums) {
vector> v;
for(int i=0;i a, pair b)->bool{
if(a.first != b.first)return a.first < b.first;
return a.second < b.second;
};
sort(v.begin(),v.end(),cmp);
vector ans;
for(auto i: v){
ans.push_back(nums[i.first-i.second][i.second]);
}
return ans;
}
};
class Solution{
public:
bool solve(struct Node* root, struct Node* root1) {
if(root == nullptr && root1 == nullptr ) {
return true;
}
if(root == nullptr or root1 == nullptr or root->data != root1->data) {
return false;
}
bool left = solve(root->left, root1->right);
bool right = solve(root->right, root1->left);
return left && right;
}
// return true/false denoting whether the tree is Symmetric or not
bool isSymmetric(struct Node* root)
{if(root == nullptr) {
return true;
}
// Code here
return solve(root->left , root->right);
}
};
class Solution {
public:
int rev(int n){
int ans = 0;
while(n){
ans = ans * 10 + (n%10);
n = n/10;
}
return ans;
}
int countNicePairs(vector& nums) {
unordered_map mp;
int mod = 1e9+7;
int count = 0;
for(int i=0;i
class Solution
{
public:
//Function to check if two trees are identical.
bool isIdentical(Node *r1, Node *r2)
{
if( r1== NULL && r2 == NULL){
return true;
}
else if( r1== NULL && r2 != NULL){
return false;
}
else if( r1 != NULL && r2 == NULL){
return false;
}
bool left = isIdentical(r1->left, r2->left);
bool right = isIdentical(r1->right, r2->right);
bool val = r1->data == r2->data;
if( left && right && val){
return true;
}
else{
return false;
}
}
};
