GeeksForGeeks - POTD | GFG POTD Answer
Canal cerrado
1 218
Suscriptores
Sin datos24 horas
-97 días
-5730 días
Archivo de publicaciones
class Solution{
public:
int longestPalinSubseq(string A) {
int n=A.length();
string B=A;
reverse(B.begin(),B.end());
vector prev(n+1,0),curr(n+1,0);
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
if(A[i-1]==B[j-1]) curr[j]=1+prev[j-1];
else curr[j]=max(curr[j-1],prev[j]);
}
prev=curr;
}
return curr[n];
}
};
class Solution {
public:
int LongestRepeatingSubsequence(string str){
vector>v(str.length()+1,vector(str.length()+1,0));
for(int i=1;i<=str.length();i++)
{
for(int j=1;j<=str.length();j++)
{
if(str[i-1]==str[j-1] && i!=j)
{
v[i][j]=1+v[i-1][j-1];
}
else{
v[i][j]=max(v[i-1][j],v[i][j-1]);
}
}
}
return v[str.length()][str.length()];
}
};
class Solution
{
public:
//Function to delete middle element of a stack.
void deleteMid(stack<int>&s, int sizeOfStack)
{
helper(s, sizeOfStack, 0);
}
void helper(std::stack<int>& s, int n, int idx) {
if (idx == n/2)
{
s.pop();
return;
}
int x = s.top();
s.pop();
idx++;
helper(s, n, idx);
s.push(x);
}
};
class twoStacks
{
int *arr;
int size;
int top1, top2;
public:
twoStacks(int n=100)
{
size = n;
arr = new int[n];
top1 = -1;
top2 = size;
}
//Function to push an integer into the stack1.
void push1(int x)
{
top1++;
if(top1<(top2-1))
arr[top1]=x;
}
//Function to push an integer into the stack2.
void push2(int x)
{
top2--;
if((top2-1)>top1)
arr[top2]=x;
}
//Function to remove an element from top of the stack1.
int pop1()
{
if(top1==-1)return -1;
top1--;
return arr[top1+1];
}
//Function to remove an element from top of the stack2.
int pop2()
{
if(top2==size)return -1;
top2++;
return arr[top2-1];
}
};
class Solution
{
public:
bool isFrequencyUnique(int n, int arr[])
{
unordered_map mp;
for(int i=0;i st;
for(auto it:mp)
st.insert(it.second);
return mp.size()==st.size()?true:false;
}
};
class Solution {
public:
long long power(int N, int R) {
// Calculate the power of N raised to R modulo 1e9 + 7
long long pow = N;
long long ans = 1;
int mod = 1e9 + 7;
while (R > 0) {
if (R & 1) {
ans = (ans * pow) % mod;
}
pow = (pow * pow) % mod;
R = R >> 1;
}
return ans;
}
};
class Solution
{
public:
int findK(int a[MAX][MAX],int n,int m,int k)
{
int sr = 0, er = n-1, sc = 0, ec = m-1;
while(sr <= er || sc <= ec)
{
if(sr <= er)
{
int dif = max((ec-sc+1),0);
if(dif < k)
{
k -= dif;
}
else
{
return a[sr][sc + k - 1];
}
sr++;
}
if(sc <= ec)
{
int dif = max(0,er-sr+1);
if(dif < k)
{
k -= dif;
}
else
{
return a[sr+k-1][ec];
}
ec--;
}
if(sr <= er)
{
int dif = max(0, ec-sc+1);
if(dif < k)
{
k -= dif;
}
else
{
return a[er][ec-k+1];
}
er--;
}
if(sc <= ec)
{
int dif = max(0, er-sr+1);
if(dif < k)
{
k -= dif;
}
else
{
return a[er - k + 1][sc];
}
sc++;
}
}
return -1;
}
};
class Solution
{
public:
//Function to find transpose of a matrix.
void transpose(vector >& matrix, int n)
{
int ans[n][n];
for(int i=0;i
class Solution
{
public:
// Function to find the smallest positive number missing from the array.
int missingNumber(int arr[], int n)
{
// Segregate positive and non-positive elements
int i = 0;
for (int j = 0; j < n; j++)
{
if (arr[j] <= 0)
{
swap(arr[i], arr[j]);
i++;
}
}
// Consider only positive elements from index i onwards
int size = n - i;
for (int j = i; j < n; j++)
{
int num = abs(arr[j]);
if (num <= size)
{
arr[i + num - 1] = -abs(arr[i + num - 1]);
}
}
// Find the first positive element
for (int j = i; j < n; j++)
{
if (arr[j] > 0)
{
return j - i + 1;
}
}
// If all positive elements are present, return n - i + 1
return size + 1;
}
};
class Solution{
public:
//Function to find triplets with zero sum.
bool findTriplets(int arr[], int n)
{
mapmp;
for(int i=0;i=1)break;
}
return count;
}
};
