diff --git a/algorithms/CPlusPlus/Arrays/smallest-possible-sum.cpp b/algorithms/CPlusPlus/Arrays/smallest-possible-sum.cpp new file mode 100644 index 00000000..b838a890 --- /dev/null +++ b/algorithms/CPlusPlus/Arrays/smallest-possible-sum.cpp @@ -0,0 +1,47 @@ +//smallest-possible-sum +//Given an array of n elements,the smallest possible value we can get by adding any elements from the given array + +//time complexity : O(n) || space complexity : O(1) +// n: size of array + +#include +using namespace std; + +int smallest_possible_sum(vector &arr) //function to calculate smallest_possible_sum +{ + int sum=0,n=arr.size(); + //smallest_possible_sum would be sum of all negative numbers in the array if there are any + // or else equal to the smallest element in the array(if all numbers are >=0) + for(int i=0;i>size; + + vector arr(size); // for storing array of n elements + for(int i=0;i>arr[i]; + + int res= smallest_possible_sum(arr); + + cout<<"The smallest possible sum is : "<