N Steps in Exactly M Jumps. Derive A Recurrence Relation For C (N, M), and Write A Recursive
N Steps in Exactly M Jumps. Derive A Recurrence Relation For C (N, M), and Write A Recursive
#include<iostream>
long long int ju(long long int n,long long int an[])
if(n<0)
return 0;
if(an[n]!=0)
return an[n];
an[n]=ju(n-1,an)+ju(n-2,an)+ju(n-3,an);
return an[n];
long long int jump(long long int n,long long int m,long long int cn[][50])
if(n<0||m<0)
return 0;
if((n!=0&&m==0)||(n==0&&m!=0))
return 0;
if(cn[n][m]!=0)
return cn[n][m];
cn[n][m]=jump(n-1,m-1,cn)+jump(n-2,m-1,cn)+jump(n-3,m-1,cn);
return cn[n][m];
int main(){
an[0]=1;
cn[0][0]=1;
bn[1]=1;
bn[2]=2;
bn[3]=4;
cin>>n;
bn[i]=bn[i-1]+bn[i-2]+bn[i-3];
dn[i][0]=0;
dn[0][i]=0;
dn[0][0]=1;
if(j>=1)
dn[j][i]+=dn[j-1][i-1];
if(j>=2)
dn[j][i]+=dn[j-2][i-1];
if(j>=3)
dn[j][i]+=dn[j-3][i-1];
ans1+=jump(n,i,cn);
cout<<"----------------------------------------------------"<<endl;
ans2+=dn[n][i];
cout<<"----------------------------------------------------"<<endl;
return 0;
}
OUTPUT:
Q2(LAB 4) : You are provided two queues, Q1 and Q2 , and one stack S. You are allowed
to dequeue
From Q1 and to enqueue in Q2 . You are allowed to both push into and pop from the
stack S.
Your task is to write a program, that will take a numbers and the final permutation of
numbers as input and outputs if it is a stack permutation or not. Your program should also
display the sequence of operations that formed the permutation.
CODE :
#include <bits/stdc++.h>
queue<int> input;
for(int i=0;i<n;i++)
input.push(ip[i]);
queue<int> output;
for(int i=0;i<n;i++)
output.push(op[i]);
stack<int> tempStack;
int a[1000],i=0;
while (!input.empty()){
input.pop();
if (ele == output.front()){
a[i]=1;i++;
output.pop();
while (!tempStack.empty()){
if (tempStack.top() == output.front()){
a[i]=3;i++;
tempStack.pop();
output.pop();
else
break;
else{
tempStack.push(ele);
a[i]=2;i++;
if(input.empty()&&tempStack.empty()){
for(int j=0;j<i;j++){
if(a[j]==1)
cout<<"enqueue(Q2, dequeue(Q1))"<<endl;
else if(a[j]==2)
cout<<"push(S, dequeue(Q1))"<<endl;
else if (a[j]==3)
cout<<"enqueue(Q2, pop(S))"<<endl;
else
cout<<"Not permutable"<<endl;
int n;
cout<<"Enter n :";
cin>>n;
int a[n];
for(int i=0;i<n;i++)
cin>>a[i];
int x[n];
cin>>x[i];
check(a,x,n);
return 0;
OUTPUT :
3. You are provided a n x n matrix, where every row and column is sorted in increasing
order. Given a key k, your task is to determine if this key is present in the matrix or not in
minimum possible time.
CODE :
#include <bits/stdc++.h>
int main(){
int n;
cin>>n;
int a[n][n];
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
cin>>a[i][j];
int key;
cin>>key;
int i=0,j=n-1,flag=0;
if(a[i][j]==key){
flag=1;
break;
else{
if(key>a[i][j]){
if(i+1>n-1){
flag=0;break;
}
else{
i++;
else if(key<a[i][j]){
if(j-1<0){
flag=0;break;
else{
j--;
if(flag==1)
else if(flag==0)
return 0;
OUTPUT :