Recursion
Recursion
CODE:
return;
if(arr[x][y]==newc)
return;
if(arr[x][y]!=prevc)
return;
arr[x][y]=newc;
paint(n,m,arr,x+1,y,prevc,newc);
paint(n,m,arr,x-1,y,prevc,newc);
paint(n,m,arr,x,y+1,prevc,newc);
paint(n,m,arr,x,y-1,prevc,newc);
return;
int main()
int t;
cin>>t;
while(t--)
int n,m;
cin>>n>>m;
int arr[100][100];
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
cin>>arr[i][j];
int x,y,k;
cin>>x>>y>>k;
// for(int i=0;i<n;i++)
// {
// for(int j=0;j<m;j++)
// cout<<arr[i][j]<<" ";
// cout<<endl;
// }
// cout<<"------------------------------------------------"<<endl;
int prevc=arr[x][y];
int newc=k;
paint(n,m,arr,x,y,prevc,newc);
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
cout<<arr[i][j]<<" ";
cout<<endl;
return 0;
2. Number of paths
The problem is to count all the possible paths from top left to bottom right of
a MxN matrix with the constraints that from each cell you can either move
to right or down.
Input:
The first line of input contains an integer T, denoting the number of test cases. The
first line of each test case is M and N, M is number of rows and N is number of
columns.
Output:
For each test case, print the number of paths.
Constraints:
1 ≤ T ≤ 30
1 ≤ M,N ≤ 10
Example:
Input
2
33
28
Output
6
8
CODE:
if(i==n-1 || j==m-1)
return 1;
if(i>=n || j>=m)
return 0;
}
int main()
int t;
cin>>t;
while(t--)
int n,m;
cin>>n>>m;
cout<<count(0,0,n,m)<<endl;
return 0;
Example 1:
Input:
N = 7
A = {9, 1, 2, 7, 6, 1, 5}
B = 8
Output: (1 1 6)(1 2 5)(1 7)(2 6)
Explaination: These are the only possible
combinations for getting sum 8.
Example 2:
Input:
N = 5
A = {8, 1, 8, 6, 8}
B = 12
Output: Empty
Explainatioin: We cannot obtain sum 12
from the given elements.
Your Task:
You don't need to read input r print anything. Your task is to complete the function
combinationSum() which takes the array A[], the length of the array N and the
sum B as input parameters and returns all the combinations, otherwise, if there is no
combination present it returns an empty list.
Constraints:
0 < N < 13
0 <= A[i] <= 9
0 < B <= 30
CODE:
#include <bits/stdc++.h>
class Solution{
public:
void combination(vector<int>& candidates, int target, vector<int> curr, vector<vector<int>>& result, int idx) {
if(target < 0)
return;
if(target == 0) {
result.push_back(curr);
return;
curr.push_back(candidates[i]);
vector<vector<int>> result;
vector<int> curr;
return result;
};
int main()
int t;
cin>>t;
while(t--)
int N, x, B;
cin>>N;
vector<int> A;
{
cin>>x;
A.push_back(x);
cin>>B;
Solution ob;
vector<vector<int>> result;
if(result.size() == 0)
cout<<"Empty"<<endl;
else{
cout<<"(";
cout<<result[i][j];
cout<<" ";
cout<<")";
cout<<endl;
return 0;
}
4. Special Keyboard
Imagine you have a special keyboard with the following keys:
Key 1: Prints 'A' on screen
Key 2: (Ctrl-A): Select screen
Key 3: (Ctrl-C): Copy selection to buffer
Key 4: (Ctrl-V): Print buffer on screen appending it after what has already been
printed.
Find maximum numbers of A's that can be produced by pressing keys on the special
keyboard N times.
Example 1:
Input: N = 3
Output: 3
Explaination: Press key 1 three times.
Example 2:
Input: N = 7
Output: 9
Explaination: The best key sequence is
key 1, key 1, key 1, key 2, key 3,
key4, key 4.
Your Task:
You do not need to read input or print anything. Your task is to complete the
function optimalKeys() which takes N as input parameter and returns the maximum
number of A's that can be on the screen after performing N operations.
CODE:
#include <bits/stdc++.h>
class Solution{
public:
if (N <= 6)
return N;
int screen[N];
// strokes.
int n;
screen[n - 1] = n;
screen[n - 1] = 0;
// will have ctrl-a, ctrl-c and then only ctrl-v all the way.
// (n-b-1)*screen[b-1];
screen[n - 1] = curr;
}
return screen[N - 1];
};
int main(){
int t;
cin>>t;
while(t--){
int N;
cin>>N;
Solution ob;
cout<<ob.optimalKeys(N)<<"\n";
return 0;
5. Josephus problem
Given the total number of persons n and a number k which indicates that k-1 persons
are skipped and kth person is killed in circle in a fixed direction.​
The task is to choose the safe place in the circle so that when you perform these
operations starting from 1 place in the circle, you are the last one remaining
st
and survive.
Example 1:
Input:
n = 3 k = 2
Output: 3
Explanation: There are 3 persons so
skipping 1 person i.e 1st person 2nd
person will be killed. Thus the safe
position is 3.
Example 2:
Input:
n = 5 k = 3
Output: 4
Explanation: There are 5 persons so
skipping 2 person i.e 3rd person will
be killed. Thus the safe position is 4.
Your Task:
You don't need to read input or print anything. You are required to complete
the function josephus () that takes two parameters n and k and returns an integer
denoting safe position.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 <= k, n <= 20
CODE:
#include <bits/stdc++.h>
int main() {
int t;
cin>>t;//testcases
while(t--)
int n,k;
cout<<josephus(n,k)<<endl;
return 0;
if(n==1)
return 1;
return ((josephus(n-1,k)+k-1)%n+1);