-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaximum odd SubArray Sum
54 lines (48 loc) · 1.4 KB
/
Maximum odd SubArray Sum
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package hackerRank.problem.practice;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class Stats1{
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter wr = new PrintWriter(System.out);
int T = Integer.parseInt(br.readLine().trim());
for(int t_i=0; t_i<T; t_i++)
{
int N = Integer.parseInt(br.readLine().trim());
String[] arr = br.readLine().split(" ");
int[] array_int = new int[N];
for(int i=0; i<arr.length; i++)
{
array_int[i] = Integer.parseInt(arr[i]);
}
int out_ = findMaxOddSubarraySum(array_int,N);
System.out.println(out_);
}
wr.close();
br.close();
}
static int findMaxOddSubarraySum(int arr[], int n)
{
int m = Integer.MAX_VALUE;
boolean isOdd = false;
int 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 (m > Math.abs(arr[i]))
m = Math.abs(arr[i]);
}
}
if (isOdd == false)
return -1;
if (sum%2 == 0)
sum = sum - m;
return sum;
}
}