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 / sowmya
# include<stdio.h>
int main()
{
int i,j,k,sum[30],l,z =0,num = 0,sub,len;
int a[30],index[15][15],temp;
sum[z] = 0;
printf("\n Enter the Max.no. 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;i++)
{
k = len/2;
l = i+k;
for(j=i;j<l;j++)
if(j<len)
{
num++;
}
if(num == k)
{
for(j =i;j<l;j++)
{
if(j<len)
sum[z] = sum[z]+a[j];
else printf("\n num <3!");
}
z++;
sum[z] = 0;
}
num = 0;
}
j = 0;
for(i = 0 ;i <z;i++)
{
printf(" sum[%d] = %d\n",i,sum[i]);
index[j][i] = sum[i];
}
for(i = 0;i<z;i++)
{
for(j = i+1;j<z;j++)
if(sum[i] < sum[j])
{
temp = sum[i];
sum[i] = sum[j];
sum[j] = temp;
}
}
j= 0;
for(i = 0;i<z;i++)
if(index[j][i] == sum[0])
sub = i;
printf(" Sub array of max. sum = { ");
for(i = sub;i<sub+k;i++)
printf(" %d ",a[i]);
printf("}");
return 0;
}
Is This Answer Correct ? | 2 Yes | 2 No |
Post New Answer View All Answers
Synonymous with pointer array a) character array b) ragged array c) multiple array d) none
List the different types of c tokens?
What are the advantages of using macro in c language?
Which header file is essential for using strcmp function?
List the difference between a While & Do While loops?
What are the types of type specifiers?
How can I invoke another program (a standalone executable, or an operating system command) from within a c program?
Hi can anyone tell what is a start up code?
What are the types of variables in c?
What is the benefit of using const for declaring constants?
How will you find a duplicate number in a array without negating the nos ?
What is the function of this pointer?
find the output? void r(int a[],int c, int n) { if(c>n) { a[c]=a[c]+c; r(a,++c,n); r(a,++c,n); } } int main() { int i,a[5]={0}; r(a,0,5); for(i=0;i<5;i++) printf("\n %d",a[i]); getch(); }
What does it mean when the linker says that _end is undefined?
What are the uses of null pointers?