Answer to Question #294016 in Java | JSP | JSF for Maya

Question #294016

Can you convert the code into java?


#include<vector>
#include<iostream>

auto f=
[](auto m,int&r){
  r=1;                         //set return flag to true
  for(auto a:m)                //for each element
    for(auto b:m)              //check with second element
      if (a.second==b.first){  //do they chain?
        int i=0;               //flag for local transitivity
        for(auto c:m)          //search for a third element
          i+=a.first==c.first&&b.second==c.second;
        r*=i>0;                //multiply with flag>0, resulting in 0 forever if one was not found
      }
}
;

int main(){
 std::vector<std::pair<int,int>> m={
  {1, 2}, {2, 4}, {6, 5}, {1, 4}
 };

 int r;
 f(m,r);
 std::cout << r << std::endl;
 
 m.emplace_back(3,6);
 f(m,r);
 std::cout << r << std::endl;
 
 m.emplace_back(3,5);
 f(m,r);
 std::cout << r << std::endl;

}
1
Expert's answer
2022-02-05T10:31:20-0500
import java.util.LinkedList;

public class Main {
    public static boolean isTransitive(LinkedList<int[]> relations) {
        for (int[] relationA : relations) {
            for (int[] relationB : relations) {
                if (relationA[1] == relationB[0]) {
                    boolean transitive = false;
                    for (int[] relationC : relations) {
                        if (relationC[0] == relationA[0] && relationC[1] == relationB[1]) {
                            transitive = true;
                            break;
                        }
                    }
                    if (!transitive) {
                        return false;
                    }
                }
            }
        }
        return true;
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS