Aalok
BAN USER// divide the number from 9 to 2
// if given number is divided by more then once from one digit then divide
record all the divider
and then sort the divider in ascending order
bool findAllDivident( int p_number, vector<int> &p_divident ){
int l_startDivide = 9;
int l_index=0;
for(; l_startDivide >= 2 && p_number != 1; ){
if( (p_number % l_startDivide ) == 0 ){
p_divident.push_back( l_startDivide ) ;
p_number /= l_startDivide;
}
else
l_startDivide--;
}
return p_number == 1? true : false;
}
//after that sort the vector we will get lowest number
- Aalok January 28, 2014// divide the number from 9 to 2
// if given number is divided by more then once from one digit then divide
record all the divider
and then sort the divider in ascending order
bool findAllDivident( int p_number, vector<int> &p_divident ){
int l_startDivide = 9;
int l_index=0;
for(; l_startDivide >= 2 && p_number != 1; ){
if( (p_number % l_startDivide ) == 0 ){
p_divident.push_back( l_startDivide ) ;
p_number /= l_startDivide;
}
else
l_startDivide--;
}
return p_number == 1? true : false;
}
//after that sort the vector we will get lowest number
- Aalok January 28, 2014Solution using recursive approach without modifying the original content.
#include <stdio.h>
void reverseTheWord(char *sentence, int len)
{
if( *sentence )
{
int i=0;
for(i; sentence[i] != ' '; i++);
reverseTheWord(sentence+i+1, len);
sentence[i] = '\0';
printf("%s ", sentence);
if(i != len ) //for make the string as before because i entered '\0' inplace of ' '(space).
sentence[i] = ' ';
}
}
int main()
{
char sentence[] = "reverse the word of this sentence";
int len = sizeof(sentence)/sizeof(sentence[0]);
reverseTheWord(sentence, len);
printf("\n%s ", sentence);
return 0;
}
o/p:- h+t+t+p://ideone.com/1zE3rK#view_edit_box
- Aalok April 09, 2013@Joe Flip, Please run the code using pen and paper then you will find that there is no need of increament of j when arr1[i] == arr2[j].
what I did ?? I just take the one element of "arr2" and iterate the first array up to less then equal to the value of element of "arr1" and compare if less and unequal then print the value of first array and increase the index and so on.
I followed the above approach and come to this solution..
#include <stdio.h>
int main()
{
int arr2[]={1,3,4,6,8,10,17,34};
int arr1[]={2,8,17,33,44,66,89,100};
int len1 = sizeof(arr1)/sizeof(arr1[0]);
int len2 = sizeof(arr2)/sizeof(arr2[0]);
int i=0,j=0;
while ( (i <len1) && (j <len2))
{
if( i <len1 && j < len2 && arr1[i] < arr2[j] )
{
printf("%d ", arr1[i]);
i++;
}
else if( arr1[i] > arr2[j])
{
printf("%d ",arr2[j]);
j++;
}
else
i++,j++;
}
while(i < len1){
printf("%d ",arr1[i]);
i++;
}
while(j <len2 ){
printf("%d ",arr2[j]);
j++;
}
return 0;
}
Let me know if i am missing anything.
- Aalok April 08, 2013I got a solution using only xor operation.If we do little modification then the Question is same as, A array is given having all the numbers repeated even times except 2 number which is repeated odd no of times then find the no??
#include <stdio.h>
int main()
{
int arr[]={1,3,4,5,6,7,8,9,10,11,13};
int lenofArr = sizeof(arr)/sizeof(arr[0]);
int i=0,j=0, from=1, to=13;
int xorResult=0, firstNo=0, secondNo=0, setBit=0;
for(i; i<lenofArr; i++)
xorResult ^= arr[i];
for(i=1; i<= to; i++)
xorResult ^= i;
int backupResult=xorResult;
while(! (backupResult & 1 ))
{
backupResult >>=1;
setBit++;
}
firstNo = secondNo = xorResult;
for(i=0; i<lenofArr; i++)
{
if( (arr[i]>>setBit) & 1)
firstNo = firstNo^arr[i];
else
secondNo = secondNo^arr[i];
}
for(i=from; i<= to; i++)
{
if((i>>setBit) & 1)
firstNo = firstNo^i;
else
secondNo = secondNo^i;
}
printf("the first No is %d", firstNo);
printf("\nthe second no is %d ", secondNo);
return 0;
}
My solution is not in module so it may happen You face problem to understand but still it is good solution.
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
using namespace std;
int main()
{
enum numbers {thirty=21,forty,fifty,sixty,seventy,eighty,ninety,hundred,thousand};
char number[][10]={"zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen",
"fourteen","fifteen","sixteen","seventeen","eighteen","ninteen","twenty","thirty","forty","fifty","sixty",
"seventy","eighty","ninety","hundred","thousand"};
char buf[10];
int n,k,placeValue,value;
cout<<"Enter number"<<endl;
cin>>n;
if(n<=20)
{
cout<<number[n]<<endl;
}
else
{
k=sprintf(buf,"%d",n);
placeValue=pow(10,k-1);
while(n>=20)
{
value=n/placeValue;
n=n%placeValue;
if(value>0)
{
if(k>2)
cout<<number[value]<<" ";
switch(k)
{
case 4:
cout<<number[thousand]<<" ";
break;
case 3:
cout<<number[hundred]<<" ";
break;
case 2:
cout<<number[18+value]<<" ";
break;
}
}
placeValue=placeValue/10;
--k;
}
if(n>0)
{
cout<<number[n];
}
cout<<endl;
}
return 0;
}
Here you can see the o/p, just you have to enter a number.
h+t+t+p://ideone.com/do6kR5#view_edit_box
one possible solution i found using "sum of subset" with little modification to this, probably there will be better solution in terms of time complexity then let me know...
my code is
#include <stdio.h>
void countPossibleSolution(int *source, int length, int sumupto, int target, int i, int *count )
{
if(sumupto == target )
(*count)++;
if( i < length && ( sumupto + source[i] ) <= target )
{
countPossibleSolution(source, length, sumupto+source[i], target, i+1, count);
countPossibleSolution(source, length, sumupto- source[i], target, i+1, count);
}
if (i < length && sumupto != target)
countPossibleSolution(source, length, sumupto, target, i+1, count);
}
int main()
{
int arr[]={1,2,3,4,5,6,9,10};
//int arr[]={2,4,6,8};
int len = sizeof(arr)/sizeof(arr[0]);
int solution[len];
int target = 12, count=0;
countPossibleSolution(arr, len, 0, target, 0, &count);
printf("No of possible sum is %d\n ", count);
return 0;
}
You can see the o/p here
h+t+t+p://ideone.com/FCyWuj#view_edit_box
// Worst Case O(nk) if array are already sorted.
void findMin(int *arr, int k , int len)
{
int i,j,min=INT_MAX, index =0,l=0;
for(i=0; i<=len-k; i++)
{
if(index > i && index < i+k)
{
if(arr[i+k-1] < min)
{
min = arr[i+k-1];
index = i+k-1;
}
}
else
{
min = INT_MAX;
for(j=i; j<i+k ;j++)
if(arr[j] <= min)
{
min = arr[j];
index = j;
}
}
printf("%d ",min);
}
}
This seems to be pair wise reverse of linked list..
This method will reverse the linked list depending on the given value n.
for n=2 // pair wise reverse.
--------------------------------------------
struct linkedlist* reverse(struct linkedlist *head, int n) {
struct linkedlist *temp,*prev,*curr;
temp = prev = head;
int count =n;
while(temp->next && count>1)
{
curr = temp->next;
temp->next = curr->next;
curr->next = prev;
prev = curr;
count--;
}
if(temp->next)
temp->next = reverse(temp->next, n);
return prev;
}
It can be simply done by using backtracking.
---------------------------------------------------------
#include <stdio.h>
void printAllSet(char *arr, char *aux, int start, int depth, int length)
{
int j;
if(start< length)
{
aux[depth]='\0';
printf("%s",aux);
printf("\n");
}
for(j=start; j<length; j++)
{
aux[depth] = arr[j];
printAllSet(arr, aux, j+1, depth+1, length);
}
}
int main()
{
char arr[]="ABC";
int length=sizeof(arr)/sizeof(arr[0]);
char aux[length];
printAllSet(arr, aux, 0, 0, length);
return 0;
}
o/p-http://ideone.com/qc4RD
Using circular double linked list and hash table will make it more easier.
In circular double linked list we need not to store the address of tail node.
1>enqueue is O(1).
2>dequeue is O(1).
3>deleteion can be done using hash table which will store value and address of node.
4>searching can be done using hash table in O(1).
calling same function two times gives the desired result by interchanging the trees.
int isSubTreeOrNot(struct BTree *tree1, struct BTree *tree2)
{
if( (!tree1 && !tree2 ) || (tree1 && !tree2) )
return 1;
else if((!tree1 && tree2))
return 0;
else if(tree1->data == tree2->data)
return( isSubTreeOrNot(tree1->left,tree2->left) && isSubTreeOrNot(tree1->right, tree2->right) );
else
return( isSubTreeOrNot(tree1->left,tree2) && isSubTreeOrNot(tree1->right, tree2) );
}
This problem can be solved by modified Dutch National Flag problem.
Instead of '0' we have -ve numbers
Instead of '1' we have 0's(zero).
and for 2 we have +ve Numbers.
------------------------------------------
So modified algo will be
void swap(int *a, int *b)
{
int c;
c = *a;
*a = *b;
*b = c;
}
void sortNumbers(int *arr, int len)
{
int low=0,mid=0,high=len-1;
while(mid <= high)
{
if(arr[mid]<0)
swap(&arr[low++],&arr[mid++]);
else if(arr[mid]==0)
mid++;
else
swap(&arr[mid], &arr[high--]);
}
}
This problem can be solved by modified inorder traversal.
conventional inorder-
---------------------
visit left child
root
then right child
---------------------
modified inorder-
------------------------
1>first visit right child
2> then root
3>then left child
-------------------------
In b/w them we can keep track of last visited node.(Let prev)
when we shall find the actual node we would have next biggest node(in prev).
O(n)
struct LinkedList* deleteDuplicate(struct LinkedList *head, int data)
{
struct LinkedList *flag , *temp=head;
int i;
//delete duplicate data if start from first node
while( (head) && (head)->data == data)
{
temp = head;
head = temp->next;
if(temp)
{
temp->next = NULL;
free(temp);
}
}
// delete node if repeated data after the start node
while(temp)
{
flag = temp;
temp = temp->next;
if(temp && temp->data == data)
{
flag->next = temp->next;
temp->next = NULL;
free(temp);
temp = flag;
}
}
return head;
}
//using binary search
int findIndex(int *arr, int low, int high, int data)
{
int mid = (low+high)/2;
if(arr[low] == data)
return low;
else if(arr[mid] >= data)
return findIndex(arr, low , mid, data);
else
return findIndex(arr, mid+1, high, data);
}
//assumed that number is present in array
Simple approach for finding subsets of given set.
#include <stdio.h>
void printAllSet(int *arr, int *aux, int start, int depth, int length)
{
if(start<= length)
{
int i;
for(i=0;i<depth;i++)
printf("%d",aux[i]);
printf("\n");
}
int j;
for(j=start; j<length; j++)
{
aux[depth] = arr[j];
printAllSet(arr, aux,start+1, depth+1, length);
start++;
}
}
int main()
{
int arr[]={1,2,3,4};
int length=sizeof(arr)/sizeof(arr[0]);
int aux[length];
printAllSet(arr, aux, 0, 0, length);
return 0;
}
//Using Two Stack
void postorder_traverse(struct tree *root)
{
struct tree *stack1[10],*stack2[10];
int top1=-1,top2=-1,t;
while(1)
{
while(root)
{
push(stack1,root,&top1);
push(stack2,root,&top2);
root=root->left;
}
if(top1==-1 && top2==-1)
break;
if(stack1[top1]==stack2[top2])
{
root = pop(stack1,&top1);
root=root->right;
}
else
{
root = pop(stack2,&top2);
printf("\t %d ",root->data);
root=NULL;
}
}
}
I assumed that the sequence of digit has given.
#include <stdio.h>
#include <string.h>
void findCombination(char (*key)[6], char *toFind, char *temp, int start, int len)
{
if(!(*toFind))
{
temp[start]='\0';
printf("%s\n ",temp);
}
else{
int i=0,j=0;
i=*toFind-'0';
if( !strcmp("NULL", key[i]) )
findCombination(key, toFind+1, temp, start,len);
else{
for(;key[i][j]; j++)
{
temp[start]= key[i][j];
findCombination(key, toFind+1, temp, start+1,len);
}
}
}
}
int main()
{
char key[][6]={"NULL", "vtfrq", "ftk", "wzbg", "rs", "NULL", "fir", "p", "io", "p"};
char toFind[]="9801";
char temp[strlen(toFind)+1];
findCombination(key, toFind, temp, 0, strlen(toFind));
return 0;
}
#include <stdio.h>
#include <limits.h>
int main()
{
unsigned int num;
printf("enter the num \n");
scanf("%d",&num);
int i=INT_MAX ^ (INT_MAX>>1);
while(!(i&num))
i>>=1;
int k=1;
while(i>k)
{
int a=i&num
int b=k&num
if( (a == 0 && b >0) || (a > 0 && b == 0)){
num^=i;
num^=k;
}
i>>=1;
k<<=1;
}
printf("the num is %d \n",num);
return 0;
}
//Explanation
1> First i found the length of binary representation of given number.
2> Then using two pointer, one pointed to LSB and another pointed to MSB of number.
3>If both pointed bit is not same than swaped the bit.
4>Now pointer pointed to MSB shifted to left and Pointer Pointed to LSB shifted to Right.
one possible recursive solution is...
#include <stdio.h>
#include <string.h>
int combination(int *temp, int start, int end)
{
int i;
if( start >= end)
return 1;
int l=0,k=0;
l = combination(temp, start+1, end);
if( (start + 1) < end && (temp[start]*10 + temp[start+1]) <= 26)
k = combination(temp, start+2, end);
return l+k;
}
int main()
{
char string[]="12512";
int temp[strlen(string)+1];
int i;
for(i=0; i<strlen(string); i++)
temp[i]=string[i]-48;
printf("%d\n", combination(temp, 0, strlen(string)));
return 0;
}
one possible approach using backtracking.
Total no of string would be 2^N-1.
#include <stdio.h>
#include <string.h>
void combination(char *string, char *temp, int start, int depth, int length)
{
int i;
if( start <= length && start != 0)
{
temp[depth+1] = '\0';
printf("%s\n ", temp);
}
for(i=start; i<length; i++)
{
temp[depth] = string[i];
combination(string, temp, i+1, depth, length);
temp[depth] = string[i];
depth++;
}
}
int main()
{
char string[]="abcd";
char temp[strlen(string)+1];
combination(string, temp, 0, 0, strlen(string));
return 0;
}
It will show core dump, because you have explicitly deallocate the memory of object "s" and in next statement u are assigning the object "s" to another object, so shallow copy can not takes place because s has no memory.
Another reason for core dump is one memory has been deallocated two times .
One approach is using ternary tree.
1> create a ternary tree of all the words.
2> nodes of this ternary tree, instead of having " isEnd" field would have a pointer of Meaning type.Meaning is a structure having description, synonyms, antonyms, meaning and pronunciation kind of fields.
3>for all non end nodes "isEnd" pointer will be NULL.
One approach is using ternary tree.
1> create a ternary tree of all the words.
2> nodes of this ternary tree, instead of having " isEnd" field would have a pointer of Meaning type.Meaning is a structure having description, synonyms, antonyms, meaning and pronunciation kind of fields.
3>for all non end nodes "isEnd" pointer will be NULL.
one approach is using modified heap data structure .
1> the node will contain three information server no, index of list and value.
2> first i will create the min heap of first element of all M sorted list.
3> Now we can get the minimum element from the top of heap than extract the next item from the server which belong to deleted item.
4> for finding the k'th smallest we need to follow the 3'rd step K times.
one more thing we could do, add an extra field in a node which will contain frequency , then first create bst with 1'st array and increase the frequency of elements by one .Now scan the 2'nd array and
search in bst one by one if element is found then increase the frequency.Now at last check that the frequency of each element of bst is same or not..
Repcinderellagale, Financial Application Engineer at Continental
Working as a Forecasting Analyst at Thorofare for more than three years. There, I am responsible for developing detailed business ...
RepColaraJoshi, Floor manager at Thomes
By profession, I am Floor manager in the Thomes store. I am passionate about astrology and read tronto cards, horoscopes ...
RepNoahTaylor, abc at A9
Accomplished software developer with many years of experience in development of applications. Excels in every stage of the life cycle ...
RepRomilKazi, Member Technical Staff at AppNexus
Training Assistant with 6+ years of experience preparing flawless presentations, assembling progress reports, and providing support and training to secretarial ...
Replindagwingard, Android test engineer at ABC TECH SUPPORT
Hello, my name is Larry and I am a commercial loan officer. We are commercial loan officers who specialize in ...
RepBruceKeter, HTML Experienced at 247quickbookshelp
Bruce , a pediatrician studies psychoanalysis where I try to understand children's needs and family dynamics. To do more research ...
Repreiviasmith, Malwarebytes customer service at ABC TECH SUPPORT
I am Reivia an outgoing and motivated Travel Consultant with over 5 years of experience in delivering professional travel related ...
Repnasliebaker, Solutions Engineer at Edinprotechnologies
Je suis Naslie , un chercheur interdisciplinaire et collaboratif axé sur l'amélioration de la santé humaine et le mentorat de ...
Replushililly, Area Sales Manager at ASAPInfosystemsPvtLtd
I am Graphic designer from Watertown. a professional within the graphic design and graphic arts industry assembles together images, typography ...
RepOliveDavin, abc at 8x8
Experienced forester working in the industry for over 3 years. Solid background in the management of private and public forest ...
Repmonamore609, Android test engineer at Cisco Systems
Data entry clerks are responsible for inputting a high volume of data from multiple sources into a database, ensuring that ...
RepAadavKiva, abc at 8x8
By profession I am a blogger and passionate about writing new and informative blog content who easily understand visitors. Nowaday ...
RepPrishaDavin, abc at 8x8
I promote and provide extraordinary internal external guest services, answer questions, offer information, and provide assistance in a courteous manner ...
h+t+t+p://ideone.com/Kc7xHM
- Aalok January 28, 2014