On 4/10/13 12:13 AM, rahul sharma wrote:
If you have any other solution ..please post that...i thnik recursion
is ok with base case...we need to scan again after first iteration...??
First of all, the array size and number of iteration both won't be N or
else the answer will always be 0.
I take
Consider n = 5. Naming the array elements as a1,a2,a3,a4,a5 , the final
required sum would be 4C0 * a5 - 4C1 * a4 + 4C2 * a3 - 4C3 * a2 + a1.
That is nothing but the pattern of a binomial expansion. Using this method,
the complexity can be reduced to O(n).
Correct me if I'm wrong!
On Tue, Apr 9,
I guess we are allowed to use the operation multiple times.
I had thought of the solution, could you please look below and tell me
whether I'm missing any case or not.
I know the complexity of the solution is very bad(exponential).
But if you have some other cool algorithm I would be happy to hear.
Your solution fails for a number of reasons:
1. If your array is size 1 or 0.
2. The last digit in the array is found by arr[n-1] - [n-2] not
arr[i+1]-arr[i]
3. Recursion here creates unnecessary overheard
How many times are you calling abc? Draw the recursion tree.
On Tue, Apr 9, 2013 at 11:29
Recursion and iteration don't differ in this algorithm. But avoid using
recursion if same can be done using iteration. In practical cases, system
does not allow very large depth in recursion. So for large values of n,
there can occur segmentation fault.
On Tue, Apr 9, 2013 at 11:43 AM, rahul shar
As code is freeing next pointer
*head = next; will not make any sense to continue the program.
struct node *tmp;
while(start!=NULL)
{
tmp=start;
start=start->next;
free(tmp);
}
On Tue, Apr 9, 2013 at 9:27 PM, Don wrote:
> "head" is not even declared, so I doubt that it would compile.
> I beli
int getsum(int *a, int n)
{
while(--n)
{
for(int i = 0; i < n; ++i)
a[i] = a[i+1] - a[i];
}
return a[0];
}
I'm not really clear about how it is intended to work. It seems that
if you start with an array of N values, each iteration reduces the
number of values by 1, so af
If you have any other solution ..please post that...i thnik recursion is ok
with base case...we need to scan again after first iteration...??
On Wed, Apr 10, 2013 at 12:12 AM, rahul sharma wrote:
> i forgot to add base case..can add wen 2 elemnts are there then there sum
> is stored and we reurn
i forgot to add base case..can add wen 2 elemnts are there then there sum
is stored and we reurn from there...i m in hurry,,,sry for that,,
On Wed, Apr 10, 2013 at 12:11 AM, Don wrote:
> It is O(N^2) because the inner loop takes N steps to execute and that
> loop will be executed N times.
>
> H
It is O(N^2) because the inner loop takes N steps to execute and that
loop will be executed N times.
However, I would suggest not using recursion. There is no reason to
not do it iteratively. Your recursive solution has no base case so it
will recurse until your computer runs out of stack space, a
It is helpful to draw a picture of the linked list. What you are doing
here is freeing the second item in the list and then pointing head to
the location which you just freed. This is sure to never free the
first item in the list, and it is not guaranteed to free the following
items either. I think
A = {5, 3, 8, 9, 16}
After one iteration A = {3-5,8-3,9-8,16-9}={-2,5,1,7}
After second iteration A = {5-(-2),1-5,7-1} sum =7+(-4)+6=9
Given an array, return sum after n iterations
my sol/
void abc(int arr[],n)
{
for(i=0;ihttps://groups.google.com/groups/opt_out.
sory..following is correct code
void deleteList(struct node** head)
{
/* deref head_ref to get the real head */
struct node* next;
while (*head != NULL)
{
next = *head->next;
free(next);
*head = next;
}
}
On Tue, Apr 9, 2013 at 9:27 PM, Don wrote:
> "hea
"head" is not even declared, so I doubt that it would compile.
I believe that you want to free head, not next.
On Apr 9, 11:31 am, rahul sharma wrote:
> Is the following code correct for linked list deletion or i need to copy
> head in some tem. pointer and dlete and theninitialize head to NULL
14 matches
Mail list logo