sagar019
BAN USER
// Keeps a count of the depth (number of rucursive calls)
int count;
// Stores the visited elements' INDEX
int* done;
// Returns the number of calls possible without going
// beyond bounds and makes sure it does not visit
// already visited elements
void get_depth_for(int *i, int m, int size)
{
// We continue only if the index is within bounds
if(m < size)
{
// To know if the node has been visited aleady
int found = 0;
for(int x=0; x<count; x++)
{
if(done[x] == m)
{
found = 1;
break;
}
}
if(0 == found)
{
// If we are here, that means
// the index has not been visited
// AND
// the index is within bounds
done[count++] = m;
// MOST IMPORTANT PART!
// Call the function recursively with
// the element at index m as the index
// of the next element
get_depth_for(i, i[m], size);
}
}
}
int main()
{
int i[4] = {3, 1, 2, 0};
int m;
int highest = 0;
done = malloc(sizeof(i));
for(m=0; m<4; m++)
{
count = 0;
get_depth_for(i, m, sizeof(i));
if ( highest < count )
{
highest = count;
}
}
printf("%d\n", highest);
return 0;
}
int sum_pair_n(const int *i, const int size, const int sum)
{
int tail = size - 1;
int head = 0;
int iSum;
while(head < tail)
{
iSum = i[head] + i[tail];
if(iSum == sum)
{
return 1;
}
else if(iSum < sum)
{
head++;
}
else if(iSum > sum)
{
tail--;
}
}
return 0;
}
int sum_pair_logn(const int *i, const int size, const int sum)
{
int head = 0;
int look_for;
while(head < size)
{
look_for = sum - i[head];
int h=head+1, t=size;
while(h<=t)
{
if(i[(h+t)/2] == look_for)
{
return 1;
}
else if(i[(h+t)/2] > look_for) t = (h+t)/2 - 1;
else if(i[(h+t)/2] < look_for) h = (h+t)/2 + 1;
}
head++;
}
return 0;
}
int main()
{
int i[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
printf("%d\n", sum_pair_n(i, 10, 5));
printf("%d\n", sum_pair_logn(i, 10, 4));
return 0;
}
int main()
{
int i[10] = {1, 5, 3, 4, 2, 6, 9, 8, 7, 0};
int k = 2;
int m;
mergesort_s(i, 10);
int *s, *f;
s=f=i;
while( *s < (*f + k ) )
{
s++;
}
int count = 0;
while(s < (i + 10))
{
if((*s - *f) == k)
{
count++;
}
f++;
while( *s < (*f + k ) )
{
s++;
}
}
printf("%d\n", count);
return 0;
}
This should time complexity : O(nlogn) + O(n) -> O(n)
- sagar019 January 25, 2014
Replcarton941, Android test engineer at 8x8
My name is Lilly. I grew up in Somerset and currently live in the US. One desire that has always ...
RepLauraJOles, Android Engineer at A9
Hi i am Laura.I live in Us from my childhood and i love my country.My father he is ...
Repannawellson007, Area Sales Manager at 8x8
Hey my name is anna And i am working as a content writer in Search engine optimization company.My component ...
RepI graduated from College with a master’s degree in arthrogryposis. After graduation I am working as a manager in ...
RepJe suis Hudson Will des États-Unis. Je travaille comme technicien dans une entreprise Éco Vannes. J'analyse et vérifie tous ...
Repjesselblagg9, Cloud Support Associate at 247quickbookshelp
Hello, I am Jesse and I live in Springfield, USA. I am working in a company as an engineering technician ...
Repellenabeaudry4, Analyst at Boomerang Commerce
I'm a 23 year-old blogger, make-up junkie and follower of Hinduism.I love Reading because it brings happiness for ...
Repman254183, Project Leader at GoDaddy
I am working as Human Resources Associates, and my duties are for obtaining, recording, and interpreting human resources information within ...
RepI am DavidAxon and I have lived in the US since my childhood.I am working in IT company In ...
Repshirleygause93, Analyst at Absolute Softech Ltd
Hi, I am Shirley from taxes, Conducted a re-profiling project which enabled our customers to receive orders more efficiently.Planning ...
Repmyershllims, abc at ASAPInfosystemsPvtLtd
Hiii, I am Myers and I am from Elizabeth. I am working as a Product designer designing most things I ...
in C
- sagar019 February 08, 2014