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
Write a program to check palindrome number in c programming?
What is malloc calloc and realloc in c?
How do you convert a decimal number to its hexa-decimal equivalent.Give a C code to do the same
Why is this loop always executing once?
What are the header files used in c language?
I just typed in this program, and it is acting strangely. Can you see anything wrong with it?
What are the advantages of using Unions?
Can a local variable be volatile in c?
What is #ifdef ? What is its application?
What do you mean by scope of a variable in c?
Tell us something about keyword 'auto'.
Given below are three different ways to print the character for ASCII code 88. Which is the correct way1) char c = 88; cout << c << " ";2) cout.put(88);3) cout << char(88) << " "; a) 1 b) 2 c) 3 d) constant
What are the rules for the identifier?
Write a program to reverse a linked list in c.
How do you do dynamic memory allocation in C applications?