• Register
0 votes
31 views

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 24 views
24 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 42 views
42 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 52 views
52 views
I am new to this. I was doing that but I got into some problems.
asked Nov 6, 2020 TeamScript 10.1k points
1 vote
2 answers 286 views
286 views
Problem : I am beginner to numpy. I am trying to execute my code but I am facing below error. &ldquo;Valueerror: all the input arrays must have same number of dimensions&rdquo;. I want to have my all the arrays with a same shape. I am trying to fix above error from past couple of days but still unable to fix it. I am looking for some help in fixing above error.
asked Apr 4, 2020 morrisBson 3.2k points
0 votes
1 answer 16 views
16 views
Problem: I am a novice Java programmer who started learning to program in Java just a few weeks ago. I got an assignment from my tutor in which I have to write a program that prints the sum of the elements of a given array. The output of the program should be ... } I am allowed to take help from the Internet so asking for help from you guys. Can anybody please provide me with a solution? Thanks!
asked Dec 29, 2020 Code Learner 5.5k points
1 vote
1 answer 29 views
29 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
1 vote
1 answer 20 views
20 views
Problem: I am very noob in java programming. I was doing my coding practice and wrote this code: public class StaticDemo { int i = 0; public static void main(String[] args) { System.out.println("in main method which is static"); // Trying to access ... -static method instanceMethod() cannot be referenced from a static context instanceMethod(); ^ 3 errors Can anyone help me out to fix this problem?
asked Apr 7, 2020 LizzyM 6.1k points
0 votes
1 answer 51 views
51 views
Problem : In Java, I want to have something as below : class MyClazz<T> {  static void doIt(T object) {    // More code  } } But I face below error &ldquo;Cannot make a static reference to the non-static type T&rdquo; I am unable to understand ... about a subject. Could someone clarify if such the use is possible, by the similar manner? Also, why was my original attempt went unsuccessful?
asked Jan 21, 2020 jwilliam 3.9k points
0 votes
1 answer 11 views
11 views
I hava a list of arry in my script. I need to count duplicate elements in arraylist in Java.
asked Oct 4, 2020 Daniel Anderson 4k points
0 votes
1 answer 11 views
11 views
I have a blog website. In this website in several blogs. so from these blog post i need to remove duplicate elements from an array.
asked Sep 28, 2020 Daniel Anderson 4k points