Given 6 integers and 1 target value, write a function to get the target value using 6 integers with any on these operations +,*,-,/
This is what I did
public class Solution {
public static void main(String[] args) {
int target =5;
int a[]={1,3,2,10,15,8};
int i,j;
for(j=0;j<6;j++)
for(i=0;i<6;i++)
if(a[i]/a[j]==target) System.out.println(a[i]+"/"+a[j]+"="+target);
for(j=0;j<6;j++)
for(i=0;i<6;i++)
if(a[i]+a[j]==target) System.out.println(a[i]+"+"+a[j]+"="+target);
for(j=0;j<6;j++)
for(i=0;i<6;i++)
if(a[i]*a[j]==target) System.out.println(a[i]+"*"+a[j]+"="+target);
for(j=0;j<6;j++)
for(i=0;i<6;i++)
if(a[i]-a[j]==target) System.out.println(a[i]+"-"+a[j]+"="+target);
}
}
I know this is wrong because I'm just using 1 operation at a time, what can I do to do multiple operations at once. For example, if it were a complex array like {45, 4, 84, 63, 91, 20, 400} and my target value was 455 which is (91*20)/4, then how can my program do that? How can it try all possible operations?