codeparrot/xlcost-text-to-code huggingface dataset:
Find Partition Line such that sum of values on left and right is equal | Java implementation of the approach ; Function that returns true if the required line exists ; To handle negative values from x [ ] ; Update arr [ ] such that arr [ i ] contains the sum of all v [ j ] such that x [ j ] = i for all valid values of j ; Update arr [ i ] such that arr [ i ] contains the sum of the subarray arr [ 0. . . i ] from the original array ; If all the points add to 0 then the line can be drawn anywhere ; If the line is drawn touching the leftmost possible points ; If the line is drawn just before the current point ; If the line is drawn touching the current point ; If the line is drawn just after the current point ; If the line is drawn touching the rightmost possible points ; Driver code
class GFG { static int MAX = 1000 ; static boolean lineExists ( int x [ ] , int y [ ] , int v [ ] , int n ) { int size = ( 2 * MAX ) + 1 ; long [ ] arr = new long [ size ] ; for ( int i = 0 ; i < n ; i ++ ) { arr [ x [ i ] + MAX ] += v [ i ] ; } for ( int i = 1 ; i < size ; i ++ ) arr [ i ] += arr [ i - 1 ] ; if ( arr [ size - 1 ] == 0 ) return true ; if ( arr [ size - 1 ] - arr [ 0 ] == 0 ) return true ; for ( int i = 1 ; i < size - 1 ; i ++ ) { if ( arr [ i - 1 ] == arr [ size - 1 ] - arr [ i - 1 ] ) return true ; if ( arr [ i - 1 ] == arr [ size - 1 ] - arr [ i ] ) return true ; if ( arr [ i ] == arr [ size - 1 ] - arr [ i ] ) return true ; } if ( arr [ size - 2 ] == 0 ) return true ; return false ; } public static void main ( String [ ] args ) { int x [ ] = { - 3 , 5 , 8 } ; int y [ ] = { 8 , 7 , 9 } ; int v [ ] = { 8 , 2 , 10 } ; int n = x . length ; if ( lineExists ( x , y , v , n ) ) System . out . printf ( " Yes " ) ; else System . out . printf ( " No " ) ; } }
Maximum sum of minimums of pairs in an array | Java implementation of the approach ; Function to return the maximum required sum of the pairs ; Sort the array ; To store the sum ; Start making pairs of every two consecutive elements as n is even ; Minimum element of the current pair ; Return the maximum possible sum ; Driver code
import java . util . Arrays ; class GFG { static int maxSum ( int a [ ] , int n ) { Arrays . sort ( a ) ; int sum = 0 ; for ( int i = 0 ; i < n - 1 ; i += 2 ) { sum += a [ i ] ; } return sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 2 , 1 , 4 , 5 } ; int n = arr . length ; System . out . println ( maxSum ( arr , n ) ) ; } }
Sort elements by modulo with K | Java implementation of the approach ; Utility function to print the contents of an array ; Function to sort the array elements based on their modulo with K ; Create K empty vectors ; Update the vectors such that v [ i ] will contain all the elements that give remainder as i when divided by k ; Sorting all the vectors separately ; Replacing the elements in arr [ ] with the required modulo sorted elements ; Add all the elements of the current vector to the array ; Print the sorted array ; Driver Code
import java . util . * ; class GFG { static void printArr ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } static void sortWithRemainder ( int [ ] arr , int n , int k ) { ArrayList < ArrayList < Integer > > v = new ArrayList < ArrayList < Integer > > ( k ) ; for ( int i = 0 ; i < k ; i ++ ) v . add ( new ArrayList < Integer > ( ) ) ; for ( int i = 0 ; i < n ; i ++ ) { int t = arr [ i ] % k ; v . get ( t ) . add ( arr [ i ] ) ; } for ( int i = 0 ; i < k ; i ++ ) { Collections . sort ( v . get ( i ) ) ; } int j = 0 ; for ( int i = 0 ; i < k ; i ++ ) { for ( int x : v . get ( i ) ) { arr [ j ] = x ; j ++ ; } } printArr ( arr , n ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 10 , 7 , 2 , 6 , 12 , 3 , 33 , 46 } ; int n = arr . length ; int k = 4 ; sortWithRemainder ( arr , n , k ) ; } }
Concatenate the strings in an order which maximises the occurrence of subsequence " ab " | Java implementation of the approach ; Custom sort function to sort the given string in the order which maximises the final score ; To store the count of occurrences of ' a ' and ' b ' in s1 ; Count the number of occurrences of ' a ' and ' b ' in s1 ; To store the count of occurrences of ' a ' and ' b ' in s2 ; Count the number of occurrences of ' a ' and ' b ' in s2 ; Since the number of subsequences ' ab ' is more when s1 is placed before s2 we return 1 so that s1 occurs before s2 in the combined string ; Function that return the concatenated string as S [ 0 ] + S [ 1 ] + ... + S [ N - 1 ] ; To store the concatenated string ; Concatenate every string in the order of appearance ; Return the concatenated string ; Function to return the maximum required score ; Sort the strings in the order which maximizes the score that we can get ; Get the concatenated string combined string ; Calculate the score of the combined string i . e . the count of occurrences of " ab " as subsequences ; Number of ' a ' has increased by one ; There are count_a number of ' a ' that can form subsequence ' ab ' with this ' b ' ; Driver code
import java . util . * ; class Gfg { public static boolean customSort ( String s1 , String s2 ) { int count_a1 = 0 , count_b1 = 0 ; for ( int i = 0 ; i < s1 . length ( ) ; i ++ ) { if ( s1 . charAt ( i ) == ' a ' ) { count_a1 ++ ; } else { count_b1 ++ ; } } int count_a2 = 0 , count_b2 = 0 ; for ( int i = 0 ; i < s2 . length ( ) ; i ++ ) { if ( s2 . charAt ( i ) == ' a ' ) { count_a2 ++ ; } else { count_b2 ++ ; } } if ( count_a1 * count_b2 > count_b1 * count_a2 ) { return true ; } else { return false ; } } public static String concatenateStrings ( String S [ ] , int N ) { String str = " " ; for ( int i = 0 ; i < N ; i ++ ) { str += S [ i ] ; } return str ; } public static int getMaxScore ( String S [ ] , int N ) { Arrays . sort ( S ) ; String combined_string = concatenateStrings ( S , N ) ; int final_score = 0 , count_a = 0 ; for ( int i = 0 ; i < combined_string . length ( ) ; i ++ ) { if ( combined_string . charAt ( i ) == ' a ' ) { count_a ++ ; } else { final_score += count_a ; } } return final_score ; } public static void main ( String [ ] args ) { String S [ ] = { " aa " , " bb " , " aab " , " bab " } ; int N = S . length ; System . out . println ( getMaxScore ( S , N ) - 10 ) ; } }
Minimum increment operations to make K elements equal | Java implementation of the approach ; Function to return the minimum number of increment operations required to make any k elements of the array equal ; Sort the array in increasing order ; Calculate the number of operations needed to make 1 st k elements equal to the kth element i . e . the 1 st window ; Answer will be the minimum of all possible k sized windows ; Find the operations needed to make k elements equal to ith element ; Slide the window to the right and subtract increments spent on leftmost element of the previous window ; Add increments needed to make the 1 st k - 1 elements of this window equal to the kth element of the current window ; Driver code
import java . util . Arrays ; class geeksforgeeks { static int minOperations ( int ar [ ] , int k ) { Arrays . sort ( ar ) ; int opsNeeded = 0 ; for ( int i = 0 ; i < k ; i ++ ) { opsNeeded += ar [ k - 1 ] - ar [ i ] ; } int ans = opsNeeded ; for ( int i = k ; i < ar . length ; i ++ ) { opsNeeded = opsNeeded - ( ar [ i - 1 ] - ar [ i - k ] ) ; opsNeeded += ( k - 1 ) * ( ar [ i ] - ar [ i - 1 ] ) ; ans = Math . min ( ans , opsNeeded ) ; } return ans ; } public static void main ( String [ ] args ) { int [ ] arr = { 3 , 1 , 9 , 100 } ; int n = arr . length ; int k = 3 ; System . out . printf ( " % d " , minOperations ( arr , k ) ) ; } }
Lexicographical ordering using Heap Sort | Java implementation to print the string in Lexicographical order ; Used for index in heap ; Predefining the heap array ; Defining formation of the heap ; Iterative heapiFy ; Just swapping if the element is smaller than already stored element ; Swapping the current index with its child ; Moving upward in the heap ; Defining heap sort ; Taking output of the minimum element ; Set first element as a last one ; Decrement of the size of the string ; Initializing the left and right index ; Process of heap sort If root element is minimum than its both of the child then break ; Otherwise checking that the child which one is smaller , swap them with parent element ; Swapping ; Changing the left index and right index ; Utility function ; To heapiFy ; Calling heap sort function ; Driver Code
class GFG { static int x = - 1 ; static String [ ] heap = new String [ 1000 ] ; static void heapForm ( String k ) { x ++ ; heap [ x ] = k ; int child = x ; String tmp ; int index = x / 2 ; while ( index >= 0 ) { if ( heap [ index ] . compareTo ( heap [ child ] ) > 0 ) { tmp = heap [ index ] ; heap [ index ] = heap [ child ] ; heap [ child ] = tmp ; child = index ; index = index / 2 ; } else { break ; } } } static void heapSort ( ) { int left1 , right1 ; while ( x >= 0 ) { String k ; k = heap [ 0 ] ; System . out . print ( k + " ▁ " ) ; heap [ 0 ] = heap [ x ] ; x = x - 1 ; String tmp ; int index = 0 ; int length = x ; left1 = 1 ; right1 = left1 + 1 ; while ( left1 <= length ) { if ( heap [ index ] . compareTo ( heap [ left1 ] ) <= 0 && heap [ index ] . compareTo ( heap [ right1 ] ) <= 0 ) { break ; } else { if ( heap [ left1 ] . compareTo ( heap [ right1 ] ) < 0 ) { tmp = heap [ index ] ; heap [ index ] = heap [ left1 ] ; heap [ left1 ] = tmp ; index = left1 ; } else { tmp = heap [ index ] ; heap [ index ] = heap [ right1 ] ; heap [ right1 ] = tmp ; index = right1 ; } } left1 = 2 * left1 ; right1 = left1 + 1 ; } } } static void sort ( String k [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { heapForm ( k [ i ] ) ; } heapSort ( ) ; } public static void main ( String [ ] args ) { String arr [ ] = { " banana " , " orange " , " apple " , " pineapple " , " berries " , " lichi " } ; int n = arr . length ; sort ( arr , n ) ; } }
Find Kth element in an array containing odd elements first and then even elements | Java implementation of the approach ; Function to return the kth element in the modified array ; First odd number ; Insert the odd number ; Next odd number ; First even number ; Insert the even number ; Next even number ; Return the kth element ; Driver code
class GFG { static int getNumber ( int n , int k ) { int [ ] arr = new int [ n ] ; int i = 0 ; int odd = 1 ; while ( odd <= n ) { arr [ i ++ ] = odd ; odd += 2 ; } int even = 2 ; while ( even <= n ) { arr [ i ++ ] = even ; even += 2 ; } return arr [ k - 1 ] ; } public static void main ( String [ ] args ) { int n = 8 , k = 5 ; System . out . println ( getNumber ( n , k ) ) ; } }
Swap Alternate Boundary Pairs | Java implementation of the above approach ; Utility function to print the contents of an array ; Function to update the array ; Initialize the pointers ; While there are elements to swap ; Update the pointers ; Print the updated array ; Driver code
class GFG { static void printArr ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } static void UpdateArr ( int arr [ ] , int n ) { int i = 0 , j = n - 1 ; while ( i < j ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; i += 2 ; j -= 2 ; } printArr ( arr , n ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int n = arr . length ; UpdateArr ( arr , n ) ; } }
Count permutation such that sequence is non decreasing | Java implementation of the approach ; To store the factorials ; Function to update fact [ ] array such that fact [ i ] = i ! ; 0 ! = 1 ; i ! = i * ( i - 1 ) ! ; Function to return the count of possible permutations ; To store the result ; Sort the array ; Initial size of the block ; Increase the size of block ; Update the result for the previous block ; Reset the size to 1 ; Update the result for the last block ; Driver Code ; Pre - calculating factorials
import java . util . Arrays ; import java . io . * ; class GFG { static int N = 20 ; static int [ ] fact = new int [ N ] ; static void pre ( ) { fact [ 0 ] = 1 ; for ( int i = 1 ; i < N ; i ++ ) { fact [ i ] = i * fact [ i - 1 ] ; } } static int CountPermutation ( int a [ ] , int n ) { int ways = 1 ; Arrays . sort ( a ) ; int size = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( a [ i ] == a [ i - 1 ] ) { size ++ ; } else { ways *= fact [ size ] ; size = 1 ; } } ways *= fact [ size ] ; return ways ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 4 , 4 , 2 , 4 } ; int n = a . length ; pre ( ) ; System . out . println ( CountPermutation ( a , n ) ) ; } }
Smallest element greater than X not present in the array | Java implementation of the approach ; Function to return the smallest element greater than x which is not present in a [ ] ; Sort the array ; Continue until low is less than or equals to high ; Find mid ; If element at mid is less than or equals to searching element ; If mid is equals to searching element ; Increment searching element ; Make high as N - 1 ; Make low as mid + 1 ; Make high as mid - 1 ; Return the next greater element ; Driver code
import java . util . * ; class GFG { static int Next_greater ( int a [ ] , int n , int x ) { Arrays . sort ( a ) ; int low = 0 , high = n - 1 , ans = x + 1 ; while ( low <= high ) { int mid = ( low + high ) / 2 ; if ( a [ mid ] <= ans ) { if ( a [ mid ] == ans ) { ans ++ ; high = n - 1 ; } low = mid + 1 ; } else high = mid - 1 ; } return ans ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 5 , 10 , 4 , 7 } , x = 4 ; int n = a . length ; System . out . println ( Next_greater ( a , n , x ) ) ; } }
Arrange N elements in circular fashion such that all elements are strictly less than sum of adjacent elements | Java implementation of the approach ; Function to print the arrangement that satisifes the given condition ; Sort the array initially ; Array that stores the arrangement ; Once the array is sorted Re - fill the array again in the mentioned way in the approach ; Iterate in the array and check if the arrangement made satisfies the given condition or not ; For the first element the adjacents will be a [ 1 ] and a [ n - 1 ] ; For the last element the adjacents will be a [ 0 ] and a [ n - 2 ] ; If we reach this position then the arrangement is possible ; Driver code
import java . util . Arrays ; class GFG { static void printArrangement ( int a [ ] , int n ) { Arrays . sort ( a ) ; int b [ ] = new int [ n ] ; int low = 0 , high = n - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) b [ low ++ ] = a [ i ] ; else b [ high -- ] = a [ i ] ; } for ( int i = 0 ; i < n ; i ++ ) { if ( i == 0 ) { if ( b [ n - 1 ] + b [ 1 ] <= b [ i ] ) { System . out . print ( - 1 ) ; return ; } } else if ( i == ( n - 1 ) ) { if ( b [ n - 2 ] + b [ 0 ] <= b [ i ] ) { System . out . print ( - 1 ) ; return ; } } else { if ( b [ i - 1 ] + b [ i + 1 ] <= b [ i ] ) { System . out . print ( - 1 ) ; return ; } } } for ( int i = 0 ; i < n ; i ++ ) System . out . print ( b [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 4 , 4 , 3 , 2 } ; int n = a . length ; printArrangement ( a , n ) ; } }
Greatest contiguous sub | Java implementation of the approach ; Function that returns the sub - array ; Data - structure to store all the sub - arrays of size K ; Iterate to find all the sub - arrays ; Store the sub - array elements in the array ; Push the vector in the container ; Sort the vector of elements ; The last sub - array in the sorted order will be the answer ; Driver code ; Get the sub - array
import java . io . * ; import java . util . * ; class GFG { static ArrayList < Integer > findSubarray ( int a [ ] , int k , int n ) { ArrayList < ArrayList < Integer > > vec = new ArrayList < ArrayList < Integer > > ( ) ; for ( int i = 0 ; i < n - k + 1 ; i ++ ) { ArrayList < Integer > temp = new ArrayList < Integer > ( ) ; for ( int j = i ; j < i + k ; j ++ ) { temp . add ( a [ j ] ) ; } vec . add ( temp ) ; } Collections . sort ( vec , new Comparator < ArrayList < Integer > > ( ) { @ Override public int compare ( ArrayList < Integer > o1 , ArrayList < Integer > o2 ) { return o1 . get ( 0 ) . compareTo ( o2 . get ( 0 ) ) ; } } ) ; return vec . get ( vec . size ( ) - 1 ) ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 4 , 3 , 2 , 5 } ; int k = 4 ; int n = a . length ; ArrayList < Integer > ans = findSubarray ( a , k , n ) ; for ( int it : ans ) { System . out . print ( it + " ▁ " ) ; } } }
Maximum Length Chain of Pairs | Set | Java implementation of the above approach ; Structure for storing pairs of first and second values . ; Class for storing pairs of first and second values . ; Function for finding max length chain ; Initialize length l = 1 ; Sort all pair in increasing order according to second no of pair ; Pick up the first pair and assign the value of second element fo pair to a temporary variable s ; Iterate from second pair ( index of the second pair is 1 ) to the last pair ; If first of current pair is greater than previously selected pair then select current pair and update value of l and s ; Return maximum length ; Driver Code ; Declaration of array of structure ; Function call
import java . util . * ; class GFG { static class Pair { int first ; int second ; Pair ( int first , int second ) { this . first = first ; this . second = second ; } } ; static int maxChainLen ( Pair p [ ] , int n ) { int l = 1 ; Arrays . sort ( p , new Comparator < Pair > ( ) { public int compare ( Pair a , Pair b ) { return a . second - b . second ; } } ) ; int s = p [ 0 ] . second ; for ( int i = 1 ; i < n ; i ++ ) { if ( p [ i ] . first > s ) { l ++ ; s = p [ i ] . second ; } } return l ; } public static void main ( String args [ ] ) { Pair p [ ] = new Pair [ 5 ] ; p [ 0 ] = new Pair ( 5 , 24 ) ; p [ 1 ] = new Pair ( 39 , 60 ) ; p [ 2 ] = new Pair ( 15 , 28 ) ; p [ 3 ] = new Pair ( 27 , 40 ) ; p [ 4 ] = new Pair ( 50 , 90 ) ; int n = p . length ; System . out . println ( maxChainLen ( p , n ) ) ; } }
Unbounded Fractional Knapsack | Java implementation of the approach ; Function to return the maximum required value ; maxratio will store the maximum value to weight ratio we can have for any item and maxindex will store the index of that element ; Find the maximum ratio ; The item with the maximum value to weight ratio will be put into the knapsack repeatedly until full ; Driver code
class GFG { static float knapSack ( int W , float wt [ ] , float val [ ] , int n ) { float maxratio = Integer . MIN_VALUE ; int maxindex = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( val [ i ] / wt [ i ] ) > maxratio ) { maxratio = ( val [ i ] / wt [ i ] ) ; maxindex = i ; } } return ( W * maxratio ) ; } public static void main ( String [ ] args ) { float val [ ] = { 14 , 27 , 44 , 19 } ; float wt [ ] = { 6 , 7 , 9 , 8 } ; int n = val . length ; int W = 50 ; System . out . println ( knapSack ( W , wt , val , n ) ) ; } }
Sort an array without changing position of negative numbers | Java implementation of the approach ; Function to sort the array such that negative values do not get affected ; Store all non - negative values ; Sort non - negative values ; If current element is non - negative then update it such that all the non - negative values are sorted ; Print the sorted array ; Driver code
import java . util . * ; class GFG { static void sortArray ( int a [ ] , int n ) { Vector < Integer > ans = new Vector < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] >= 0 ) ans . add ( a [ i ] ) ; } Collections . sort ( ans ) ; int j = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] >= 0 ) { a [ i ] = ans . get ( j ) ; j ++ ; } } for ( int i = 0 ; i < n ; i ++ ) System . out . print ( a [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , - 6 , - 3 , 8 , 4 , 1 } ; int n = arr . length ; sortArray ( arr , n ) ; } }
Find a triplet in an array whose sum is closest to a given number | Java implementation of the above approach ; Function to return the sum of a triplet which is closest to x ; Sort the array ; To store the closest sum Assigning long to avoid overflow condition when array has negative integers ; Fix the smallest number among the three integers ; Two pointers initially pointing at the last and the element next to the fixed element ; While there could be more pairs to check ; Calculate the sum of the current triplet ; If the sum is more closer than the current closest sum ; If sum is greater then x then decrement the second pointer to get a smaller sum ; Else increment the first pointer to get a larger sum ; Return the closest sum found ; Driver code
import static java . lang . Math . abs ; import java . util . * ; class GFG { static int solution ( Vector < Integer > arr , int x ) { Collections . sort ( arr ) ; long closestSum = Integer . MAX_VALUE ; for ( int i = 0 ; i < arr . size ( ) - 2 ; i ++ ) { int ptr1 = i + 1 , ptr2 = arr . size ( ) - 1 ; while ( ptr1 < ptr2 ) { int sum = arr . get ( i ) + arr . get ( ptr1 ) + arr . get ( ptr2 ) ; if ( abs ( x - sum ) < abs ( x - closestSum ) ) { closestSum = sum ; } if ( sum > x ) { ptr2 -- ; } else { ptr1 ++ ; } } } return ( int ) closestSum ; } public static void main ( String [ ] args ) { Vector arr = new Vector ( Arrays . asList ( - 1 , 2 , 1 , - 4 ) ) ; int x = 1 ; System . out . println ( solution ( arr , x ) ) ; } }
Check whether we can sort two arrays by swapping A [ i ] and B [ i ] | Java implementation of above approach ; Function to check whether both the array can be sorted in ( strictly increasing ) ascending order ; Traverse through the array and find out the min and max variable at each position make one array of min variables and another of maximum variable ; Maximum and minimum variable ; Assign min value to B [ i ] and max value to A [ i ] ; Now check whether the array is sorted or not ; Driver code
import java . io . * ; class GFG { static boolean IsSorted ( int [ ] A , int [ ] B , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int x , y ; x = Math . max ( A [ i ] , B [ i ] ) ; y = Math . min ( A [ i ] , B [ i ] ) ; A [ i ] = x ; B [ i ] = y ; } for ( int i = 1 ; i < n ; i ++ ) { if ( A [ i ] <= A [ i - 1 ] B [ i ] <= B [ i - 1 ] ) return false ; } return true ; } public static void main ( String [ ] args ) { int [ ] A = { 1 , 4 , 3 , 5 , 7 } ; int [ ] B = { 2 , 2 , 5 , 8 , 9 } ; int n = A . length ; if ( IsSorted ( A , B , n ) == true ) { System . out . println ( " True " ) ; } else { System . out . println ( " False " ) ; } } }
Find the largest possible k | Java program to find the largest possible k - multiple set ; Function to find the largest possible k - multiple set ; Sort the given array ; To store k - multiple set ; Traverse through the whole array ; Check if x / k is already present or not ; Print the k - multiple set ; Driver code ; Function call
import java . util . * ; class GFG { static void K_multiple ( int a [ ] , int n , int k ) { Arrays . sort ( a ) ; HashSet < Integer > s = new HashSet < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( a [ i ] % k == 0 && ! s . contains ( a [ i ] / k ) ) a [ i ] % k != 0 ) s . add ( a [ i ] ) ; } for ( Integer i : s ) System . out . print ( i + " ▁ " ) ; } public static void main ( String args [ ] ) { int a [ ] = { 2 , 3 , 4 , 5 , 6 , 10 } ; int k = 2 ; int n = a . length ; K_multiple ( a , n , k ) ; } }