codeparrot/xlcost-text-to-code huggingface dataset:
Check if uppercase characters in a string are used correctly or not | package whatever do not write package name here ; Function to check if the character c is in lowercase or not ; Function to check if the character c is in uppercase or not ; Utility function to check if uppercase characters are used correctly or not ; Length of string ; If the first character is in lowercase ; Otherwise ; Check if all characters are in uppercase or not ; If all characters are in uppercase ; Check if all characters except the first are in lowercase ; Function to check if uppercase characters are used correctly or not ; Stores whether the use of uppercase characters are correct or not ; If correct ; Otherwise ; Driver Code ; Given string ; Function call to check if use of uppercase characters is correct or not
import java . io . * ; class GFG { static boolean isLower ( char c ) { return c >= ' a ' && c <= ' z ' ; } static boolean isUpper ( char c ) { return c >= ' A ' && c <= ' Z ' ; } static boolean detectUppercaseUseUtil ( String S ) { int N = S . length ( ) ; int i ; if ( isLower ( S . charAt ( 0 ) ) ) { i = 1 ; while ( i < N && isLower ( S . charAt ( i ) ) ) ++ i ; return i == N ? true : false ; } else { i = 1 ; while ( i < N && isUpper ( S . charAt ( i ) ) ) ++ i ; if ( i == N ) return true ; else if ( i > 1 ) return false ; while ( i < N && isLower ( S . charAt ( i ) ) ) ++ i ; return i == N ? true : false ; } } static void detectUppercaseUse ( String S ) { boolean check = detectUppercaseUseUtil ( S ) ; if ( check ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { String S = " GeeKs " ; detectUppercaseUse ( S ) ; } }
Queries to count array elements from a given range having a single set bit | package whatever do not write package name here ; Check if N has only one set bit ; Function to build segment tree ; If se is leaf node ; Update the node ; Stores mid value of segment [ ss , se ] ; Recursive call for left Subtree ; Recursive call for right Subtree ; Update the Current Node ; Function to update a value at Xth index ; If ss is equal to X ; Update the Xth node ; Update the tree ; Stores the mid of segment [ ss , se ] ; Update current node ; Count of numbers having one set bit ; If L > se or R < ss Invalid Range ; If [ ss , se ] lies inside the [ L , R ] ; Stores the mid of segment [ ss , se ] ; Return the sum after recursively traversing left and right subtree ; Function to solve queries ; Initialise Segment tree ; Build segment tree ; Perform queries ; Driver Code ; Input ; Function call to solve queries
import java . io . * ; class GFG { static int check ( int N ) { if ( Integer . bitCount ( N ) == 1 ) return 1 ; else return 0 ; } static void build_seg_tree ( int ss , int se , int si , int tree [ ] , int arr [ ] ) { if ( ss == se ) { tree [ si ] = check ( arr [ ss ] ) ; return ; } int mid = ( ss + se ) / 2 ; build_seg_tree ( ss , mid , 2 * si + 1 , tree , arr ) ; build_seg_tree ( mid + 1 , se , 2 * si + 2 , tree , arr ) ; tree [ si ] = tree [ 2 * si + 1 ] + tree [ 2 * si + 2 ] ; } static void update ( int ss , int se , int si , int X , int V , int tree [ ] , int arr [ ] ) { if ( ss == se ) { if ( ss == X ) { arr [ X ] = V ; tree [ si ] = check ( V ) ; } return ; } int mid = ( ss + se ) / 2 ; if ( X <= mid ) update ( ss , mid , 2 * si + 1 , X , V , tree , arr ) ; else update ( mid + 1 , se , 2 * si + 2 , X , V , tree , arr ) ; tree [ si ] = tree [ 2 * si + 1 ] + tree [ 2 * si + 2 ] ; } static int query ( int l , int r , int ss , int se , int si , int tree [ ] ) { if ( r < ss l > se ) return 0 ; if ( l <= ss && r >= se ) return tree [ si ] ; int mid = ( ss + se ) / 2 ; return query ( l , r , ss , mid , 2 * si + 1 , tree ) + query ( l , r , mid + 1 , se , 2 * si + 2 , tree ) ; } static void Query ( int arr [ ] , int N , int [ ] [ ] Q ) { int tree [ ] = new int [ 4 * N ] ; build_seg_tree ( 0 , N - 1 , 0 , tree , arr ) ; for ( int i = 0 ; i < Q . length ; i ++ ) { if ( Q [ i ] [ 0 ] == 1 ) System . out . print ( query ( Q [ i ] [ 1 ] , Q [ i ] [ 2 ] , 0 , N - 1 , 0 , tree ) + " ▁ " ) ; else update ( 0 , N - 1 , 0 , Q [ i ] [ 1 ] , Q [ i ] [ 2 ] , tree , arr ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 12 , 11 , 16 , 2 , 32 } ; int [ ] [ ] Q = { { 1 , 0 , 2 } , { 2 , 4 , 24 } , { 1 , 1 , 4 } } ; int N = arr . length ; Query ( arr , N , Q ) ; } }
Find the smallest value of N such that sum of first N natural numbers is â ‰¥ X | Java program for the above approach ; Function to check if sum of first N natural numbers is >= X ; Finds minimum value of N such that sum of first N natural number >= X ; Binary Search ; Checks if sum of first ' mid ' natural numbers is greater than equal to X ; Update res ; Update high ; Update low ; Driver Code ; Input ; Finds minimum value of N such that sum of first N natural number >= X
import java . util . * ; class GFG { static boolean isGreaterEqual ( int N , int X ) { return ( N * ( N + 1 ) / 2 ) >= X ; } static int minimumPossible ( int X ) { int low = 1 , high = X , res = - 1 ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; if ( isGreaterEqual ( mid , X ) ) { res = mid ; high = mid - 1 ; } else low = mid + 1 ; } return res ; } public static void main ( String [ ] args ) { int X = 14 ; System . out . print ( minimumPossible ( X ) ) ; } }
Find sum of non | Java Find the sum of all non - repeated elements in an array ; Find the sum of all non - repeated elements in an array ; sort all elements of array ; Driver code
import java . util . Arrays ; public class GFG { static int findSum ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int sum = arr [ 0 ] ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] != arr [ i + 1 ] ) { sum = sum + arr [ i + 1 ] ; } } return sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 1 , 1 , 4 , 5 , 6 } ; int n = arr . length ; System . out . println ( findSum ( arr , n ) ) ; } }
Length of the longest subsequence such that XOR of adjacent elements is equal to K | Java program for the above approach ; Function to find maximum length of subsequence having XOR of adjacent elements equal to K ; Store maximum length of subsequence ; Stores the dp - states ; Base case ; Iterate over the range [ 1 , N - 1 ] ; Iterate over the range [ 0 , i - 1 ] ; If arr [ i ] ^ arr [ j ] = = K ; Update the dp [ i ] ; Update the maximum subsequence length ; If length of longest subsequence is less than 2 then return 0 ; Driver Code ; Input ; Print the length of longest subsequence
import java . io . * ; import java . util . * ; class GFG { public static int xorSubsequence ( int a [ ] , int n , int k ) { int ans = 0 ; int dp [ ] = new int [ n ] ; dp [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = i - 1 ; j >= 0 ; j -- ) { if ( ( a [ i ] ^ a [ j ] ) == k ) dp [ i ] = Math . max ( dp [ i ] , dp [ j ] + 1 ) ; } ans = Math . max ( ans , dp [ i ] ) ; dp [ i ] = Math . max ( 1 , dp [ i ] ) ; } return ans >= 2 ? ans : 0 ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 4 , 3 , 5 } ; int K = 1 ; int N = arr . length ; System . out . println ( xorSubsequence ( arr , N , K ) ) ; } }
Check if a Binary Tree contains duplicate subtrees of size 2 or more | Java program to find if there is a duplicate sub - tree of size 2 or more . ; A binary tree Node has data , pointer to left child and a pointer to right child ; This function returns empty string if tree contains a duplicate subtree of size 2 or more . ; If current node is NULL , return marker ; If left subtree has a duplicate subtree . ; Do same for right subtree ; Serialize current subtree ; If current subtree already exists in hash table . [ Note that size of a serialized tree with single node is 3 as it has two marker nodes . ; Function to find if the Binary Tree contains duplicate subtrees of size 2 or more ; Driver program to test above functions
import java . util . HashSet ; public class Main { static char MARKER = ' $ ' ; class Node { int data ; Node left , right ; Node ( int data ) { this . data = data ; } } ; public static String dupSubUtil ( Node root , HashSet < String > subtrees ) { String s = " " ; if ( root == null ) return s + MARKER ; String lStr = dupSubUtil ( root . left , subtrees ) ; if ( lStr . equals ( s ) ) return s ; String rStr = dupSubUtil ( root . right , subtrees ) ; if ( rStr . equals ( s ) ) return s ; s = s + root . data + lStr + rStr ; if ( s . length ( ) > 3 && subtrees . contains ( s ) ) return " " ; subtrees . add ( s ) ; return s ; } public static String dupSub ( Node root ) { HashSet < String > subtrees = new HashSet < > ( ) ; return dupSubUtil ( root , subtrees ) ; } public static void main ( String args [ ] ) { Node root = new Node ( ' A ' ) ; root . left = new Node ( ' B ' ) ; root . right = new Node ( ' C ' ) ; root . left . left = new Node ( ' D ' ) ; root . left . right = new Node ( ' E ' ) ; root . right . right = new Node ( ' B ' ) ; root . right . right . right = new Node ( ' E ' ) ; root . right . right . left = new Node ( ' D ' ) ; String str = dupSub ( root ) ; if ( str . equals ( " " ) ) System . out . print ( " ▁ Yes ▁ " ) ; else System . out . print ( " ▁ No ▁ " ) ; } }
Count pairs of similar rectangles possible from a given array |  ; Get the count of all pairs of similar rectangles ; Initialize the result value and map to store the ratio to the rectangles ; Calculate the rectangular ratio and save them ; Calculate pairs of similar rectangles from its common ratio ; Driver code
import java . util . * ; class GFG { public static int getCount ( int rows , int columns , int [ ] [ ] sides ) { int res = 0 ; Map < Double , Integer > ratio = new HashMap < Double , Integer > ( ) ; for ( int i = 0 ; i < rows ; i ++ ) { double rectRatio = ( double ) sides [ i ] [ 0 ] / sides [ i ] [ 1 ] ; if ( ratio . get ( rectRatio ) == null ) { ratio . put ( rectRatio , 0 ) ; } ratio . put ( rectRatio , ratio . get ( rectRatio ) + 1 ) ; } for ( double key : ratio . keySet ( ) ) { int val = ratio . get ( key ) ; if ( val > 1 ) { res += ( val * ( val - 1 ) ) / 2 ; } } return res ; } public static void main ( String [ ] args ) { int [ ] [ ] A = { { 4 , 8 } , { 10 , 20 } , { 15 , 30 } , { 3 , 6 } } ; int columns = 2 ; int rows = 4 ; System . out . println ( getCount ( rows , columns , A ) ) ; } }
Non | Java program to find first non - repeating element . ; Driver code
class GFG { static int firstNonRepeating ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int j ; for ( j = 0 ; j < n ; j ++ ) if ( i != j && arr [ i ] == arr [ j ] ) break ; if ( j == n ) return arr [ i ] ; } return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 9 , 4 , 9 , 6 , 7 , 4 } ; int n = arr . length ; System . out . print ( firstNonRepeating ( arr , n ) ) ; } }
Non | Efficient Java program to find first non - repeating element . ; Insert all array elements in hash table ; Traverse array again and return first element with count 1. ; Driver code
import java . util . * ; class GFG { static int firstNonRepeating ( int arr [ ] , int n ) { Map < Integer , Integer > m = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( m . containsKey ( arr [ i ] ) ) { m . put ( arr [ i ] , m . get ( arr [ i ] ) + 1 ) ; } else { m . put ( arr [ i ] , 1 ) ; } } for ( int i = 0 ; i < n ; i ++ ) if ( m . get ( arr [ i ] ) == 1 ) return arr [ i ] ; return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 9 , 4 , 9 , 6 , 7 , 4 } ; int n = arr . length ; System . out . println ( firstNonRepeating ( arr , n ) ) ; } }
Non | Efficient Java program to print all non - repeating elements . ; Insert all array elements in hash table ; Traverse through map only and using for - each loop for iteration over Map . entrySet ( ) ; Driver code
import java . util . * ; class GFG { static void firstNonRepeating ( int arr [ ] , int n ) { Map < Integer , Integer > m = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( m . containsKey ( arr [ i ] ) ) { m . put ( arr [ i ] , m . get ( arr [ i ] ) + 1 ) ; } else { m . put ( arr [ i ] , 1 ) ; } } for ( Map . Entry < Integer , Integer > x : m . entrySet ( ) ) if ( x . getValue ( ) == 1 ) System . out . print ( x . getKey ( ) + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 9 , 4 , 9 , 6 , 7 , 4 } ; int n = arr . length ; firstNonRepeating ( arr , n ) ; } }
Minimum substring removals required to make all remaining characters of a string same | Java program to implement the above approach ; Function to count minimum operations required to make all characters equal by repeatedly removing subString ; Remove consecutive duplicate characters from str ; Stores length of the String ; Stores frequency of each distinct characters of the String str ; Iterate over all the characters of the String str ; Update frequency of str [ i ] ; Decrementing the frequency of the String str [ 0 ] ; Decrementing the frequency of the String str [ N - 1 ] ; Stores the required count ; Iterate over all characters of the String str ; Update ans ; Driver Code ; Given String
import java . util . * ; class GFG { static void minOperationNeeded ( char [ ] str ) { str = modstring ( str ) ; int N = str . length ; int res [ ] = new int [ 256 ] ; for ( int i = 0 ; i < N ; ++ i ) { res [ str [ i ] ] += 1 ; } res [ str [ 0 ] ] -= 1 ; res [ str [ N - 1 ] ] -= 1 ; int ans = Integer . MAX_VALUE ; for ( int i = 0 ; i < N ; ++ i ) { ans = Math . min ( ans , res [ str [ i ] ] ) ; } System . out . print ( ( ans + 1 ) + "NEW_LINE"); } private static char [ ] modstring ( char [ ] str ) { String s = " " ; boolean b = true ; for ( int i = 1 ; i < str . length ; ++ i ) { if ( str [ i - 1 ] != str [ i ] ) b = true ; if ( b ) { s += str [ i - 1 ] ; b = false ; } } return s . toCharArray ( ) ; } public static void main ( String [ ] args ) { String str = " ABCDABCDABCDA " ; minOperationNeeded ( str . toCharArray ( ) ) ; } }
Print all root to leaf paths of an N | Java program for the above approach ; Structure of an N ary tree node ; Parameterized Constructor ; Function to print the root to leaf path of the given N - ary Tree ; Print elements in the vector ; Utility function to print all root to leaf paths of an Nary Tree ; If root is null ; Insert current node 's  data into the vector ; If current node is a leaf node ; Print the path ; Pop the leaf node and return ; Recur for all children of the current node ; Recursive Function Call ; Function to print root to leaf path ; If root is null , return ; Stores the root to leaf path ; Utility function call ; Driver Code ; Given N - Ary tree ; Function Call
import java . util . ArrayList ; class GFG { static class Node { int data ; ArrayList < Node > child ; public Node ( int x ) { this . data = x ; this . child = new ArrayList < > ( ) ; } } ; static void printPath ( ArrayList < Integer > vec ) { for ( int ele : vec ) { System . out . print ( ele + " ▁ " ) ; } System . out . println ( ) ; } static void printAllRootToLeafPaths ( Node root , ArrayList < Integer > vec ) { if ( root == null ) return ; vec . add ( root . data ) ; if ( root . child . isEmpty ( ) ) { printPath ( vec ) ; vec . remove ( vec . size ( ) - 1 ) ; return ; } for ( int i = 0 ; i < root . child . size ( ) ; i ++ ) printAllRootToLeafPaths ( root . child . get ( i ) , vec ) ; vec . remove ( vec . size ( ) - 1 ) ; } static void printAllRootToLeafPaths ( Node root ) { if ( root == null ) return ; ArrayList < Integer > vec = new ArrayList < > ( ) ; printAllRootToLeafPaths ( root , vec ) ; } public static void main ( String [ ] args ) { Node root = new Node ( 1 ) ; ( root . child ) . add ( new Node ( 2 ) ) ; ( root . child ) . add ( new Node ( 3 ) ) ; ( root . child . get ( 0 ) . child ) . add ( new Node ( 4 ) ) ; ( root . child . get ( 1 ) . child ) . add ( new Node ( 5 ) ) ; ( root . child . get ( 1 ) . child ) . add ( new Node ( 6 ) ) ; ( root . child . get ( 1 ) . child . get ( 1 ) . child ) . add ( new Node ( 7 ) ) ; ( root . child . get ( 1 ) . child . get ( 1 ) . child ) . add ( new Node ( 8 ) ) ; printAllRootToLeafPaths ( root ) ; } }
Minimum removals to make a string concatenation of a substring of 0 s followed by a substring of 1 s | Java program for the above approach ; Function to count minimum removals required to make a given string concatenation of substring of 0 s followed by substring of 1 s ; Stores the length of the string ; Precompute the count of 0 s ; Check for the last character ; Traverse and update zeroCount array ; If current character is 0 , ; Update aCount [ i ] as aCount [ i + 1 ] + 1 ; Update as aCount [ i + 1 ] ; Keeps track of deleted 1 s ; Stores the count of removals ; Traverse the array ; If current character is 1 ; Update ans ; If all 1 s are deleted ; Return the minimum number of deletions ; Driver Code
import java . io . * ; class GFG { public static int minimumDeletions ( String s ) { int n = s . length ( ) ; int zeroCount [ ] = new int [ n ] ; zeroCount [ n - 1 ] = ( s . charAt ( n - 1 ) == '0' ) ? 1 : 0 ; for ( int i = n - 2 ; i >= 0 ; i -- ) zeroCount [ i ] = ( s . charAt ( i ) == '0' ) ? zeroCount [ i + 1 ] + 1 : zeroCount [ i + 1 ] ; int oneCount = 0 ; int ans = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . charAt ( i ) == '1' ) { ans = Math . min ( ans , oneCount + zeroCount [ i ] ) ; oneCount ++ ; } } ans = Math . min ( ans , oneCount ) ; return ( ans == Integer . MAX_VALUE ) ? 0 : ans ; } public static void main ( String [ ] args ) { String str = "00101101" ; System . out . println ( minimumDeletions ( str ) ) ; } }
Pairs of Positive Negative values in an array | Java program to find pairs of positive and negative values present in an array . ; Print pair with negative and positive value ; For each element of array . ; Try to find the negative value of arr [ i ] from i + 1 to n ; If absolute values are equal print pair . ; If size of vector is 0 , therefore there is no element with positive negative value , print "0" ; Sort the vector ; Print the pair with negative positive value . ; Driven Program
import java . util . * ; import java . lang . * ; class GFG { public static void printPairs ( int arr [ ] , int n ) { Vector < Integer > v = new Vector < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( Math . abs ( arr [ i ] ) == Math . abs ( arr [ j ] ) ) v . add ( Math . abs ( arr [ i ] ) ) ; if ( v . size ( ) == 0 ) return ; Collections . sort ( v ) ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) System . out . print ( - v . get ( i ) + " ▁ " + v . get ( i ) ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 8 , 9 , - 4 , 1 , - 1 , - 8 , - 9 } ; int n = arr . length ; printPairs ( arr , n ) ; } }
Check if an array can be divided into pairs whose sum is divisible by k | JAVA program to check if arr [ 0. . n - 1 ] can be divided in pairs such that every pair is divisible by k . ; Returns true if arr [ 0. . n - 1 ] can be divided into pairs with sum divisible by k . ; An odd length array cannot be divided into pairs ; Create a frequency array to count occurrences of all remainders when divided by k . ; Count occurrences of all remainders ; Traverse input array and use freq [ ] to decide if given array can be divided in pairs ; Remainder of current element ; If remainder with current element divides k into two halves . ; Then there must be even occurrences of such remainder ; If remainder is 0 , then there must be two elements with 0 remainder ; Then there must be even occurrences of such remainder ; Else number of occurrences of remainder must be equal to number of occurrences of k - remainder ; Driver code ; Function call
import java . util . HashMap ; public class Divisiblepair { static boolean canPairs ( int ar [ ] , int k ) { if ( ar . length % 2 == 1 ) return false ; HashMap < Integer , Integer > hm = new HashMap < > ( ) ; for ( int i = 0 ; i < ar . length ; i ++ ) { int rem = ( ( ar [ i ] % k ) + k ) % k ; if ( ! hm . containsKey ( rem ) ) { hm . put ( rem , 0 ) ; } hm . put ( rem , hm . get ( rem ) + 1 ) ; } for ( int i = 0 ; i < ar . length ; i ++ ) { int rem = ( ( ar [ i ] % k ) + k ) % k ; if ( 2 * rem == k ) { if ( hm . get ( rem ) % 2 == 1 ) return false ; } else if ( rem == 0 ) { if ( hm . get ( rem ) % 2 == 1 ) return false ; } else { if ( hm . get ( k - rem ) != hm . get ( rem ) ) return false ; } } return true ; } public static void main ( String [ ] args ) { int arr [ ] = { 92 , 75 , 65 , 48 , 45 , 35 } ; int k = 10 ; boolean ans = canPairs ( arr , k ) ; if ( ans ) System . out . println ( " True " ) ; else System . out . println ( " False " ) ; } }
Subarray with no pair sum divisible by K | Java Program to find the subarray with no pair sum divisible by K ; function to find the subarray with no pair sum divisible by k ; hash table to store the remainders obtained on dividing by K ; s : starting index of the current subarray , e : ending index of the current subarray , maxs : starting index of the maximum size subarray so far , maxe : ending index of the maximum size subarray so far ; insert the first element in the set ; Removing starting elements of current subarray while there is an element in set which makes a pair with mod [ i ] such that the pair sum is divisible . ; include the current element in the current subarray the ending index of the current subarray increments by one ; compare the size of the current subarray with the maximum size so far ; Driver Code
import java . io . * ; import java . util . * ; public class GFG { static void subarrayDivisibleByK ( int [ ] arr , int n , int k ) { int [ ] mp = new int [ 1000 ] ; int s = 0 , e = 0 , maxs = 0 , maxe = 0 ; mp [ arr [ 0 ] % k ] ++ ; for ( int i = 1 ; i < n ; i ++ ) { int mod = arr [ i ] % k ; while ( mp [ k - mod ] != 0 || ( mod == 0 && mp [ mod ] != 0 ) ) { mp [ arr [ s ] % k ] -- ; s ++ ; } mp [ mod ] ++ ; e ++ ; if ( ( e - s ) > ( maxe - maxs ) ) { maxe = e ; maxs = s ; } } System . out . print ( " The ▁ maximum ▁ size ▁ is ▁ " + ( maxe - maxs + 1 ) + " and the subarray is as followsNEW_LINE"); for ( int i = maxs ; i <= maxe ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String args [ ] ) { int k = 3 ; int [ ] arr = { 5 , 10 , 15 , 20 , 25 } ; int n = arr . length ; subarrayDivisibleByK ( arr , n , k ) ; } }
Find three element from different three arrays such that a + b + c = sum | Java program to find three element from different three arrays such that a + b + c is equal to given sum ; Function to check if there is an element from each array such that sum of the three elements is equal to given sum . ; Driver code
class GFG { static boolean findTriplet ( int a1 [ ] , int a2 [ ] , int a3 [ ] , int n1 , int n2 , int n3 , int sum ) { for ( int i = 0 ; i < n1 ; i ++ ) for ( int j = 0 ; j < n2 ; j ++ ) for ( int k = 0 ; k < n3 ; k ++ ) if ( a1 [ i ] + a2 [ j ] + a3 [ k ] == sum ) return true ; return false ; } public static void main ( String [ ] args ) { int a1 [ ] = { 1 , 2 , 3 , 4 , 5 } ; int a2 [ ] = { 2 , 3 , 6 , 1 , 2 } ; int a3 [ ] = { 3 , 2 , 4 , 5 , 6 } ; int sum = 9 ; int n1 = a1 . length ; int n2 = a2 . length ; int n3 = a3 . length ; if ( findTriplet ( a1 , a2 , a3 , n1 , n2 , n3 , sum ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }