codeparrot/xlcost-text-to-code huggingface dataset:
Count pairs from two sorted arrays whose sum is equal to a given value x | Java implementation to count pairs from both sorted arrays whose sum is equal to a given value ; function to count all pairs from both the sorted arrays whose sum is equal to a given value ; insert all the elements of 1 st array in the hash table ( unordered_set ' us ' ) ; for each element of 'arr2[] ; find ( x - arr2 [ j ] ) in ' us ' ; required count of pairs ; Driver Code
import java . util . * ; class GFG { static int countPairs ( int arr1 [ ] , int arr2 [ ] , int m , int n , int x ) { int count = 0 ; HashSet < Integer > us = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < m ; i ++ ) us . add ( arr1 [ i ] ) ; for ( int j = 0 ; j < n ; j ++ ) if ( us . contains ( x - arr2 [ j ] ) ) count ++ ; return count ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 1 , 3 , 5 , 7 } ; int arr2 [ ] = { 2 , 3 , 5 , 8 } ; int m = arr1 . length ; int n = arr2 . length ; int x = 10 ; System . out . print ( " Count ▁ = ▁ " + countPairs ( arr1 , arr2 , m , n , x ) ) ; } }
Count pairs from two sorted arrays whose sum is equal to a given value x | Java implementation to count pairs from both sorted arrays whose sum is equal to a given value ; function to count all pairs from both the sorted arrays whose sum is equal to a given value ; traverse ' arr1 [ ] ' from left to right traverse ' arr2 [ ] ' from right to left ; if this sum is equal to ' x ' , then increment ' l ' , decrement ' r ' and increment ' count ' ; if this sum is less than x , then increment l ; else decrement ' r ' ; required count of pairs ; Driver Code
import java . io . * ; class GFG { static int countPairs ( int arr1 [ ] , int arr2 [ ] , int m , int n , int x ) { int count = 0 ; int l = 0 , r = n - 1 ; while ( l < m && r >= 0 ) { if ( ( arr1 [ l ] + arr2 [ r ] ) == x ) { l ++ ; r -- ; count ++ ; } else if ( ( arr1 [ l ] + arr2 [ r ] ) < x ) l ++ ; else r -- ; } return count ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 1 , 3 , 5 , 7 } ; int arr2 [ ] = { 2 , 3 , 5 , 8 } ; int m = arr1 . length ; int n = arr2 . length ; int x = 10 ; System . out . println ( " Count ▁ = ▁ " + countPairs ( arr1 , arr2 , m , n , x ) ) ; } }
Count pairs from two linked lists whose sum is equal to a given value | Java implementation to count pairs from both linked lists whose sum is equal to a given value Note : here we use java . util . LinkedList for linked list implementation ; method to count all pairs from both the linked lists whose sum is equal to a given value ; traverse the 1 st linked list ; for each node of 1 st list traverse the 2 nd list ; if sum of pair is equal to ' x ' increment count ; required count of pairs ; Driver method
import java . util . Arrays ; import java . util . Iterator ; import java . util . LinkedList ; class GFG { static int countPairs ( LinkedList < Integer > head1 , LinkedList < Integer > head2 , int x ) { int count = 0 ; Iterator < Integer > itr1 = head1 . iterator ( ) ; while ( itr1 . hasNext ( ) ) { Iterator < Integer > itr2 = head2 . iterator ( ) ; Integer t = itr1 . next ( ) ; while ( itr2 . hasNext ( ) ) { if ( ( t + itr2 . next ( ) ) == x ) count ++ ; } } return count ; } public static void main ( String [ ] args ) { Integer arr1 [ ] = { 3 , 1 , 5 , 7 } ; Integer arr2 [ ] = { 8 , 2 , 5 , 3 } ; LinkedList < Integer > head1 = new LinkedList < > ( Arrays . asList ( arr1 ) ) ; LinkedList < Integer > head2 = new LinkedList < > ( Arrays . asList ( arr2 ) ) ; int x = 10 ; System . out . println ( " Count ▁ = ▁ " + countPairs ( head1 , head2 , x ) ) ; } }
Count quadruples from four sorted arrays whose sum is equal to a given value x | Java implementation to count quadruples from four sorted arrays whose sum is equal to a given value x ; find the ' value ' in the given array ' arr [ ] ' binary search technique is applied ; ' value ' found ; ' value ' not found ; function to count all quadruples from four sorted arrays whose sum is equal to a given value x ; generate all triplets from the 1 st three arrays ; calculate the sum of elements in the triplet so generated ; check if ' x - T ' is present in 4 th array or not ; increment count ; required count of quadruples ; Driver code ; four sorted arrays each of size ' n '
class GFG { static boolean isPresent ( int [ ] arr , int low , int high , int value ) { while ( low <= high ) { int mid = ( low + high ) / 2 ; if ( arr [ mid ] == value ) return true ; else if ( arr [ mid ] > value ) high = mid - 1 ; else low = mid + 1 ; } return false ; } static int countQuadruples ( int [ ] arr1 , int [ ] arr2 , int [ ] arr3 , int [ ] arr4 , int n , int x ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) for ( int k = 0 ; k < n ; k ++ ) { int T = arr1 [ i ] + arr2 [ j ] + arr3 [ k ] ; if ( isPresent ( arr4 , 0 , n - 1 , x - T ) ) count ++ ; } return count ; } public static void main ( String [ ] args ) { int [ ] arr1 = { 1 , 4 , 5 , 6 } ; int [ ] arr2 = { 2 , 3 , 7 , 8 } ; int [ ] arr3 = { 1 , 4 , 6 , 10 } ; int [ ] arr4 = { 2 , 4 , 7 , 8 } ; int n = 4 ; int x = 30 ; System . out . println ( " Count ▁ = ▁ " + countQuadruples ( arr1 , arr2 , arr3 , arr4 , n , x ) ) ; } }
Count quadruples from four sorted arrays whose sum is equal to a given value x | Java implementation to count quadruples from four sorted arrays whose sum is equal to a given value x ; count pairs from the two sorted array whose sum is equal to the given ' value ' ; traverse ' arr1 [ ] ' from left to right traverse ' arr2 [ ] ' from right to left ; if the ' sum ' is equal to ' value ' , then increment ' l ' , decrement ' r ' and increment ' count ' ; if the ' sum ' is greater than ' value ' , then decrement r ; else increment l ; required count of pairs ; function to count all quadruples from four sorted arrays whose sum is equal to a given value x ; generate all pairs from arr1 [ ] and arr2 [ ] ; calculate the sum of elements in the pair so generated ; count pairs in the 3 rd and 4 th array having value ' x - p _ sum ' and then accumulate it to ' count ' ; required count of quadruples ; Driver program to test above ; four sorted arrays each of size ' n '
class GFG { static int countPairs ( int arr1 [ ] , int arr2 [ ] , int n , int value ) { int count = 0 ; int l = 0 , r = n - 1 ; while ( l < n & r >= 0 ) { int sum = arr1 [ l ] + arr2 [ r ] ; if ( sum == value ) { l ++ ; r -- ; count ++ ; } else if ( sum > value ) r -- ; else l ++ ; } return count ; } static int countQuadruples ( int arr1 [ ] , int arr2 [ ] , int arr3 [ ] , int arr4 [ ] , int n , int x ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) { int p_sum = arr1 [ i ] + arr2 [ j ] ; count += countPairs ( arr3 , arr4 , n , x - p_sum ) ; } return count ; } static public void main ( String [ ] args ) { int arr1 [ ] = { 1 , 4 , 5 , 6 } ; int arr2 [ ] = { 2 , 3 , 7 , 8 } ; int arr3 [ ] = { 1 , 4 , 6 , 10 } ; int arr4 [ ] = { 2 , 4 , 7 , 8 } ; int n = arr1 . length ; int x = 30 ; System . out . println ( " Count ▁ = ▁ " + countQuadruples ( arr1 , arr2 , arr3 , arr4 , n , x ) ) ; } }
Count quadruples from four sorted arrays whose sum is equal to a given value x | Java implementation to count quadruples from four sorted arrays whose sum is equal to a given value x ; function to count all quadruples from four sorted arrays whose sum is equal to a given value x ; unordered_map ' um ' implemented as hash table for < sum , frequency > tuples ; count frequency of each sum obtained from the pairs of arr1 [ ] and arr2 [ ] and store them in ' um ' ; generate pair from arr3 [ ] and arr4 [ ] ; calculate the sum of elements in the pair so generated ; if ' x - p _ sum ' is present in ' um ' then add frequency of ' x - p _ sum ' to ' count ' ; required count of quadruples ; Driver program to test above ; four sorted arrays each of size ' n '
import java . util . * ; class GFG { static int countQuadruples ( int arr1 [ ] , int arr2 [ ] , int arr3 [ ] , int arr4 [ ] , int n , int x ) { int count = 0 ; Map < Integer , Integer > m = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) if ( m . containsKey ( arr1 [ i ] + arr2 [ j ] ) ) m . put ( ( arr1 [ i ] + arr2 [ j ] ) , m . get ( ( arr1 [ i ] + arr2 [ j ] ) ) + 1 ) ; else m . put ( ( arr1 [ i ] + arr2 [ j ] ) , 1 ) ; for ( int k = 0 ; k < n ; k ++ ) for ( int l = 0 ; l < n ; l ++ ) { int p_sum = arr3 [ k ] + arr4 [ l ] ; if ( m . containsKey ( x - p_sum ) ) count += m . get ( x - p_sum ) ; } return count ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 1 , 4 , 5 , 6 } ; int arr2 [ ] = { 2 , 3 , 7 , 8 } ; int arr3 [ ] = { 1 , 4 , 6 , 10 } ; int arr4 [ ] = { 2 , 4 , 7 , 8 } ; int n = arr1 . length ; int x = 30 ; System . out . println ( " Count ▁ = ▁ " + countQuadruples ( arr1 , arr2 , arr3 , arr4 , n , x ) ) ; } }
How to learn Pattern printing easily ? | Java program to find triplets a [ i ] > a [ j ] > a [ k ] and i < j < k ; Driver code
import java . util . * ; class GFG { public static void main ( String [ ] args ) { int N = 4 , i , j , min ; System . out . println ( " Value ▁ of ▁ N : ▁ " + N ) ; for ( i = 1 ; i <= N ; i ++ ) { for ( j = 1 ; j <= N ; j ++ ) { min = i < j ? i : j ; System . out . print ( N - min + 1 ) ; } System . out . println ( ) ; } } }
Number of subarrays having sum exactly equal to k | Java program for the above approach ; calculate all subarrays ; calculate required sum ; check if sum is equal to required sum
import java . util . * ; class Solution { public static void main ( String [ ] args ) { int arr [ ] = { 10 , 2 , - 2 , - 20 , 10 } ; int k = - 10 ; int n = arr . length ; int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int sum = 0 ; for ( int j = i ; j < n ; j ++ ) { sum += arr [ j ] ; if ( sum == k ) res ++ ; } } System . out . println ( res ) ; } }
Number of subarrays having sum exactly equal to k | Java program to find number of subarrays with sum exactly equal to k . ; Function to find number of subarrays with sum exactly equal to k . ; HashMap to store number of subarrays starting from index zero having particular value of sum . ; Sum of elements so far . ; Add current element to sum so far . ; If currsum is equal to desired sum , then a new subarray is found . So increase count of subarrays . ; currsum exceeds given sum by currsum - sum . Find number of subarrays having this sum and exclude those subarrays from currsum by increasing count by same amount . ; Add currsum value to count of different values of sum . ; Driver Code
import java . util . HashMap ; import java . util . Map ; public class GfG { static int findSubarraySum ( int arr [ ] , int n , int sum ) { HashMap < Integer , Integer > prevSum = new HashMap < > ( ) ; int res = 0 ; int currsum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { currsum += arr [ i ] ; if ( currsum == sum ) res ++ ; if ( prevSum . containsKey ( currsum - sum ) ) res += prevSum . get ( currsum - sum ) ; Integer count = prevSum . get ( currsum ) ; if ( count == null ) prevSum . put ( currsum , 1 ) ; else prevSum . put ( currsum , count + 1 ) ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 2 , - 2 , - 20 , 10 } ; int sum = - 10 ; int n = arr . length ; System . out . println ( findSubarraySum ( arr , n , sum ) ) ; } }
Count pairs whose products exist in array | Java program to count pairs whose product exist in array ; Returns count of pairs whose product exists in arr [ ] ; find product in an array ; if product found increment counter ; return Count of all pair whose product exist in array ; Driver Code
import java . io . * ; class GFG { static int countPairs ( int arr [ ] , int n ) { int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int product = arr [ i ] * arr [ j ] ; for ( int k = 0 ; k < n ; k ++ ) { if ( arr [ k ] == product ) { result ++ ; break ; } } } } return result ; } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 2 , 4 , 12 , 5 , 3 } ; int n = arr . length ; System . out . println ( countPairs ( arr , n ) ) ; } }
Count pairs whose products exist in array | A hashing based Java program to count pairs whose product exists in arr [ ] ; Returns count of pairs whose product exists in arr [ ] ; Create an empty hash - set that store all array element ; Insert all array element into set ; Generate all pairs and check is exist in ' Hash ' or not ; if product exists in set then we increment count by 1 ; return count of pairs whose product exist in array ; Driver program
import java . util . * ; class GFG { static int countPairs ( int arr [ ] , int n ) { int result = 0 ; HashSet < Integer > Hash = new HashSet < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { Hash . add ( arr [ i ] ) ; } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int product = arr [ i ] * arr [ j ] ; if ( Hash . contains ( product ) ) { result ++ ; } } } return result ; } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 2 , 4 , 12 , 5 , 3 } ; int n = arr . length ; System . out . println ( countPairs ( arr , n ) ) ; } }
Master Theorem For Subtract and Conquer Recurrences | Java code for above the approach . ; Driver Code
class clg { static int fib ( int n ) { if ( n <= 1 ) return n ; return fib ( n - 1 ) + fib ( n - 2 ) ; } public static void main ( String [ ] args ) { int n = 9 ; System . out . println ( fib ( n ) ) ; } }
Tail Recursion | Java Code for Tail Recursion ; A tail recursive function to calculate factorial ; A wrapper over factTR ; Driver code
class GFG { static int factTR ( int n , int a ) { if ( n == 0 ) return a ; return factTR ( n - 1 , n * a ) ; } static int fact ( int n ) { return factTR ( n , 1 ) ; } static public void main ( String [ ] args ) { System . out . println ( fact ( 5 ) ) ; } }
Minimum and maximum count of elements at D distance from arr [ i ] in either direction | Java program for the above approach ; Function to find the minimum and maximum number of points included in a range of distance D ; Stores the minimum and maximum number of points that lies over the distance of D ; Iterate the array ; Count of elements included to left of point at index i ; Update the minimum number of points ; Update the maximum number of points ; Count of elements included to right of point at index i ; Update the minimum number of points ; Update the maximum number of points ; Return the array ; Function to perform the Binary Search to the left of arr [ i ] over the given range ; Base Case ; Binary Search for index to left ; update index ; Return the number of elements by subtracting indices ; Function to perform the Binary Search to the right of arr [ i ] over the given range ; Base Case ; Binary Search for index to right ; Update the index ; Return the number of elements by subtracting indices ; Driver Code ; Function Call
import java . io . * ; import java . lang . Math ; import java . util . * ; class GFG { public static int [ ] minMaxRange ( int [ ] arr , int D , int N ) { int max = 1 , min = N ; for ( int i = 0 ; i < N ; i ++ ) { int dist = leftSearch ( arr , arr [ i ] - D , i ) ; min = Math . min ( min , dist ) ; max = Math . max ( max , dist ) ; dist = rightSearch ( arr , arr [ i ] + D , i ) ; min = Math . min ( min , dist ) ; max = Math . max ( max , dist ) ; } return new int [ ] { min , max } ; } public static int leftSearch ( int [ ] arr , int val , int i ) { if ( i == 0 ) return 1 ; int left = 0 , right = i - 1 ; int ind = - 1 ; while ( left <= right ) { int mid = ( left + right ) / 2 ; if ( arr [ mid ] < val ) { left = mid + 1 ; } else { right = mid - 1 ; ind = mid ; } } return ind != - 1 ? i - ind + 1 : 1 ; } public static int rightSearch ( int [ ] arr , int val , int i ) { if ( i == arr . length - 1 ) return 1 ; int left = i + 1 ; int right = arr . length - 1 ; int ind = - 1 ; while ( left <= right ) { int mid = ( left + right ) / 2 ; if ( arr [ mid ] > val ) { right = mid - 1 ; } else { left = mid + 1 ; ind = mid ; } } return ind != - 1 ? ind - i + 1 : 1 ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 5 , 9 , 14 } ; int N = arr . length ; int D = 4 ; int [ ] minMax = minMaxRange ( arr , D , N ) ; System . out . print ( minMax [ 0 ] + " ▁ " + minMax [ 1 ] ) ; } }
Given two unsorted arrays , find all pairs whose sum is x | Java program to find all pairs in both arrays whose sum is equal to given value x ; Function to print all pairs in both arrays whose sum is equal to given value x ; Driver code
import java . io . * ; class GFG { static void findPairs ( int arr1 [ ] , int arr2 [ ] , int n , int m , int x ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < m ; j ++ ) if ( arr1 [ i ] + arr2 [ j ] == x ) System . out . println ( arr1 [ i ] + " ▁ " + arr2 [ j ] ) ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 1 , 2 , 3 , 7 , 5 , 4 } ; int arr2 [ ] = { 0 , 7 , 4 , 3 , 2 , 1 } ; int x = 8 ; findPairs ( arr1 , arr2 , arr1 . length , arr2 . length , x ) ; } }
Given two unsorted arrays , find all pairs whose sum is x | JAVA Code for Given two unsorted arrays , find all pairs whose sum is x ; Function to find all pairs in both arrays whose sum is equal to given value x ; Insert all elements of first array in a hash ; Subtract sum from second array elements one by one and check it 's present in array first  or not ; Driver program to test above function
import java . util . * ; class GFG { public static void findPairs ( int arr1 [ ] , int arr2 [ ] , int n , int m , int x ) { HashMap < Integer , Integer > s = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) s . put ( arr1 [ i ] , 0 ) ; for ( int j = 0 ; j < m ; j ++ ) if ( s . containsKey ( x - arr2 [ j ] ) ) System . out . println ( x - arr2 [ j ] + " ▁ " + arr2 [ j ] ) ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 1 , 0 , - 4 , 7 , 6 , 4 } ; int arr2 [ ] = { 0 , 2 , 4 , - 3 , 2 , 1 } ; int x = 8 ; findPairs ( arr1 , arr2 , arr1 . length , arr2 . length , x ) ; } }
Altitude of largest Triangle that can be inscribed in a Rectangle | C ++ program for the above approach ; Function to find the greatest altitude of the largest triangle triangle that can be inscribed in the rectangle ; If L is greater than B ; Stores the maximum altitude value ; Return res ; Driver Code ; Given Input ; Function call
import java . io . * ; class GFG { static int largestAltitude ( int L , int B ) { if ( L > B ) { int t = L ; L = B ; B = t ; } int res = Math . min ( B / 2 , L ) ; return res ; } public static void main ( String [ ] args ) { int L = 3 ; int B = 4 ; System . out . print ( largestAltitude ( L , B ) ) ; } }
Check if a given Binary Tree is height balanced like a Red | Java Program to check if a given Binary Tree is balanced like a Red - Black Tree ; Returns returns tree if the Binary tree is balanced like a Red - Black tree . This function also sets value in maxh and minh ( passed by reference ) . maxh and minh are set as maximum and minimum heights of root . ; Base case ; To store max and min heights of left subtree ; To store max and min heights of right subtree ; Check if left subtree is balanced , also set lmxh and lmnh ; Check if right subtree is balanced , also set rmxh and rmnh ; Set the max and min heights of this node for the parent call ; See if this node is balanced ; A wrapper over isBalancedUtil ( ) ; Driver code
class GFG { static class Node { int key ; Node left , right ; Node ( int key ) { left = null ; right = null ; this . key = key ; } } static class INT { static int d ; INT ( ) { d = 0 ; } } static boolean isBalancedUtil ( Node root , INT maxh , INT minh ) { if ( root == null ) { maxh . d = minh . d = 0 ; return true ; } INT lmxh = new INT ( ) , lmnh = new INT ( ) ; INT rmxh = new INT ( ) , rmnh = new INT ( ) ; if ( isBalancedUtil ( root . left , lmxh , lmnh ) == false ) return false ; if ( isBalancedUtil ( root . right , rmxh , rmnh ) == false ) return false ; maxh . d = Math . max ( lmxh . d , rmxh . d ) + 1 ; minh . d = Math . min ( lmnh . d , rmnh . d ) + 1 ; if ( maxh . d <= 2 * minh . d ) return true ; return false ; } static boolean isBalanced ( Node root ) { INT maxh = new INT ( ) , minh = new INT ( ) ; return isBalancedUtil ( root , maxh , minh ) ; } public static void main ( String args [ ] ) { Node root = new Node ( 10 ) ; root . left = new Node ( 5 ) ; root . right = new Node ( 100 ) ; root . right . left = new Node ( 50 ) ; root . right . right = new Node ( 150 ) ; root . right . left . left = new Node ( 40 ) ; System . out . println ( isBalanced ( root ) ? " Balanced " : " Not ▁ Balanced " ) ; } }
Maximum Fixed Point ( Value equal to index ) in a given Array | Java implementation of the above approach ; Function to find the maximum index i such that arr [ i ] is equal to i ; Traversing the array from backwards ; If arr [ i ] is equal to i ; If there is no such index ; Driver code ; Given Input ; Function Call
import java . io . * ; class GFG { static void findLargestIndex ( int arr [ ] , int n ) { for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( arr [ i ] == i ) { System . out . println ( i ) ; return ; } } System . out . println ( - 1 ) ; } public static void main ( String [ ] args ) { int arr [ ] = { - 10 , - 5 , 0 , 3 , 7 } ; int n = arr . length ; findLargestIndex ( arr , n ) ; } }
Cumulative frequency of count of each element in an unsorted array | Java program to print the cumulative frequency according to the order given ; Function to print the cumulative frequency according to the order given ; Insert elements and their frequencies in hash map . ; traverse in the array ; add the frequencies ; if the element has not been visited previously ; mark the hash 0 as the element 's cumulative frequency  has been printed ; Driver Code
class GFG { static void countFreq ( int a [ ] , int n ) { int hm [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) hm [ a [ i ] ] ++ ; int cumul = 0 ; for ( int i = 0 ; i < n ; i ++ ) { cumul += hm [ a [ i ] ] ; if ( hm [ a [ i ] ] != 0 ) { System . out . println ( a [ i ] + " - > " + cumul ) ; } hm [ a [ i ] ] = 0 ; } } public static void main ( String [ ] args ) { int a [ ] = { 1 , 3 , 2 , 4 , 2 , 1 } ; int n = a . length ; countFreq ( a , n ) ; } }
Find pairs in array whose sums already exist in array | A simple Java program to find pair whose sum already exists in array ; Function to find pair whose sum exists in arr [ ] ; Driver code
import java . io . * ; public class GFG { static void findPair ( int [ ] arr , int n ) { boolean found = false ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { for ( int k = 0 ; k < n ; k ++ ) { if ( arr [ i ] + arr [ j ] == arr [ k ] ) { System . out . println ( arr [ i ] + " ▁ " + arr [ j ] ) ; found = true ; } } } } if ( found == false ) System . out . println ( " Not ▁ exist " ) ; } static public void main ( String [ ] args ) { int [ ] arr = { 10 , 4 , 8 , 13 , 5 } ; int n = arr . length ; findPair ( arr , n ) ; } }
Maximum number of mangoes that can be bought | Java program for the above approach ; Function to check if mid number of mangoes can be bought ; Store the coins ; If watermelons needed are greater than given watermelons ; Store remaining watermelons if vl watermelons are used to buy mangoes ; Store the value of coins if these watermelon get sold ; Increment coins by ex ; Number of mangoes that can be buyed if only x coins needed for one mango ; If the condition is satisfied , return true ; Otherwise return false ; Function to find the maximum number of mangoes that can be bought by selling watermelons ; Initialize the boundary values ; Store the required result ; Binary Search ; Store the mid value ; Check if it is possible to buy mid number of mangoes ; Otherwise , update r to mid - 1 ; Return the result ; Driver code ; Given Input ; Function Call
class GFG { static boolean check ( int n , int m , int x , int y , int vl ) { int temp = m ; if ( vl > n ) return false ; int ex = n - vl ; ex *= y ; temp += ex ; int cr = temp / x ; if ( cr >= vl ) return true ; return false ; } static int maximizeMangoes ( int n , int m , int x , int y ) { int l = 0 , r = n ; int ans = 0 ; while ( l <= r ) { int mid = l + ( r - l ) / 2 ; if ( check ( n , m , x , y , mid ) ) { ans = mid ; l = mid + 1 ; } else r = mid - 1 ; } return ans ; } public static void main ( String [ ] args ) { int W = 4 , C = 8 , x = 4 , y = 4 ; System . out . println ( maximizeMangoes ( W , C , x , y ) ) ; } }