Max Odd Sum | Practice | GeeksforGeeks

Given an array of integers, check whether there is a subsequence with odd sum and if yes, then finding the maximum odd sum. If no subsequence contains odd sum, print -1.

Example 1:
Input:
N=4
arr[] = {4, -3, 3, -5}
Output: 7
Explanation:
The subsequence with maximum odd
sum is 4 + 3 = 7

Example 2:
Input:
N=5
arr[] = {2, 5, -4, 3, -1}
Output: 9
Explanation:
The subsequence with maximum odd 
sum is 2 + 5 + 3 + -1 = 9

Your Task:
Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function findMaxOddSubarraySum() that takes array arr and integer N as parameters and returns the desired value.
 
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).

Constraints:
2 ≤ N ≤ 107
-103 <= arr[i] <= 103
 
0
hasnainraza1998hr1 month ago
C++, 0.5
long long int findMaxOddSubarraySum(long long int arr[], long long int n)   {       bool flag = true;       long long int maxSum = 0;       int minAbsValue = INT_MAX;       for(int i=0;i<n;i++){           if(arr[i]>0)               maxSum += arr[i];           if(arr[i]%2!=0){               flag = false;               if(abs(arr[i])<minAbsValue)                   minAbsValue = abs(arr[i]);           }       }       if(flag == true)           return -1;       if(maxSum%2==0)           maxSum -= minAbsValue;       return maxSum;   }
-1
Ramdeo Yadav1 year ago
Ramdeo Yadav
https://uploads.disquscdn.c...
0
363-AISHWARYA THORAT1 year ago
363-AISHWARYA THORAT
Correct Answer.Execution Time:1.36
JAVA SOLUTION.:
class Solution{     long findMaxOddSubarraySum(long arr[], long n) {
    boolean isOdd = false;
    long sum = 0;      for (int i = 0 ; i < n ; i++)     {
        if (arr[i] > 0)             sum = sum + arr[i];
        if (arr[i] % 2 != 0)         {             isOdd = true;             if (min_odd > Math.abs(arr[i]))                 min_odd = Math.abs(arr[i]);         }     }
    if (isOdd == false)     return -1;
    if (sum % 2 == 0)         sum = sum - min_odd;
    return sum; }}
0
TayfunYirdem2 years ago
TayfunYirdem
Two-lines solution with LINQhttps://ide.geeksforgeeks.o...
+1
rahul495 years ago
rahul49
how to deal with LTE issue in my code
0
||||| | R A T H O R E | |||||5 years ago
||||| | R A T H O R E | |||||
few test cases you guys can use to check your algorithm.....
552 5 -4 3 -144 -3 3 -532 4 6384136 27 165 43 85 136 242 -101 171 112 -223 -60 -191 13 176 -210 176 -78 -14 -39 118 -183 179 32 -220 112 -127 -183 -115 179 52 -228 -192 -181 -83 143 206 -239 -208 -21 123 171 169 34 -213 -52 74 65 120 163 -224 -159 230 206 123 112 -80 246 31 55 175 -166 77 86 -245 96 -21 63 107 -126 145 -168 -205 64 117 184 114 -207 0 -163 58 26 -72 38 -166 153 -99 4 149 182 -190 -74 118 -11 -238 -24 -164 -156 -211 45 -180 184 128 217 -149 -153 152 67 242 -98 6 51 30 36 191 115 -61 194 -131 190 -21 -219 -133 -153 21 231 -75 -41 177 -183 106 247 103 -164 215 56 -67 -31 -126 -222 121 -18 79 -247 -231 20 118 -42 -35 90 -101 46 -27 -132 -5 96 201 171 -195 129 238 14 -22 91 100 -57 -250 -216 14 -126 164 237 106 -7 241 -23 115 109 186 182 -199 187 -22 25 157 224 -129 108 145 -221 -13 -15 43 68 178 -107 -239 178 -221 26 154 193 13 -137 -212 -144 90 154 68 -122 -62 119 167 167 246 74 -7 220 -67 240 249 22 -25 -106 -160 -245 -111 204 36 -81 -168 -208 214 -53 -243 105 54 98 -139 -128 78 49 93 -4 -182 90 172 61 60 -145 51 -89 -20 128 55 70 -14 194 -124 -228 215 -42 166 32 8 174 -113 -188 -126 -150 -214 202 149 129 -200 218 -179 223 -119 131 180 183 144 -90 -87 -51 231 149 246 209 23 63 -82 -60 -155 176 216 -166 90 -160 -66 126 -208 186 -143 195 6 -71 168 137 162 98 -78 -91 -241 86 -40 92 -163 -44 51 -37 122 71 5 69 -151 -29 154 189 61 190 -83 -45 -22 -123 -100 234 -92 170 -26 172 19 146 -169 -120 -166 42 222 -78 100 -125 135 -28 49 -110 -208 148 -37 48 -60 -226 -160 -41 -169 69 86 -18 -95 244 -246 129 19 23 26 100 5 110 -108 -171 134 243 -45 -129 260107 -6 249 161 -123 200 -14 -190 68 -145 -187 -201 -6 -39 55 184 41 125 205 -136 -161 18 243 168 55 132 72 232 -33 -220 -157 -176 -124 -157 236 3 -207 -176 64 -37 -71 127 12 25 -162 169 -40 -18 44 -233 96 -15 -113 -59 -97 193 -177 78 175 41 -40 -232 -33 86 213 -195 -160 108 -120 154 -179 -89 -117 -65 39 -177 -146 101 55 0 118 -247 235 -244 -55 -111 199 -130 217 -24 13 -73 -154 231 115 -190 -214 205 20 -232 -39 92 -218 -54 129 71 20 234 -78 177 -16 -210 33 -178 148 80 -187 97 200 -220 -177 -36 -191 -228 -203 174 -168 185 -18 -46 204 193 148 236 -110 28 -91 12 12 -67 -209 98 -27 74 22 -128 -96 85 71 207 115 -3 -79 26 19 -32 -49 -47 -97 183 157 209 -22 56 47 -30 -166 58 84 -52 241 126 148 -35 -198 -79 -61 -191 -244 -240 -234 -26 -141 -211 -250 128 -141 -197 -169 -136 88 239 176 -183 -103 -27 37 -19 -218 -128 31 125 100 -71 -160 4 100 -119 63 107 244 -69 -169 -147 -30 183 232 -69 237 165 46 75 154 -28 142 -199 47 -218 -116 -69 -244 165 -193 -42 -155 249 212 47 233 26 -96 227 59 -163 182 132 -229 16 -187 110 -68 -39 -65 -164 35 180 240 -167 64 226
and its correct answer97-12597315309
ACCEPTED code for further help time complexity O(N) and space O(1)http://ide.geeksforgeeks.or...
0
This comment was deleted.
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.