#include<stdio.h>
#include<string.h>
int main()
{
void permute(char[],char[],int[],int,int);
char A[100];
scanf("%s",A);
char B[100]="";
int length=strlen(A);
int level=0;
int used[100]={0};
permute(A,B,used,length,level);
return 0;
}
void permute(char* A,char* B,int* used,int length,int level)
{
if(level==length)
{
B[level]='';
printf("%s\n",B);
return;
}
else
{
for(int i=0;i<length;i++)
{
if(used[i]==1)
{
continue;
}
used[i]=1;
B[level]=A[i];
permute(A,B,used,length,level+1);
used[i]=0;
B[level]='';
}
return;
}
}
A blog on programming and software design interview problems and solutions. Also there are some application development related stuff.
Pages
Sunday, July 22, 2012
C Program to write the permutations of the characters of a string using Recursion
Saturday, July 21, 2012
Alternating Letters
Examples:
Input | Output | Explanation |
exasperated | Length = 5 | eaeae |
counterintuitive | Length = 6 | tititi |
insignificant | Length = 6 | ininin |
Aragorn | Length = 3 | rar |
a2b-c2d- | Length = 4 | 2-2- |
cat | Length = 0 | No subsequence having length > 3 |
ca | Length = 0 | No subsequence having length > 3 |
#include<stdio.h>
#include<string.h>
int main()
{
int A[256]={0},max=0,B[256]={0};
char s[1000];
scanf("%s",s);
for(int i=0;i<strlen(s);i++)
{
A[s[i]]++;
if(s[i]>max)
{
max=s[i];
}
}
int k=0;
for(int i=0;i<=max;i++)
{
if(A[i]!=0)
{
B[k]=i;
k++;
}
}
int maxcount=0;
for(int i=0;i<k;i++)
{
for(int j=i+1;j<k;j++)
{
int count=0;
int order=0;
char curr='';
for(int c=0;c<strlen(s);c++)
{
if(s[c]==B[i])
{
if(order==0)
{
count++;
order=1;
curr=B[i];
}
else if(curr==B[j])
{
count++;
curr=B[i];
}
}
else if(s[c]==B[j])
{
if(order==0)
{
count++;
order=1;
curr=B[j];
}
else if(curr==B[i])
{
count++;
curr=B[j];
}
}
}
if(maxcount<count)
{
maxcount=count;
}
}
}
if(maxcount>=3)
{
printf("%d\n",maxcount);
}
else
{
printf("0\n");
}
return 0;
}
Tuesday, July 17, 2012
Simple Minded Hashing
For example, the string “acm” is mapped to 1 + 3 + 13 = 17. Unfortunately, this method does not give one-to-one mapping. The string “adl” also maps to 17 (1 + 4 + 12). This is called collision.
In this problem you will have to find the number of strings of length L, which maps to an integer S, using the above hash function. You have to consider strings that have only lowercase letters in strictly ascending order.
Suppose L = 3 and S = 10, there are 4 such strings.
- abg
- acf
- ade
- bce
agb also produces 10 but the letters are not strictly in ascending order.
bh also produces 10 but it has 2 letters.
Input
There will be several cases. Each case consists of 2 integers L and S. (0 < L, S < 10000). Input is terminated with 2 zeros.
Output
For each case, output Case #: where # is replaced by case number. Then output the result. Follow the sample for exact format. The result will fit in 32 bit signed integers.
Sample Input | Output for Sample Input |
3 10 2 3 0 0 | Case 1: 4 Case 2: 1 |
#include<stdio.h>
int main()
{
int fun(int,int,int,int,int,int,int[]);
int t,inp,num,A[10000]={0};
scanf("%d %d",&inp,&num);
if(inp==0 && num==0)
{
return 0;
}
else
{
printf("Count= %d\n",fun(1,inp,0,0,num,0,A));
}
return 0;
}
int fun(int level,int inp,int sum,int curr,int num,int count,int A[10000])
{
if(level==inp)
{
if(num-sum<=26 && num-sum>curr)
{
for(int i=0;i<level-1;i++)
{
printf("%d ",A[i]);
}
printf("%d\n",num-sum);
count+=1;
return count;
}
else
{
return count;
}
}
else
{
for(int i=curr+1;i<=(num/(inp-1));i++)
{
sum+=i;
A[level-1]=i;
count=fun(level+1,inp,sum,i,num,count,A);
sum-=i;
}
return count;
}
}
Saturday, July 7, 2012
Determinant Calculator
#include<stdio.h>
int main()
{
int determinant(int A[][100],int n,int i,int s);
int A[100][100],i,n,B[100];
printf("Enter the number of rows or columns: \n");
scanf("%d",&n);
printf("Enter the square matrix: \n");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
scanf("%d",&A[i][j]);
}
}
printf("The determinant is: \n");
int s=0;
s=determinant(A,n,i,s);
printf("%d\n",s);
fflush(stdin);
getchar();
return 0;
}
int determinant(int A[][100],int n,int i,int s)
{
int B[100][100];
int a=s;
if(n==2)
{
return A[0][0]*A[1][1] - A[0][1]*A[1][0];
}
else
{
//int s=0;
for(int j=0;j<n;j++)
{
int x=0;
for(int p=1;p<n;p++)
{
int y=0;
for(int q=0;q<n;q++)
{
if(q!=j)
{
B[x][y]=A[p][q];
y++;
}
}
x++;
}
if(j%2==0)
{
s+=A[0][j]*determinant(B,n-1,j,a);
}
else
{
s-=A[0][j]*determinant(B,n-1,j,a);
}
}
return s;
}
}
Output:
Enter the number of rows or columns: 4
Enter the square matrix:
1 13 4 6
23 21 18 5
4 2 7 9
1 10 15 12
The determinant is:
20353
Monday, April 16, 2012
Merging non-overlapping intervals
Problem:
Given a set of non overlapping intervals
Example 1: (1,4) (6,10) (14, 19) and another interval (13, 17) merge them as (1,4) (6,10) (13,19)
Example 2: (1,5) (6, 15) (20, 21) (23, 26) (27, 30) (35, 40)
New interval (14, 33)
Output should be
(1,5) (6, 33) (35, 40)
This is because the new interval overlaps with (6, 15) (20, 21) (23, 26) (27, 30)
Input:
The first line contains a number T, the number of test cases.
The next line contains an integer N, which is the number of non-overlapping intervals.
The next N lines contain two space separated integers which denote the start and end points of an interval. This is followed by a blank line.
The next line contains two space separated integers which denote the new interval.
Output:
The output for each test case should contain a line of the form “Case #X: “ where X is the number of the test case.
The next lines should contain the merged intervals in the ascending order.
C++ Code:
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int A[100],B[100],t,a,b,n,d=1;
cin>>t;
while(t--)
{
int m=1;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>A[i];
cin>>B[i];
}
sort(A,A+n);
sort(B,B+n);
cin>>a>>b;
int j=0;
while(j<=n-1 && a>A[j] && a>B[j])
{
j++;
}
int k=n-1;
while(k>=0 && b<B[k] && b<A[k])
{
k--;
}
if(j<k)
{
if(a<A[j])
{
A[j]=a;
}
if(b>B[k])
{
B[j]=b;
}
else
{
B[j]=B[k];
}
}
else if(j>k)
{
m=0;
}
else
{
if(B[j]<b)
{
B[j]=b;
}
}
if(m==1)
{
cout<<"Case #"<<d<<": "<<endl;
for(int i=0;i<=j;i++)
{
cout<<A[i]<<" "<<B[i]<<endl;
}
for(int i=k+1;i<n;i++)
{
cout<<A[i]<<" "<<B[i]<<endl;
}
}
else
{
cout<<"Case #"<<d<<": "<<endl;
for(int i=0;i<j;i++)
{
cout<<A[i]<<" "<<B[i]<<endl;
}
cout<<a<<" "<<b<<endl;
for(int i=k+1;i<n;i++)
{
cout<<A[i]<<" "<<B[i]<<endl;
}
}
d++;
}
return 0;
}
Sample Input:
1
6
1 5
6 15
20 21
23 26
27 30
35 40
14 33
Sample Output:
Case #1:
1 5
6 33
35 40
Thursday, January 19, 2012
Find the next higher number with the same digits
//Code to find the next higher number with the same digits.
#include<iostream>
using namespace std;
int main()
{
long long int m,n,A[1000],i,j,k,temp,same;
cin>>n;
same=0;
i=0;
while(n>0)
{
A[i]=n%10;
n=n/10;
i++;
}
//Finding the first digit at which the next digit is less than this digit starting from the left.
for(j=0;j<i-1;j++)
{
if(A[j+1]<A[j])
{
//Finding the smallest digit greater than the pivotal digit.
for(k=0;k<=j;k++)
{
if(A[k]>A[j+1])
{
break;
}
}
//Swapping the two digits.
temp=A[k];
A[k]=A[j+1];
A[j+1]=temp;
same=1;
break;
}
}
//If the next higher number is possible with the same number of digits
if(same==1)
{
m=A[i-1];
for(k=i-2;k>j;k--)
{
m=m*10+A[k];
}
for(j=0;j<=k;j++)
{
m=m*10+A[j];
}
}
//If the next higher number needs to have greater number of digits.
else
{
m=A[0];
m=m*10+A[0];
for(j=1;j<i;j++)
{
m=m*10+A[j];
}
}
cout<<m<<endl;
fflush(stdin);
getchar();
return 0;
}
Input: 37723971
Output: 37727139
Tuesday, December 6, 2011
C++ Program to pick the scan codes from a keyboard
- /* This program shows how to pick up the scan codes from a keyboard */
- /* These define the scan codes(IBM) for the keys. All numbers are in decimal.*/
- #define PAGE_UP 73
- #define HOME 71
- #define END 79
- #define PAGE_DOWN 81
- #define UP_ARROW 72
- #define LEFT_ARROW 75
- #define DOWN_ARROW 80
- #define RIGHT_ARROW 77
- #define F1 59
- #define F2 60
- #define F3 61
- #define F4 62
- #define F5 63
- #define F6 64
- #define F7 65
- #define F8 66
- #define F9 67
- #define F10 68
- #include <iostream>
- #include <conio.h>
- using namespace std;
- int main()
- {
- char KeyStroke;
- cout << "Press Escape to quit." << endl;
- do
- {
- KeyStroke = getch();
- if (KeyStroke == 0)
- {
- KeyStroke = getch(); // Even though there are 2 getch() it reads one keystroke
- switch (KeyStroke)
- {
- case PAGE_UP:
- cout << "PAGE UP" << endl;
- break;
- case PAGE_DOWN:
- cout << "PAGE DOWN" << endl;
- break;
- case HOME:
- cout << "HOME" << endl;
- break;
- case END:
- cout << "END" << endl;
- break;
- case UP_ARROW:
- cout << "UP ARROW" << endl;
- break;
- case DOWN_ARROW:
- cout << "DOWN ARROW" << endl;
- break;
- case LEFT_ARROW:
- cout << "LEFT_ARROW" << endl;
- break;
- case RIGHT_ARROW:
- cout << "RIGHT_ARROW" << endl;
- break;
- case F1:
- cout << "F1" << endl;
- break;
- case F2:
- cout << "F2" << endl;
- break;
- case F3:
- cout << "F3" << endl;
- break;
- case F4:
- cout << "F4" << endl;
- break;
- case F5:
- cout << "F5" << endl;
- break;
- case F6:
- cout << "F6" << endl;
- break;
- case F7:
- cout << "F7" << endl;
- break;
- case F8:
- cout << "F8" << endl;
- break;
- case F9:
- cout << "F9" << endl;
- break;
- case F10:
- cout << "F10" << endl;
- break;
- default:
- cout << "Some other key." << endl;
- }
- }
- else
- cout << KeyStroke << endl;
- }
- while (KeyStroke != 27); // 27 = Escape key
- }