codeparrot/xlcost-text-to-code huggingface dataset:
Maximum of minimum difference of all pairs from subsequences of given size | Java program to implement the above approach ; Function to check a subsequence can be formed with min difference mid ; If a subsequence of size B with min diff = mid is possible return true else false ; Function to find the maximum of all minimum difference of pairs possible among the subsequence ; Sort the Array ; Stores the boundaries of the search space ; Store the answer ; Binary Search ; If subsequence can be formed with min diff mid and size B ; Right half ; Left half ; Driver Code
import java . util . * ; class GFG { static boolean can_place ( int A [ ] , int n , int B , int mid ) { int count = 1 ; int last_position = A [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( A [ i ] - last_position >= mid ) { last_position = A [ i ] ; count ++ ; if ( count == B ) { return true ; } } } return false ; } static int find_min_difference ( int A [ ] , int n , int B ) { Arrays . sort ( A ) ; int s = 0 ; int e = A [ n - 1 ] - A [ 0 ] ; int ans = 0 ; while ( s <= e ) { int mid = ( s + e ) / 2 ; if ( can_place ( A , n , B , mid ) ) { ans = mid ; s = mid + 1 ; } else { e = mid - 1 ; } } return ans ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 2 , 3 , 5 } ; int n = A . length ; int B = 3 ; int min_difference = find_min_difference ( A , n , B ) ; System . out . print ( min_difference ) ; } }
Count of triplets in an Array such that A [ i ] * A [ j ] = A [ k ] and i < j < k | Java program to implement the above approach ; Returns total number of valid triplets possible ; Stores the count ; Map to store frequency of array elements ; Increment the frequency of A [ j + 1 ] as it can be a valid A [ k ] ; If target exists in the map ; Return the final count ; Driver Code
import java . util . * ; class GFG { static int countTriplets ( int A [ ] , int N ) { int ans = 0 ; HashMap < Integer , Integer > map = new HashMap < Integer , Integer > ( ) ; for ( int j = N - 2 ; j >= 1 ; j -- ) { if ( map . containsKey ( A [ j + 1 ] ) ) map . put ( A [ j + 1 ] , map . get ( A [ j + 1 ] ) + 1 ) ; else map . put ( A [ j + 1 ] , 1 ) ; for ( int i = 0 ; i < j ; i ++ ) { int target = A [ i ] * A [ j ] ; if ( map . containsKey ( target ) ) ans += map . get ( target ) ; } } return ans ; } public static void main ( String [ ] args ) { int N = 5 ; int A [ ] = { 2 , 3 , 4 , 6 , 12 } ; System . out . print ( countTriplets ( A , N ) ) ; } }
Missing vertex among N axis | Java Program to implement the above approach ; Driver Code ; Number of rectangles ; Stores the coordinates ; Insert the coordinates
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void MissingPoint ( Vector < pair > V , int N ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < V . size ( ) ; i ++ ) { if ( mp . containsKey ( V . get ( i ) . first ) ) mp . put ( V . get ( i ) . first , V . get ( i ) . first + 1 ) ; else mp . put ( V . get ( i ) . first , 1 ) ; } int x = 0 , y = 0 ; for ( Map . Entry < Integer , Integer > it : mp . entrySet ( ) ) { if ( it . getValue ( ) % 2 == 1 ) { x = it . getKey ( ) ; break ; } } mp . clear ( ) ; for ( int i = 0 ; i < V . size ( ) ; i ++ ) { if ( mp . containsKey ( V . get ( i ) . second ) ) mp . put ( V . get ( i ) . second , V . get ( i ) . second + 1 ) ; else mp . put ( V . get ( i ) . second , 1 ) ; } for ( Map . Entry < Integer , Integer > it : mp . entrySet ( ) ) { if ( it . getValue ( ) % 2 == 1 ) { y = it . getKey ( ) ; break ; } } System . out . print ( x + " ▁ " + y ) ; } public static void main ( String [ ] args ) { int N = 2 ; Vector < pair > V = new Vector < pair > ( ) ; V . add ( new pair ( 1 , 1 ) ) ; V . add ( new pair ( 1 , 2 ) ) ; V . add ( new pair ( 4 , 6 ) ) ; V . add ( new pair ( 2 , 1 ) ) ; V . add ( new pair ( 9 , 6 ) ) ; V . add ( new pair ( 9 , 3 ) ) ; V . add ( new pair ( 4 , 3 ) ) ; MissingPoint ( V , N ) ; } }
Longest alternating subsequence with maximum sum | Set 2 | Java Program to implement the above approach ; Function to check the sign of the element ; Function to calculate and return the maximum sum of longest alternating subsequence ; Iterate through the array ; Stores the first element of a sequence of same sign ; Traverse until an element with opposite sign is encountered ; Update the maximum ; Update the maximum sum ; Update i ; Return the maximum sum ; Driver Code
import java . util . * ; class GFG { static int sign ( int x ) { if ( x > 0 ) return 1 ; else return - 1 ; } static int findMaxSum ( int arr [ ] , int size ) { int max_sum = 0 , pres , i , j ; for ( i = 0 ; i < size ; i ++ ) { pres = arr [ i ] ; j = i ; while ( j < size && sign ( arr [ i ] ) == sign ( arr [ j ] ) ) { pres = Math . max ( pres , arr [ j ] ) ; j ++ ; } max_sum = max_sum + pres ; i = j - 1 ; } return max_sum ; } public static void main ( String [ ] args ) { int arr [ ] = { - 2 , 8 , 3 , 8 , - 4 , - 15 , 5 , - 2 , - 3 , 1 } ; int size = arr . length ; System . out . println ( findMaxSum ( arr , size ) ) ; } }
Check if an array can be split into subsets of K consecutive elements | Java Program to implement the above approach ; Function to check if a given array can be split into subsets of K consecutive elements ; Stores the frequencies of array elements ; Traverse the map ; Check if all its occurrences can be grouped into K subsets ; Traverse next K elements ; If the element is not present in the array ; If it cannot be split into required number of subsets ; Driver Code
import java . util . * ; class GFG { static boolean groupInKConsecutive ( int [ ] arr , int K ) { HashMap < Integer , Integer > count = new HashMap < Integer , Integer > ( ) ; for ( int h : arr ) { if ( count . containsKey ( h ) ) count . put ( h , count . get ( h ) + 1 ) ; else count . put ( h , 1 ) ; } for ( Map . Entry < Integer , Integer > c : count . entrySet ( ) ) { int cur = c . getKey ( ) ; int n = c . getValue ( ) ; if ( n > 0 ) { for ( int i = 1 ; i < K ; ++ i ) { if ( ! count . containsKey ( cur + i ) ) { return false ; } count . put ( cur + i , count . get ( cur + i ) - n ) ; if ( count . get ( cur + i ) < 0 ) return false ; } } } return true ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 6 , 2 , 3 , 4 , 7 , 8 } ; int k = 3 ; if ( groupInKConsecutive ( arr , k ) ) { System . out . print ( " True " ) ; } else { System . out . print ( " False " ) ; } } }
Count of substrings of a given Binary string with all characters same | Java program for the above approach ; Function to count number of sub - Strings of a given binary String that contains only 1 ; Iterate untill L and R cross each other ; Check if reached the end of String ; Check if encountered '1' then extend window ; Check if encountered '0' then add number of Strings of current window and change the values for both l and r ; Return the answer ; Function to flip the bits of String ; Function to count number of sub - Strings of a given binary String that contains only 0 s & 1 s ; count of subString which contains only 1 s ; Flip the character of String s 0 to 1 and 1 to 0 to count the subString with consecutive 0 s ; count of subString which contains only 0 s ; Driver Code ; Given String str ; Function call
import java . util . * ; class GFG { static int countSubAllOnes ( String s ) { int l = 0 , r = 0 , ans = 0 ; while ( l <= r ) { if ( r == s . length ( ) ) { ans += ( ( r - l ) * ( r - l + 1 ) ) / 2 ; break ; } if ( s . charAt ( r ) == '1' ) r ++ ; else { ans += ( ( r - l ) * ( r - l + 1 ) ) / 2 ; l = r + 1 ; r ++ ; } } return ans ; } static String flip ( char [ ] s ) { for ( int i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] == '1' ) s [ i ] = '0' ; else s [ i ] = '1' ; } return String . valueOf ( s ) ; } static int countSubAllZerosOnes ( String s ) { int only_1s = countSubAllOnes ( s ) ; s = flip ( s . toCharArray ( ) ) ; int only_0s = countSubAllOnes ( s ) ; return only_0s + only_1s ; } public static void main ( String [ ] args ) { String s = "011" ; System . out . print ( countSubAllZerosOnes ( s ) + "NEW_LINE"); } }
Count of primes in a given range that can be expressed as sum of perfect squares | Java program to implement the above approach ; Function to check if a prime number satisfies the condition to be expressed as sum of two perfect squares ; Function to check if a number is prime or not ; Corner cases ; Function to return the count of primes in the range which can be expressed as the sum of two squares ; If i is a prime ; If i can be expressed as the sum of two squares ; Return the count ; Driver code
import java . util . * ; class GFG { static boolean sumSquare ( int p ) { return ( p - 1 ) % 4 == 0 ; } static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } static int countOfPrimes ( int L , int R ) { int count = 0 ; for ( int i = L ; i <= R ; i ++ ) { if ( isPrime ( i ) ) { if ( sumSquare ( i ) ) count ++ ; } } return count ; } public static void main ( String [ ] args ) { int L = 5 , R = 41 ; System . out . println ( countOfPrimes ( L , R ) ) ; } }
Count of array elements which are greater than all elements on its left | Java program to implement the above approach ; Function to return the count of array elements with all elements to its left smaller than it ; Stores the count ; Stores the maximum ; Iterate over the array ; If an element greater than maximum is obtained ; Increase count ; Update maximum ; Driver Code
import java . util . * ; class GFG { static int count_elements ( int arr [ ] , int n ) { int count = 1 ; int max = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] > max ) { count += 1 ; max = arr [ i ] ; } } return count ; } public static void main ( String s [ ] ) { int arr [ ] = { 2 , 1 , 4 , 6 , 3 } ; int n = arr . length ; System . out . print ( count_elements ( arr , n ) ) ; } }
Count of bitonic substrings from the given string | Java + program for the above approach ; Function to find all the bitonic sub Strings ; Pick starting point ; Iterate till length of the String ; Pick ending point for String ; SubString from i to j is obtained ; SubStrings of length 1 ; Increase count ; For increasing sequence ; Check for strictly increasing ; Increase count ; Check for decreasing sequence ; Increase count ; Print the result ; Driver Code ; Given String ; Function call
import java . util . * ; class GFG { static void subString ( char str [ ] , int n ) { int c = 0 ; for ( int len = 1 ; len <= n ; len ++ ) { for ( int i = 0 ; i <= n - len ; i ++ ) { int j = i + len - 1 ; char temp = str [ i ] , f = 0 ; if ( j == i ) { c ++ ; continue ; } int k = i + 1 ; while ( k < n && temp < str [ k ] ) { temp = str [ k ] ; k ++ ; f = 2 ; } if ( k > j ) { c ++ ; f = 2 ; } while ( k < n && temp > str [ k ] && f != 2 ) { k ++ ; f = 0 ; } if ( k > j && f != 2 ) { c ++ ; f = 0 ; } } } System . out . print ( c + "NEW_LINE"); } public static void main ( String [ ] args ) { char str [ ] = " bade " . toCharArray ( ) ; subString ( str , str . length ) ; } }
Check if ceil of number divided by power of two exist in sorted array | Java implementation to check if a number divided by power of two exist in the sorted array ; Function to find there exist a number or not in the array ; Loop to check if there exist a number by divided by power of 2 ; Binary Search ; Condition to check the number is found in the array or not ; Otherwise divide the number by increasing the one more power of 2 ; Driver Code
import java . util . Scanner ; public class GreeksForGreeksQuestions { static int findNumberDivByPowerofTwo ( int [ ] ar , int k , int n ) { int found = - 1 , m = k ; while ( m > 0 ) { int l = 0 ; int r = n - 1 ; while ( l <= r ) { int mid = ( l + r ) / 2 ; if ( ar [ mid ] == m ) { found = m ; break ; } else if ( ar [ mid ] > m ) { r = mid - 1 ; } else if ( ar [ mid ] < m ) { l = mid + 1 ; } } if ( found != - 1 ) { break ; } m = m / 2 ; } return found ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 5 , 7 , 8 , 10 } ; int k = 4 , n = 5 ; System . out . println ( findNumberDivByPowerofTwo ( arr , k , n ) ) ; } }
Largest subset having with sum less than equal to sum of respective indices | Java program to implement the above approach ; Function to find the length of the longest subset ; Stores the sum of differences between elements and their respective index ; Stores the size of the subset ; Iterate over the array ; If an element which is smaller than or equal to its index is encountered ; Increase count and sum ; Store the difference with index of the remaining elements ; Sort the differences in increasing order ; Include the differences while sum remains positive or ; Return the size ; Driver code ; Function Calling
import java . util . * ; class GFG { public static int findSubset ( int [ ] a , int n ) { int sum = 0 ; int cnt = 0 ; Vector < Integer > v = new Vector < > ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { if ( a [ i - 1 ] - i <= 0 ) { sum += a [ i - 1 ] - i ; cnt += 1 ; } else { v . add ( a [ i - 1 ] - i ) ; } } Collections . sort ( v ) ; int ptr = 0 ; while ( ptr < v . size ( ) && sum + v . get ( ptr ) <= 0 ) { cnt += 1 ; ptr += 1 ; sum += v . get ( ptr ) ; } return cnt ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 1 , 6 , 7 , 8 , 2 } ; int n = arr . length ; System . out . println ( findSubset ( arr , n ) ) ; } }
Queries for count of array elements with values in given range with updates | Java code for queries for number of elements that lie in range [ l , r ] ( with updates ) ; Function to set arr [ index ] = x ; Function to get count of elements that lie in range [ l , r ] ; Traverse array ; If element lies in the range [ L , R ] ; Increase count ; Function to solve each query ; Driver code
import java . util . * ; import java . lang . * ; class GFG { static void setElement ( int [ ] arr , int n , int index , int x ) { arr [ index ] = x ; } static int getCount ( int [ ] arr , int n , int l , int r ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] >= l && arr [ i ] <= r ) { count ++ ; } } return count ; } static void SolveQuery ( int arr [ ] , int n , ArrayList < List < Integer > > Q ) { int x ; for ( int i = 0 ; i < Q . size ( ) ; i ++ ) { if ( Q . get ( i ) . get ( 0 ) == 1 ) { x = getCount ( arr , n , Q . get ( i ) . get ( 1 ) , Q . get ( i ) . get ( 2 ) ) ; System . out . print ( x + " ▁ " ) ; } else { setElement ( arr , n , Q . get ( i ) . get ( 1 ) , Q . get ( i ) . get ( 2 ) ) ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 2 , 3 , 4 , 4 , 5 , 6 } ; int n = arr . length ; ArrayList < List < Integer > > Q = new ArrayList < > ( ) ; Q . add ( Arrays . asList ( 1 , 3 , 5 ) ) ; Q . add ( Arrays . asList ( 1 , 2 , 4 ) ) ; Q . add ( Arrays . asList ( 1 , 1 , 2 ) ) ; Q . add ( Arrays . asList ( 2 , 1 , 7 ) ) ; Q . add ( Arrays . asList ( 1 , 1 , 2 ) ) ; SolveQuery ( arr , n , Q ) ; } }
Construct a sequence from given frequencies of N consecutive integers with unit adjacent difference | Java program for the above approach ; Function generates the sequence ; Map to store the frequency of numbers ; Sum of all frequencies ; Try all possibilities for the starting element ; If the frequency of current element is non - zero ; vector to store the answer ; Copy of the map for every possible starting element ; Decrement the frequency ; Push the starting element to the vector ; The last element inserted is i ; Try to fill the rest of the positions if possible ; If the frequency of last - 1 is non - zero ; Decrement the frequency of last - 1 ; Insert it into the sequence ; Update last number added to sequence ; Break from the inner loop ; If the size of the sequence vector is equal to sum of total frequqncies ; Return sequence ; If no such sequence if found return empty sequence ; Function call to print the sequence ; The required sequence ; If the size of sequence if zero it means no such sequence was found ; Otherwise print the sequence ; Driver Code ; Frequency of all elements from 0 to n - 1 ; Number of elements whose frequencies are given ; Function call
import java . util . * ; class GFG { static Vector < Integer > generateSequence ( int [ ] freq , int n ) { HashMap < Integer , Integer > m = new HashMap < Integer , Integer > ( ) ; int total = 0 ; for ( int i = 0 ; i < n ; i ++ ) { m . put ( i , freq [ i ] ) ; total += freq [ i ] ; } for ( int i = 0 ; i < n ; i ++ ) { if ( m . containsKey ( i ) ) { Vector < Integer > sequence = new Vector < Integer > ( ) ; @ SuppressWarnings ( " unchecked " ) HashMap < Integer , Integer > mcopy = ( HashMap < Integer , Integer > ) m . clone ( ) ; if ( mcopy . containsKey ( i ) && mcopy . get ( i ) > 0 ) mcopy . put ( i , mcopy . get ( i ) - 1 ) ; sequence . add ( i ) ; int last = i ; for ( int i1 = 0 ; i1 < total - 1 ; i1 ++ ) { if ( mcopy . containsKey ( last - 1 ) && mcopy . get ( last - 1 ) > 0 ) { mcopy . put ( last - 1 , mcopy . get ( last - 1 ) - 1 ) ; sequence . add ( last - 1 ) ; last -- ; } else if ( mcopy . containsKey ( last + 1 ) ) { mcopy . put ( last + 1 , mcopy . get ( last + 1 ) - 1 ) ; sequence . add ( last + 1 ) ; last ++ ; } else break ; } if ( sequence . size ( ) == total ) { return sequence ; } } } Vector < Integer > empty = new Vector < Integer > ( ) ; return empty ; } static void PrintSequence ( int freq [ ] , int n ) { Vector < Integer > sequence = generateSequence ( freq , n ) ; if ( sequence . size ( ) == 0 ) { System . out . print ( " - 1" ) ; } else { for ( int i = 0 ; i < sequence . size ( ) ; i ++ ) { System . out . print ( sequence . get ( i ) + " ▁ " ) ; } } } public static void main ( String [ ] args ) { int freq [ ] = { 2 , 2 , 2 , 3 , 1 } ; int N = 5 ; PrintSequence ( freq , N ) ; } }
Minimum distance between any most frequent and least frequent element of an array | Java implementation of the approach ; Function to find the minimum distance between any two most and least frequent element ; Initialize sets to store the least and the most frequent elements ; Initialize variables to store max and min frequency ; Initialize HashMap to store frequency of each element ; Loop through the array ; Store the count of each element ; Store the least and most frequent elements in the respective sets ; Store count of current element ; If count is equal to max count ; Store in max set ; If count is greater then max count ; Empty max set ; Update max count ; Store in max set ; If count is equal to min count ; Store in min set ; If count is less then max count ; Empty min set ; Update min count ; Store in min set ; Initialize a variable to store the minimum distance ; Initialize a variable to store the last index of least frequent element ; Traverse array ; If least frequent element ; Update last index of least frequent element ; If most frequent element ; Update minimum distance ; Traverse array from the end ; If least frequent element ; Update last index of least frequent element ; If most frequent element ; Update minimum distance ; Print the minimum distance ; Driver Code ; Given array ; Function Call
import java . util . * ; class GFG { public static void getMinimumDistance ( int a [ ] , int n ) { Set < Integer > min_set = new HashSet < > ( ) ; Set < Integer > max_set = new HashSet < > ( ) ; int max = 0 , min = Integer . MAX_VALUE ; HashMap < Integer , Integer > frequency = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { frequency . put ( a [ i ] , frequency . getOrDefault ( a [ i ] , 0 ) + 1 ) ; } for ( int i = 0 ; i < n ; i ++ ) { int count = frequency . get ( a [ i ] ) ; if ( count == max ) { max_set . add ( a [ i ] ) ; } else if ( count > max ) { max_set . clear ( ) ; max = count ; max_set . add ( a [ i ] ) ; } if ( count == min ) { min_set . add ( a [ i ] ) ; } else if ( count < min ) { min_set . clear ( ) ; min = count ; min_set . add ( a [ i ] ) ; } } int min_dist = Integer . MAX_VALUE ; int last_min_found = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( min_set . contains ( a [ i ] ) ) last_min_found = i ; if ( max_set . contains ( a [ i ] ) && last_min_found != - 1 ) { min_dist = Math . min ( min_dist , i - last_min_found ) ; } } last_min_found = - 1 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( min_set . contains ( a [ i ] ) ) last_min_found = i ; if ( max_set . contains ( a [ i ] ) && last_min_found != - 1 ) { min_dist = Math . min ( min_dist , last_min_found - i ) ; } } System . out . println ( min_dist ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 2 , 3 , 2 , 3 , 3 } ; int N = arr . length ; getMinimumDistance ( arr , N ) ; } }
Construct a string that has exactly K subsequences from given string | Java program for the above approach ; Function that computes the String s ; Length of the given String str ; List that stores all the prime factors of given k ; Find the prime factors ; Initialize the count of each character position as 1 ; Loop until the list becomes empty ; Increase the character count by multiplying it with the prime factor ; If we reach end then again start from beginning ; Store the output ; Print the String ; Driver code ; Given String ; Function Call
import java . util . * ; class GFG { static void printSubsequenceString ( String str , int k ) { int n = str . length ( ) ; int i ; Vector < Integer > factors = new Vector < Integer > ( ) ; for ( i = 2 ; i <= Math . sqrt ( k ) ; i ++ ) { while ( k % i == 0 ) { factors . add ( i ) ; k /= i ; } } if ( k > 1 ) factors . add ( k ) ; int [ ] count = new int [ n ] ; Arrays . fill ( count , 1 ) ; int index = 0 ; while ( factors . size ( ) > 0 ) { count [ index ++ ] *= factors . get ( factors . size ( ) - 1 ) ; factors . remove ( factors . get ( factors . size ( ) - 1 ) ) ; if ( index == n ) index = 0 ; } String s = " " ; for ( i = 0 ; i < n ; i ++ ) { while ( count [ i ] -- > 0 ) { s += str . charAt ( i ) ; } } System . out . print ( s ) ; } public static void main ( String [ ] args ) { String str = " code " ; int k = 20 ; printSubsequenceString ( str , k ) ; } }
Replace each element of Array with it 's corresponding rank | Java program for the above approach ; Function to assign rank to array elements ; Copy input array into newArray ; Sort newArray [ ] in ascending order ; Map to store the rank of the array element ; Update rank of element ; Assign ranks to elements ; Driver Code ; Given array arr [ ] ; Function Call ; Print the array elements
import java . util . * ; class GFG { static void changeArr ( int [ ] input ) { int newArray [ ] = Arrays . copyOfRange ( input , 0 , input . length ) ; Arrays . sort ( newArray ) ; int i ; Map < Integer , Integer > ranks = new HashMap < > ( ) ; int rank = 1 ; for ( int index = 0 ; index < newArray . length ; index ++ ) { int element = newArray [ index ] ; if ( ranks . get ( element ) == null ) { ranks . put ( element , rank ) ; rank ++ ; } } for ( int index = 0 ; index < input . length ; index ++ ) { int element = input [ index ] ; input [ index ] = ranks . get ( input [ index ] ) ; } } public static void main ( String [ ] args ) { int [ ] arr = { 100 , 2 , 70 , 2 } ; changeArr ( arr ) ; System . out . println ( Arrays . toString ( arr ) ) ; } }
Find pairs in array whose sum does not exist in Array | Java program to implement the above approach ; Function to print all pairs with sum not present in the array ; Corner Case ; Stores the distinct array elements ; Generate all possible pairs ; Calculate sum of current pair ; Check if the sum exists in the HashSet or not ; Driver Code
import java . util . * ; class GFG { public static void findPair ( int [ ] arr , int n ) { int i , j ; if ( n < 2 ) { System . out . println ( " - 1" ) ; } HashSet < Integer > hashMap = new HashSet < Integer > ( ) ; for ( Integer k : arr ) { hashMap . add ( k ) ; } for ( i = 0 ; i < n - 1 ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { int sum = arr [ i ] + arr [ j ] ; if ( ! hashMap . contains ( sum ) ) { System . out . println ( " ( " + arr [ i ] + " , ▁ " + arr [ j ] + " ) " ) ; } } } } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 4 , 2 , 6 } ; int n = arr . length ; findPair ( arr , n ) ; } }