Java Program of Array Compatibility

Question:

Two arrays are said to be compatible if they are of the same size and if the ith element in the first array is greater than or equal to the ith element in the second array for all i elements.If the array size is zero or lesser then display the message “Invalid array size”.Write a Java program to find whether 2 arrays are compatible or not.If the arrays are compatible display the message as “Arrays are Compatible” ,if not then display the message as “Arrays are Not Compatible”.

Sample Input 1:

Enter the size for First array:

5

Enter the elements for First array:

5

14

17

19

15

Enter the size for Second array:

5

Enter the elements for Second array:

2

5

9

15

7

Sample Output 1:

Arrays are Compatible

Sample Input 2:

Enter the size for First array:

3

Enter the elements for First array:

1

4

7

Enter the size for Second array:

5

Enter the elements for Second array:

2

5

9

5

7

Sample Output 2:

Arrays are Not Compatible

Sample Input 3:

Enter the size for First array:

-2

Sample Output 3:

Invalid array size

Code:

CompatibleArrays.java

import java.util.*;
public class CompatibleArrays 
{
    public static void main (String[] args) 
    {
        Scanner sc=new Scanner(System.in);
        
        //1st array
        System.out.println("Enter the size for First array:");
        int n1=sc.nextInt();
        int flag=0;
        if(n1>0)
        {
            int[] arr1=new int[n1];
            
            System.out.println("Enter the elements for First array:");
            for(int i=0;i<n1;i++)
            {
                arr1[i]=sc.nextInt();
            }
            
            //2nd array
            System.out.println("Enter the size for Second array:");
            int n2=sc.nextInt();
            if(n2>0)
            {
                    int[] arr2=new int[n2];
                    
                    System.out.println("Enter the elements for Second array:");
                    for(int i=0;i<n2;i++)
                    {
                        arr2[i]=sc.nextInt();
                    }
                    
                    if(n1==n2)
                    {
                        for(int i=0; i<n1;i++)
                        {
                            if(arr1[i]>=arr2[i])
                            {
                                flag++;
                            }
                        }
                        
                        if(flag==n1)
                        {
                            System.out.println("Arrays are Compatible");
                        }
                        else
                        {
                            System.out.println("Arrays are not Compatible");
                        }
                        
                    }
                    else
                    {
                        System.out.println("Arrays are Not Compatible");
                    }
            }
            else
            {
                System.out.println("Invalid array size");
            }
        }
        else
        {
            System.out.println("Invalid array size");
        }
    }
}

Recommended:

  1. Array square
  2. Generate number using odd digits
  3. Alternate Number Difference
  4. Next Greatest number
  5. Mark Comparison
  6. Print the characters in descending order
  7. Vowels in a fishBowl
  8. Least offer
  9. Ascending and descending order
  10. Mail Domain
  11. Count repeating words
  12. Sentence – Convert to upper and lower
  13. Count consecutive repeating characters
  14. Zig zag Array
  15. Pass and Fail Count
  16. Search a Course
  17. Average and Grade Calculation
  18. String – Find and replace the character (first occurrence)
  19. Sort the first and second half of an array
  20. Retail Shop
  21. Palindrome
  22. Numerology
  23. InitCap
  24. Array Compatiblilty
  25. Sum of the maximum and the minimum element
  26. String Concatenation
  27. Find Average Age
  28. Login
  29.  

Leave a Comment

Your email address will not be published. Required fields are marked *

x