If we have an array of Interger values, find out a sub array
which has a maximum value of the array and start and end
positions of the array..The sub array must be contiguious.
Take the start add to be 4000.
For Ex if we have an array arr[] =
{-1,-2,-5,9,4,3,-6,8,7,6,5,-3}
here the sub array of max would be
{8,7,6,5} coz the sum of max contiguous array is 8+7+6+5 =
26.The start and end position is 4014(8) and 4020(5).
Answer Posted / manoj
Another approach
# include<stdio.h>
struct index {
int sum;
int start;
int end;
};
int main()
{
struct index sumidx[30],*tmp;
int i,j,z =0,len;
int a[30];
printf("\n Enter the Number of elements to be entered
in the array :\n");
scanf("%d",&len);
printf("\n Enter the array Values : \n");
for(i=0;i<len;i++)
scanf("%d",&a[i]);
for(i = 0;i <len - 1;i++)
{
sumidx[z].sum = a[i];
sumidx[z].start = i;
sumidx[z].end = i;
for(j=i+1;j<len;j++)
if(sumidx[z].sum < (sumidx[z].sum + a[j]))
{
sumidx[z].sum = sumidx[z].sum + a[j];
sumidx[z].start = i;
sumidx[z].end = j;
}
else
break;
z++;
}
tmp = sumidx;
for(i = 0;i<z;i++)
{
if(sumidx[i].start != sumidx[i].end)
if(tmp->sum < sumidx[i].sum)
tmp = sumidx+i;
}
printf("\n sum = %d start = %d end
%d\n",tmp->sum,tmp->start,tmp->end);
return 0;
}
| Is This Answer Correct ? | 2 Yes | 2 No |
Post New Answer View All Answers
What is the right type to use for boolean values in c? Is there a standard type? Should I use #defines or enums for the true and false values?
What is string concatenation in c?
Explain how do you use a pointer to a function?
What are the differences between new and malloc in C?
How can I prevent another program from modifying part of a file that I am modifying?
What are types of structure?
What is static and auto variables in c?
Explain pointer. What are function pointers in C?
cin.ignore(80, _ _);This statement a) ignores all input b) ignores the first 80 characters in the input c) ignores all input till end-of-line d) iteration
Which is better between malloc and calloc?
What is d scanf?
Explain what is a program flowchart and explain how does it help in writing a program?
What are linker error?
What are different types of operators?
What is non linear data structure in c?