codeparrot/xlcost-text-to-code huggingface dataset:
Minimum peak elements from an array by their repeated removal at every iteration of the array | Java program for the above approach ; Function to return the list of minimum peak elements ; Length of original list ; Initialize resultant list ; Traverse each element of list ; Length of original list after removing the peak element ; Traverse new list after removal of previous min peak element ; Update min and index , if first element of list > next element ; Update min and index , if last element of list > previous one ; Update min and index , if list has single element ; Update min and index , if current element > adjacent elements ; Remove current min peak element from list ; Insert min peak into resultant list ; Print resultant list ; Driver Code ; Given array arr [ ] ; Function Call
import java . util . * ; import java . lang . * ; class GFG { static void minPeaks ( ArrayList < Integer > list ) { int n = list . size ( ) ; ArrayList < Integer > result = new ArrayList < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int min = Integer . MAX_VALUE ; int index = - 1 ; int size = list . size ( ) ; for ( int j = 0 ; j < size ; j ++ ) { if ( j == 0 && j + 1 < size ) { if ( list . get ( j ) > list . get ( j + 1 ) && min > list . get ( j ) ) { min = list . get ( j ) ; index = j ; } } else if ( j == size - 1 && j - 1 >= 0 ) { if ( list . get ( j ) > list . get ( j - 1 ) && min > list . get ( j ) ) { min = list . get ( j ) ; index = j ; } } else if ( size == 1 ) { min = list . get ( j ) ; index = j ; } else if ( list . get ( j ) > list . get ( j - 1 ) && list . get ( j ) > list . get ( j + 1 ) && min > list . get ( j ) ) { min = list . get ( j ) ; index = j ; } } list . remove ( index ) ; result . add ( min ) ; } System . out . println ( result ) ; } public static void main ( String [ ] args ) { ArrayList < Integer > arr = new ArrayList < > ( Arrays . asList ( 1 , 9 , 7 , 8 , 2 , 6 ) ) ; minPeaks ( arr ) ; } }
Search an element in a reverse sorted array | Java Program to implement the above approach ; Function to search if element X is present in reverse sorted array ; Store the first index of the subarray in which X lies ; Store the last index of the subarray in which X lies ; Store the middle index of the subarray ; Check if value at middle index of the subarray equal to X ; Element is found ; If X is smaller than the value at middle index of the subarray ; Search in right half of subarray ; Search in left half of subarray ; If X not found ; Driver Code
class GFG { static int binarySearch ( int arr [ ] , int N , int X ) { int start = 0 ; int end = N ; while ( start <= end ) { int mid = start + ( end - start ) / 2 ; if ( X == arr [ mid ] ) { return mid ; } else if ( X < arr [ mid ] ) { start = mid + 1 ; } else { end = mid - 1 ; } } return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 4 , 3 , 2 , 1 } ; int N = arr . length ; int X = 5 ; System . out . println ( binarySearch ( arr , N , X ) ) ; } }
Construct an AP series consisting of A and B having minimum possible Nth term | Java program for the above approach ; Function to check if both a and b are present in the AP series or not ; Iterate over the array arr [ ] ; If a is present ; If b is present ; If both are present ; Otherwise ; Function to print all the elements of the Arithmetic Progression ; Function to construct AP series consisting of A and B with minimum Nth term ; Stores the resultant series ; Initialise ans [ i ] as INT_MAX ; Maintain a smaller than b ; swap ( a and b ) ; Difference between a and b ; Check for all possible combination of start and common difference d ; Initialise arr [ 0 ] as start ; Check if both a and b are present or not and the Nth term is the minimum or not ; Update the answer ; Print the resultant array ; Driver Code ; Function call
import java . io . * ; class GFG { public static boolean check_both_present ( int [ ] arr , int N , int a , int b ) { boolean f1 = false , f2 = false ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == a ) { f1 = true ; } if ( arr [ i ] == b ) { f2 = true ; } } if ( f1 && f2 ) { return true ; } else { return false ; } } public static void print_array ( int [ ] ans , int N ) { for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( ans [ i ] + " ▁ " ) ; } } public static void build_AP ( int N , int a , int b ) { int [ ] arr = new int [ N ] ; int [ ] ans = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) ans [ i ] = Integer . MAX_VALUE ; int flag = 0 ; if ( a > b ) { a += ( b - ( b = a ) ) ; } int diff = b - a ; for ( int start = 1 ; start <= a ; start ++ ) { for ( int d = 1 ; d <= diff ; d ++ ) { arr [ 0 ] = start ; for ( int i = 1 ; i < N ; i ++ ) { arr [ i ] = arr [ i - 1 ] + d ; } if ( check_both_present ( arr , N , a , b ) && arr [ N - 1 ] < ans [ N - 1 ] ) { for ( int i = 0 ; i < N ; i ++ ) { ans [ i ] = arr [ i ] ; } } } } print_array ( ans , N ) ; } public static void main ( String [ ] args ) { int N = 5 , A = 20 , B = 50 ; build_AP ( N , A , B ) ; } }
Find the word from a given sentence having given word as prefix | Java program for the above approach ; Function to find the position of the string having word as prefix ; Initialize an ArrayList ; Extract words from the sentence ; Traverse each word ; Traverse the characters of word ; If prefix does not match ; Otherwise ; Return the word ; Return - 1 if not present ; Driver code
import java . util . * ; import java . io . * ; import java . lang . Math ; class GFG { static String isPrefixOfWord ( String sentence , String Word ) { String a [ ] = sentence . split ( " ▁ " ) ; ArrayList < String > v = new ArrayList < > ( ) ; for ( String e : a ) v . add ( e ) ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) { for ( int j = 0 ; j < v . get ( i ) . length ( ) ; j ++ ) { if ( v . get ( i ) . charAt ( j ) != Word . charAt ( j ) ) break ; else if ( j == Word . length ( ) - 1 ) return v . get ( i ) ; } } return " - 1" ; } public static void main ( final String [ ] args ) { String s = " Welcome ▁ to ▁ Geeksforgeeks " ; String word = " Gee " ; System . out . println ( isPrefixOfWord ( s , word ) ) ; } }
Paths requiring minimum number of jumps to reach end of array | Java Program to implement the above approach ; Pair Class instance ; Stores the current index ; Stores the path travelled so far ; Stores the minimum jumps required to reach the last index from current index ; Constructor ; Minimum jumps required to reach end of the array ; Stores the maximum number of steps that can be taken from the current index ; Checking if index stays within bounds ; Stores the minimum number of jumps required to jump from ( i + j ) - th index ; Function to find all possible paths to reach end of array requiring minimum number of steps ; Storing the neighbours of current index element ; Function to find the minimum steps and corresponding paths to reach end of an array ; Stores the minimum jumps from each position to last position ; Driver Code
import java . util . * ; class GFG { public static class Pair { int idx ; String psf ; int jmps ; Pair ( int idx , String psf , int jmps ) { this . idx = idx ; this . psf = psf ; this . jmps = jmps ; } } public static int [ ] minJumps ( int [ ] arr ) { int dp [ ] = new int [ arr . length ] ; Arrays . fill ( dp , Integer . MAX_VALUE ) ; int n = dp . length ; dp [ n - 1 ] = 0 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { int steps = arr [ i ] ; int min = Integer . MAX_VALUE ; for ( int j = 1 ; j <= steps && i + j < n ; j ++ ) { if ( dp [ i + j ] != Integer . MAX_VALUE && dp [ i + j ] < min ) { min = dp [ i + j ] ; } } if ( min != Integer . MAX_VALUE ) dp [ i ] = min + 1 ; } return dp ; } public static void possiblePath ( int [ ] arr , int [ ] dp ) { Queue < Pair > queue = new LinkedList < > ( ) ; queue . add ( new Pair ( 0 , " " + 0 , dp [ 0 ] ) ) ; while ( queue . size ( ) > 0 ) { Pair tmp = queue . remove ( ) ; if ( tmp . jmps == 0 ) { System . out . println ( tmp . psf ) ; continue ; } for ( int step = 1 ; step <= arr [ tmp . idx ] ; step ++ ) { if ( tmp . idx + step < arr . length && tmp . jmps - 1 == dp [ tmp . idx + step ] ) { queue . add ( new Pair ( tmp . idx + step , tmp . psf + " ▁ - > ▁ " + ( tmp . idx + step ) , tmp . jmps - 1 ) ) ; } } } } public static void Solution ( int arr [ ] ) { int dp [ ] = minJumps ( arr ) ; possiblePath ( arr , dp ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 3 , 3 , 0 , 2 , 1 , 2 , 4 , 2 , 0 , 0 } ; int size = arr . length ; Solution ( arr ) ; } }
Length of longest increasing absolute even subsequence | Java program for the above approach ; Function to find the longest increasing absolute even subsequence ; Length of arr ; Stores length of required subsequence ; Traverse the array ; Traverse prefix of current array element ; Check if the subsequence is LIS and have even absolute difference of adjacent pairs ; Update lis [ ] ; Stores maximum length ; Find the length of longest absolute even subsequence ; Return the maximum length of absolute even subsequence ; Driver code ; Given array arr [ ] and brr [ ] ; Function call
import java . util . * ; import java . io . * ; class GFG { static void EvenLIS ( int arr [ ] ) { int n = arr . length ; int lis [ ] = new int [ n ] ; Arrays . fill ( lis , 1 ) ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( Math . abs ( arr [ i ] ) > Math . abs ( arr [ j ] ) && Math . abs ( arr [ i ] ) % 2 == 0 && Math . abs ( arr [ j ] ) % 2 == 0 && lis [ i ] < lis [ j ] + 1 ) lis [ i ] = lis [ j ] + 1 ; } } int maxlen = 0 ; for ( int i = 0 ; i < n ; i ++ ) maxlen = Math . max ( maxlen , lis [ i ] ) ; System . out . println ( maxlen ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 11 , - 22 , 43 , - 54 , 66 , 5 } ; int N = arr . length ; EvenLIS ( arr ) ; } }
Count balanced nodes present in a binary tree | Java program to implement the above approach ; Structure of a Tree Node ; Function to get the sum of left subtree and right subtree ; Base case ; Store the sum of left subtree ; Store the sum of right subtree ; Check if node is balanced or not ; Increase count of balanced nodes ; Return subtree sum ; Driver Code ; Insert nodes in tree ; Store the count of balanced nodes
class GFG { static int res = 0 ; static class Node { int data ; Node left ; Node right ; Node ( int val ) { data = val ; left = right = null ; } } ; static int Sum ( Node root ) { if ( root == null ) { return 0 ; } int leftSubSum = Sum ( root . left ) ; int rightSubSum = Sum ( root . right ) ; if ( root . left != null && root . right != null && leftSubSum == rightSubSum ) res += 1 ; return root . data + leftSubSum + rightSubSum ; } public static void main ( String [ ] args ) { Node root = new Node ( 9 ) ; root . left = new Node ( 2 ) ; root . left . left = new Node ( - 1 ) ; root . left . right = new Node ( 3 ) ; root . right = new Node ( 4 ) ; root . right . right = new Node ( 0 ) ; res = 0 ; Sum ( root ) ; System . out . print ( res ) ; } }
Print alternate nodes from all levels of a Binary Tree | Java program to implement the above approach ; Structure of a Node ; Print alternate nodes of a binary tree ; Store nodes of each level ; Store count of nodes of current level ; Print alternate nodes of the current level ; If left child exists ; Store left child ; If right child exists ; Store right child ; Driver Code ; Create a tree
import java . util . * ; class GFG { static class Node { int data ; Node left ; Node right ; Node ( int val ) { data = val ; left = right = null ; } } ; static void PrintAlternate ( Node root ) { Queue < Node > Q = new LinkedList < > ( ) ; Q . add ( root ) ; while ( ! Q . isEmpty ( ) ) { int N = Q . size ( ) ; for ( int i = 0 ; i < N ; i ++ ) { Node temp = Q . peek ( ) ; Q . remove ( ) ; if ( i % 2 == 0 ) { System . out . print ( temp . data + " ▁ " ) ; } if ( temp . left != null ) { Q . add ( temp . left ) ; } if ( temp . right != null ) { Q . add ( temp . right ) ; } } System . out . println ( ) ; } } public static void main ( String [ ] args ) { Node root ; root = new Node ( 71 ) ; root . left = new Node ( 88 ) ; root . right = new Node ( 99 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 5 ) ; root . right . left = new Node ( 6 ) ; root . right . right = new Node ( 7 ) ; root . left . left . left = new Node ( 8 ) ; root . left . left . right = new Node ( 9 ) ; root . left . right . left = new Node ( 10 ) ; root . left . right . right = new Node ( 11 ) ; root . right . left . right = new Node ( 13 ) ; root . right . right . left = new Node ( 14 ) ; PrintAlternate ( root ) ; } }
Count subarrays for every array element in which they are the minimum | Java implementation of the above approach ; Function to required count subarrays ; For storing count of subarrays ; For finding next smaller element left to a element if there is no next smaller element left to it than taking - 1. ; For finding next smaller element right to a element if there is no next smaller element right to it than taking n . ; Taking exact boundaries in which arr [ i ] is minimum ; Similarly for right side ; Driver code ; Given array arr [ ] ; Function call
import java . util . * ; public class gfg { static int [ ] countingSubarray ( int arr [ ] , int n ) { int a [ ] = new int [ n ] ; int nsml [ ] = new int [ n ] ; Arrays . fill ( nsml , - 1 ) ; int nsmr [ ] = new int [ n ] ; Arrays . fill ( nsmr , n ) ; Stack < Integer > st = new Stack < Integer > ( ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { while ( st . size ( ) > 0 && arr [ ( int ) st . peek ( ) ] >= arr [ i ] ) st . pop ( ) ; nsmr [ i ] = ( st . size ( ) > 0 ) ? ( int ) st . peek ( ) : n ; st . push ( i ) ; } while ( st . size ( ) > 0 ) st . pop ( ) ; for ( int i = 0 ; i < n ; i ++ ) { while ( st . size ( ) > 0 && arr [ ( int ) st . peek ( ) ] >= arr [ i ] ) st . pop ( ) ; nsml [ i ] = ( st . size ( ) > 0 ) ? ( int ) st . peek ( ) : - 1 ; st . push ( i ) ; } for ( int i = 0 ; i < n ; i ++ ) { nsml [ i ] ++ ; nsmr [ i ] -- ; int r = nsmr [ i ] - i + 1 ; int l = i - nsml [ i ] + 1 ; a [ i ] = r * l ; } return a ; } public static void main ( String [ ] args ) { int N = 5 ; int arr [ ] = { 3 , 2 , 4 , 1 , 5 } ; int a [ ] = countingSubarray ( arr , N ) ; System . out . print ( " [ " ) ; int n = a . length - 1 ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( a [ i ] + " , ▁ " ) ; System . out . print ( a [ n ] + " ] " ) ; } }
Lexicographically smallest permutation of a string that contains all substrings of another string | Java program for the above approach ; Function to reorder the String B to contain all the subStrings of A ; Find length of Strings ; Initialize array to count the frequencies of the character ; Counting frequencies of character in B ; Find remaining character in B ; Declare the reordered String ; Loop until freq [ j ] > 0 ; Decrement the value from freq array ; Check if A [ j ] > A [ 0 ] ; Check if A [ j ] < A [ 0 ] ; Append the remaining characters to the end of the result ; Push all the values from frequency array in the answer ; Return the answer ; Driver Code ; Given Strings A and B ; Function Call
class GFG { static String reorderString ( char [ ] A , char [ ] B ) { int size_a = A . length ; int size_b = B . length ; int freq [ ] = new int [ 300 ] ; for ( int i = 0 ; i < size_b ; i ++ ) freq [ B [ i ] ] ++ ; for ( int i = 0 ; i < size_a ; i ++ ) freq [ A [ i ] ] -- ; for ( int j = ' a ' ; j <= ' z ' ; j ++ ) { if ( freq [ j ] < 0 ) return " - 1" ; } String answer = " " ; for ( int j = ' a ' ; j < A [ 0 ] ; j ++ ) while ( freq [ j ] > 0 ) { answer += j ; freq [ j ] -- ; } int first = A [ 0 ] ; for ( int j = 0 ; j < size_a ; j ++ ) { if ( A [ j ] > A [ 0 ] ) break ; if ( A [ j ] < A [ 0 ] ) { answer += String . valueOf ( A ) ; A = new char [ A . length ] ; break ; } } while ( freq [ first ] > 0 ) { answer += String . valueOf ( ( char ) first ) ; -- freq [ first ] ; } answer += String . valueOf ( A ) ; for ( int j = ' a ' ; j <= ' z ' ; j ++ ) while ( freq [ j ] -- > 0 ) answer += ( ( char ) j ) ; return answer ; } public static void main ( String [ ] args ) { String A = " aa " ; String B = " ababab " ; System . out . print ( reorderString ( A . toCharArray ( ) , B . toCharArray ( ) ) ) ; } }
Find the interval which contains maximum number of concurrent meetings | Java implementation of the above approach ; Function to find time slot of maximum concurrent meeting ; Sort array by start time of meeting ; Declare Minheap ; Insert first meeting end time ; Initialize max_len , max_start and max_end ; Traverse over sorted array to find required slot ; Pop all meetings that end before current meeting ; Push current meeting end time ; Update max_len , max_start and max_end if size of queue is greater than max_len ; Print slot of maximum concurrent meeting ; Driver Code ; Given array of meetings ; Function Call
import java . util . * ; import java . lang . * ; class GFG { static void maxConcurrentMeetingSlot ( int [ ] [ ] meetings ) { Arrays . sort ( meetings , ( a , b ) -> ( a [ 0 ] != b [ 0 ] ) ? a [ 0 ] - b [ 0 ] : a [ 1 ] - b [ 1 ] ) ; PriorityQueue < Integer > pq = new PriorityQueue < > ( ) ; pq . add ( meetings [ 0 ] [ 1 ] ) ; int max_len = 0 , max_start = 0 ; int max_end = 0 ; for ( int [ ] k : meetings ) { while ( ! pq . isEmpty ( ) && k [ 0 ] >= pq . peek ( ) ) pq . poll ( ) ; pq . add ( k [ 1 ] ) ; if ( pq . size ( ) > max_len ) { max_len = pq . size ( ) ; max_start = k [ 0 ] ; max_end = pq . peek ( ) ; } } System . out . println ( max_start + " ▁ " + max_end ) ; } public static void main ( String [ ] args ) { int meetings [ ] [ ] = { { 100 , 200 } , { 50 , 300 } , { 300 , 400 } } ; maxConcurrentMeetingSlot ( meetings ) ; } }
Number from a range [ L , R ] having Kth minimum cost of conversion to 1 by given operations | Java implementation of the above approach ; Function to calculate the cost ; Base case ; Even condition ; Odd condition ; Return cost ; Function to find Kth element ; Array to store the costs ; Sort the array based on cost ; Driver code ; Given range and6 K ; Function call
import java . util . * ; class GFG { static int func ( int n ) { int count = 0 ; if ( n == 2 n == 1 ) return 1 ; if ( n % 2 == 0 ) count = 1 + func ( n / 2 ) ; if ( n % 2 != 0 ) count = 1 + func ( n * 3 + 1 ) ; return count ; } static void findKthElement ( int l , int r , int k ) { ArrayList < Integer > arr = new ArrayList < > ( ) ; for ( int i = l ; i <= r ; i ++ ) arr . add ( i ) ; ArrayList < List < Integer > > result = new ArrayList < > ( ) ; for ( int i : arr ) result . add ( Arrays . asList ( i , func ( i ) ) ) ; Collections . sort ( result , ( s1 , s2 ) -> s1 . get ( 1 ) - s2 . get ( 1 ) ) ; System . out . println ( result . get ( k - 1 ) . get ( 0 ) ) ; } public static void main ( String [ ] args ) { int l = 12 ; int r = 15 ; int k = 2 ; findKthElement ( l , r , k ) ; } }
Check if a string represents a hexadecimal number or not | Java implementation of the above approach ; Function to check if the string represents a hexadecimal number ; Size of string ; Iterate over string ; Check if the character is invalid ; Print true if all characters are valid ; Driver Code ; Given string ; Function Call
import java . util . * ; import java . io . * ; class GFG { public static void checkHex ( String s ) { int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { char ch = s . charAt ( i ) ; if ( ( ch < '0' ch > '9' ) && ( ch < ' A ' ch > ' F ' ) ) { System . out . println ( " No " ) ; return ; } } System . out . println ( " Yes " ) ; } public static void main ( String [ ] args ) { String s = " BF57C " ; checkHex ( s ) ; } }
Longest subsequence forming an Arithmetic Progression ( AP ) | Java program for the above approach ; Function that finds the longest arithmetic subsequence having the same absolute difference ; Stores the length of sequences having same difference ; Stores the resultant length ; Iterate over the array ; Update length of subsequence ; Return res ; Driver Code ; Given array arr [ ] ; Function Call
import java . io . * ; import java . util . * ; class GFG { static int lenghtOfLongestAP ( int A [ ] , int n ) { Map < Integer , Map < Integer , Integer > > dp = new HashMap < Integer , Map < Integer , Integer > > ( ) ; int res = 2 ; for ( int i = 0 ; i < n ; ++ i ) { for ( int j = i + 1 ; j < n ; ++ j ) { int d = A [ j ] - A [ i ] ; Map < Integer , Integer > temp ; if ( dp . containsKey ( d ) ) { temp = dp . get ( d ) ; if ( temp . containsKey ( i ) ) temp . put ( j , temp . get ( i ) + 1 ) ; else temp . put ( j , 2 ) ; } else { temp = new HashMap < Integer , Integer > ( ) ; temp . put ( j , 2 ) ; } dp . put ( d , temp ) ; res = Math . max ( res , temp . get ( j ) ) ; } } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 20 , 1 , 15 , 3 , 10 , 5 , 8 } ; int N = arr . length ; System . out . println ( lenghtOfLongestAP ( arr , N ) ) ; } }
Length of longest increasing prime subsequence from a given array | Java program for the above approach ; Function to find the prime numbers till 10 ^ 5 using Sieve of Eratosthenes ; False here indicates that it is not prime ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p , set them to non - prime ; Function which computes the length of the LIS of Prime Numbers ; Create an array of size n ; Create boolean array to mark prime numbers ; Initialize all values to true ; Precompute N primes ; Compute optimized LIS having prime numbers in bottom up manner ; Check for LIS and prime ; Return maximum value in lis [ ] ; Driver Code ; Given array ; Size of array ; Function call
import java . util . * ; class GFG { static final int N = 100005 ; static void SieveOfEratosthenes ( boolean prime [ ] , int p_size ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= p_size ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i <= p_size ; i += p ) prime [ i ] = false ; } } } static int LISPrime ( int arr [ ] , int n ) { int [ ] lisp = new int [ n ] ; boolean [ ] prime = new boolean [ N + 1 ] ; for ( int i = 0 ; i < prime . length ; i ++ ) prime [ i ] = true ; SieveOfEratosthenes ( prime , N ) ; lisp [ 0 ] = prime [ arr [ 0 ] ] ? 1 : 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( ! prime [ arr [ i ] ] ) { lisp [ i ] = 0 ; continue ; } lisp [ i ] = 1 ; for ( int j = 0 ; j < i ; j ++ ) { if ( prime [ arr [ j ] ] && arr [ i ] > arr [ j ] && lisp [ i ] < lisp [ j ] + 1 ) { lisp [ i ] = lisp [ j ] + 1 ; } } } return Arrays . stream ( lisp ) . max ( ) . getAsInt ( ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 5 , 3 , 2 , 5 , 1 , 7 } ; int M = arr . length ; System . out . print ( LISPrime ( arr , M ) ) ; } }
Check if a given number can be expressed as pair | Java program of the above approach ; Function to check if the number is pair - sum of sum of first X natural numbers ; Check if the given number is sum of pair of special numbers ; X is the sum of first i natural numbers ; t = 2 * Y ; Condition to check if Y is a special number ; Driver Code ; Function call
import java . util . * ; import java . lang . * ; class GFG { static void checkSumOfNatural ( int n ) { int i = 1 ; boolean flag = false ; while ( i * ( i + 1 ) < n * 2 ) { int X = i * ( i + 1 ) ; int t = n * 2 - X ; int k = ( int ) Math . sqrt ( t ) ; if ( k * ( k + 1 ) == t ) { flag = true ; break ; } i += 1 ; } if ( flag ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } public static void main ( String [ ] args ) { int n = 25 ; checkSumOfNatural ( n ) ; } }