• Register
0 votes

Problem :

My function accepts the two arrays of integers and returns true if below conditions are met

1.      Arrays have the same length and

2.      Each m.element should be less than each n.element of the same index

All my test cases are passing except when int[] m = {1, 2, 3} and int[] n = {4, 5, 1}.

It is returning true even if m[2] > n[2]. Here digitDifference check is not working correctly but I am not getting any error.

Please find below my code:

public static boolean ArrayLess(int[] m, int[] n) {

    int j = 0;

    boolean sameLength = (m.length == n.length);

    boolean digitDifference = (m[j] < n[j]);

    for (j = 0; j <= m.length - 1; j++) {}

    return (sameLength && digitDifference);


6 5 3
7,540 points

Please log in or register to answer this question.

1 Answer

0 votes

Solution :

I guess your for loop does nothing, so I think you are only comparing the elements in the first index of your arrays.

Your code must look something like below :

public static boolean ArrayLess (int[] m, int[] n) {
boolean sameLength = (m.length == n.length);
if (!sameLength)
   return false;
    boolean digitDifference = true;
    for (int j = 0; j <= m.length - 1 && digitDifference;j++) {
        digitDifference = (m[j] < n[j]);
    return digitDifference;

Now the newly updated for loop in above code compares each pair of elements having the same index, and terminates if it finds a pair that against your requirement that (m[j] < n[j]).

9 7 4
38,600 points

Related questions

0 votes
1 answer 2 views
Problem: If you could lead me in the right way, I would appreciate it.
asked 4 days ago salim1 39k points
0 votes
1 answer 4 views
Problem: I will request someone to answer my question Please, I have searched on the web but I got no useful information for this, now I am in difficulty continuing my learning
asked Mar 29 tuhin1 48.9k points
0 votes
1 answer 45 views
I have to work with comparision between objects of two arrays this is actually my University Assignment someone please help me
asked Nov 10, 2020 Han Li 710 points
1 vote
1 answer 1 view
1 view
Problem: C program to compare two arrays using pointers
asked Mar 30 Ashty 6k points
0 votes
1 answer 5 views
Problem: How to compare two arrays for matches in javascript?
asked Mar 20 Ifra 24.4k points
1 vote
1 answer 74 views
Problem: Hello good souls, var myarr1 = [1, 2, 3, 4, 5]; var myarr2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; I have two simple arrays like above. I want to sort out the common values between those two arrays. So can you find common elements in two arrays javascript for me? Please keep the program as simple as possible that I can understand easily. Thanks for your effort and valuable time buddy.
asked Apr 6, 2020 Gavin 15.3k points
0 votes
1 answer 5 views
Problem: I&rsquo;m new in Java. I want to know. What is the easiest way to sum two arrays element-by-element? I know that you can use a for loop such as the following: int[] a = {0, 1, 2}; int[] b = {3, 4, 5}; int[] c = new int[a. ... Is there an easy way to do this in Java? The method that comes to mind is using the RealVector class from Apache Commons Math, but that method is rather verbose.
asked Mar 8 Shovo210 14k points
1 vote
1 answer 40 views
Problem: Let me put it as sim as I can. I have an if statement in my code and it returns a value. Since IF is a boolean statement it should return value whether it&rsquo;s true or false. So, in my program when a method tests an argument and returns a true or ... as a parameter in a different method inside an IF statement. How should I do that? Any kind of help is appreciated by my side. Thanks.
asked Mar 17, 2020 Gavin 15.3k points
0 votes
1 answer 111 views
I am new to this. I was doing that but I got into some problems.
asked Nov 6, 2020 TeamScript 13.5k points
0 votes
1 answer 305 views
Problem: Hello programmers! I am learning to program, and I just got a little confused about boolean values and logical operators. I tried to run the following program: #include<iostream> using namespace std; int main() { bool check = false && true || true; ... program, I got the following output: Condition evaluated to true I am so confused about how this condition evaluates to true. Any help?
asked Dec 18, 2020 Code Learner 9.2k points