Is there something wrong with my id array? - java

This program pulls two columns from the input.txt file where the first column indicates the value of the object, and the second column represents the weight. The values are imported and placed into two arrays: the value array and the weight array. The knapsack calculations are then made. There are 23 objects in total represented by the rows of the arrays. My code correctly calculates the total value that is being held in the knapsack, and will print out the correct IDs if the weight capacity entered is 5, but for any other weight the IDs being held in the id array are not correct, but the total value printed out is. Here is my code for both files, and if anyone is able to figure out how to correctly save and print the IDs being held in the knapsack please let me know . . .
input.txt file:
17 5
12 8
15 22
17 11
33 21
43 15
15 4
44 35
23 19
10 23
55 39
8 6
21 9
20 28
20 13
45 29
18 16
21 19
68 55
10 16
33 54
3 1
5 9
knapsack.java file:
//We did borrow concepts from:
//http://www.sanfoundry.com/java-program-solve-knapsack-problem-using-dp/
import java.util.Scanner;
import java.util.*;
import java.lang.*;
import java.io.*;
public class knapsack
{
static int max(int a, int b)
{
if(a > b)
{
//System.out.println(a);
return a;
}
else
//System.out.println(b);
return b;
}
static int knapSack(int maxCapacity, int weight[], int value[], int n)
{
int track = 0;
int i, w;
int foo1 = 0;
int foo2 = 0;
K = new int[n+1][maxCapacity+1];
// Build table K[][] in bottom up manner
for (i = 0; i <= n; i++)
{
for (w = 0; w <= maxCapacity; w++)
{
if (i==0 || w==0)
K[i][w] = 0;
else if (weight[i-1] <= w)
{
//K[i][w] = max(value[i-1] + K[i-1][w-weight[i-1]], K[i-1][w]);
if(value[i-1] + K[i-1][w-weight[i-1]] > K[i-1][w])
{
K[i][w] = value[i-1] + K[i-1][w-weight[i-1]];
//System.out.println("A: "+i);
}
else
{
K[i][w] = K[i-1][w];
id[track++] = i;
//System.out.println("B: "+i);
}
}
else
{
K[i][w] = K[i-1][w];
}
}
//System.out.println(K[foo1][foo2]);
}
return K[n][maxCapacity];
}
public static void main(String args[])throws java.io.FileNotFoundException
{
Scanner sc = new Scanner(System.in);
int n = 23;
File file = new File("input.txt");
Scanner scanner = new Scanner(file);
id = new Integer [n];
//knapval = new int[n];
//knapweight = new int [n];
int []value = new int[n];
int []weight = new int[n];
for(int i=0; i<n; i++)
{
value[i] = scanner.nextInt();
weight[i] = scanner.nextInt();
}
System.out.println("Enter the maximum capacity: ");
int maxCapacity = sc.nextInt();
System.out.println("The maximum value that can be put in a knapsack with a weight capacity of "+maxCapacity+" is: " + knapSack(maxCapacity, weight, value, n));
System.out.println();
System.out.println("IDs Of Objects Held In Knapsack: ");
//System.out.println();
for(int z = 0; z < n && id[z] != null; z++)
{
System.out.println(id[z]);
}
if(id[0] == null)
System.out.println("All objects are too heavy, knapsack is empty.");
sc.close();
scanner.close();
}
protected static Integer [] id;
protected static int [][]K;
}

