Friday, 8 July 2016

spiral matrix for odd number of rows

#include<stdio.h>
int k=0,res[100]={0},flag=0,a[100][100]={0},n,m;

void spiral(){
int i,row;
row=0;
while(row<=n/2){
for(i=row;i<m-1-row;i++){
 res[k]=a[row][i];
 printf(" %d",res[k]);
 k++;
}
for(i=row;i<n-1-row;i++){
 res[k]=a[i][m-1-row];
 printf(" %d",res[k]);
 k++;
}
for(i=m-1-row;i>row && row<n/2;i--){
 res[k]=a[n-1-row][i];
 printf(" %d",res[k]);
 k++;
}
for(i=n-1-row;i>row;i--){
 res[k]=a[i][row];
 printf(" %d",res[k]);
 k++;
}
row++;
}
if(flag==1 && (n==m)){
printf(" %d",a[n/2][n/2]);
}
if(flag==1 && (n!=m))
printf(" %d",a[n-row][m-row]);
}
// DRIVER PROGRAM
void main(){
int i,j;
//clrscr();
scanf("%d %d",&n,&m);
for(i=0;i<n;i++){
for(j=0;j<m;j++){
scanf("%d",&a[i][j]);
}
}
// CHECK IF NUMBER OF ROW IS ODD OR EVEN
if(n%2!=0)
flag=1;
else
flag=0;
spiral();

//getch();
}

To find the given sting is palindrome or circular palindrome or not a palindrome

#include<stdio.h>
#include<conio.h>
int first,last,len,palindrome=0,true,flag=0;
char s[100];
// TO CHECK WHETHER THE GIVEN STRING IS PALINDROME 
int ispal(){
last=len-1;
for(first=0;first<len/2;first++){
if(s[first]!=s[last--]){
return 0;
}
}
return 1;
}
// TO CHECK WHETHER THE GIVEN STRING IS CIRCULAR PALINDROME
int pal(int f,int l){
int count=0;
for(;count<len;){
if(f==len) // IF FIRST REACHES THE LAST CHARACTER OF THE             f=0;          STRING..
if(l==-1)  // IF LAST REACHES THE FIRST CHARACTER OF THE
l=len-1;      STRING
if(s[f++]!=s[l--]){
return 0;
}
count++;
}
return 1;
}
//TO PRINT THE CIRCULAR PALINDROME STRING
void print(int ii){
int k;
for(k=0;k<len;k++){
if(ii==len)
ii=0;
printf("%c",s[ii++]);
}
}
//DRIVER PROGRAM
void main(){
int i,j;
clrscr();
scanf("%s",s);
for(len=0;s[len]!='\0';len++);
palindrome=ispal();
if(palindrome==1){
printf("palindrome");
}
else{
j=0;
for(i=1;i<len&&true==0;i++,j++){
true=pal(i,j);
if(true==1){
first=i;
last=j;
flag=1;
}
}
if(flag==1){
printf("%s is a circular palindrome of ",s);
print(first);
}
else
printf("\nNot palindrome");
}
getch();
}

Friday, 1 July 2016

Anagram Substring Search (Or Search for all permutations)

Given a text txt[0..n-1] and a pattern pat[0..m-1], write a function search(char pat[], char txt[]) that prints all occurrences of pat[] and its permutations (or anagrams) in txt[]. You may assume that n > m. 
Expected time complexity is O(n)
Examples:
1) Input:  txt[] = "BACDGABCDA"  pat[] = "ABCD"
   Output:   Found at Index 0
             Found at Index 5
             Found at Index 6
2) Input: txt[] =  "AAABABAA" pat[] = "AABA"
   Output:   Found at Index 0
             Found at Index 1
             Found at Index 4
// C++ program to search all anagrams of a pattern in a text
#include<iostream>
#include<cstring>
#define MAX 256
using namespace std;
// This function returns true if contents of arr1[] and arr2[]
// are same, otherwise false.
bool compare(char arr1[], char arr2[])
{
    for (int i=0; i<MAX; i++)
        if (arr1[i] != arr2[i])
            return false;
    return true;
}
// This function search for all permutations of pat[] in txt[]
void search(char *pat, char *txt)
{
    int M = strlen(pat), N = strlen(txt);
    // countP[]:  Store count of all characters of pattern
    // countTW[]: Store count of current window of text
    char countP[MAX] = {0}, countTW[MAX] = {0};
    for (int i = 0; i < M; i++)
    {
        (countP[pat[i]])++;
        (countTW[txt[i]])++;
    }
    // Traverse through remaining characters of pattern
    for (int i = M; i < N; i++)
    {
        // Compare counts of current window of text with
        // counts of pattern[]
        if (compare(countP, countTW))
            cout << "Found at Index " << (i - M) << endl;
        // Add current character to current window
        (countTW[txt[i]])++;
        // Remove the first character of previous window
        countTW[txt[i-M]]--;
    }
    // Check for the last window in text
    if (compare(countP, countTW))
        cout << "Found at Index " << (N - M) << endl;
}
/* Driver program to test above function */
int main()
{
    char txt[] = "BACDGABCDA";
    char pat[] = "ABCD";
    search(pat, txt);
    return 0;
}
Output:
Found at Index 0
Found at Index 5
Found at Index 6