technical_123
BAN USER- 3of 3 votes
AnswersGiven an array, with positive and negative integers, arrange it in such a way that, positive numbers occupy even positions and negative numbers occupy odd position. All the remaining extra positive or negative integers should be stored at the end of the array. Remember, the elements of the array should remain in the same order.
- technical_123 in India
EG: Input array {1,-2,3,-4,-5,-6,-7,8,9,4,10,11,12}
output array {1,-2,3,-4,8,-5,9,-6,4,-7,10,11,12}| Report Duplicate | Flag | PURGE
Amazon Software Analyst
// I think this is brute force! but easier to understand for beginners.
void printsets( int arr[])
{
int i,j,k=0;
for(i=0;i<size;i++)
{ for(j=i;j<size;j++)
{
for(k=0;k<j;k++)
{cout<< arr[k]<<endl;}
}
}
}
// I think this is brute force! but easier to understand for beginners.
void printsets( int arr[])
{
int i,j,k=0;
for(i=0;i<size;i++)
{ for(j=i;j<size;j++)
{
for(k=0;k<j;k++)
{cout<< arr[k]<<endl;}
}
}
}
//Find the majority element in O(n) by maintaining two pointers in array.
findCandidate(a[], size)
1. Initialize index and count of majority element
maj_index = 0, count = 1
2. Loop for i = 1 to size – 1
(a)If a[maj_index] == a[i]
count++
(b)Else
count--;
(c)If count == 0
maj_index = i;
count = 1
3. Return a[maj_index]
Volatile variable: Certain variables maybe required by some outside processes, CPU makes an arrangement to make this happen. It basically doesn't optimize this variable when the memory of the process is optimized while rolling out and other such things.CPU keeps the volatile variable available for any process to use, and change! Same applies to threads.
Static : The static keyword can be used to declare variables, functions, class data members and class functions.By default, an object or variable that is defined outside all blocks has static duration and external linkage. Static duration means that the object or variable is allocated when the program starts and is deallocated when the program ends. External linkage means that the name of the variable is visible from outside the file in which the variable is declared. Conversely, internal linkage means that the name is not visible outside the file in which the variable is declared.
Can anyone describe in detail, the alternative option (other than trie)?
- technical_123 August 14, 2013That can be done! Nonetheless, I think there ought to be a better solution to this.
Shifting all the elements is a pain( complexity wise )
Above should be done without using another array.
- technical_123 August 11, 2013
So basically this works all the time!
Can you tell us how did you think of this formula impromptu? Some logic that you must have thought of?
- technical_123 August 18, 2013Do enlighten us!