# how to compare the values in arrays

I have a question where i want to find f(a) = f(b) using any pigeon hole principle. I have made the program and working fine by giving me the right results.

Here is my code:

public class array1

{

public static void main(String args[])

{

final int TOTAL = 50;

int x,y;

int[] numbers = new int[TOTAL];

for( x = 0; x < numbers.length; x++)

{

y =( ((x * x * x) + ( 3 * x)) % 50);

// System.out.print("when x = " + x);

numbers[x] = y;

}

int z = 0;

for(int l = 0; l < numbers.length; l++)

{

z = numbers[l];

System.out.println("When x = " + l + " Then Y = " + z);

}

}

}

THe code above gives me the following result:

When x = 0 Then Y = 0

When x = 1 Then Y = 4

When x = 2 Then Y = 14

When x = 3 Then Y = 36

When x = 4 Then Y = 26

When x = 5 Then Y = 40

When x = 6 Then Y = 34

When x = 7 Then Y = 14

When x = 8 Then Y = 36

When x = 9 Then Y = 6

When x = 10 Then Y = 30

When x = 11 Then Y = 14

When x = 12 Then Y = 14

When x = 13 Then Y = 36

When x = 14 Then Y = 36

When x = 15 Then Y = 20

When x = 16 Then Y = 44

When x = 17 Then Y = 14

When x = 18 Then Y = 36

When x = 19 Then Y = 16

When x = 20 Then Y = 10

When x = 21 Then Y = 24

When x = 22 Then Y = 14

When x = 23 Then Y = 36

When x = 24 Then Y = 46

When x = 25 Then Y = 0

When x = 26 Then Y = 4

When x = 27 Then Y = 14

When x = 28 Then Y = 36

When x = 29 Then Y = 26

When x = 30 Then Y = 40

When x = 31 Then Y = 34

When x = 32 Then Y = 14

When x = 33 Then Y = 36

When x = 34 Then Y = 6

When x = 35 Then Y = 30

When x = 36 Then Y = 14

When x = 37 Then Y = 14

When x = 38 Then Y = 36

When x = 39 Then Y = 36

When x = 40 Then Y = 20

When x = 41 Then Y = 44

When x = 42 Then Y = 14

When x = 43 Then Y = 36

When x = 44 Then Y = 16

When x = 45 Then Y = 10

When x = 46 Then Y = 24

When x = 47 Then Y = 14

When x = 48 Then Y = 36

When x = 49 Then Y = 46

So the output is good. Now, I have to find when two Y have the same value..

so when x = 2 and Y = 14, and when x = 7, Y = 14. So in this way I have to find all the duplicate values of Y and print the their following x (index)

Guys I am almost done with my project. Could please tell me or write the code for me how to do it.

waiitng for your reply..

Mayur Soneta

We try to focus here on helping people learn how to do things themselves, rather than just handing them the answer. Please don't just post the solution. In the long run, you're helping nobody.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

Originally posted by Mayur Soneta:

Guys give me some hint...or code or ideas...

Are you familiar with associative arrays?

If you are not looking for anything space-wise efficient, could you not create an array of 50 elements -- one for each possible value of Y? Each element of this array a sequence of X values that yield Y (the element's index in this array).

How would you go about solving this problem? Think out aloud and the gurus here will point you in the right direction.

"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- Antoine de Saint-Exupery

I want to count the all the duplicates Y corresponding to their X's (index value)

Where is the y value coming from? Is it a hard-coded value? The first value with a duplicate? All values that have a duplicate?

What do you want to do with the result once you calculate it? Print it to the console? Add it to an array?

Do you know about the Collections framework?

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter

[ January 30, 2007: Message edited by: Dan Walin ]

When x = 2 Then Y = 14

When x = 11 Then Y = 14

When x = 12 Then Y = 14

........

So I would like to print all the same Values of Y with Their Index values of X. Please guys let me know I have to submit this my tomorrow.. I am almost done, but need some hintt how to do this..

When x = 7 Then Y = 14

When x = 11 Then Y = 14

When x = 12 Then Y = 14

When x = 17 Then Y = 14

When x = 22 Then Y = 14

When x = 27 Then Y = 14

When x = 32 Then Y = 14

When x = 36 Then Y = 14

When x = 37 Then Y = 14

When x = 42 Then Y = 14

When x = 47 Then Y = 14

==========================

When x = 3 Then Y = 36

When x = 8 Then Y = 36

When x = 13 Then Y = 36

When x = 14 Then Y = 36

When x = 18 Then Y = 36

When x = 23 Then Y = 36

When x = 28 Then Y = 36

When x = 33 Then Y = 36

When x = 38 Then Y = 36

When x = 39 Then Y = 36

When x = 43 Then Y = 36

When x = 48 Then Y = 36

I want to get the results in this way, I'm getting now.. but only when I am hard coding.. below is my code:

/**

* @(#)array1.java

*

*

* @author

* @version 1.00 2007/1/26

*/

public class array1

{

public static void main(String args[])

{

final int TOTAL = 50;

int x,y;

int[] numbers = new int[TOTAL];

for( x = 0; x < numbers.length; x++)

{

y =( ((x * x * x) + ( 3 * x)) % 50);

// System.out.print("when x = " + x);

numbers[x] = y;

}

int z = 0;

for(int l = 0; l < numbers.length; l++)

{

z = numbers[l];

if ( numbers[l] == 14)

System.out.println("When x = " + l + " Then Y = " + numbers[l]);

}

System.out.println("==========================");

for(int l = 0; l < numbers.length; l++)

{

z = numbers[l];

if ( numbers[l] == 36)

System.out.println("When x = " + l + " Then Y = " + numbers[l]);

}

} // End of main

}// End of Class

I dont want to hard code.. in the for loop as you can seee.

Guys give me some hint.. how do I print the results.. with comparing the numbers in array..

[ January 30, 2007: Message edited by: Dan Walin ]

Note: I'm new to java, so the syntax may be wrong, but I've had to do similar things in C++ and something like that worked...a bit crude, but it worked...

[ January 30, 2007: Message edited by: Don Sartain ]

Don<br /> <br />"An unexamined life is not worth living." -- Socrates

Sorry if I'm not explaining myself well, but I hope it helps.

Nate

If it is just the solution you want there are sites like Rent A Coder that may be of more help. I think the whole spirit of the ranch is to foster learning through trial and a bit of coaching, or to answer specific pinpointed questions regarding the language and its constructs. I know I've been close to volunteering for Euthanasia a few times on some of the projects I was working on, but with a bit of coaching and experimenting myself I'd like to think I learned and actually absorbed valuable lessons in the process.

[ January 30, 2007: Message edited by: Nathan Leniz ]

The very existence of flamethrowers proves that at some time, some where, some place, someone once said to themselves "I'd really like to set those people on fire over there, but I just can't get close enough".

Originally posted by Mayur Soneta:

PLEASE GIVE THE SOLUTION....

We don't do that here. We will help you learn, we will answer questions, we will give you advice. But we don't give solutions to problems. A lot of times people are asking for homework solutions, which is generally a violation of a school's code of ethics. We don't wish to be involved in that.

Note that Dan didn't say the code would work, he said it was a hint to help you figure it out. Look at his code, see what he is doing, then work it into YOUR code. His example is not a complete program.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |