How do I find the longest sequence in an array? I have an array of 5 (users input) and array of 10(random lotto numbers 1-100),I want to find the longest sequence the user guessed.For example : lottery numbers are : 10 12 13 15 17 18 19 20 32 65 and user guessed 1 2 15 17 18.This would be a 3 number sequence , I want to find out if the user guessed 2 ,3 , 4 or all numbers.How can I do this ? Here is my code , please help I have been stuck on this for hours.
// to check if user guessed a sequence
int counter=0;
String prize3;
int counter1,counter2,counter3,counter4;
for (int i = 0; i < lottery.length - 5; i++) { // 1-5
for (int j = 0; j < numbers.length; j++) {
if (lottery[i] == numbers[j]) {
counter++;
}
}
}
for (int i = 1; i < lottery.length - 4; i++) { // 2-6
for (int j = 0; j < numbers.length; j++) {
if (lottery[i] == numbers[j]) {
counter++;
}
}
}
for (int i = 2; i < lottery.length - 3; i++) { // 3 -7 numbers of lottery array
for (int j = 0; j < numbers.length; j++) {
if (lottery[i] == numbers[j]) {
counter++;
}
}
}
for (int i = 3; i < lottery.length - 2; i++) { // 4 - 8 numbers of lottery array
for (int j = 0; j < numbers.length; j++) {
if (lottery[i] == numbers[j]) {
counter++;
}
}
}
for (int i = 4; i < lottery.length - 1; i++) { // 5 -9 numbers of lottery array
for (int j = 0; j < numbers.length; j++) {
if (lottery[i] == numbers[j]) {
counter++;
}
}
}
for (int i = 5; i < lottery.length; i++) { // 6 -10 numbers of lottery array
for (int j = 0; j < numbers.length; j++) {
if (lottery[i] == numbers[j]) {
counter++;
}
}
}
for (int i = 0; i < numbers.length && i < 2; i++) { // first 2 numbers of input
counter1 = numbers[i];
}
for (int i = 0; i < numbers.length && i < 3; i++) { // first 3 numbers of input
counter2 = numbers[i];
}
for (int i = 0; i < numbers.length && i < 4; i++) { // first 4 numbers of input
counter3 = numbers[i];
}
for (int i = 0; i < numbers.length && i < 5; i++) { // all numbers of input
counter4 = numbers[i];
}
int[] numbers = new int[]{10, 12, 13, 15, 17, 18, 19, 20, 32, 65};
int[] lottery = new int[]{1, 2, 15, 17, 18};
int count = 0;
for (int i = 0; i < lottery.length; i++) {
for (int j = 0; j < numbers.length; j++) {
if (lottery[i] == numbers[j]) {
count++;
break;
}
}
}
System.out.printf("You guessed %d sequence.%n", count);
Based on your input provided, I'm assuming that the lottery numbers array is always sorted. Under this assumption, it would be very appropriate to apply binary search.
Here is my very simple brute force approach.
int[] random = {10, 12, 13, 15, 17, 18, 19, 20, 32, 65};
int[] input = {1,2,15,17,18};
int cnt=0;
for(int i=0;i<input.length;i++)
{
int present = Arrays.binarySearch(random,input[i]);
if(present>=0)
{
for(int j=present;j< random.length && i<input.length;j++, i++)
{
if(random[j]==input[i])
cnt++;
}
}
if(cnt>0)
break;
}
The variable cnt will output you with the length of the sequence.
Related
I created this program that allows the user to input 5 numbers for array 1 and 5 numbers for array 2, the idea of the program is to iterate through those arrays and find the matching values for example: user types on input 1 = 1, 2, 3, 4 and 5 and the same for input 2, the lowest matching value is 1 and my program does that, if there is no matching value displays a message that there is no matching values and my program does that. However, if the user inputs something like this on 1 = 3, 4, 5, 7, 2 and input 2 = 9, 12, 8, 7, 15, what my program does in this case, variable min1 on array1 find lowest value which is 2 and variable min2 on array2 find lowest value which is 7 so in theory does not match, but they are asking me to find the lowest MATCHING values so both of them have 7 so it should display 7, I have some code there that select the 7 on each array and display them, now I have to figure out how to add that temporal variable into the displays so it only displays one or the other, as of now it displays 7 and then no matching value, tried adding it to the if statement but couldn't make it
import java.util.Scanner;
public class SmallestArrayItem {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int array1[] = new int[5];
int array2[] = new int[5];
System.out.println("Please enter 5 values for array 1");
for(int i = 0; i < array1.length; i++) {
int userInput = keyboard.nextInt();
array1[i] = userInput;
}
System.out.println("Please enter 5 values for array 2");
for(int i = 0; i < array2.length; i++) {
int userInput = keyboard.nextInt();
array2[i] = userInput;
}
int min1 = array1[0];
for(int index1 = 1; index1 < array1.length; index1++) {
if(array1[index1] < min1) {
min1 = array1[index1];
}
}
int min2 = array2[0];
for(int index2 = 1; index2 < array2.length; index2++) {
if(array2[index2] < min2) {
min2 = array1[index2];
}
}
int tmpval = Integer.MAX_VALUE;
for(int i = 0; i < array1.length; i++){
for(int j = 0; j < array2.length; j++){
if(array1[i] == array2[j]){
// same value
if(tmpval > array1[i]){
tmpval = array1[i];
}
}
}
}
System.out.println(tmpval);
if(min1 == min2) {
System.out.println("The Smalest match in the array is : " + min1);
} else if(min1 != min2) {
System.out.println("There is no smallest matching integer!");
}
}
}
Your code look great, its just a problem with your logic. In your question, you talked about nesting for loops, so you were on the right track.
int[] arr1 = {3,4,5,7,2};
int[] arr2 = {9,12,8,7,15};
int tmpval = Integer.MAX_VALUE;
for(int i = 0; i < arr1.length; i++){
for(int j = 0; j < arr2.length; j++){
if(arr1[i] == arr2[j]){
// same value
if(tmpval > arr1[i]){
tmpval = arr1[i];
}
}
}
}
System.out.println(tmpval);
It's not clear from your description if the matching values have to be at the same position in the two arrays. If we assume that they do then something like this would work:
int[] arr1 = {3,4,5,7,2};
int[] arr2 = {9,12,8,7,15};
int minIdx = -1;
for(int i = 0; i < arr1.length; i++)
{
if(arr1[i] == arr2[i] && (minIdx < 0 || arr1[i] < arr1[minIdx]))
{
minIdx = i;
}
}
if(minIdx < 0)
System.out.println("No match");
else
System.out.println("Min match: " + arr1[minIdx]);
Note how we use a negative index as an indicator that we haven't found a match. The standard trick of using Integer.MAX_VALUE as our starting value isn't really safe as it could be a matching value in the array.
If the matching values can appear at any position in the arrays then you'll need to compare each value in array1 with every value in array2:
for(int i = 0; i < arr1.length; i++)
{
for(int j = 0; j < arr2.length; j++)
{
if(arr1[i] == arr2[j] && (minIdx < 0 || arr1[i] < arr1[minIdx]))
{
minIdx = i;
}
}
}
Thanks for all your help guys, this is the final code:
public class SmallestArrayItem {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int array1[] = new int[5];
int array2[] = new int[5];
System.out.println("Please enter 5 values for array 1");
for(int i = 0; i < array1.length; i++) {
int userInput = keyboard.nextInt();
array1[i] = userInput;
}
System.out.println("Please enter 5 values for array 2");
for(int i = 0; i < array2.length; i++) {
int userInput = keyboard.nextInt();
array2[i] = userInput;
}
int minIdx = -1;
for(int i = 0; i < array1.length; i++)
{
for(int j = 0; j < array2.length; j++)
{
if(array1[i] == array2[j] && (minIdx < 0 || array1[i] < array1[minIdx]))
{
minIdx = i;
}
}
}
if(minIdx < 0)
System.out.println("There is no smallest matching integer!");
else
System.out.println("The Smallest match in the array is : " + array1[minIdx]);
}
}
This is my code:
for (int i = 4; i >= 1; i--) {
for (int j = 1; j < i; j++) {
System.out.print(" ");
}
for (int k = i; k <= 4; k++) {
System.out.print(k+"");
}
System.out.println();
}
Current output:
4
34
234
1234
Desired output:
1
21
321
4321
What changes are necessary in order for me to get the desired output as shown above?
Let the first loop (i) run from 1 to 4 and the second (j) from 4 to i.
This reverses your output.
You did every thing right, just the last for should have a very minor change:
for (int k = 5-i; k >= 1; k--){
Here you go:
public static void main(String[] args) {
for (int i = 1; i <= 4; i++) {
for (int j = 4; j > i; j--) {
System.out.print(" ");
}
for (int k = i; k >= 1; k--){
System.out.print(k + "");
}
System.out.println();
}
}
Your loops are incorrect, you can refer the below code with inline comments:
for (int i = 1; i <= 4; i++) { //iterate from 1 to 4
//Loop from i+1 to insert spaces first
for (int j = i+1; j <= 4; j++) {
System.out.print(" ");
}
//Loop from i to insert the number next to each other
for (int j = i; j >= 1; j--) {
System.out.print(j);
}
System.out.println(); //insert a new line
}
for (int i = 1; i <= 4; i++)
{
for (int k = i; k <= 4; k++)
{
System.out.print(" ");
}
for (int j = 1; j < i; j++)
{
System.out.print(j);
}
System.out.println();
}
Sorting a 2-dimensional can be done in two ways :
Converting 2-dimensional to 1-dimensional and then sorting it.
Directly sorting the 2-dimensional array.
I would like to know the second method. Sorting it directly.
Try this. You can sort two dimensional array Directly
int array[][] = { { 12, 43, 21, 87, 32 }, { 43, 75, 21, 45, 65 } };
int t = 0;
for (int x = 0; x < 2; x++) {
for (int y = 0; y < 5; y++) {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 5; j++) {
if (array[i][j] > array[x][y]) {
t = array[x][y];
array[x][y] = array[i][j];
array[i][j] = t;
}
}
}
}
}
System.out.println("The Sorted Array:");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 5; j++) {
System.out.print(array[i][j] + "\t");
}
System.out.println();
}
I try to convert 1D to 2D array, but I keep getting java.lang.ArrayIndexOutOfBoundsException, and I have tried whatever I could find on the stackoverflow or internet, but I do not understand why I have this issue?
public class Arrayto2DArray {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int[] a = {0,1, 6, 83, 4, 5, 12, 7};
int[][] b = new int[4][4];
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b[i].length; j++) {
b[i][j]=0;
System.out.print(b[i][j]);
}
System.out.println();
}
System.out.println("--------------------------");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < b[i].length; j++) {
try{
b[i][j] = a[i+j*4];
}catch(Exception e){
e.printStackTrace();
System.out.println(e);
}
}
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
System.out.print(b[i][j]);
}
System.out.println();
}
}
}
I kind of know why I get this error and it because of this line
b[i][j] = a[i+j*4];
but I cannot come up any formula better than this.
Consider the second for-loop
Lets say when i = 3 and j= 3
a[i+j*4] evaluates to a[15] which is out of the array
When you declare you 2-d array, you specified int[][] b = new int[4][];, meaning that the first inner for loop for (int j = 0; j < b[i].length; j++) should result in a NullPointerException since b[i].length has no predefined length. Before intering the inner for loop, you should define the size of each b[i] like b[i] = new int[somenumber]
In regards to convert the 1d loop to a 2d, you need to define the rule around spliting it into the 2-d array. Then accordingly the second for loop need modification
EDIT:You modified your code to have an int[4][4] array, which means you have 16 placeholders. Your 1-d array contain only 8 placeholders. It depends on how you want to sort the array, like it can be
b 0 1 2 3
0 0 1 6 83
1 4 5 12 7
2 0 0 0 0
3 0 0 0 0
or any other pattern
Assuming the length of 1-d array is 8 and the total index of 2-d array is 16, the following is more of a general solution:
public static void main(String[] args) {
// TODO code application logic here
int[] a = { 0, 1, 6, 83, 4, 5, 12, 7 };
int[][] b = new int[4][4];
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b[i].length; j++) {
b[i][j] = 0;
System.out.print(b[i][j] + "\t");
}
System.out.println();
}
System.out.println("--------------------------");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < b[i].length; j++) {
try {
if ((j + i * 4) < a.length)
b[i][j] = a[j + i * 4];
} catch (Exception e) {
e.printStackTrace();
System.out.println(e);
}
}
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
System.out.print(b[i][j] + "\t");
}
System.out.println();
}
}
What is wrong is your 2D array is a 4*4, so that means for each index, there will be 4 elements i.e. say values. So there would be a total of 16 values, so you would need a 1D array with 16 elements for the above code to work. Right now you have only 8 values in your 1D array.
I think your code should change like this
public class Arrayto2DArray {
public static void main(String[] args) {
// TODO code application logic here
int[] a = {0,1, 6, 83, 4, 5, 12, 7};
int[][] b = new int[4][2];
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b[i].length; j++) {
b[i][j]=0;
System.out.print(b[i][j]);
}
System.out.println();
}
System.out.println("--------------------------");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < b[i].length; j++) {
try{
b[i][j] = a[i+j*4];
}catch(Exception e){
e.printStackTrace();
System.out.println(e);
}
}
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 2; j++) {
System.out.print(b[i][j]+" ");
}
System.out.println();
}
}
}
Your correct formula should be like.
int[] a = {0,1, 6, 83, 4, 5, 12, 7};
int[][] b = new int[4][2];
int k=0;
for (int i = 0; i < b.length; i++) { // Column of 2D array
for (int j = 0; j < b[0].length; j++) { // Row of 2D array
b[i][j]= a[k++];
System.out.println(b[i][j]);
}
}
Edit:
For general case, If your row is fixed at 4 but column is not fixed than your 2D conversion formula should be like below.
int col = a.length / 4;
int remainder = a.length % 4;
if (remainder > 0) {
col = col + 1; // Get the correct column size.
}
int[][] b = new int[4][col];
int k = 0;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < col; j++) {
if (k < a.length) {
b[i][j] = a[k];
System.out.println(b[i][j]);
}
k++;
}
}
My professor wants me to print out the matrices side by side with the "+" between the two matrices and then a "=" sign. In the end he wants us to add the matrices together.
This is the work so far.
So the result would come out as:
1 2 3 9 8 7 10 10 10
4 5 6 + 6 5 4 = 10 10 10
7 8 9 3 2 1 10 10 10
enter code here public static void main(String[] args) {
int matrix1[][] = {{1,2,3},{4,5,6},{6,7,8}};
int matrix2[][] = {{9,8,7},{6,5,4},{3,2,1}};
int result1;
int[][] result2 = new int[2][3];
for (int i = 0; i < matrix1.length; i++) {
for (int j = 0; j < matrix1[0].length; j++) {
System.out.printf(matrix1[i][j] + " ");
System.out.print("");
}
System.out.println("");
}
for (int i = 0; i < matrix2.length; i++) {
for (int j = 0; j < matrix2[0].length; j++) {
System.out.printf(matrix2[i][j] + " ");
}
System.out.println("");
}
}
My problem is, how could I print it side by side with the solutions?
Consider the two printing loops for your matrices:
for (int i = 0; i < matrix1.length; i++) {
for (int j = 0; j < matrix1[0].length; j++) {
System.out.printf(matrix1[i][j] + " ");
}
System.out.println("");
}
for (int i = 0; i < matrix2.length; i++) {
for (int j = 0; j < matrix2[0].length; j++) {
System.out.printf(matrix2[i][j] + " ");
}
System.out.println("");
}
They print matrix 1, then 2 - and so the matrices will be below each other.
If you want the matrices side by side, you need to print line 1 of every matrix, then - after a new line - line 2 of every matrix, etc. By re-arranging how the loops go through the matrices, you could have your new layout.
You unfortunately cannot print them one at a time, you need to take it row by row. This solution requires both matrix1 and matrix2 to be of equal height. But here's a template that should get you started.
for (int i = 0; i < matrix1.length; i++) {
for (int j = 0; j < matrix1[i].length; j++) {
}
if (i == matrix1/2) {
} else { //One part of if handles when "+" is needed, other one doesn't
}
for (int j = 0; j < matrix2[i].length; j++) {
}
if (i == matrix1/2) {
}
for (int j = 0; j < ???; j++) {
}
}