Wednesday, September 19, 2012

Counting the number of valid bracket permutations for a given length


Example:
Input: 3
Output: 5
Explanation:
The following permutations are valid:
((())),(())(),()(()),(()()),()()()
The following permutations are invalid:
)()()(,())(() etc.
Program:
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
int main()
{
int fun(char[],int,int,int);
int n;
cin>>n;
char A[100]="";
cout<<fun(A,0,0,n)<<endl;
}
int fun(char A[],int open,int close,int n)
{
static int count=0;
if(open-close<0 || open-close>n || open>n)
{
return count;
}
else if(strlen(A)==2*n)
{
//cout<<A<<endl;
count++;
//cout<<count<<" "<<endl;
return count;
}
else
{
A[strlen(A)]='(';
A[strlen(A)+1]=0;
fun(A,open+1,close,n);
A[strlen(A)-1]=0;
A[strlen(A)]=')';
A[strlen(A)+1]=0;
fun(A,open,close+1,n);
A[strlen(A)-1]=0;
return count;
}
}
Sample Input: 15
Sample Output: 9694845

Tuesday, September 11, 2012

Directory Path Parsing

Input: "C:\\a\\b\\c\\..\\.\\..\\g\\."
Output: "C:\\a\\g"

#include<iostream>
#include<string.h>
#include<signal.h>
using namespace std;
int main()
{
void fun();
char A[100]="C:\\a\\b\\c\\..\\.\\..\\g\\.\\..";
//char A[100]="C:\\a\\b";
char B[100]="";
char temp[100]="";
int top=-1;
char C[20][100];
int i=0;
while(i<strlen(A) && A[i]!='\\')
{
B[i]=A[i];
i++;
}
B[i]=0;
while(i<strlen(A))
{
int j=0;
while(i<strlen(A) && A[i]!='\\')
{
temp[j]=A[i];
i++;
j++;
}
temp[j]=0;
if(top>=0 && strcmp(temp,"..")==0)
{
top--;
}
else if(strcmp(temp,".")==0)
{
}
else
{
top++;
strcpy(C[top],temp);
}
i++;
}
for(int j=0;j<=top;j++)
{
strcat(B,C[j]);
strcat(B,"\\");
}
cout<<B<<endl;
}

Wednesday, August 29, 2012

Grid Traversal

There is a rectangular grid of size m * n . Bob is in location ( x, y ) inside grid. He can move in 4 directions up, down, right and left. He will die if he steps outside of rectangular grid. Find the probability that bob is alive given initial position of bob as ( x, y ) and number of steps he moves as N. (Given that Bob moves only one step at a time).

#include<iostream>
using namespace std;
int main()
{
    void fun(int,int,int,int,float*,float*,int,int);
    int m,n,x,y,N;
    float a=0.0,b=0.0,*p=&a,*q=&b;
    cin>>m>>n>>x>>y>>N;
    fun(m,n,x,y,p,q,0,N);
    cout<<(*q)/((*q)+(*p));
}
void fun(int m,int n,int x,int y,float *p,float *q,int S,int N)
{    
    if(S==N && x>=0 && y>=0 && x<m && y<n)
    {
        (*q)++;
        return;
    }
    else if((S<=N) && (x<0 || y<0 || x>=m || y>=n))
    {
        (*p)++;
        return;
    }
    else
    {
        fun(m,n,x+1,y,p,q,S+1,N);
        fun(m,n,x-1,y,p,q,S+1,N);
        fun(m,n,x,y+1,p,q,S+1,N);
        fun(m,n,x,y-1,p,q,S+1,N);
    }
}

Wednesday, August 22, 2012

Excel column header generator


#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char out[27]={'Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y',0};
char ans[100]={0};
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int j=0;
while(n>0)
{
int c=n%strlen(out);
//Using the remainder as the index of the character array.
ans[j]=out[c];
if(c==0)
{
n--;
}
n/=strlen(out);
j++;
}
//Reversing the answer string.
for(int i=0;i<strlen(ans)/2;i++)
{
char t=ans[i];
ans[i]=ans[strlen(ans)-i-1];
ans[strlen(ans)-i-1]=t;
}
cout<<ans<<endl;
}
return 0;
}

Sample Input: The first line is the number of test cases.
7
1
25
26
27
51
52
676

Sample Output:
A
Y
Z
AA
AY
AZ
YZ

Tuesday, August 7, 2012

C++ Program for Quicksort


#include<iostream>
using namespace std;
int main()
{
int A[7]={3,6,1,2,7,5,4};
void quicksort(int[],int,int);
quicksort(A,0,6);
for(int i=0;i<7;i++)
{
cout<<A[i]<<" ";
}
cout<<endl;
return 0;
}
void quicksort(int A[7],int low,int high)
{
if(low>=high)
{
return;
}
int p=A[low];
int i=low+1;
int j=high;
while(j>=i)
{
while(A[i]<=p)
{
i++;
}
while(A[j]>p)
{
j--;
}
if(j>i)
{
int t=A[i];
A[i]=A[j];
A[j]=t;
}
}
int t=A[j];
A[j]=A[low];
A[low]=t;
quicksort(A,low,j-1);
quicksort(A,j+1,high);
}

Monday, August 6, 2012

C++ Program for Mergesort


#include<iostream>
using namespace std;
int main()
{
void mergesort(int[],int,int);
int A[7]={1,2,5,3,7,6,4};
int low=0,high=6;
mergesort(A,0,6);
for(int i=low;i<=high;i++)
{
cout<<A[i]<<" ";
}
cout<<endl;
return 0;
}
void mergesort(int A[7],int low,int high)
{
if(low==high)
{
return;
}
int mid=(low+high)/2;
mergesort(A,low,mid);
mergesort(A,mid+1,high);
int a=low;
int b=mid+1;
int B[100],j=0;
while(a<=mid && b<=high)
{
if(A[a]<A[b])
{
B[j]=A[a];
j++;
a++;
}
else if(A[b]<A[a])
{
B[j]=A[b];
j++;
b++;
}
}
while(a<=mid)
{
B[j]=A[a];
j++;
a++;
}
while(b<=high)
{
B[j]=A[b];
j++;
b++;
}
int k=0;
for(int i=low;i<=high;i++)
{
A[i]=B[k];
k++;
}
}

Monday, July 23, 2012

Given an array of positive integers, print out all the numbers which are repeated an even number of times without using additional storage


using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text.RegularExpressions;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
//This part assumes that all the elements in the array lie in the range (min,min + arraylength -1).
int[] arr1 = { 1, 2, 3, 4, 4, 5, 2, 1, 1, 1 };
int count = 1;
int min = arr1.Min();
int max = arr1.Max();
for (int i = 0; i < arr1.GetLength(0); i++)
{
arr1[Math.Abs(arr1[i])] *= -1;
}
for (int i = 0; i < arr1.GetLength(0); i++)
{
Console.Write(arr1[i] + " ");
}
Console.WriteLine();
for (int i = min; i <= max; i++) { if (arr1[i] > 0)
{
Console.Write(i + " ");
}
}
Console.WriteLine();
//This part sorts the array and finds the frequency map and counts the frequency of each element.
int[] arr2 = { 1, 2, 3, 4, 4, 5, 2, 1, 1, 1 };
Array.Sort(arr2);
for (int i = 0; i < arr2.GetLength(0) - 1; i++)
{
count = 1;
while (arr2[i + 1] == arr2[i])
{
i++;
count++;
}
if (count % 2 == 0)
{
Console.WriteLine(arr2[i] + " " + count);
}
}
}
}
}