Skip to content

Commit 87651b3

Browse files
authored
Create Manimum odd SubArray Sum
1 parent c5c643a commit 87651b3

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

Manimum odd SubArray Sum

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package hackerRank.problem.practice;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.io.PrintWriter;
7+
8+
public class Stats1{
9+
public static void main(String[] args) throws IOException {
10+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
11+
PrintWriter wr = new PrintWriter(System.out);
12+
int T = Integer.parseInt(br.readLine().trim());
13+
for(int t_i=0; t_i<T; t_i++)
14+
{
15+
int N = Integer.parseInt(br.readLine().trim());
16+
String[] arr = br.readLine().split(" ");
17+
int[] array_int = new int[N];
18+
for(int i=0; i<arr.length; i++)
19+
{
20+
array_int[i] = Integer.parseInt(arr[i]);
21+
}
22+
23+
int out_ = findMaxOddSubarraySum(array_int,N);
24+
System.out.println(out_);
25+
}
26+
27+
wr.close();
28+
br.close();
29+
}
30+
static int findMaxOddSubarraySum(int arr[], int n)
31+
{
32+
int m = Integer.MAX_VALUE;
33+
boolean isOdd = false;
34+
int sum = 0;
35+
for (int i=0 ; i<n ; i++)
36+
{
37+
if (arr[i] > 0)
38+
sum = sum + arr[i];
39+
if (arr[i]%2 != 0)
40+
{
41+
isOdd = true;
42+
if (m > Math.abs(arr[i]))
43+
m = Math.abs(arr[i]);
44+
}
45+
}
46+
47+
if (isOdd == false)
48+
return -1;
49+
if (sum%2 == 0)
50+
sum = sum - m;
51+
52+
return sum;
53+
}
54+
}

0 commit comments

Comments
 (0)