Program to find the largest sum of contiguous integers in
the array. O(n)

Answer Posted / deepika jadav

the cod is written in c#...similar to java and c.....so interpret it accordingly..

static void Main(string[] args)
{
//int[] arr={2,3,-5,-2,-2,5,6,7,8,-2,3,4};
int[] arr = {-2,-3,-4,-5,-6,-7 };
int max=0;
int sum=0;

int min = 0;
int flag = 0;
for (int i = 0; i < arr.Length;i++)
{
if (arr[i] < 0)
{
sum = 0;
flag++;
if(flag==1)
min = arr[i];
if (flag == arr.Length)
Console.WriteLine("The smallest no amogst negative nos is {0} hence the largest aum is {1}",min,min);
if (min < arr[i])
min = arr[i];
continue;
}
else
{
sum = sum + arr[i];
}

if (max < sum)
max = sum;


}
if(flag!=arr.Length)
Console.WriteLine("the Largest Sum is {0}",max);
Console.ReadLine();
}

Is This Answer Correct ?    0 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

why nlogn is the lower limit of any sort algorithm?

2538


How can you relate the function with the structure? Explain with an appropriate example.

3123


Cluster head selection in Wireless Sensor Network using C programming language.

3370


can you use proc sql to manpulate a data set or would u prefer to use proc report ? if so why ? make up an example and explain in detail

2561


Can you send Code for Run Length Encoding Of BMP Image in C Language in linux(i.e Compression and Decompression) ?

4066






create a C-code that will display the total fare of a passenger of a taxi if the driver press enter,the timer will stop. Every 10 counts is 2 pesos. Initial value is 25.00

6574


Write a routine to implement the polymarker function

4576


write a c program to input initial & final time in the format hh:mm and find the time intervel between them? Ex inputs are initial 06:30 final 00:05 and 23:22 final 22.30

2433


why do you use macros? Explain a situation where you had to incorporate macros in your proc report? use a simple instream data example with code ?

2494


write a function to give demostrate the functionality of 3d in 1d. function prototye: change(int value,int indexX,int indexY,int indexZ, int [] 1dArray); value=what is the date; indexX=x-asix indexY=y-axis indexZ=z-axis and 1dArray=in which and where the value is stored??

4382


could you please send the program code for multiplying sparse matrix in c????

3273


Write a Program in 'C' To Insert a Unique Number Only. (Hint: Just Like a Primary Key Numbers In Database.) Please Some One Suggest Me a Better Solution for This question ??

2123


Sir... please give some important coding questions asked by product companies..

2016


What is data _null_? ,Explain with code when u need to use it in data step programming ?

3008


Hi, i have a project that the teacher want a pyramid of numbers in C# or java...when we click a button...the pyramid should be generated in a listbox/or JtextArea...and the pyramid should have the folowing form: 1 232 34543 4567654 567898765 67890109876 7890123210987 890123454321098 90123456765432109 0123456789876543210 Plz help with codes...didn't find anything on the net.

2883