TechTycoon
BAN USERCan you please try finding value for following array {1,2,8,7,6,0} with your logic ?
 TechTycoon June 24, 2013Good Solution, but requires small modification at
1. how will it handle when a file contains value in a single line without any new line character.
2. If my number digit size vary in two files, then the logic will fails for 5492 and 999999
3. With the above logic the solution for following values will be 10131817 instead of 11497
File1:
5
4
9
8
File 2:
5
9
9
9
import java.util.*;
public class KNumberSubset {
private int number;
private int sum;
private LinkedList<Integer> subset;
private int[] numbers={1,2,3,4,5,6};
public KNumberSubset(int[] numbers,int number) {
// TODO Autogenerated constructor stub
this.numbers=numbers;
this.number=number;
sum=0;
subset=new LinkedList<Integer>();
}
public void findSubset(int startPoint)
{
if(sum==number)
{
System.out.println(subset+" :: "+sum);
}
else
for(int i=startPoint;i<6;i++)
{
sum=sum+numbers[i];
if(sum>10)
{
sum=sumnumbers[i];
break;
}
subset.add((int)numbers[i]);
findSubset(i+1);
sum=sumnumbers[i];
subset.removeLast();
}
}
public static void main(String args[])
{
int[] numbers={1,2,3,4,5,6};
int number=10;
Arrays.sort(numbers);
KNumberSubset ki=new KNumberSubset(numbers,number);
ki.findSubset(0);
}
}

TechTycoon
March 03, 2012 Both are similar, though slightly different in terms of goal and implementation. In a LinkedList, each element is linked to its previous and next element making it easier to delete or insert in the middle of the list. A ArrayList is more as its name subjects used as an array.
Performance is similar, though LinkedList is optimized when inserting elements before the end of the list, where ArrayList is optimized when adding elements at the end.
Hi friends,
I have implemented the above in different logic using java
Let us take the number 15432 for which expected output is 21345
so, here the logic is first we need to identify the smaller number comparing with the prev number beginning from the end
for number 15432
1st comp: currNum=2 prevNum=1(at the beginnin) cond= is curNum<prevNum
if cond failed then store the currNum to Array: 2
2nd comp: currNum=3 preNum=2, here also cond fails and stores the value to array: 2,3
Note: array here will be by default in sort order
.
.
5th comp: curNum=1 preNum=5, here cond satisfies and our logic will start,
now we found the number or hotspot, now start comparing the values in array with the hotspot number
is 1 < firstElment in Array (1<2) if yes replace the number 1 with 2 and 2 to 1 in array
and start adding all the array values to one's position of obtained digit one by one
After Swap num: 2 and in Array 1,3,4,5
After adding values of array: 21345
Output : 21345 is achieved :)
code:
******************
public int findNumber(int num)
{
int currentNum=1;
int prevNum;
ArrayList numbers=new ArrayList();
int actualNum=num;
while(num>0)
{
prevNum=currentNum;
currentNum=num%10;
num=num/10;
if(currentNum<prevNum)
{
for(int i=0;i<numbers.size();i++)
{
int tempNum=(Integer) numbers.get(i);
if(currentNum<tempNum)
{
num=(num*10)+tempNum;
numbers.set(i, currentNum);
for(int j=0;j<numbers.size();j++)
{
int tempNum2=(Integer)numbers.get(j);
num=(num*10)+tempNum2;
}
return num;
}
}
}
else
{
numbers.add(currentNum);
}
}
return actualNum;
}
@Gopal
 TechTycoon November 05, 2013Thanks for the comment,
even for you I/P {3,2,1,3,1,5} the above code will work, assuming that the subset may contains duplicate values
[1, 1, 2, 3, 3] :: 10
[1, 1, 3, 5] :: 10
[1, 1, 3, 5] :: 10
[2, 3, 5] :: 10
[2, 3, 5] :: 10