Your way of recording your solution in the id array is flawed. At the time you do id[track++] = i;, you don’t yet know whether i will be in your final solution. Because of the nested loops you may even add i more than once. This in turn may lead to overflowing the array with a java.lang.ArrayIndexOutOfBoundsException: 23 (this happens for max capacity 12 and above).
I suggest instead of using id, after your solution is complete you track your way backward through the K array (by Java naming conventions, it should be a small k). It holds all the information you need to find out which objects were included in the maximum value.
private static void printKnapsack(int maxCapacity, int weight[], int value[], int n) {
if (K[n][maxCapacity] == 0) {
System.out.println("No objects in knapsack");
} else {
int w = maxCapacity;
for (int i = n; i > 0; i--) {
if (K[i][w] > K[i - 1][w]) { // increased value from object i - 1
System.out.format("ID %2d value %2d weight %2d%n", i, value[i - 1], weight[i - 1]);
// check that value in K agrees with value[i - 1]
assert K[i - 1][w - weight[i - 1]] + value[i - 1] == K[i][w];
w -= weight[i - 1];
}
}
}
}
The above prints the objects backward. Example run:
Enter the maximum capacity:
13
The maximum value that can be put in a knapsack with a weight capacity of 13 is: 36
ID 13 value 21 weight 9
ID 7 value 15 weight 4
If you want the objects in forward order, inside the for loop put them into a list (you may for instance use id from your old attempt), and then print the items from the list in opposite order.

Related

Checking for Duplicates in an Array Backwards

This is the same person who had trouble with the last array problem just one or two days ago.
We've a new assignment which asks us to find and replace duplicates in an array of randomly generated numbers. I wrote a code and sent it to my teacher for feedback; she responded with this solution:
So, take the first random num and store into the first slot (this can be done before the loop). Then, start a loop that creates the second random num and tests backwards to see if there are duplicates from the ones already stored. So, a backwards loops that tests for duplicates and counts down to 0 from the current location and replaces duplicates. Once that test passes, then you'll go to the next element, create a new random number, and then test the ones before it for duplicates.
I've done this here, and it's reduced the number of randomly generated numbers, but I still run into the stray duplicate:
import java.lang.Object;
import java.util.Random;
public class Prog433a {
public static void main(String[]args) {
Random randslct = new Random();
int[] list = new int[20];
int counter = 0;
int index = 0;
int min2 = 0;
System.out.println("\nAfter");
for (int k = 0; k < list.length - 1; k++) {
list [k] = randslct.nextInt(30) + 1;
for (int z = list.length - 1; z >= 0; z--) {
if (list[k] == list[z] && z!=k) {
while (list[k] == list[z]) {
list [k] = randslct.nextInt(30) + 1;
}
}
}
}
int min = list[0];
while (counter < list.length - 1) {
for (int x = 0; x < list.length - 1; x++) { // scroll through the indexes.
if (list[x] < min) {
min = list[x];
index = x; // keep the index of the biggest number.
}
}
System.out.println(list[index]);
min = 100 * (list[index]);
list[index] = 100 * (list[index]); // change the value in the original array so it won't find the same max again
counter++;
}
}
}
System Output:
After
2
5
6
10
11
12
13
15
16
17
19
22
22
24
25
27
28
29
29
After
1
2
2
4
5
7
8
9
10
13
15
16
21
24
25
26
28
29
30
After
1
2
3
5
6
7
11
12
13
14
15
16
18
21
22
25
26
27
29
After
2
3
3
4
6
10
12
14
15
16
17
20
22
23
24
25
26
27
30
After
7
8
11
12
13
14
15
16
17
17
18
19
20
21
23
24
27
29
30
I posted my output towards the bottom.
Because this is an introductory coding class, I'd prefer if the solution did not involve Sets or any of the like. But alas, beggars cannot be choosers.
Is there something I have forgotten to add?
Your problem is that when you detect a duplicate you generate a new number, but you never go back and check that the the newly generated number is not a duplicate of the numbers you already checked. When you run into a duplicate you'll need to reset the checking loop through some mechanism.
I fixed up the code to work around the problem, but it's not the prettiest solution. I also did some minor optimisation as you were looping through unnecessary indices.
import java.util.Random;
public class Prog433a {
public static void main(String[] args) {
Random randslct = new Random();
int[] list = new int[20];
int counter = 0;
int index = 0;
int min2 = 0;
System.out.println("\nAfter");
for(int k = 0; k < list.length - 1; k++) {
list[k] = randslct.nextInt(30) + 1;
boolean unique = true;
for(int z = k - 1; z >= 0; z--) {
if(list[k] == list[z]) {
if(list[k] == list[z]) {
unique = false;
break;
}
}
}
if(!unique) {
// Repeat last index
--k;
}
}
int min = list[0];
while(counter < list.length - 1) {
for(int x = 0; x < list.length - 1; x++) { // scroll through the indexes.
if(list[x] < min) {
min = list[x];
index = x; // keep the index of the biggest number.
}
}
System.out.println(list[index]);
min = 100 * (list[index]);
list[index] = 100 * (list[index]); // change the value in the original array so it won't find the same max again
counter++;
}
}
}
Your mistake is when you try to add a new number. You just check, if it isn't the same as the one before, but not if it is the same as twice before. You can do this as follow:
boolean isDuplicate(int index, int[] list){
for(int i=index-1; i>=0;i--){
if(int[i]==int[index])
return false
}
return true;
}
instead of your inner of the for-loop you can now write:
do{
list[k] = randslct.nextInt(30) + 1;
}while(isDuplicate(k, list));
Also you should change your output, e.g. give the already written output a negative value and ignore negative values. If you want to change the numbers up to e.g. 200 your code now won't work.
Lets take this by example. Consider that the current list that has been generated is:
list = [5, 7, 9, 3, 8, 9]
where 9 is the current number.
Now in the for-loop, you iterate from list[6] to list[0]. Here, in comparision, you come to 2nd index (list[2]) where the condition
list[k] == list[z] && z != k
turns out to be true and a new random number is generated. Lets assume that here the new random number that you generated is '8'. The loop terminates successfully and your array now has a duplicate.

