A subset sum algorithm which uses negative and positive integers - java

The checkNum is the target value. array is the array of integers i am looking at. I am using the matrix to hold True and False value to see my possible sums. The checkPower method is only checking if the index i+1 is = 2^n. I am having trouble writing an algorithm that can solve the subset sum problem using negative integers also. I wrote an algorithm but I cannot use it, because as the input size increases I am running out of memory. I was wondering if anyone came up with an algorithm to solve the problem. I would prefer if it was written java, or it could be a pseudo code.
public static ArrayList<Integer> populateArray(boolean[][]matrix, int []array, int checkNum){
int usedElement = 0;
ArrayList<Integer>resultArray = new ArrayList<>();
for(int i = 0; i < matrix.length; i++){
if(checkPower(i+1)){
matrix[i][usedElement] = true;
usedElement++;
if(array[usedElement-1] == checkNum){
resultArray.add(array[usedElement-1]);
return resultArray;
}
}
else{
int tempSum = 0;
int remainingIndex = (int) (i - Math.pow(2, usedElement-1));
matrix[i][usedElement-1] = true;
tempSum += array[usedElement-1];
for(int k = 0 ; k < matrix[remainingIndex].length; k++){
if(matrix[remainingIndex][k]){
matrix[i][k] = true;
tempSum += array[k];
}
}
if(tempSum == checkNum){
for(int j = 0 ; j < matrix[j].length; j++){
if(matrix[i][j]){
resultArray.add(array[j]);
}
}
return resultArray;
}
}
}
return resultArray;
}

Related

Search minimum absolute value difference in java

How can I optimize my algorithm to find the minimum absolute value difference in a given array. Here is my approach which checks each and every element and returning the value.
static int minAbsVal(int[] myArray){
ArrayList<Integer> diffs= new ArrayList(); // all absolute value differences
for (int i = 0; i < myArray.length; i++) {
for (int j = 0; j < myArray.length; j++) {
if (myArray[j]!=myArray[i]){
int absVal =Math.abs( myArray[i]- myArray[j]);
diffs.add(absVal);
}
}
}
int min = diffs.get(0);
for (int i = 1; i < diffs.size(); i++) {
if (min>diffs.get(i)){
min = diffs.get(i);
}
}
return min;
}
Check this solution. If array contains duplicates than it returns 0 if you wanna avoid than you can add conditions also.
static int minAbsVal(int[] myArray){
Arrays.sort(myArray);
ArrayList<Integer> diffs= new ArrayList<Integer>(); // all absolute value differences
for (int i = 0; i < myArray.length-1; i++) {
diffs.add(Math.abs( myArray[i+1] - myArray[i]));
}
Collections.sort(diffs);
return diffs.get(0);
}
Or you can also use :-
static int minAbsVal(int[] myArray){
Arrays.sort(myArray);
int minimum=Math.abs( myArray[1] - myArray[0]);
for (int i = 1; i < myArray.length-1; i++) {
int diff = Math.abs( myArray[i+1] - myArray[i]);
if (diff == 0)
return 0;
else if (minimum > diff)
minimum = diff;
}
return minimum;
}
Check the following solution,
static int minimumAbsoluteDifference(int n, int[] arr) {
int a = Math.abs(arr[1]-arr[0]);
Arrays.sort(arr);
for(int x=0;x<n-1;x++){
if(Math.abs(arr[x]-arr[x+1])<a){
a = Math.abs(arr[x]-arr[x+1]);
if(a==0){
return a;
}
}
}
return a;
}
Dependson the size of your array a good approach would be to divide it in different smaller arrays and look for the minimum of each in pararell and then compare those results .
Anyways you dont need so many iterations, you can just set the baseValue to the first element of the array and when iterating the array compare the baseValue vs the current, if the current is smaller then asign baseValue to current (I would rename baseValue to minValue in that case)

how to check the divergence of 5 numbers in java

