Tuesday, August 14, 2012

Remove white spaces in a string in one pass O(n) time

// INPUT : how    are   you  ?
//OUTPUT : howareyou?

#include<stdio.h>
#define MAX 1024

void removeSpaces(char *str){
int i=0,j=0;
while(str[i] != '\0'){
if(str[i] != ' '){
str[j] = str[i];
j++;
}
i++;
}
str[j] = '\0';
}

int main(){
char str[MAX];
fgets(str,MAX,stdin);
printf("%s\n",str);
removeSpaces(str);
printf("%s\n",str);
return 0;
}

Monday, August 13, 2012

Rotating a N X N matrix clockwise by 90 degrees


#include<stdio.h>
#define N 4

void display(int A[][N]){
int i,j;
for(i=0;i<N;i++){
for(j=0;j<N;j++){
printf("%d\t",A[i][j]);
}
printf("\n");
}
}

void rotate(int A[][N]){
int i,j;
int temp;
for(j=0;j<N/2;j++){
for(i=0;i<N-1-2*j;i++){
temp = A[j][i+j];
A[j][i+j] = A[N-1-i-j][j];
A[N-1-i-j][j] = A[N-1-j][N-1-i-j];
A[N-1-j][N-1-i-j] = A[i+j][N-1-j];
A[i+j][N-1-j] = temp;
}
}
}

int main(){
int A[N][N] = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
//int A[N][N] = {{1,2,3},{4,5,6},{7,8,9}};
printf("original matrix :\n");
display(A);
rotate(A);
printf("after rotating 90deg clockwise :\n");
display(A);
return 0;
}

Maximum contiguous sum in Circular Array


// find max contiguous sum in circular array
// INPUT : {8,-8,9,-9,10,-11,12} OUTPUT : 22
// INPUT : {1,-1,2,-1,1} OUTPUT : 3

#include<stdio.h>
#include<stdlib.h>

int findMaxContSum(int A[],int len){
int i,c,max;
for(i=0;i<len;i++){
//printf("%d\n",A[i]);
}
int *S = (int *)malloc(sizeof(int)*len);
int *start = (int *)malloc(sizeof(int)*len);
for(i=0;i<len;i++){
start[i] = -1;
}
S[0] = A[0];
start[0] = 0;
for(i=1;;i=(i+1)%len){
c = (i-1+len)%len;
if(start[i] == start[c]){
break;
}
if(S[c] + A[i] > A[i]){
S[i] = S[c] + A[i];
start[i] = start[c];
} else {
S[i] = A[i];
start[i] = i;
}
}
max = S[0];
for(i=1;i<len;i++){
if(S[i] > max){
max = S[i];
}
}
free(start);
free(S);
return max;
}

int main(){
//int A[] = {8,-8,9,-9,10,-11,12};
int A[] = {1,-1,2,-1,1};
int max = findMaxContSum(A,sizeof(A)/sizeof(int));
printf("%d",max);
return 0;
}

Sunday, August 12, 2012

Reverse a stack in place (without extra memory)



// You are given access to functions push, pop and isempty
void reverse(stack s){
        if(!isempty(s)){
                int temp = pop(s);
                reverse(s);
                pushtobottom(s,temp);
        }
}
 
pushtobottom(stack s, int data){
        if(!isempty(s)){
                temp = pop(s);
                pushtobottom(s,data);
                push(s,temp);
        } else {
                push(s,data);
        }
}

Pairwise swapping of nodes in a Linked List

//pairwise swapping of linkedlist nodes
//INPUT : 1->2->3->4->5->6->7->8->9->10
// OUTPUT : 2->1->4->3->6->5->8->7->10->9

#include<stdio.h>
#include<stdlib.h>

typedef struct node{
int data;
struct node *next;
}Node;

void pairwiseSwap(Node **headRef){
Node *current = *headRef;
Node *temp;
Node *prev = NULL;
while(current != NULL && current->next != NULL){
temp = current->next;
current->next = temp->next;
temp->next = current;
if(prev != NULL){
prev->next = temp;
}
if(current == *headRef){
*headRef = temp;
}
prev = current;
current = current->next;
}
}

Node* newNode(int data){
Node *n = (Node*)malloc(sizeof(Node));
n->data = data;
n->next = NULL;
return n;
}

void buildList(Node **headRef){
int i;
for(i=10;i>0;i--){
Node *n = newNode(i);
n->next = *headRef;
*headRef = n;
}
}

void freeMem(Node *head){
    Node *temp;
    while(head != NULL){
        temp = head;
        head = head->next;
        free(temp);
    }
}

void display(Node *head){
while(head->next != NULL){
printf("%d->",head->data);
head = head->next;
}
printf("%d\n",head->data);
}

void main(){
Node *head = NULL;
buildList(&head);
display(head);
pairwiseSwap(&head);
display(head);
freeMem(head);
}