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> transpose(vector>& matrix) {
vector> res(matrix[0].size(), vector(matrix.size()));
for(int i=0;i
class Solution{
public:
//Complete this function
//Function to check whether there is a subarray present with 0-sum or not.
bool subArrayExists(int arr[], int n)
{
unordered_map mpp;
int sum =0;
mpp[0] =true;
for(int i =0;i< n;i++){
sum+=arr[i];
if(mpp[sum]) return true;
mpp[sum] = true;
}
return false;
}
};
class Solution {
public:
vector inorderTraversal(TreeNode* root) {
vector ans;
if(root==NULL){
return ans;
}
TreeNode* curr = root;
while(curr!=NULL){
if(curr->left==NULL){
ans.push_back(curr->val);
curr = curr->right;
}
else{
TreeNode* prev = curr->left;
while(prev->right != NULL && prev->right != curr){
prev = prev->right;
}
if(prev->right==NULL){
prev->right = curr;
curr= curr->left;
}
else{
prev->right = NULL;
ans.push_back(curr->val);
curr=curr->right;
}
}
}
return ans;
}
};
class Solution {
public:
int smithNum(int n) {
int sum = 0;
int num =n;
vector primes;
for(int i =2;i< n;i++){
while(num%i == 0){
primes.push_back(i);
num/=i;
}
}
num =0;
while(n){
int digit = n%10;
num+=digit;
n/=10;
}
for(int i =0;i< primes.size();i++){
while(primes[i]){
int digit =primes[i]%10;
sum+=digit;
primes[i]/=10;
}
}
return num == sum;
}
};
class Solution {
public:
string tree2str(TreeNode* root) {
if(root==nullptr){
return "";
}
string s = to_string(root->val);
if(root->left){
s+= '(' + tree2str(root->left) + ')';
}
if(root->right){
if(root->left==nullptr){
s += "()";
}
s+= '(' + tree2str(root->right) + ')';
}
return s;
}
};
#define ll long long
class Solution
{
public:
bool isPrime(int n)
{
for(int i=2;i*i<=n;i++)
{
if(n%i==0)
return 0;
}
return 1;
}
int minNumber(int arr[],int N)
{
ll sum=0;
for(int i=0;i<N;i++)
sum+=arr[i];
ll temp=sum;
while(1)
{
if(isPrime(temp))
return temp-sum;
else
temp++;
}
return 0;
}
};
class Solution {
public:
string largestOddNumber(string num) {
int n=num.size();
for(int i=n-1;i>=0;i--){
if(num[i]%2==1){
return num.substr(0, i+1);
}
}
return "";
}
};
class Solution{
public:
long countSubarrays(int a[], int n, int L, int R){
vector<int> left(n,-1);
vector<int> right(n,n);
stack<int> s;
for(int i=0;i<n;i++){
while(!s.empty() && a[s.top()]<=a[i]){
s.pop();
}
if(!s.empty()){
left[i]=s.top();
}
s.push(i);
}
s=stack<int>();
for(int i=n-1;i>=0;i--){
while(!s.empty() && a[s.top()]<a[i]){
s.pop();
}
if(!s.empty()){
right[i]=s.top();
}
s.push(i);
}
long res=0;
for(int i=0;i<n;i++){
if(a[i]>=L && a[i]<=R){
int ways=(i-left[i])*(right[i]-i);
res+=ways;
}
}
return res;
}
};
class Solution {
public:
int totalMoney(int n) {
int s = 1, ans = 0;
while (n > 0) {
for (int i = 0; i < 7 && n-- > 0; ++i) ans += s + i;
s++;
}
return ans;
}
};
class Solution {
public:
int countX(int L, int R, int X) {
int rem;
int ans=0;
int i=L+1;
int times =0;
while(i>0 && i