generation of random output 3n+1 pblm

I have been trying to solve the 3n+1 question in java.However my output seems to be very random. The question is
Consider the following algorithm:
1. input n
2. print n
3. if n = 1 then STOP
4. if n is odd then tex2html_wrap_inline44
5. else tex2html_wrap_inline46
6. GOTO 2
Given the input 22, the following sequence of numbers will be printed 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
It is conjectured that the algorithm above will terminate (when a 1 is printed) for any integral input value. Despite the simplicity of the algorithm, it is unknown whether this conjecture is true. It has been verified, however, for all integers n such that 0 < n < 1,000,000 (and, in fact, for many more numbers than this.)
Given an input n, it is possible to determine the number of numbers printed (including the 1). For a given n this is called the cycle-length of n. In the example above, the cycle length of 22 is 16.
For any two numbers i and j you are to determine the maximum cycle length over all numbers between i and j.
The Input
The input will consist of a series of pairs of integers i and j, one pair of integers per line. All integers will be less than 1,000,000 and greater than 0.
You should process all pairs of integers and for each pair determine the maximum cycle length over all integers between and including i and j.
You can assume that no operation overflows a 32-bit integer.
The Output
For each pair of input integers i and j you should output i, j, and the maximum cycle length for integers between and including i and j. These three numbers should be separated by at least one space with all three numbers on one line and with one line of output for each line of input. The integers i and j must appear in the output in the same order in which they appeared in the input and should be followed by the maximum cycle length (on the same line).
My code is as given below
class CC
{
int c,f,l,m;
int returnCount(int i,int j)
{
f=0;
for(int k=i;k<=j;k++)
{
l=k;
c=0;
while(l>1)
{
if(l%2==0)
{
l=l/2;
c++;
}
else
{
l=3*l+1;
c++;
}
}
if(f<c)
f=++c;
}
return f;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int i,j;
CC obj=new CC();
while(sc.hasNextInt())
{
i=sc.nextInt();
j=sc.nextInt();
System.out.println(i+" "+j+" "+obj.returnCount(i,j));
}}}
Now my Input is
605293 606510
956739 956006
826611 825983
756134 756776
478642 479101
815892 815933
719220 719135
929349 929040
And expected output is
605293 606510 341
956739 956006 352
826611 825983 313
756134 756776 362
478642 479101 338
815892 815933 269
719220 719135 274
929349 929040 339
However my output is
605293 606510 341
956739 956006 0
826611 825983 0
756134 756776 362
478642 479101 338
815892 815933 269
719220 719135 0
929349 929040 0
Please help me find the mistake
The problem is that your in your first line the first number is smaller than the second one, but in the second line the first number is bigger than the second one. You have to switch the numbers or find out the bigger one upfront like that:
import java.util.Scanner;
public class CC {
int c, f, l, m;
int returnCount(int i, int j) {
int smaller = Math.min(i, j);
int bigger = Math.max(i, j);
f = 0;
for (int k = smaller; k <= bigger; k++) {
l = k;
c = 0;
while (l > 1) {
if (l % 2 == 0) {
l = l / 2;
c++;
} else {
l = 3 * l + 1;
c++;
}
}
if (f < c)
f = ++c;
}
return f;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int i, j;
CC obj = new CC();
while (sc.hasNextInt()) {
i = sc.nextInt();
j = sc.nextInt();
System.out.println(i + " " + j + " " + obj.returnCount(i, j));
}
}
}
The input and output looks then like that:
956739
956006
956739 956006 352