I have an array of five integers count[5] and I want to check if their difference is more than 3.
A brute force could be to do : if(count[5]-count[4])>3) ,if(count[5]-count[3]>3)
Is there a better way to do it ?
Not a very efficient way to do it, but
Integer min = Arrays.<Integer>asList(arr).stream().min(Comparator.naturalOrder());
Integer max = Arrays.<Integer>asList(arr).stream().min(Comparator.naturalOrder());
Integer spread = max - min;
will do the trick.
If the array is large, you'll want to do a reduce with a running tracker for min and max at the same time. Let me know if you really need that.
Here a Java code implementation:
public static boolean diff(int[] ar) {
boolean result = true;
for (int i = 0; i < ar.length; i++) {
for (int j = 0; j < ar.length; j++) {
if (Math.abs(ar[i] - ar[j]) > 3) {
result = false;
}
}
}
return result;
}
You can use for loop in case if the numbers in the array are to increase.
int[5] count = {1,2,3,4,5}
for(int i = 0; i < count.length; i++){
for(int j = 0; j < count.length; j++){
if(((count[i] - count[j]) > 3) || ((count[i] - count[j])<-3)){
//TODO
}
}
}

Finding uniques integers in an array

i have an array of integers like this one :
A={1,1,4,4,4,1,1}
i want to count the each number once , for this example the awnser is 2 becuase i want to count 1 once and 4 once
i dont want to use sorting methods
i am unable to find a way to solve it using java.
i did this but it gives me 0
public static void main(String args[]) {
int a[] = { 1,1,4,4,4,4,1,1};
System.out.print(new Test4().uniques(a));
}
public int uniques(int[] a) {
int unique = 0;
int tempcount = 0;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a.length; j++) {
if (a[i] == a[j]) {
tempcount++;
}
}
if (tempcount <= 2) {
unique=a[i];
}
tempcount = 0;
}
return unique;
}
the purpose of the question is to understand the logic of it but not solving it using ready methods or classes
This one should work. I guess this might be not the most elegant way, but it is pretty straightforward and uses only simple arrays. Method returns number of digits from array, but without counting duplicates - and this I believe is your goal.
public int uniques(int[] a) {
int tempArray[] = new int[a.length];
boolean duplicate = false;
int index = 0;
int digitsAdded = 0;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < tempArray.length; j++) {
if (a[i] == tempArray[j]) {
duplicate = true;
}
}
if(!duplicate) {
tempArray[index] = a[i];
index++;
digitsAdded++;
}
duplicate = false;
}
//this loop is needed if you have '0' in your input array - when creating temp
//array it is filled with 0s and then any 0 in input is treated as a duplicate
//again - not most elegant solution, maybe I will find better later...
for(int i = 0; i < a.length; i++) {
if(a[i] == 0) {
digitsAdded++;
break;
}
}
return digitsAdded;
}
Okay first of all in your solution you are returning the int unique, that you are setting as the value that is unique a[i]. So it would only return 1 or 4 in your example.
Next, about an actual solution. You need to check if you have already seen that number. What you need to check is that for every number in the array is only appears in front of your position and not before. You can do this using this code below.
public int uniques(int[] a) {
int unique = 1;
boolean seen = false;
for (int i = 1; i < a.length; i++) {
for (int j = 0; j < i; j++) {
if (a[i] == a[j]) {
seen = true;
}
}
if (!seen) {
unique++;
}
seen = false;
}
return unique;
}
In this code you are iterating over the number you have seen and comparing to the number you are checking (a[i]). You know that for it to be unique you cant have seen it before.
I see two possible solutions:
using set
public int unique(int[] a) {
Set<Integer> set = new HashSet<>();
for (int i : a) {
set.add(i);
}
return set.size();
}
using quick sort
public int unique(int[] a) {
Arrays.sort(a);
int cnt = 1;
int example = a[0];
for (int i = 1; i < a.length; i++) {
if (example != a[i]) {
cnt++;
example = a[i];
}
}
return cnt;
}
My performance tests say that second solution is faster ~ 30%.
if restricted to only arrays, consider trying this:
Lets Take a temporary array of the same size of orignal array, where we store each unique letter and suppose a is your orignal array,
int[] tempArray= new int[a.length];
int tempArraycounter = 0;
bool isUnique = true;
for (int i = 0; i < a.length; i++)
{
isUnique = true;
for (int j = 0; j < tempArray.length; j++)
{
if(tempArray[j] == a[i])
isUnique = false;
}
if(isUnique)
{
tempArray[tempArraycounter] = a[i];
tempArraycounter++;
isUnique = false;
}
}
now tempArraycounter will be your answer ;)
Try Following code:
int test[]={1,1,4,4,4,1,1};
Set<Integer> set=new LinkedHashSet<Integer>();
for(int i=0;i<test.length;i++){
set.add(test[i]);
}
System.out.println(set);
Output :
[1, 4]
At the end set would contain unique integers.

