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
What is the match merge ? compare data step match merge with proc sql merge - how many types are there ? data step vs proc sql
Given a spherical surface, write bump-mapping procedure to generate the bumpy surface of an orange
How to palindrom string in c language?
To Write a C program to remove the repeated characters in the entered expression or in entered characters(i.e) removing duplicates. String contains only lowercase characters ['a'-'z']
How can you relate the function with the structure? Explain with an appropriate example.
write a program for area of circumference of shapes
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
How do you verify if the two sentences/phrases input is an anagram using predefined functions in string.h and by using arrays?
Set up procedure for generating a wire frame display of a polyhedron with the hidden edges of the object drawn with dashed lines
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 ?
Write a routine to implement the polymarker function
how to create a 3x3 two dimensional array that will give you the sums on the left and bottom columns
Develop a routine to reflect an object about an arbitrarily selected plane
write a simple calculator c program to perform addition, subtraction, mul and div.
What is the difference between proc means and proc tabulate ? explain with a simple example when you have to use means or tabulate?