Check if a graph constructed from an array based on given conditions consists of a cycle or not | Java program for the above approach ; Function to check if the graph constructed from given array contains a cycle or not ; Traverse the array ; If arr [ i ] is less than arr [ i - 1 ] and arr [ i ] ; Driver Code ; Given array ; Size of the array
import java . io . * ; class GFG { static void isCycleExists ( int [ ] arr , int N ) { for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ i ] < arr [ i - 1 ] && arr [ i ] < arr [ i + 1 ] ) { System . out . println ( " Yes " ) ; return ; } } System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 2 , 4 , 5 } ; int N = arr . length ; isCycleExists ( arr , N ) ; } }