Finding Multiples of a number between a range [Java]

We have been given a range A<=B and a number M. We have to find how many multiples of M lie in the given range.
My Solution:
import java.util.Scanner;
class ABC {
public static void main(String args[] ) throws Exception {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
for (int i = 0; i < N; i++) {
long A = sc.nextLong();
long B = sc.nextLong();
long M = sc.nextLong();
int res = 0;
while(A<=B)
{
if(A%M==0)res++;
A++;
}
System.out.println(res+"");
}
}
}
Now this is not very efficient. Please tell me how this problem can be solved in least amount of time.
The smallest integer n1 such that n1*M ≥ A is n1=ceil(A/M), and the largest integer n2 such that n2*M ≤ B is n2=floor(B/M). The number of integers between n1 and n2 inclusive is max_of(n2−n1+1 ; 0).
Combining the above we have the answer:
max_of(floor(Z/X)−ceil(Y/X)+1 ; 0)
This is a somewhat standard problem in competitive programming :D
Following should do it (after some more testing).
int r = (b/m - a/m) + (a % m == 0 ? 1 : 0);
explanation
find the amount of multiples m between a/m and b/m
if a is a multiple of m add one more (a % m == 0 ? 1 : 0)
small example PoC
public static void main(String[] args) throws Exception {
int[][] pairs = {{10, 24}, {10, 25}, {11, 24}, {11, 25}, {10, 27}};
int m = 5;
for (int[] pair : pairs) {
int a = pair[0];
int b = pair[1];
int r = (b/m - a/m) + (a % m == 0 ? 1 : 0);
System.out.printf("a: %d b: %d result = %d ", a, b, r);
for (int i = a; i <= b; i++) {
if (i % m == 0) {
System.out.print(" " + i);
}
}
System.out.println("");
}
}
output
a: 10 b: 24 result = 3 10 15 20
a: 10 b: 25 result = 4 10 15 20 25
a: 11 b: 24 result = 2 15 20
a: 11 b: 25 result = 3 15 20 25
a: 10 b: 27 result = 4 10 15 20 25
Try this code :
long A = sc.nextLong();
long B = sc.nextLong();
long M = sc.nextLong();
if (M > A) {
A = M;
}
if(M > B){
System.out.println("0");
return;
}
System.out.println( (((B-A)/M)+1) + "");
Explanation :
if 2 is first multiple than we dont need to check for 3, we have to add 2 to get next multiple so we dont need to traverse from first value to last and check if value is multiple or not, we just need to find first multiple and than number of steps it will take to reach last number means our B by adding M to A.
This works great.
int multiples = 0;
for(int i = x; i<=y; i++){
if(i%z==0)
multiples++;
}
As long as y>x, if that isn't always true just add an additional if statement that checks if y>x or x>y. :)

i want to print all armstrong number between 1 to 1000 in a textbox using awt or swing but i only get last value by my code .So plc help me

i want to print all armstrong number between 1 to 1000 in a textfield using awt or swing but i only get last value by my code .So pls help me
public void actionPerformed(ActionEvent e)
{
String s1=tf.getText();
int n1=Integer.parseInt(s1);
for(int n=0;n<10000;n++)
{
int sum=0;
int number=n;
int original=number;
while(number>0)
{
int r=number%10;
sum+=r*r*r;
number=number/10;
}
if(sum==original)
{
tf1.setText(String.valueOf(original[i]));
}
}
}
For those who don't know, an Armstrong number (or narcissistic number) is a number with n digits that is equal to the sum of each of its digits to the nth power.
(x1*10(n-1))+(x1*10(n-2))...+(x1*10(n-n)) = (x1)n+(x2)n...+(xn)n
This means that if the number is 1 digit, the power will be 1.
Therefore there are 10 1 digit numbers that are Armstrong numbers:
0 = 01
1 = 11
2 = 21
3 = 31
4 = 41
5 = 51
6 = 61
7 = 71
8 = 81
9 = 91
Your code, as written, will not identify any of those numbers as Armstrong numbers.
Your code will also incorrectly identify some numbers as 4 digit Armstrong numbers because you only look for the the cubes (3rd power) of your numbers not the 4th power.
(You don't have to worry about twos because there are no two digit Armstrong numbers)
In order to correctly determine all the possible Armstrong numbers between 1 and 10000, you need to write a "power" loop that finds the nth power of a number by multiplying the number n times.
This would look something like:
//... beginning of your original function
//added a string to hold all the values before printing
string holder = "";
for(int n=0;n<10000;n++){
int sum=0;
//n=original you had duplicate variables (just use n as original)
int number = n;
//while there are still digits left
while(number>0){
//get the smallest digit
int r=number%10;
//----------"Power" loop-----------
int foo = n;
//once smaller than 10, it's only a power of 1 (which is itself)
while(foo>=10){
//this means foo = foo/10
foo /= 10;
//this means r = r*r
r*=r;
}
//this means sum = sum+r
sum += r;
//you should have the hang of it by now
number/=10;
}
//if the sum equals the original number
if(sum==n){
//put that number into the end of a string (separated by newlines `\n`)
holder+=n+"\n";
}
}
//All done, so set the text box value
tf1.setText(holder);
//... whatever code you want to finish up
This should also take care of your problem with the textBox getting overwritten each time. By saving the numbers into a string and then printing all of them at once, only once (no overwriting), you'll get better results.
You always set the current found value. But you should set the previous found values + current found value.
tf1.setText(String.valueOf(original));
But more performant would be to use a stringbuilder object and append the result each time and set this value to the textfield outside the loop.
public void actionPerformed(ActionEvent e)
{
StringBuilder s = new StringBuilder ();
for(int n=0;n<10000;n++)
{
int sum=0;
int number=n;
int original=number;
while(number>0)
{
int r=number%10;
sum+=r*r*r;
number=number/10;
}
if(sum==original)
{
s.append(original + " ");
}
}
tf1.setText (stringBuilder.toString ());
}
Easy, all you do is change the setText() method of the TextField1 component with append().
It works! The remaining will do! Try it once.
public void actionPerformed(ActionEvent e)
{
String s1=tf.getText();
int n1=Integer.parseInt(s1);
for(int n=0;n<10000;n++)
{
int sum=0;
int number=n;
int original=number;
while(number>0)
{
int r=number%10;
sum+=r*r*r;
number=number/10;
}
if(sum==original)
{
tf1.append(String.valueOf(original[i] + " "));
}
}
}
Very simple program in C to list all armstrong number between 1 to 1000000.
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
long a = 1, c=0, b, e, f, d = 0,g=0,p,j,count=0;
printf("All armstron number between 1 and 1000000 is listed below!\n");
while (c <= 1000000)
{
j = c;
if (j >= 10)
{
while (j >= 10)
{
j = j / 10;
g++;
}
}
p = g + 1;
g = 0;
a = c;
f = a;
while (a >= 10)
{
b = a % 10;
d = d + pow(b,p);
a = a / 10;
}
e = pow(a,p) + d;
d = 0;
if (e == f)
{
count++;
printf("%ld\t",count );
printf("%ld\n", f);
}
c++;
}
getch();
}

How to find a duplicate of length 5 in a single array. Java

I create an array that then prints in sets of five. I want to then be able to search the array by 5 to see if there are any duplicates. I've tried but I can only think of a way to search by each value not five. If anyone can point me in the right direction, that would be great. Thanks.
public class findPat {
static int arr [] = new int [10];
static int st = 1;
static int end = 56;
static double t1;
static double t2;
public static void main(String[] args){
t1=System.currentTimeMillis();
for(int n=0; n<100; n++){
for (int i=0; i<arr.length; i++)
arr[i]= (int) (Math.random()* (end-st +1)) +st;
for (int i=0; i<5; i++){
if (i%5==0)
System.out.println();
System.out.print("\t" + arr[i]);}
}
t2=System.currentTimeMillis();
System.out.println();
System.out.println();
System.out.println("\t" + "Total run time is " + ((t2-t1)) + "ms");
}
}
the console looks like this:
18 22 42 14 38
2 2 14 9 8
6 29 38 37 33
6 41 41 27 7
20 41 38 11 50
16 17 41 21 19
40 33 9 10 7
12 54 10 30 36
however each row is in the same array but is just printing 5 at a time.
the console will have more than just those few lines. I want to be able to search the array and check each row against the rest to see how many times it appears, if it does.
You could implement this using a Hashtable. Using your code as a base, I've written an example implementation but without knowing what it is you are trying to do, I can't judge if this is what you are looking for.
import java.util.Hashtable;
public class findPat {
static final int COUNT = 100;
static Hashtable<String, Integer> compareSet = new Hashtable<String, Integer>();
static String groupInteger = "";
static int arr [] = new int [5];
static int st = 1;
static int end = 56;
static double t1;
static double t2;
public static void main(String[] args) {
t1=System.currentTimeMillis();
for(int n = 0; n < COUNT; n++){
for (int i = 0; i < arr.length; i++) {
arr[i] = (int) (Math.random()* (end - st + 1)) + st;
}
for (int i = 1; i <= 5; i++) {
groupInteger += arr[i-1];
System.out.print("\t" + arr[i-1]);
if (i % 5 == 0) {
System.out.println();
if (compareSet.containsKey(groupInteger)) {
System.out.println("duplicate found");
int currentCount = compareSet.get(groupInteger);
compareSet.put(groupInteger, currentCount + 1);
} else {
compareSet.put(groupInteger, 1);
}
groupInteger = "";
}
}
}
t2=System.currentTimeMillis();
System.out.println();
System.out.println();
System.out.println("\t" + "Total run time is " + ((t2 - t1)) + "ms");
}
}
This code keeps track of the unique sets of random numbers by adding them (creating a key value that is the same for every set that has the same values in the same order, the concatenated string takes care of this).
Your code ran in 13 seconds on my system, mine takes 17 seconds. Now if runtime is of crucial importance, you might want to look into hashing techniques. But I'm not sure if you will be able to shave off a lot as you will have to add some extra code which will take extra time.

Categories