Generating an array of primes and using it in the program

I have generated first 10 million primes using the sieve of eratosthenes in Java. Following is the code for the same:
private static int[] gprime(int max) {
boolean[] isComposite = new boolean[max + 1];
for (int i = 2; i * i <= max; i++) {
if (!isComposite [i]) {
for (int j = i; i * j <= max; j++) {
isComposite [i*j] = true;
}
}
}
int numPrimes = 0;
for (int i = 2; i <= max; i++) {
if (!isComposite [i]) numPrimes++;
}
int [] primes = new int [numPrimes];
int index = 0;
for (int i = 2; i <= max; i++) {
if (!isComposite [i]) primes [index++] = i;
}
return primes;
}
I need to use this array a lot of times in my program after generating this array of primes. If i keep writing
gprime(179424673)[i]
(179,424,673 is the ten millionth prime) whenever I need to access any item from the array, the execution is very inefficient as it would run the method over and over again.
How do I simply get an array to access any prime from that array?
P.S. Why do I have downvotes on this question? Are people who are new to programming simply downvoted and discouraged like this to ask any further questions on stackoverflow? I clearly mentioned what my question is along with the code of what I have done so far.
Store it as a class level constant,
public static final int[] PRIMES = gprime(179424673); // <-- use the biggest value
// you will need here.
Then you can access it (instead of gprime(179424673)[i]) with
PRIMES[i]

Recursion - Adding the subset of an array

I'm trying to decide if the sum of subset is a set num or not)...
I've read through most of the questions so far here on stackoverflow and have come up with nothing. I think the issue I'm finding is that I want to add together the elements in the combitorial subsets created. All together this should be done recursively. With the current code I have, I'm getting a stackoverflow error for recursion. (ironic)
So to clarify:
int[] array = {1,2,3,4,5};
the subset would be the size of say 2 and combinations would be
{1,2},{1,3},{1,4},{1,5},{2,3},{2,4},{2,5},{3,4},{3,5},{4,5}
from this data I want to see if the subset say... equals 6, then the answers would be: {1,5} and {2,4} leaving me with true as a answer. In respect to the signature I would like to keep it the same because it corresponds with another method (outside of the issue because it only sends the array, n, and num to the method)
public static boolean subset(int[] array, int n, int num) {
int count = 0;
int sum = 0;
int[] subarray = new int[n];
int[] temp = new int[array.length - 1];
int[] copy = array;
subarray[count] = array[0];
for (int i = 0; i < n; i++) {
subarray[count] = array[i];
count++;
System.arraycopy(array, i, temp, 0, n);
}
for (int j = 0; j < subarray.length; j++) {
sum += subarray[j];
if (sum == num)
return true;
}
subset(copy, n, goal);
return false;
}
Not an answer but a plausible idea for one?
for (int i = 0; i < array.length; i++) {
// New sublist to store values from 0 to i
int[] list = new int[array.length - 1];
for (int j = 0; j < array.length; j++) {
list[j] = array[j+1];
}
// Here you call this recursively with your Parent list from i+1
// index and working list from 0 to i
subsetSum(list, n, goal);
}
int sum = 0;
for (int i = 0; i < array.length; i++) {
sum += array[i];
}
if (sum == goal) {
return true;
}
return false;

Categories

Resources