csgeeg
BAN USER- 0of 2 votes
AnswersGiven a list of quantity of books and corresponding prices.User wants to purchase Q quantity of books. Provide an algorithm which suggest user Q Quantity of books with minimum price
- csgeeg in India
e.g
Quantity of Books 10 20 30 15 25
price of books 100 200 120 130 165
user wants to purchase 500 quantity of books.come up with minimum price| Report Duplicate | Flag | PURGE
Amazon Yahoo Software Engineer / Developer Algorithm
its better if we do the above question using BST where each node contain timestamp and customer id . tree is designed using customer id . each time for new entry in log comes,seach will be log(n) only on the basis of customer id. And as node is already present check the time if its more than one day print else
update the time. And if customer id is not present then create a new node for that customer...
#include <stdio.h>
int main()
{
int num;
scanf("%d",&num);
if((num & 0x000F) == num)
{
num = num ^ 0x000F;
printf("The 1s complement is %d\n",num);
}
else if((num&0x00FF) == num)
{
num = num ^ 0x00FF;
printf("The 1s complement is %d\n",num);
}
else if((num&0x0FFF) == num)
{
num = num ^ 0x0FFF;
printf("The 1s complement is %d\n",num);
}
else
{
num = num ^ 0xFFFF;
printf("The 1s complement is %d\n",num);
}
return 0;
}
let assume temp,head and ptr pointing to head of linklist
My Algo is:
void function(struct node *head)
{
while(head->next != NULL)
{
ptr = head;
head = head->next;
temp = head;
while(num != k && temp->next != NULL)//reversing till K element
{
temp = temp->next;
head->next = ptr;
ptr = head;
head = temp;
num++;
}
num = 1;
while(num != m && head->next != NULL)//traversing till m element
{
head head->next;
}
}
}
struct node *temp = head;
void find_node(struct node *head,int sum)
{
if(head == NULL)
{
return;
}
find_node(head->left,sum);
if(Nested_find_node(temp,(sum-(head->data))) == SUCCESS)
{
Printf("The Second Node of Pair%d",head->data);
return;
}
find_node(head->left,sum);
}
int Nested_find_node(struct node *head,int X)
{
if(head == NULL)
{
return;
}
Nested_find_node(head->left,X);
if(head->data == X)
{
Printf("The First Node of Pair%d",head->data);
return SUCCESS;
}
Nested_find_node(head->left,X);
}
its like doing Nested Inorder inside Inorder and searching the element sum-X. (X= every node of the element)Complexity((logn)(log(n)) without extra apace ...
- csgeeg January 26, 2013#include <stdio.h>
#define row 6
#define col 2
void mirror_image(int array[6][2],int row1,int col1)
{
int index = (row1/2)-1;
int i,j,temp;
//printf("%d%d\n",row,col);
for(i=0;i<col1;i++)
{
for(j=0;j<(row1/2);j++)
{
temp = array[i][index-j];
array[i][index-j] = array[i][index+j+1];
array[i][index+j+1] = temp;
}
}
for(i=0;i<col1;i++)
{
for(j=0;j<row1;j++)
{
printf("%d",array[i][j]);
}
printf("\n");
}
}
int main()
{
int array[row][col];
int index_row,index_col;
for(index_col=0;index_col<col;index_col++)
{
for(index_row=0;index_row<row;index_row++)
{
scanf("%d",&array[index_row][index_col]);
}
}
mirror_image(array,row,col);
return 0;
}
#include <stdio.h>
int array[5][5]={0,0,0,1,1,1,1,1,0,1,0,1,1,1,0,0,0,1,0,0,1,1,1,1,1};
void disp_function(int m,int n)
{
if((m+1)<5)
{
if(array[m+1][n]==1)
{
printf("%d",array[m+1][n]);
disp_function(m+1,n);
}
}
if((n+1)<5)
{
if(array[m][n+1]==1)
{
printf("%d",array[m][n+1]);
disp_function(m,n+1);
}
}
printf("\n");
}
int main()
{
int i,j;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(array[i][j]==1)
{
printf("%d",array[i][j]);
disp_function(i,j);
}
}
}
return 0;
}
#define M 4
#define N 3
int main()
{
int array[M][N];
int t_array[N][M];
int i,j;
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
scanf("%d",&array[i][j]);
}
}
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
t_array[j][i] = array[M-i-1][j];
}
}
for(i=0;i<N;i++)
{
for(j=0;j<M;j++)
{
printf("%d ",t_array[i][j]);
}
printf("\n");
}
return 0;
}
#include <stdio.h>
int main()
{
int array[12]={1,6,7,8,3,2,4,-6,4,13,1,17};
int K,max_value,temp,i=0;
scanf("%d",&K);
temp = K;
max_value = array[0];
for( i=0; i<12;i++)
{
if(K > 0)
{
if(max_value < array[i+1])
{
max_value = array[i+1];
}
K--;
}
else
{
printf("Max value %d\n",max_value);
max_value = array[i+1];
K=temp;
}
}
return 0;
}
#include <stdio.h>
int main()
{
int i=0,j=0;
char *first = (char*)malloc(sizeof(50));
char *second = (char*)malloc(sizeof(50));
scanf("%[^\n]s\n",first);
scanf("%[^\n]s\n",second);
while(*first != '\0')
{
if(*first != ' ')
{
i = i + *first;
}
first++;
}
while(*second != '\0')
{
if(*second != ' ')
{
j = j + *second;
}
second++;
}
if(i == j)
{
printf("Anagram");
}
else
{
printf("not anagram");
}
return 0;
}
#include <stdio.h>
struct link
{
int data;
struct link *next;
};
//struct link *global_ptr = NULL;
void insert_node(struct link **ptr,int value)
{
//global_ptr = *ptr;
struct link *temp = (struct link*)malloc(sizeof(struct link));
struct link *temp_ptr = NULL;
temp->data = value;
temp->next = NULL;
if(*ptr == NULL)
{
*ptr = temp;
}
else
{
temp_ptr = (*ptr);
while(temp_ptr->next != NULL)
{
temp_ptr = temp_ptr->next;
}
temp_ptr->next = temp;
}
}
void display_list(struct link *disp_list)
{
while(disp_list != NULL)
{
printf("The value in the list %d\n",disp_list->data);
disp_list = disp_list->next;
}
}
void display_swap_nodes(struct link **root_node)
{
struct link *test_ptr=NULL;
test_ptr = *root_node;
struct link *temp_node1=NULL;
struct link *temp_node2=NULL;
while(test_ptr->next != NULL)
{
temp_node1 = test_ptr->next;
if(temp_node1->next != NULL)
{
temp_node2 = temp_node1->next;
}
else
{
temp_node2 = temp_node1->next;
temp_node2->next = NULL;
break;
}
test_ptr = temp_node1->next;
if(temp_node2->next != NULL)
{
test_ptr->next = temp_node2->next;
}
else
{
test_ptr->next = temp_node2;
}
test_ptr = temp_node2;
}
}
int main()
{
struct link *link_ptr = NULL;
int count;
int value;
for(count =0 ; count<8; count++)
{
scanf("%d",&value);
insert_node(&link_ptr,value);
}
display_swap_nodes(&link_ptr);
display_list(link_ptr);
return 0;
}
#include <stdio.h>
#define MX 7 //max no of array
//target is value sum value
//let assume given array sequence[MX] ={0, 1, 2, 2, 3, 4, 5};//assuming array is not sorted
int main()
{
int array[MX];
for(count = 0; count<MX;count++)
{
array[MX] = 0;
}
for(count = 0; count<MX;count++)
{
array[sequence[count]] = 1;
}
for(count = 0; count<MX;count++)
{
index = target - sequence[count];
if(index > 0)
{
if(array[index] == 1)
{
print(pair(sequence[count],index));
array[index] = 0;
array[sequence[count]] = 0;
}
}
}
return 0;
}
complexity : 0(n)
it can be done like this..array size Max 9
lets say elements are 123 262 482
we have to take array[MAX-1] extra space
scan the the given no
1st no i got 1 so a[1] 1
2nd no i got 2 so a[2] 2
after scanning all the no we end up getting
a[1] 1 a[2] 4 a[3] 1 a[4] 1 a[5] 0 a[6] 1 a[7] 0 a[8] 1 a[9] 0
print that index in which value is > n/3 (4 in this case)
so ans is 2
- csgeeg June 22, 2013