From 50bfffbc3735117a3ecfbd8bd73220288d0b6c4f Mon Sep 17 00:00:00 2001 From: Anudeep <58522843+Nagulapally-Anudeep@users.noreply.github.com> Date: Wed, 13 Oct 2021 18:13:44 +0530 Subject: [PATCH] chore(CPlusPlus): add smallest-possible-sum (#560) --- .../Arrays/smallest-possible-sum.cpp | 47 +++++++++++++++++++ algorithms/CPlusPlus/README.md | 1 + 2 files changed, 48 insertions(+) create mode 100644 algorithms/CPlusPlus/Arrays/smallest-possible-sum.cpp 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 : "<