Interview Question
Quality Assurance Engineer Interview

Amazonfind the largest sum of consecutive integers in an array
Interview Answers
8 Answers
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
def largestSum(arr): curr = arr[0] lsum = arr[0] if len(arr) lsum: lsum = curr return lsum largestSum([2,3,2,1]
Prashant on
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
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
a = [29, 22, 0.22, 0, 999, 22] a.sort() max_a = a[1] print(max_a)
Anonymous on
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
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
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