-2

In comparison of two arrays in Java how to check if all the elements in array1 is greater than array2? I want to return yes if all the elements in array1 are greater than array2 else no.

Termininja
  • 6,620
  • 12
  • 48
  • 49
Chaitanya
  • 63
  • 1
  • 2
  • 10
  • Show us your code and we can help ;) – beeb Aug 12 '16 at 10:58
  • for(i=0;ib[j] System.out.println("Yes");else System.out.println("No");}} i wrote like this but..it is not comparing all the elements if 1st element is greater than other it is printing yes i want to compare all the elemst in array1 and if all the elements greater only return yes. – Chaitanya Aug 12 '16 at 10:58
  • 2
    update the question with your code – sidgate Aug 12 '16 at 10:58
  • possible dublicate of http://stackoverflow.com/questions/14897366/comparing-two-integer-arrays-in-java – beeb Aug 12 '16 at 11:05
  • its is not the same code as I use I just want to check if every element in array1 is greater than every corresponding element in array2 – Chaitanya Aug 12 '16 at 11:08
  • 1
    actually the concept is the same, you'll just need to tweak it a little. It also does checks that my answer doesn't do, so you should look at it closely as well. – Tyler Aug 12 '16 at 11:10

4 Answers4

0

use a boolean for your check instead of System.out.println

using your example... Assuming a is array 1 and b is array 2. You to make sure all elements inside array 1 is bigger than all elements inside array 2 right?

boolean bolSmaller = false;
for(i=0;i<n;i++){
    for(j=0;j<n;j++){
        if(a[i] <= b[j]){
            //if element in array 2 is bigger than element in array 1..
            bolSmaller=true;
        }
    }
}

Now you only have to check the boolean, if it is true, then array 2 has elements bigger than array 1.

Tyler
  • 957
  • 11
  • 27
0

This approach costs only O(n):

public int getMinimalElement(int[] array) {
    int min = array[0];
    for (int i = 0; i < array.length; i++)
        if (array[i] < min)
            min = array[i];
    return min;
}

public int getMaxElement(int[] array) {
    int max = array[0];
    for (int i = 0; i < array.length; i++)
        if (array[i] > max)
            max = array[i];
    return max;
}

public void compare(int[] array1, int[] array2) {
    if (getMinimalElement(array1) > getMaxElement(array2))
        System.out.println("Yes");
    else
        System.out.println("No");
}
shure
  • 361
  • 3
  • 7
  • Your logic will compare the minimum element in the two arrays. What they are looking for is comparison of element at each index. – Grasshopper Aug 12 '16 at 11:17
0

Find here an example:

boolean greater = true;
for(int i=0;i<a.length;i++){
    for(int j=0;j<b.length;j++){
        if (a[i]<=b[j])
        {
            greater = false;
        }
    }
}
if (greater){
    System.out.println("Yes");
}else{
    System.out.println("No");
}
Yorchus
  • 1
  • 4
0

Here's the logic:
1. Find max element in array2
2. Iterate over array1 and compare each element with max.
3. If all elements in array1 are greater than max then you are done.
Here's the code:

    int max = Integer.MIN_VALUE;
    for(int i=0; i < arr2.length; i++){
        if(max < arr2[i])
            max = arr2[i];
    }


    boolean allGreater = true;
    for(int i=0; i < arr1.length; i++){
        if(max >= arr1[i]){
            allGreater = false;
            break;
        }
    }

    System.out.println("Is arr1 is greater than arr2? Ans:" + allGreater);
A Joshi
  • 151
  • 1
  • 6