Interview Question

Quality Assurance Engineer Interview

-

Amazon

find the largest sum of consecutive integers in an array

AnswerAdd Tags

Interview Answers

8 Answers

4

int consecutive_Sum (int arr[]){ int prev =0,temp_sum=0,ret_sum=0,i=0; for(i=0;arr[i]!='\0';i++){ if(arr[i] == (prev+1)){ temp_sum += arr[i]; prev = arr[i]; if(ret_sum < temp_sum){ ret_sum = temp_sum; } }else{ temp_sum = arr[i]; prev = arr[i]; } } return ret_sum; }

Suman on

0

def largestSum(arr): curr = arr[0] lsum = arr[0] if len(arr) lsum: lsum = curr return lsum largestSum([-2,3,2,-1]

Prashant on

0

int consecutive_Sum (int arr[]){ int prev =0,temp_sum=0,ret_sum=0,i=0; for(i=0;arr[i]!='\0';i++){ if(arr[i] == (prev+1)){ temp_sum += arr[i]; prev = arr[i]; if(ret_sum < temp_sum){ ret_sum = temp_sum; } }else{ temp_sum = arr[i]; prev = arr[i]; } } return ret_sum; }

Anonymous on

1

def maxSubArray(self, nums: List[int]) -> int: result = -sys.maxsize sum_ = 0 for i in range(0, len(nums)): sum_ = max(sum_ + nums[i], nums[i]) result = max(result, sum_) return result

Anonymous on

0

a = [29, -22, 0.22, 0, 999, -22] a.sort() max_a = a[-1] print(max_a)

Anonymous on

0

public static void main(String[] args) { int largerSumOfConsecutive =0; int arr[] = {11,-3,5,7,4,-9}; for(int i =0; ilargerSumOfConsecutive) { largerSumOfConsecutive= sum; } } System.out.println("largest sum of consecutive integers in an array is : "+largerSumOfConsecutive); }

Java solution on

2

int a[]= {-10,2,3,-2,0,5,-15}; max_so_far = Integer.MIN_VALUE; max_ending_here = 0; for(int i=0; i

Charankumar.H on

0

def maxSum(self, nums) max = nums[0] sum = 0 for i in nums: sum+=i if sum > max: max = sum if sum <0: sum = 0 return max

Anonymous on

Add Answers or Comments

To comment on this, Sign In or Sign Up.