nishant.cena
BAN USERHere is the O(n) solution. I think it can be further reduced if tree is used.
void twoArrayPairs(int a[], int b[], int m, int n)
{
int i = 0, j = 1, r;
if (n < m || n==m)
{
m = n - 1;
}
r = j;
std::multimap <int, int> pair;
while (i < m && j < n)
{
if (a[i]>b[j])
{
pair.insert({ i, j });
}
if (j == n-1)
{
i++;
j = r + 1;
r++;
}
else
j++;
}
//int size = pair.size();
for (const auto &p : pair)
{
cout << p.first << " " << p.second << endl;
}
}
Yes value swap, that one way to do it.
- nishant.cena May 12, 2014Yes, I second this opinion.
- nishant.cena May 12, 2014This should be comment instead not an answer.
- nishant.cena March 05, 2014I have written the code in different manner using the v at most:-
std::stack<int> s;
std::stack<int> t;
int v;
s.push(1);
s.push(2);
s.push(3);
s.push(4);
s.push(5);
int size=s.size();
for(int i=0; i<size-1; i++)
{
for(int j=0; j<size-2; j++)
{
v=s.top();
s.pop();
if(!s.empty())
{
t.push(s.top());
s.pop();
}
t.push(v);
}
if(i!=size-3)
{
for(int k=0; k<size-2; k++)
{
v=t.top();
t.pop();
if(!t.empty())
{
s.push(t.top());
t.pop();
}
s.push(v);
}
}
else
{
while(!t.empty())
{
s.push(t.top());
t.pop();
}
}
}
please mention the code here.
- nishant.cena May 03, 2013level order traversal it is.
- nishant.cena April 23, 2013Dynamic programming:-Maximum subarray problem.
- nishant.cena April 10, 2013how will you store the data to start binary search on that? As we dont know the size of it.
- nishant.cena March 30, 2013int sum(struct node * root){
if(root == NULL) return 0;
return (root->data - sum(root->left)-sum(root->right));}
level order traversal..
- nishant.cena March 09, 2013skip list best suited if the linked list contains sorted data.
- nishant.cena February 14, 2013
Yes, It should be 0. I think he wanted to type a*b?. In that case it would be 1.
- nishant.cena May 26, 2015