aneeshas.kollam
BAN USERThe previous algorithm will fail for some conditions, like linked list having 5 items and we need to swap 4 th element. So we need two pointers for holding the location, once the from node is found then upcoming iterations we need to increment the to pointer from start. Sudo code is below
void swapNodeValue(Node*head,const int n)
{
if(n < 1)
return; #wrong n value
Node* from = NULL;
Node* to = head;
int counter = 0;
bool isFromMet = false;
while(head->next !=NULL)
{
counter++;
if(counter > n)
{
to = to->next;
}
else if(counter == n)
{
from = head;
}
head = head->next;
}
if(counter < n)
return; #not enough length
int value = to->data;
to->data = from->data;
from->data = value;
}
O(n) solution
// Returns length of the longest contiguous subsequence
int findLongestConseqSubseq(int arr[], int n)
{
unordered_set<int> S;
int ans = 0;
// Hash all the array elements
for (int i = 0; i < n; i++)
S.insert(arr[i]);
// check each possible sequence from the start
// then update optimal length
for (int i=0; i<n; i++)
{
// if current element is the starting
// element of a sequence
if (S.find(arr[i]-1) == S.end())
{
// Then check for next elements in the
// sequence
int j = arr[i];
while (S.find(j) != S.end())
j++;
// update optimal length if this length
// is more
ans = max(ans, j - arr[i]);
}
}
return ans;
}
We can create directed graph from the matrix. like the value to the next node as the cost of traversal. Then we can use standard algorithms like Dijkstra’s Algorithm , Prim’s Algorithm etc to find out the path between the start and end node.
- aneeshas.kollam July 26, 2016In question, it has asked for getting the total number of AMAZON string from the array. So the best way is store the string in a map [need to handle the duplicate char also], iterate through the 2D array and update the count of chars in map [if present]. Finally check the smallest number from the map [need to some extra calculation for duplicate numbers], can print as the output.
Time complexity for the alg is O(n+m)
Repvivekwebediting, IC3 at Bazaarvoice
Mantra to break boyfriend's marriage is the best and the most effective mantra. If you interested in this mantra ...
RepRhondaPBenedict, SEO at IIT-D
Mantra to break boyfriend's marriage is the best and the most effective mantra. If you interested in this mantra ...
RepDiscover the best preschool for kids Charlotte. Visit Pal-A-Roo’se, one of the trusted early childhood & child development center in ...
Repperryks, xyz at AMD
Perry is a good awarded astrologer. she have such a good experience of solving people problems very well. people was ...
RepEdwards IVF Surrogate is one of the best & most successful provider of surrogate services.We provide moral, emotional, ethical and ...
Need some more clarification on qn. Like
- aneeshas.kollam October 11, 20211. Is the start position of iteration is always start of the channel?
2. Is there any requirement like after reaching the max channel no. it will come to the start of channel no?
We can apply simple algm for resolving this:
1. find the min and max of the channel no's present in the array.
2. Get the start position,
3. if it is always start of the channel means, the answer will be max value of channel.