DMX technologies (A Bangalore's Startup) written Round Programming Questions
1) Spiral Matrix Printing
For example : if given n=2 , then output is 3 2
0 1
if given n=3 , then output is 4 3 2
5 0 1
6 7 8
C++ Program
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
int a[n+1][n+1];
int num=n*n-1;
for(int i=1,j=n;i<=n,j>=0;i++,j--)
{
for(int k=i;k<=j;k++) ///upper row
a[i][k]=num--;
for(int k=i+1;k<=j;k++)
a[k][j]=num--;
for(int k=j-1;k>=i;k--)
a[j][k]=num--;
for(int k=j-1;k>i;k--)
a[k][i]=num--;
// if(num<0) break;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
return 0;
}
For example : if given n=2 , then output is 3 2
0 1
if given n=3 , then output is 4 3 2
5 0 1
6 7 8
C++ Program
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
int a[n+1][n+1];
int num=n*n-1;
for(int i=1,j=n;i<=n,j>=0;i++,j--)
{
for(int k=i;k<=j;k++) ///upper row
a[i][k]=num--;
for(int k=i+1;k<=j;k++)
a[k][j]=num--;
for(int k=j-1;k>=i;k--)
a[j][k]=num--;
for(int k=j-1;k>i;k--)
a[k][i]=num--;
// if(num<0) break;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
return 0;
}
-------------------------------------------------------------------------------------------------------------------------
2) All Permutations of a given string using Recursion
Link :- http://nikhilnihal.blogspot.in/2016/07/printing-all-permutation-of-string.html
3) Number of maximum continuous '1' that can be formed by flipping m '0's in an given array.
Given an array and m as input.
for example : 1 1 0 1 0 0 m=2
then answer is 5
Because after flipping 1 1 1 1 1 0 , it gives 5 continuous '1's.
C++ Program
#include<bits/stdc++.h>
using namespace std;
void fun(int a[],int n,int m)
{
vector<int> v; v.push_back(0);
for(int i=0;i<n;i++)
if(a[i]==0)
v.push_back(i+1);
int conti=0,max_conti=0;
for(int i=m;i<v.size();i++)
{
//cout<<v[i]<<" ";
conti=v[i]-v[i-m]-1+m;
if(conti>max_conti)
max_conti=conti;
}
cout<<max_conti<<endl;
}
int main()
{
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++)
cin>>a[i];
int m; cin>>m;
fun(a,n,m);
return 0;
}
Comments
Post a Comment