hi, let Avik connect you.
** You can give your thought about this content and request modifications if needed from Ask For Modifications page. Thank You.
** You can check all the posts on the Posts page.
** More about me in the About Me section.
Ninja and Subarrays
Problem: Ninja and Subarrays
Problem Link: www.codingninjas.com/codestudio/problems/ninja-and-subarrays_2674106
Companies Asked: Microsoft
Approach:
Traverse through the array.
Move by adding each ith and (i+1)th elements.
Take max among the sums acquired.
Return the result.
Time and Space Complexity:
Time Complexity: O(N)
Space Complexity: O(1)
Code [C++]
#include <bits/stdc++.h>
int sumOfSmallestAndSecondSmallest(int n, vector<int> &arr){
int maxSum = 0;
for(int i=0; i<n-1; i++){
maxSum = max(maxSum, arr[i] + arr[i+1]);
}
return maxSum;
}