Psycho
BAN USERSimple guy. Loves Programming.
- 0of 0 votes
AnswersState the difference between this two statement.
- Psycho in India
char str[] = "/root//foo/bar" ;
char *str = "/root//foo/bar" ;
Now, you have given an assignment str[in1] = str[in2]
where in1 and in2 both initialize with 0.
In first type of declaration no problem. But in second type of declaration 'Segmentation fault' is there. Why this happens?| Report Duplicate | Flag | PURGE
Adobe Software Engineer / Developer C - 0of 0 votes
AnswersJust Example : "Given 8 cue balls , one is weighing lesser than other 7. Find that(light weight) ball using just 2 chances on balance weight."
- Psycho in India
How to find a general solution to these kind of question? How to divide this set of balls? Is there any finer method or general formula?| Report Duplicate | Flag | PURGE
Software Engineer / Developer Brain Teasers - 0of 0 votes
AnswersAn unsorted array is given, where there is no specific range in which the elements occur. There is only one duplicate element present in it. Let it is a[i]. It should be within the half of the size of the array from where it appears for first time. i.e. the duplicate element should be within (i+(arr_size/2)), at ith index it appears for 1st time. Find the duplicate element with minimum number comparisons.
- Psycho in United States| Report Duplicate | Flag | PURGE
Microsoft
- 0 Answers C++ Topic : Related to 'Multiset' functionality
Consider this sample piece of code!
multiset<int> iends; multiset<int>::iterator et1; while(et1!=iends.end()){ multiset<int>::iterator te=et1; et1++; iends.erase(te); } while(et1!=iends.end()){ multiset<int>::iterator te=et1; iends.erase(te); et1++; }
What is the difference between this two pieces of code?
- Psycho December 09, 2012| Flag | PURGE
You have enqueue a 'null' node to indicate the end of a level, this can be achieve also by having 'size' variables which will indicate sizes of current level and next level. When ever we found a node deficient in having a child - we return the depth of that node.
- Psycho October 23, 2012#include <stdio.h>
char* removeSlash (char *str) {
int rindex = 0, windex = 0 ;
char lchar = ' ' ;
while ( str[rindex] != '\0' ) {
if ( str[rindex] == '/' ) {
if ( lchar != '/' )
str[windex++] = str[rindex] ;
} else { str[windex++] = str[rindex] ; }
lchar = str[rindex++] ;
}
str[windex] = '\0' ;
return str ;
}
int main () {
char str[] = "/root//foo/bar" ;
printf ( "\n%s\n", removeSlash (str) ) ;
return 0 ;
}
If previous one is a 'space' and current character is not a space then increase the word count.
#include <stdio.h>
int countWords (char *str) {
int index = 0 ;
char lchar = ' ', cur ;
int count = 0 ;
if ( str == NULL )
return 0 ;
while (str[index] != '\0') {
if (str[index] != ' ' && lchar == ' ')
count ++ ;
lchar = str[index++] ;
}
return count ;
}
int main () {
char *str = "a " ;
printf ( "\nCount words = %d\n", countWords (str) ) ;
return 0 ;
}
/* The output of this function is stored at *x and *y */
void getTwoElements(int arr[], int n, int *x, int *y) {
int xor1; /* Will hold xor of all elements and numbers from 1 to n */
int set_bit_no; /* Will have only single set bit of xor1 */
int i;
*x = 0;
*y = 0;
xor1 = arr[0];
/* Get the xor of all array elements elements */
for(i = 1; i < n; i++)
xor1 = xor1^arr[i];
/* XOR the previous result with numbers from 1 to n+2*/
for(i = 1; i <= n+2; i++)
xor1 = xor1^i;
/* Get the rightmost set bit in set_bit_no */
set_bit_no = xor1 & ~(xor1-1);
/* Now divide elements in two sets by comparing rightmost set
bit of xor1 with bit at same position in each element. Also, get XORs
of two sets. The two XORs are the output elements.
The following two for loops serve the purpose */
for(i = 0; i < n; i++) {
if(arr[i] & set_bit_no)
*x = *x ^ arr[i]; /* arr[i] belongs to first set */
else
*y = *y ^ arr[i]; /* arr[i] belongs to second set*/
}
for(i = 1; i <= n+2; i++) {
if(i & set_bit_no)
*x = *x ^ i; /* i belongs to first set */
else
*y = *y ^ i; /* i belongs to second set*/
}
/* Now *x and *y hold the desired output elements */
}
void findConsecutiveCharString (char str[], int size) {
int sindex = 0, i, templen = 1 ;
int maxlen = 1 , index = 0;
for ( i = 1 ; i < size ; i ++ ) {
if ( str[i] == (str[sindex]+templen) )
templen ++ ;
else {
if ( templen > maxlen ) {
index = sindex ;
maxlen = templen ;
}
sindex = i ;
templen = 1 ;
}
}
printf ( "\nStart index = %d, maxLength = %d", index, maxlen) ;
}
What is all elements are negative??
just change this one!
void findMaxSumSubset(int *temp,int *x1,int *x2,int *sum_till_now)
{
int i,j,max_sum=0;
for(i=0;i<ROW;i++)
{
(*sum_till_now)+=temp[i];
if((*sum_till_now)>max_sum)
{
max_sum=*sum_till_now;
*x2=i;
}
if((*sum_till_now)<=0)
{
*sum_till_now=0;
*x1=i+1;
}
}
*sum_till_now=max_sum;
}
int matchParenthesis (char str[], int len) {
int i = 0 ;
stack<char> s ;
for ( ; i < len ; i ++ ) {
if ( str[i] == '(' || str[i] == '{' || str[i] == '[' )
s.push (str[i]) ;
else {
switch (str[i]) {
case ')' : if ( s.top() != '(' )
return 0 ;
s.pop () ;
break ;
case '}' : if ( s.top() != '{' )
return 0 ;
s.pop () ;
break ;
case ']' : if ( s.top() != '[' )
return 0 ;
s.pop () ;
break ;
}
}
}
return (s.empty() ? 1 : 0) ;
}
in java you can do that,
Runtime runtime = Runtime.getRuntime () ;
At the starting of your program:
Do,
long maxMemory = runtime.maxMemory();
long allocatedMemory = runtime.totalMemory();
long freeMemory = runtime.freeMemory();
And when you finish your program just check the same! And compare!
- Psycho September 25, 2012At max (3/2)n operations are performed.
It may stop while performing in-place operations, so for then the outer while (1) loop is there.
It will stop when the alphabets are placed completely. It means that all elements are in proper position!
void changeStringa1b2c3d4Toabcd1234(char str[], int size) {
int mid = size>>1 ;
int inext = mid - 1, restartIndex = 1;
char ctemp = str[inext];
while (1) {
do {
if ( ctemp >= 'a' && ctemp <= 'z' )
inext = (ctemp - 'a') ;
else inext = (ctemp-'1')+(size>>1) ;
swap (&ctemp, &str[inext]);
} while ( ctemp != str[inext] ) ;
while ( (restartIndex < mid) && str[restartIndex] == ('a'+restartIndex) ) {
restartIndex ++ ;
}
if ( restartIndex < mid ) {
ctemp = str[restartIndex] ;
} else break ;
}
}
A Bottom-up Approach (Worst case O(n) ):
Node *LCA(Node *root, Node *p, Node *q) {
if (!root) return NULL;
if (root == p || root == q) return root;
Node *L = LCA(root->left, p, q);
Node *R = LCA(root->right, p, q);
if (L && R) return root; // if p and q are on both sides
return L ? L : R; // either one of p,q is on one side OR p,q is not in L&R subtrees
}
search - determines whether a specific word is present in the trie or not. returns 1 and 0 accordingly.
printSelectedWord - determines whether a specific string or substring is present in trie or not. Returns 0 and 1 accordingly!
stringInput holds the current string which is under scanner.
mark is a SIZE*SIZE matrix - tracks which cells are visited.
int isSafe (int i, int j, trie_t trie) {
if ( i >= 0 && i < SIZE && j >= 0 && j < SIZE && mark[i][j] == 0 )
return 1 ;
return 0 ;
}
void dfs_print (int i, int j, trie_t trie, int index ) {
int r[] = {-1,-1,-1,0,1,1,1,0} ;
int c[] = {-1,0,1,1,1,0,-1,-1} ;
int k ;
stringInput[index] = matrix[i][j] ;
stringInput[index+1] = '\0' ;
if ( printSelectedWord(trie,stringInput) == 0 )
return ;
mark[i][j] = 1 ;
for ( k = 0 ; k < 8 ; k ++ ) {
if ( isSafe (i+r[k],j+c[k],trie) == 1 ) {
stringInput[index+1] = matrix[i+r[k]][j+c[k]] ;
stringInput[index+2] = '\0' ;
if ( printSelectedWord(trie,stringInput) == 1 ) {
mark[i+r[k]][j+c[k]] = 1 ;
if (search(&trie,stringInput)) {
printf ( "\nThe matched input is : %s", stringInput) ;
}
dfs_print (i+r[k],j+c[k],trie,index+1) ;
mark[i+r[k]][j+c[k]] = 0 ;
}
}
}
mark[i][j] = 0 ;
}
void printAllWords (trie_t trie) {
int i, j;
int index = 0 ;
for ( i = 0 ; i < SIZE ; i ++ ) {
for ( j = 0 ; j < SIZE ; j ++ ) {
index = 0 ;
dfs_print (i,j,trie,index) ;
}
}
}
Initialize another chess board i.e. 8*8 grid. Fill each state with false state. For each component of opposition fill the opponents move with true value. And all the components' cell of the own marked with true.
Now for king's movement if there is any cell with false value - then there is no checkmate. otherwise if all cells which can be reached by king in its next move are true then its checkmate.
RepOliviaJBlack, Consultant at Apkudo
I am Olivia from San Diego. I am working as a checker in the Thom McAn Store company. My strong ...
RepShirleyCWest, Software Engineer at Agilent Technologies
Hello, me Shirley and I from Savage. I am an artist and I love to doing art and I am ...
The question is quite unclear to me,
- Psycho October 30, 20121. is it circular dll. ?
2. Delete every Nth node means delete the nodes untill only one node is left.
3. If you delete Nth node then your next count should start from (N+1) th node?