bytecode
BAN USERI have two solutions for this:
Time O(nlogn) Space O(1) : Sort the number, traverse and keep track that a number appearing is appearing even or odd number of times. Sorting takes O(nlogn) while traversing is O(n).
Time O(N) Space O(#UniqueElementInArray)
Traverse the array XORing each element and inserting it in a hash table at the same time.
When we are done with the first traverse we will have XOR of all element in the array.
Now since XORing a number with itself is 0 we would be left with XOR of all the numbers that appeared odd number of times.
Now XOR this variable with all the keys present in the hash table, since hash table stored all the unique values it will nullify all the integers in XOR variable and would also insert XOR of any integer which is not present in initial XOR(Even integers).
After all this we would be left will XOR of all the integers having even occurrence, and since we only have one number with even occurrence we would have that number in XOR.
Example: int[] a = { 1, 1, 1, 2, 4, 4 };
xorVariable = 1^1^1^2^4^4 = (1^2);
xorVariable = xorVariable^1^2^4 ( from the hash table)
= 1^2^1^2^4 = 4.
Even I am thinking on a similar line, but I was wondering if there is a way without using the set of number, for the above case you do use a space of O(N), actually O(C*N) where C is the number of caterpillar and N is the number of leaves.
For example in above case array = { 2,4,5 }
If we can come up with a way to calculate the unique factors and then subtract the # multiples.
Here the unique factors would be {2,5} as every leave which will be exhausted by caterpillar with step 4 would anyway would be eaten by caterpillar with step 2.
Once we have this the total # uneaten leaves would be N - (N%2 + N%5 - N%(2*5)) = 10 - (5 + 2 - 1) = 4.
One way to calculate the unique factors would be to find LCM of all the numbers and find all unique factors of that LCM which are also present in the array.
For any given value of 'Kth' glass at level 'L', we can find the three glasses just below it(those glasses who would get equal amount of overflow liquid from the above glass).
The three glasses below glass #K at level L would be:
K, K + L and K + L + 1 of course at level (L+1).
Suppose we want to calculate the amount of liquid in glass K at level L
One way to calculate it would be to calculate the amount of liquid in each of the glass at level L and return the amount in Kth glass. Since we know the glass # below each glass, starting from glass 1 level 1 we can easily calculate this.
Problem : Too much space and calculation of unnecessary values.
If we have a way to determine the glasses above the given glass in the question, it would be very easy to calculate the liquid present in it.
As in if the glass in question is #K at level L, all we need is to find the # of glass at level (L-1) and similarly till we reach the top level.
Please suggest.
if ( k==0 )
check if array[k]>127
must be the 1st byte of 2 byte char.
else if array[k]<128
it must be the 1 byte char.
if ( k!=0 )
check if (array[k] range 0-127 )
either it is the 1 byte array, or the 2nd byte of two byte character
check if ( array[k-1] lie in range 0-127)
1 byte character as 1st byte of 2 byte cant contain value less then 128
else if ( array[k-1] lie in range 128-255 )
then that k-1 is either the 1st byte of 2 byte or 2nd byte of 2byte, and
we cannot decide exactly about the one at index k, so we will keep on
moving left until when we can find uniquely about a particular indexed char,
and will move back upto k to find the value.
A similar argument will be used for solving if the value at index k is more than 127.
I have a bit confusion in the question, how can C be the child of both A and B, and if the argument is that B can again have folders of same name, the how will we come to know about cycle.
so considering C the child of A only ( and not B ) I have the following solution.
We can create an array of size 130 ( we could have done it in size 52 but this will be simple to understand )
in every statement:
array[firstRightElement], array[2ndRightElement], array[nextRightElement]=LeftElement;
so for above example:
array[B],array[C], array[a]=A;
similarly array[C],[D],[b],[c] will be B;
array[A] & [d] will be D
now the array will look something like this {D,A,A,B.... }
start from left most index and for every value try tracing its path..like starting from first, A whose parent is D, look for array[D] parent B, look for array[B] parent A so index=traced value.
Was the question to write a function to return longest substring( and not sequence ) which is repeated in a string?
If the question is just what stated above, wont it be write a function to return that the string have all distinct characters.
Example: apple, this string also contains a sequence of length 1 that is 'p' and is repeated.
Use a bit array of size 100, for each of 50 students selected, set the bit of a particular student as soon as he is selected.
Now for a student's rank which is say i in 100 students, start from index 0 and move toward i, counting number of set bits, this will tell us the rank in 50.
Assume the number of slices from i_th pie is n_i so for n_i of every pie i, calculate the GCD of all these numbers.
- bytecode November 19, 2016This is the maximum count of slices we can have if all slices are from same pie.
Now if count_person <= GCD: return GCD
else return (GCD/count_person)*GCD