Created
May 17, 2016 18:09
-
-
Save digvijaybhakuni/adc4d813c06d139456e93db2ca9c1f1b to your computer and use it in GitHub Desktop.
Test Max SubSet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.dgstack.dev.test_dev; | |
import java.util.Arrays; | |
public class TestSubSetProblem { | |
public static void main(String[] args) { | |
int[] arr = {-1, 8, 4, -2, 5, -5}; | |
Arrays.sort(arr); | |
int len = arr.length; | |
int s =0 , e = 0; | |
print(arr); | |
int max = arr[len -1]; | |
for(int i = len; i >= 0; i--){ | |
for (int j = 1; j <= i; j++) { | |
int sm = sum(Arrays.copyOfRange(arr, i-j, i)); | |
if(sm < max){ | |
break; | |
}else{ | |
max = sm; | |
s = i-j; | |
e = i; | |
} | |
} | |
} | |
print(Arrays.copyOfRange(arr, s, e)); | |
} | |
static void print(int[] a){ | |
for (int i : a) { | |
System.out.print(i+ ", "); | |
} | |
System.out.println(); | |
} | |
static int sum(int[] a){ | |
int sum = 0; | |
for (int i : a) { | |
sum+=i; | |
} | |
//print(a); | |
return sum; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment