codeparrot/xlcost-text-to-code huggingface dataset:
Make palindromic string non | Java Program to rearrange letters of string to find a non - palindromic string if it exists ; Function to print the non - palindromic string if it exists , otherwise prints - 1 ; If all characters are not same , set flag to 1 ; Update frequency of the current character ; If all characters are same ; Print characters in sorted manner ; Driver Code
class GfG { static void findNonPalinString ( char s [ ] ) { int freq [ ] = new int [ 26 ] ; int flag = 0 ; for ( int i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] != s [ 0 ] ) flag = 1 ; freq [ s [ i ] - ' a ' ] ++ ; } if ( flag == 0 ) System . out . println ( " - 1" ) ; else { for ( int i = 0 ; i < 26 ; i ++ ) for ( int j = 0 ; j < freq [ i ] ; j ++ ) System . out . print ( ( char ) ( ' a ' + i ) ) ; } } public static void main ( String [ ] args ) { String s = " abba " ; findNonPalinString ( s . toCharArray ( ) ) ; } }
Minimum operations of given type to make all elements of a matrix equal | Java implementation of the approach ; Function to return the minimum number of operations required ; Create another array to store the elements of matrix ; If not possible ; Sort the array to get median ; To count the minimum operations ; If there are even elements , then there are two medians . We consider the best of two as answer . ; Return minimum operations required ; Driver code
import java . util . * ; class GFG { static int minOperations ( int n , int m , int k , int matrix [ ] [ ] ) { int [ ] arr = new int [ n * m ] ; int mod = matrix [ 0 ] [ 0 ] % k ; for ( int i = 0 ; i < n ; ++ i ) { for ( int j = 0 ; j < m ; ++ j ) { arr [ i * m + j ] = matrix [ i ] [ j ] ; if ( matrix [ i ] [ j ] % k != mod ) { return - 1 ; } } } Arrays . sort ( arr ) ; int median = arr [ ( n * m ) / 2 ] ; int minOperations = 0 ; for ( int i = 0 ; i < n * m ; ++ i ) minOperations += Math . abs ( arr [ i ] - median ) / k ; if ( ( n * m ) % 2 == 0 ) { int median2 = arr [ ( ( n * m ) / 2 ) - 1 ] ; int minOperations2 = 0 ; for ( int i = 0 ; i < n * m ; ++ i ) minOperations2 += Math . abs ( arr [ i ] - median2 ) / k ; minOperations = Math . min ( minOperations , minOperations2 ) ; } return minOperations ; } public static void main ( String [ ] args ) { int matrix [ ] [ ] = { { 2 , 4 , 6 } , { 8 , 10 , 12 } , { 14 , 16 , 18 } , { 20 , 22 , 24 } } ; int n = matrix . length ; int m = matrix [ 0 ] . length ; int k = 2 ; System . out . println ( minOperations ( n , m , k , matrix ) ) ; } }
Count distinct elements in an array | Java program to count distinct elements in a given array ; Pick all elements one by one ; If not printed earlier , then print it ; Driver code
class GFG { static int countDistinct ( int arr [ ] , int n ) { int res = 1 ; for ( int i = 1 ; i < n ; i ++ ) { int j = 0 ; for ( j = 0 ; j < i ; j ++ ) if ( arr [ i ] == arr [ j ] ) break ; if ( i == j ) res ++ ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 12 , 10 , 9 , 45 , 2 , 10 , 10 , 45 } ; int n = arr . length ; System . out . println ( countDistinct ( arr , n ) ) ; } }
Count distinct elements in an array | Java program to count all distinct elements in a given array ; First sort the array so that all occurrences become consecutive ; Traverse the sorted array ; Move the index ahead while there are duplicates ; Driver code
import java . util . Arrays ; class GFG { static int countDistinct ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { while ( i < n - 1 && arr [ i ] == arr [ i + 1 ] ) { i ++ ; } res ++ ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 10 , 5 , 4 , 9 , 120 , 4 , 6 , 10 } ; int n = arr . length ; System . out . println ( countDistinct ( arr , n ) ) ; } }
Choose n elements such that their mean is maximum | Java implementation of the approach ; Utility function to print the contents of an array ; Function to print the array with maximum mean ; Sort the original array ; Construct new array ; Print the resultant array ; Driver code
import java . util . Arrays ; class GfG { static void printArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } static void printMaxMean ( int arr [ ] , int n ) { int newArr [ ] = new int [ n ] ; Arrays . sort ( arr , 0 , 2 * n ) ; for ( int i = 0 ; i < n ; i ++ ) newArr [ i ] = arr [ i + n ] ; printArray ( newArr , n ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 8 , 3 , 1 , 3 , 7 , 0 , 4 } ; int n = arr . length ; printMaxMean ( arr , n / 2 ) ; } }
Average of remaining elements after removing K largest and K smallest elements from array | Java implementation of the above approach ; Function to find average ; base case if 2 * k >= n means all element get removed ; first sort all elements ; sum of req number ; find average ; Driver code
import java . io . * ; import java . util . * ; class GFG { static double average ( int arr [ ] , int n , int k ) { double total = 0 ; if ( 2 * k >= n ) return 0 ; Arrays . sort ( arr ) ; int start = k , end = n - k - 1 ; for ( int i = start ; i <= end ; i ++ ) total += arr [ i ] ; return ( total / ( n - 2 * k ) ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 4 , 4 , 5 , 6 } ; int n = arr . length ; int k = 2 ; System . out . println ( average ( arr , n , k ) ) ; } }
Minimum sum after subtracting multiples of k from the elements of the array | Java program of the above approach ; function to calculate minimum sum after transformation ; no element can be reduced further ; if all the elements of the array are identical ; check if a [ i ] can be reduced to a [ 0 ] ; one of the elements cannot be reduced to be equal to the other elements ; if k = 1 then all elements can be reduced to 1 ; Driver code
import java . io . * ; import java . util . * ; class GFG { static int min_sum ( int n , int k , int a [ ] ) { Arrays . sort ( a ) ; if ( a [ 0 ] < 0 ) return - 1 ; if ( k == 0 ) { if ( a [ 0 ] == a [ n - 1 ] ) return ( n * a [ 0 ] ) ; else return - 1 ; } else { int f = 0 ; for ( int i = 1 ; i < n ; i ++ ) { int p = a [ i ] - a [ 0 ] ; if ( p % k == 0 ) continue ; else { f = 1 ; break ; } } if ( f > 0 ) return - 1 ; else { if ( k == 1 ) return n ; else return ( n * ( a [ 0 ] % k ) ) ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 4 , 5 } ; int K = 1 ; int N = arr . length ; System . out . println ( min_sum ( N , K , arr ) ) ; } }
In | Java program in - place Merge Sort ; Merges two subarrays of arr [ ] . First subarray is arr [ l . . m ] Second subarray is arr [ m + 1. . r ] Inplace Implementation ; If the direct merge is already sorted ; Two pointers to maintain start of both arrays to merge ; If element 1 is in right place ; Shift all the elements between element 1 element 2 , right by 1. ; Update all the pointers ; l is for left index and r is right index of the sub - array of arr to be sorted ; Same as ( l + r ) / 2 , but avoids overflow for large l and r ; Sort first and second halves ; Function to print an array ; Driver program to test above functions
public class GFG { static void merge ( int arr [ ] , int start , int mid , int end ) { int start2 = mid + 1 ; if ( arr [ mid ] <= arr [ start2 ] ) { return ; } while ( start <= mid && start2 <= end ) { if ( arr [ start ] <= arr [ start2 ] ) { start ++ ; } else { int value = arr [ start2 ] ; int index = start2 ; while ( index != start ) { arr [ index ] = arr [ index - 1 ] ; index -- ; } arr [ start ] = value ; start ++ ; mid ++ ; start2 ++ ; } } } static void mergeSort ( int arr [ ] , int l , int r ) { if ( l < r ) { int m = l + ( r - l ) / 2 ; mergeSort ( arr , l , m ) ; mergeSort ( arr , m + 1 , r ) ; merge ( arr , l , m , r ) ; } } static void printArray ( int A [ ] , int size ) { int i ; for ( i = 0 ; i < size ; i ++ ) System . out . print ( A [ i ] + " ▁ " ) ; System . out . println ( ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 12 , 11 , 13 , 5 , 6 , 7 } ; int arr_size = arr . length ; mergeSort ( arr , 0 , arr_size - 1 ) ; printArray ( arr , arr_size ) ; } }
Minimum Increment / decrement to make array elements equal | Java program to find minimum Increment or decrement to make array elements equal ; Function to return minimum operations need to be make each element of array equal ; Initialize cost to 0 ; Sort the array ; Middle element ; Find Cost ; If n , is even . Take minimum of the Cost obtained by considering both middle elements ; Find cost again ; Take minimum of two cost ; Return total cost ; Driver Code
import java . util . * ; class GfG { static int minCost ( int A [ ] , int n ) { int cost = 0 ; Arrays . sort ( A ) ; int K = A [ n / 2 ] ; for ( int i = 0 ; i < n ; ++ i ) cost += Math . abs ( A [ i ] - K ) ; if ( n % 2 == 0 ) { int tempCost = 0 ; K = A [ ( n / 2 ) - 1 ] ; for ( int i = 0 ; i < n ; ++ i ) tempCost += Math . abs ( A [ i ] - K ) ; cost = Math . min ( cost , tempCost ) ; } return cost ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 6 , 7 , 10 } ; int n = A . length ; System . out . println ( minCost ( A , n ) ) ; } }
Print array elements in alternatively increasing and decreasing order | Java program to print array elements in alternative increasing and decreasing order ; Function to print array elements in alternative increasing and decreasing order ; First sort the array in increasing order ; start with 2 elements in increasing order ; till all the elements are not printed ; printing the elements in increasing order ; else printing the elements in decreasing order ; increasing the number of elements to printed in next iteration ; Driver Code
import java . util . * ; class Solution { static void printArray ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int l = 0 , r = n - 1 , flag = 0 , i ; int k = 2 ; while ( l <= r ) { if ( flag == 0 ) { for ( i = l ; i < l + k && i <= r ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; flag = 1 ; l = i ; } { for ( i = r ; i > r - k && i >= l ; i -- ) System . out . print ( arr [ i ] + " ▁ " ) ; flag = 0 ; r = i ; } k ++ ; } } public static void main ( String args [ ] ) { int n = 6 ; int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; printArray ( arr , n ) ; } }
Count triplets in a sorted doubly linked list whose product is equal to a given value x | Java implementation to count triplets in a sorted doubly linked list whose product is equal to a given value ' x ' ; structure of node of doubly linked list ; function to count pairs whose product equal to given ' value ' ; The loop terminates when either of two pointers become null , or they cross each other ( second . next == first ) , or they become same ( first == second ) ; pair found ; increment count ; move first in forward direction ; move second in backward direction ; if product is greater than ' value ' move second in backward direction ; else move first in forward direction ; required count of pairs ; function to count triplets in a sorted doubly linked list whose product is equal to a given value ' x ' ; if list is empty ; get pointer to the last node of the doubly linked list ; traversing the doubly linked list ; for each current node ; count pairs with product ( x / current . data ) in the range first to last and add it to the ' count ' of triplets ; required count of triplets ; A utility function to insert a new node at the beginning of doubly linked list ; allocate node ; put in the data ; Driver code ; start with an empty doubly linked list ; insert values in sorted order
import java . util . * ; class GFG { static class Node { int data ; Node next , prev ; } ; static int countPairs ( Node first , Node second , int value ) { int count = 0 ; while ( first != null && second != null && first != second && second . next != first ) { if ( ( first . data * second . data ) == value ) { count ++ ; first = first . next ; second = second . prev ; } else if ( ( first . data * second . data ) > value ) second = second . prev ; else first = first . next ; } return count ; } static int countTriplets ( Node head , int x ) { if ( head == null ) return 0 ; Node current , first , last ; int count = 0 ; last = head ; while ( last . next != null ) last = last . next ; for ( current = head ; current != null ; current = current . next ) { first = current . next ; count += countPairs ( first , last , x / current . data ) ; } return count ; } static Node insert ( Node head , int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . next = temp . prev = null ; if ( ( head ) == null ) ( head ) = temp ; else { temp . next = head ; ( head ) . prev = temp ; ( head ) = temp ; } return head ; } public static void main ( String args [ ] ) { Node head = null ; head = insert ( head , 9 ) ; head = insert ( head , 8 ) ; head = insert ( head , 6 ) ; head = insert ( head , 5 ) ; head = insert ( head , 4 ) ; head = insert ( head , 2 ) ; head = insert ( head , 1 ) ; int x = 8 ; System . out . println ( " Count ▁ = ▁ " + countTriplets ( head , x ) ) ; } }
Check if the characters of a given string are in alphabetical order | Java implementation of above approach ; Function that checks whether the string is in alphabetical order or not ; if element at index ' i ' is less than the element at index ' i - 1' then the string is not sorted ; Driver code ; check whether the string is in alphabetical order or not
public class GFG { static boolean isAlphabaticOrder ( String s ) { int n = s . length ( ) ; for ( int i = 1 ; i < n ; i ++ ) { if ( s . charAt ( i ) < s . charAt ( i - 1 ) ) { return false ; } } return true ; } static public void main ( String [ ] args ) { String s = " aabbbcc " ; if ( isAlphabaticOrder ( s ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
Product of non | Java program to find the product of all non - repeated elements in an array ; Function to find the product of all non - repeated elements in an array ; sort all elements of array ; Driver code
import java . util . Arrays ; class GFG { static int findProduct ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int prod = 1 * arr [ 0 ] ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] != arr [ i + 1 ] ) { prod = prod * arr [ i + 1 ] ; } } return prod ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 1 , 1 , 4 , 5 , 6 } ; int n = arr . length ; System . out . println ( findProduct ( arr , n ) ) ; } }
Check if it is possible to rearrange rectangles in a non | Java implementation of above approach ; Function to check if it possible to form rectangles with heights as non - ascending ; set maximum ; replace the maximum with previous maximum ; replace the minimum with previous minimum ; print NO if the above two conditions fail at least once ; Driver code ; initialize the number of rectangles ; initialize n rectangles with length and breadth
import java . io . * ; class GFG { static int rotateRec ( int n , int L [ ] , int B [ ] ) { int m = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { if ( Math . max ( L [ i ] , B [ i ] ) <= m ) m = Math . max ( L [ i ] , B [ i ] ) ; else if ( Math . min ( L [ i ] , B [ i ] ) <= m ) m = Math . min ( L [ i ] , B [ i ] ) ; else { return 0 ; } } return 1 ; } public static void main ( String [ ] args ) { int n = 3 ; int L [ ] = { 5 , 5 , 6 } ; int B [ ] = { 6 , 7 , 8 } ; if ( rotateRec ( n , L , B ) == 1 ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }
Find a point such that sum of the Manhattan distances is minimized | Java implementation of above approach ; Function to print the required points which minimizes the sum of Manhattan distances ; Sorting points in all k dimension ; Output the required k points ; Driver code ; function call to print required points
import java . util . Arrays ; class GFG { static void minDistance ( int n , int k , int point [ ] [ ] ) { for ( int i = 0 ; i < k ; i ++ ) Arrays . sort ( point [ i ] ) ; for ( int i = 0 ; i < k ; i ++ ) System . out . print ( point [ i ] [ ( int ) Math . ceil ( ( double ) ( n / 2 ) - 1 ) ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 4 ; int k = 4 ; int point [ ] [ ] = { { 1 , 5 , 2 , 4 } , { 6 , 2 , 0 , 6 } , { 9 , 5 , 1 , 3 } , { 6 , 7 , 5 , 9 } } ; minDistance ( n , k , point ) ; } }
Sort first k values in ascending order and remaining n | Java program to sort first k elements in increasing order and remaining n - k elements in decreasing ; function to print half of the array in ascending order and the other half in descending order ; Store the k elements in an array ; Store the remaining n - k elements in an array ; sorting the array from 0 to k - 1 places ; sorting the array from k to n places ; storing the values in the final array arr ; printing the array ; Driver code
import java . util . * ; class GFG { static void printOrder ( int [ ] arr , int n , int k ) { int len1 = k , len2 = n - k ; int [ ] arr1 = new int [ k ] ; int [ ] arr2 = new int [ n - k ] ; for ( int i = 0 ; i < k ; i ++ ) arr1 [ i ] = arr [ i ] ; for ( int i = k ; i < n ; i ++ ) arr2 [ i - k ] = arr [ i ] ; Arrays . sort ( arr1 , 0 , k ) ; Arrays . sort ( arr2 , k , n - k ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( i < k ) arr [ i ] = arr1 [ i ] ; else { arr [ i ] = arr2 [ len2 - 1 ] ; len2 -- ; } } for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 4 , 6 , 2 , 1 , 3 , 8 , 9 , - 1 } ; int k = 4 ; int n = arr . length ; printOrder ( arr , n , k ) ; } }
Find the largest number that can be formed with the given digits | Java program to generate largest possible number with given digits ; Function to generate largest possible number with given digits ; Declare a hash array of size 10 and initialize all the elements to zero ; store the number of occurrences of the digits in the given array into the hash table ; Traverse the hash in descending order to print the required number ; Print the number of times a digits occurs ; Driver code
class GFG { static void findMaxNum ( int arr [ ] , int n ) { int [ ] hash = new int [ 10 ] ; for ( int i = 0 ; i < n ; i ++ ) { hash [ arr [ i ] ] ++ ; } for ( int i = 9 ; i >= 0 ; i -- ) { for ( int j = 0 ; j < hash [ i ] ; j ++ ) System . out . print ( i ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 0 } ; int n = arr . length ; findMaxNum ( arr , n ) ; } }
Sort a nearly sorted array using STL | A STL based Java program to sort a nearly sorted array . ; Given an array of size n , where every element is k away from its target position , sorts the array in O ( n Log n ) time . ; Sort the array using inbuilt function ; An utility function to print array elements ; Driver code
import java . util . * ; public class GFG { static void sortK ( int [ ] arr , int n , int k ) { Arrays . sort ( arr ) ; } static void printArray ( int [ ] arr , int size ) { for ( int i = 0 ; i < size ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( ) ; } public static void main ( String [ ] args ) { int k = 3 ; int [ ] arr = { 2 , 6 , 3 , 12 , 56 , 8 } ; int n = arr . length ; sortK ( arr , n , k ) ; System . out . println ( " Following ▁ is ▁ sorted ▁ array " ) ; printArray ( arr , n ) ; } }
Equally divide into two sets such that one set has maximum distinct elements | Java program to equally divide n elements into two sets such that second set has maximum distinct elements . ; Driver code
import java . util . * ; class Geeks { static int distribution ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int count = 1 ; for ( int i = 1 ; i < n ; i ++ ) if ( arr [ i ] > arr [ i - 1 ] ) count ++ ; return Math . min ( count , n / 2 ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 1 , 2 , 1 , 3 , 4 } ; int n = arr . length ; System . out . println ( distribution ( arr , n ) ) ; } }
Print all the pairs that contains the positive and negative values of an element | Java program to find pairs of positive and negative values present in an array ; Function to print pairs of positive and negative values present in the array ; Store all the positive elements in the unordered_set ; Start traversing the array ; Check if the positive value of current element exists in the set or not ; Print that pair ; Driver code
import java . util . * ; class GFG { static void printPairs ( int arr [ ] , int n ) { Set < Integer > pairs = new HashSet < Integer > ( ) ; boolean pair_exists = false ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] > 0 ) pairs . add ( arr [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < 0 ) if ( pairs . contains ( - arr [ i ] ) ) { System . out . println ( arr [ i ] + " , ▁ " + - arr [ i ] ) ; pair_exists = true ; } } if ( pair_exists == false ) System . out . println ( " No ▁ such ▁ pair ▁ exists " ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 4 , 8 , 9 , - 4 , 1 , - 1 , - 8 , - 9 } ; int n = arr . length ; printPairs ( arr , n ) ; } }
Sort 3 numbers | Java program to sort an array of size 3
import java . io . * ; import java . util . * ; class GFG { public static void main ( String [ ] args ) { int a [ ] = { 10 , 12 , 5 } ; Arrays . sort ( a ) ; for ( int i = 0 ; i < 3 ; i ++ ) System . out . print ( a [ i ] + " ▁ " ) ; } }
Print triplets with sum less than k | A Simple Java program to count triplets with sum smaller than a given value ; Fix the first element as A [ i ] ; Fix the second element as A [ j ] ; Now look for the third number ; Driver Code
import java . io . * ; class GFG { static int printTriplets ( int arr [ ] , int n , int sum ) { for ( int i = 0 ; i < n - 2 ; i ++ ) { for ( int j = i + 1 ; j < n - 1 ; j ++ ) { for ( int k = j + 1 ; k < n ; k ++ ) if ( arr [ i ] + arr [ j ] + arr [ k ] < sum ) System . out . println ( arr [ i ] + " , ▁ " + arr [ j ] + " , ▁ " + arr [ k ] ) ; } } return 0 ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 1 , 3 , 4 , 7 } ; int n = arr . length ; int sum = 12 ; printTriplets ( arr , n , sum ) ; } }
Count number of triplets in an array having sum in the range [ a , b ] | Java program to count triplets with sum that lies in given range [ a , b ] . ; Function to count triplets ; Initialize result ; Fix the first element as A [ i ] ; Fix the second element as A [ j ] ; Now look for the third number ; Driver Code
import java . util . * ; class GFG { public static int countTriplets ( int [ ] arr , int n , int a , int b ) { int ans = 0 ; for ( int i = 0 ; i < n - 2 ; i ++ ) { for ( int j = i + 1 ; j < n - 1 ; j ++ ) { for ( int k = j + 1 ; k < n ; k ++ ) { if ( arr [ i ] + arr [ j ] + arr [ k ] >= a && arr [ i ] + arr [ j ] + arr [ k ] <= b ) { ans ++ ; } } } } return ans ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 7 , 5 , 3 , 8 , 4 , 1 , 9 } ; int n = arr . length ; int a = 8 , b = 16 ; System . out . println ( " " + countTriplets ( arr , n , a , b ) ) ; } }