Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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


Please Help Members By Posting Answers For Below Questions

Write a program to check palindrome number in c programming?

962


What is malloc calloc and realloc in c?

1216


How do you convert a decimal number to its hexa-decimal equivalent.Give a C code to do the same

1053


Why is this loop always executing once?

999


What are the header files used in c language?

995


I just typed in this program, and it is acting strangely. Can you see anything wrong with it?

942


What are the advantages of using Unions?

1067


Can a local variable be volatile in c?

937


What is #ifdef ? What is its application?

1054


What do you mean by scope of a variable in c?

972


Tell us something about keyword 'auto'.

1010


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

1085


What are the rules for the identifier?

1114


Write a program to reverse a linked list in c.

1066


How do you do dynamic memory allocation in C applications?

1045