Orion arm, Laniakea
BAN USERCreate a dictionary out of the second string.
Traverse first string by word and verify if the word is present in the dictionary.
if present then remove it from the dictionary.
else, add the word to the output buffer.
unordered_set<string> dict;
void createDictonary(string str)
{
istringstream iss(str);
for (string s; iss >> s;)
dict.insert(s);
}
void func(string str1, string str2)
{
createDictonary(str1);
istringstream iss(str2);
for (string s; iss >> s;)
{
unordered_set<string>::iterator pos = dict.find(s);
if (pos != dict.end())
dict.remove(s);
else
cout << s << " ";
}
cout << endl;
}
Sum the total work hours.
Divide by the number of workers.
you can deal with odd number of workers/work hours by incrementing the work hours until workhours%#ofworkers is zero and then divide by #ofworkers.
the question is about getting the minimum number of hours required not about how/who they are assigned to.
Get the TotalSum.
targetSum = TotalSum/2
iterate and sum from the beginning until the sum is equal to targetSum.
return array[0 to index] & array[index to len]
you could think about when the totalSum is odd/even and handle it accordingly.
DS: Graph of locations.
Identify the location (node) with the given coordinates.
Return 5 closest nodes.
Maintain a min and max heap.
Pick elements from top of max heap followed by top of min heap until the elements on top of the two heaps are the same at which pick from either of the heap and return.
o(n) memory, o(nlogn) speed.
can be done in O(n) .
Since the order of non zero numbers does not matter!
algo:
Identify the first zero from the start.
Identify the first non zero from the end.
if no cross over
switch.
continue until crossover.
can be done in O(n) .
Since the order of non zero numbers does not matter!
algo:
Identify the first zero from the start.
Identify the first non zero from the end.
if no cross over
switch.
continue until crossover.
sleeky, kudos
- Orion arm, Laniakea January 02, 2016I'd give you more votes if I could to get your answer to top. There are some pretty unreasonable answers at the top right now.
- Orion arm, Laniakea December 31, 2015I'm not sure if I understanding it right, why can't we just pre-process the string by creating a array (if provided the element doesn't exceed 2^64 for a 64 bit machine) or else create a array of pointers pointing to the location of the element within the string or to object with pointer to the location of the element in string and a variable indicating the length of the element.
- Orion arm, Laniakea December 24, 2015what is 'w' please
- Orion arm, Laniakea December 10, 2015Take first K sums. i.e.
a[ ] = { # , # , # , # , ....... }
| X | X | X | ......
b[ ] = { # , # , # , # , ...... }
}
void Summer ( int index, int k )
{
static sums = 0;
//hypothetical heap
Heap myHeap;
myHeap.Add(a[index] + b[index]); sums++; //a[i] + b[i]
if (sums >= k) return;
myHeap.Add(a[index] + b[index+1]); sum++; //a[i] + b[i+1]
if (sums >= k) return;
myHeap.Add(a[index+1] + b[index]); sum++ //a[i+1] + b[i]
if (sums >= k) return;
Summer(index+1, k);
}
we now have the first k highest sums (doesn't matter if you need the first k lowest sums index changes and index+1 will be index-1 in the function). Go through the heap to get the kth highest or lowest depending on your need and max or min heap constructed.
- Orion arm, Laniakea November 23, 2015thumbs up!
- Orion arm, Laniakea April 11, 2015nice one Mr. Manoj
- Orion arm, Laniakea October 30, 2014If there is no repetitions then binary search tree with pre-order traversal will deliver. Again space is the con.
- Orion arm, Laniakea October 29, 2014Nice one Khush. I'd consider it 2 * O(n) but appreciable.
- Orion arm, Laniakea October 10, 2014My bad, thanks noted and edited.
- Orion arm, Laniakea September 21, 2014No Recursion needed.
Complexity: O(n) * no of combinations
The simplest way to solve this is to consider the number of diff combinations (num_of_comb) and representing that in binary starting from 0 to (num_of_comb - 1).
Ex a?b?c? has 8 comb. i.e 2^3 . i.e. 2 power no of '?'s. Obviously the possible combinations are 000, 001, 010, ..... 110, 111.
Complexity: O(n) * no of combinations.
O(n) to find no of '?'s.
Down below is the complete working code with minor description along. Comment if you can improvise further.
#include <iostream>
#include <cmath>
using namespace std;
void stringReplace ( char *str )
{
char *temp = str;
int count = 0, num_of_comb = 0, value = 0;
while ( *temp ) {
if ( *temp == '?' )
count++;
temp++;
}
temp = str;
num_of_comb = pow(2,count); // count the num of combinations
cout << "There are " << num_of_comb << " different possible combinations";
while ( value < num_of_comb ) {
cout << endl;
int calc = value;
/* use the standard Math to convert num to its binary form, modify its output to the present context" */
while ( *temp ) {
if ( *temp == '?' ) {
char ch = ( calc % 2 ) ? '1' : '0';
calc /= 2;
cout << ch << flush; // do use 'flush' instead of unwanted 'newline' on Linux console to flush the buffer
}
else cout << *temp << flush;
temp++;
}
temp = str;
value++;
}
}
int main ()
{
char A[] = "????"; // makes it easy to see the combinations. You could obviously add letters
stringReplace(A);
while (1);
}
* Make a simple hash array to find the number of times each digit has been found.
* Find the pattern of repetition. ( I have considered the pattern to be the one which repeats the most in the given input. )
* Go through the hash array for counts considering modulus to find the non repeated number.
Here is the working code. ( can be improved further. )
void findNonRepeated(char* s ) {
char a[10];
char P1 = 0, P2 = 0; // pattern 1 and pattern 2
int P1count = 0, P2count = 0; // pattern counter
for ( int i = 0; i < 10; i++ ) a[i] = 0;
while (*s) {
a[*s]++;
s++;
}
for ( int i = 0; i < 10; i++ ) {
if ( a[i] > 1 ) {
if ( a[i] != P1 && P1 == 0 ) P1 = a[i];
else if ( a[i] != P2 && P2 == 0 ) P2 = a[i];
if ( (a[i] % P1) == 0 ) P1count += 1;
else if ( (a[i] % P2 ) == 0 ) P2count += 1;
}
}
int P;
P = ( P1count > P2count ) ? P1 : P2;
cout << "\nThe non repeated numbers are: " << endl;
for ( int i = 0; i < 10; i++ ){
if ( (a[i] % P) != 0 ) cout << i << ", ";
}
cout << "\n";
}
Repsheenaamajors, System Administrator at Achieve Internet
Teach art classes to a diverse array of students of varying ages and abilities. Strong desire to incorporate a multidimensional ...
Rep
- Orion arm, Laniakea April 30, 2017