I am trying to use Java to solve the following Given an arra

I am trying to use Java to solve the following.

Given an arrangement of dominos, determine, whether or not it is a legal arrangement and return True or False accordingly. For example, this is an illegal arrangement [2 3][4 2][2 2][3 5] and this is a legal arrangement of Dominos. [2 3][3 5][5 4][4 5].

I have no clue how to begin this to write this program.

Solution

Let\'s look at the legal arrangement [2 3] [3 5] [5 4] [4 5]. The careful observation told us that except first last elements, other elements should have same adjacency elements. But, here if we have an odd length of array [2 3] [3 5] [5 4][4], this should be a illegal. The algorithm is we will made a check at the begining if the length is odd, we will return false. We will loop from 2nd element to n-2 and check if all adjacents are equal.

import java.util.Scanner;
public class DominosArrangement{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println(\"Enter length of the array:\");
int n = sc.nextInt();
int[] arr = new int[n];
System.out.println(\"Enter Elements:\");
for(int i=0;i<n;i++)
arr[i] = sc.nextInt();
boolean is_legal = true;
if(n%2 !=0 )
is_legal = false;
for(int i=1;i<n-2;i+=2){
if(arr[i]!=arr[i+1])
is_legal = false;
}
System.out.println(is_legal);

}
}

Here\'s a sample output from my terminal:

lokesh1729@lokesh1729-Inspiron-N5050:/tmp$ java DominosArrangement
Enter length of the array:
8
Enter Elements:
2
6
6
3
3
4
4
9
true

I am trying to use Java to solve the following. Given an arrangement of dominos, determine, whether or not it is a legal arrangement and return True or False ac

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site