Check whether Array represents a Fibonacci Series or not | Java program to check if the elements of a given array can form a Fibonacci Series ; Returns true if a permutation of arr [ 0. . n - 1 ] can form a Fibonacci Series ; Sort array ; After sorting , check if every element is equal to the sum of previous 2 elements ; Driver code
import java . util . Arrays ; class GFG { public static boolean checkIsFibonacci ( int arr [ ] , int n ) { if ( n == 1 n == 2 ) return true ; Arrays . sort ( arr ) ; for ( int i = 2 ; i < n ; i ++ ) { if ( ( arr [ i - 1 ] + arr [ i - 2 ] ) != arr [ i ] ) return false ; } return true ; } public static void main ( String [ ] args ) { int arr [ ] = { 8 , 3 , 5 , 13 } ; int n = arr . length ; if ( checkIsFibonacci ( arr , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }