codeparrot/xlcost-text-to-code huggingface dataset:
XOR of elements in a given range with updates using Fenwick Tree | Java Program to find XOR of elements in given range [ L , R ] . ; Returns XOR of arr [ 0. . index ] . This function assumes that the array is preprocessed and partial XORs of array elements are stored in BITree [ ] . ; Traverse ancestors of BITree [ index ] ; XOR current element of BIT to ans ; Update index to that of the parent node in getXor ( ) view by subtracting LSB ( Least Significant Bit ) ; Updates the Binary Index Tree by replacing all ancestors of index by their respective XOR with val ; Traverse all ancestors and XOR with ' val ' . ; XOR ' val ' to current node of BIT ; Update index to that of the parent node in updateBit ( ) view by adding LSB ( Least Significant Bit ) ; Constructs and returns a Binary Indexed Tree for the given array ; Create and initialize the Binary Indexed Tree ; Store the actual values in BITree [ ] using update ( ) ; Driver Code ; Create the Binary Indexed Tree ; Solve each query in Q ; Update the values of all ancestors of idx
import java . util . * ; class GFG { static int getXOR ( int BITree [ ] , int index ) { int ans = 0 ; index += 1 ; while ( index > 0 ) { ans ^= BITree [ index ] ; index -= index & ( - index ) ; } return ans ; } static void updateBIT ( int BITree [ ] , int n , int index , int val ) { index = index + 1 ; while ( index <= n ) { BITree [ index ] ^= val ; index += index & ( - index ) ; } } static int [ ] constructBITree ( int arr [ ] , int n ) { int [ ] BITree = new int [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) BITree [ i ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) updateBIT ( BITree , n , i , arr [ i ] ) ; return BITree ; } static int rangeXor ( int BITree [ ] , int l , int r ) { return getXOR ( BITree , r ) ^ getXOR ( BITree , l - 1 ) ; } public static void main ( String [ ] args ) { int A [ ] = { 2 , 1 , 1 , 3 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } ; int n = A . length ; int [ ] [ ] q = { { 1 , 0 , 9 } , { 2 , 3 , 6 } , { 2 , 5 , 5 } , { 2 , 8 , 1 } , { 1 , 0 , 9 } } ; int [ ] BITree = constructBITree ( A , n ) ; for ( int i = 0 ; i < q . length ; i ++ ) { int id = q [ i ] [ 0 ] ; if ( id == 1 ) { int L = q [ i ] [ 1 ] ; int R = q [ i ] [ 2 ] ; System . out . print ( " XOR ▁ of ▁ elements ▁ " + " in ▁ given ▁ range ▁ is ▁ " + rangeXor ( BITree , L , R ) + "NEW_LINE"); } else { int idx = q [ i ] [ 1 ] ; int val = q [ i ] [ 2 ] ; A [ idx ] ^= val ; updateBIT ( BITree , n , idx , val ) ; } } } }
Count of Double Prime numbers in a given range L to R | Java program to find the count of Double Prime numbers in the range L to R ; Array to make Sieve where arr [ i ] = 0 indicates non prime and arr [ i ] = 1 indicates prime ; Array to find double prime ; Function to find the number double prime numbers in range ; Assume all numbers as prime ; Check if the number is prime ; check for multiples of i ; Make all multiples of ith prime as non - prime ; Check if number at ith position is prime then increment count ; Indicates count of numbers from 1 to i that are also prime and hence double prime ; If number is not a double prime ; finding cumulative sum ; Driver code
import java . util . * ; import java . lang . * ; class GFG { static int [ ] arr = new int [ 1000001 ] ; static int [ ] dp = new int [ 1000001 ] ; static void count ( ) { int maxN = 1000000 , i , j ; for ( i = 0 ; i < maxN ; i ++ ) arr [ i ] = 1 ; arr [ 0 ] = 0 ; arr [ 1 ] = 0 ; for ( i = 2 ; i * i <= maxN ; i ++ ) { if ( arr [ i ] == 1 ) { for ( j = 2 * i ; j <= maxN ; j += i ) { arr [ j ] = 0 ; } } } int cnt = 0 ; for ( i = 0 ; i <= maxN ; i ++ ) { if ( arr [ i ] == 1 ) cnt ++ ; if ( arr [ cnt ] == 1 ) dp [ i ] = 1 ; else dp [ i ] = 0 ; } for ( i = 1 ; i <= maxN ; i ++ ) dp [ i ] += dp [ i - 1 ] ; } public static void main ( String [ ] args ) { int L = 4 , R = 12 ; count ( ) ; System . out . println ( dp [ R ] - dp [ L - 1 ] ) ; } }
Count of ungrouped characters after dividing a string into K groups of distinct characters | Java code to implement the above approach ; Create array where index represents alphabets ; Fill count of every alphabet to corresponding array index ; Count for every element how much is exceeding from no . of groups then sum them ; Print answer ; Driver code
import java . util . * ; class GFG { static void findUngroupedElement ( String s , int k ) { int n = s . length ( ) ; int [ ] b = new int [ 26 ] ; for ( int i = 0 ; i < 26 ; i ++ ) b [ i ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) { char p = s . charAt ( i ) ; b [ p - ' a ' ] += 1 ; } int sum = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( b [ i ] > k ) sum += b [ i ] - k ; } System . out . println ( sum ) ; } public static void main ( String srgs [ ] ) { String s = " stayinghomesaveslife " ; int k = 1 ; findUngroupedElement ( s , k ) ; } }
Generate an array of given size with equal count and sum of odd and even numbers | Java implementation to find the array containing same count of even and odd elements with equal sum of even and odd elements ; Function to find the array such that the array contains the same count of even and odd elements with equal sum of even and odd elements ; Length of array which is not divisible by 4 is unable to form such array ; Loop to find the resulted array containing the same count of even and odd elements ; Find the total sum of even elements ; Find the total sum of odd elements ; Find the difference between the total sum of even and odd elements ; The difference will be added in the last odd element ; Driver Code
class GFG { static void findSolution ( int N ) { if ( N % 4 != 0 ) System . out . print ( - 1 + "NEW_LINE"); else { int temp = 0 , sum_odd = 0 ; int sum_even = 0 ; int result [ ] = new int [ N ] ; for ( int i = 0 ; i < N ; i += 2 ) { temp += 2 ; result [ i + 1 ] = temp ; sum_even += result [ i + 1 ] ; result [ i ] = temp - 1 ; sum_odd += result [ i ] ; } int diff = sum_even - sum_odd ; result [ N - 2 ] += diff ; for ( int i = 0 ; i < N ; i ++ ) System . out . print ( result [ i ] + " ▁ " ) ; System . out . print ( "NEW_LINE"); } } public static void main ( String [ ] args ) { int N = 8 ; findSolution ( N ) ; } }
Number of ways to place two queens on a N * N chess | Java implementation to find the number of ways to place two queens on the N * N chess board ; Function to find number of valid positions for two queens in the N * N chess board ; Driver Code ; Function Call
class GFG { static double possiblePositions ( double n ) { double term1 = Math . pow ( n , 4 ) ; double term2 = Math . pow ( n , 3 ) ; double term3 = Math . pow ( n , 2 ) ; double term4 = n / 3 ; double ans = ( Math . ceil ( term1 / 2 ) ) - ( Math . ceil ( 5 * term2 ) / 3 ) + ( Math . ceil ( 3 * term3 ) / 2 ) - term4 ; return ( long ) ans ; } public static void main ( String [ ] args ) { double n ; n = 3 ; double ans = possiblePositions ( n ) ; System . out . print ( ans + "NEW_LINE"); } }
Group all co | Java implementation to group mutually coprime numbers into one group with minimum group possible ; Function to group the mutually co - prime numbers into one group ; Loop for the numbers less than the 4 ; Integers 1 , 2 and 3 can be grouped into one group ; Consecutive even and odd numbers ; Driver Code ; Function Call
class GFG { static void mutually_coprime ( int n ) { if ( n <= 3 ) { for ( int j = 1 ; j < n + 1 ; j ++ ) System . out . print ( j + " ▁ " ) ; System . out . println ( ) ; } else { System . out . println ( "1 ▁ 2 ▁ 3" ) ; for ( int j = 4 ; j < n ; j += 2 ) { System . out . println ( j + " ▁ " + ( j + 1 ) ) ; if ( n % 2 == 0 ) System . out . println ( n ) ; } } } public static void main ( String [ ] args ) { int n = 9 ; mutually_coprime ( n ) ; } }
Maximum sum subset having equal number of positive and negative elements | Java implementation to find the maximum sum subset having equal number of positive and negative elements in the subset ; Function to find maximum sum subset with equal number of positive and negative elements ; Loop to store the positive and negative elements in two different array ; Sort both the array ; Pointers starting from the highest elements ; Find pairs having sum greater than zero ; Driver code
import java . util . * ; class GFG { static int findMaxSum ( int [ ] arr , int n ) { Vector < Integer > a = new Vector < Integer > ( ) ; Vector < Integer > b = new Vector < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 ) { a . add ( arr [ i ] ) ; } else if ( arr [ i ] < 0 ) { b . add ( arr [ i ] ) ; } } Collections . sort ( a ) ; Collections . sort ( b ) ; int p = a . size ( ) - 1 ; int q = b . size ( ) - 1 ; int s = 0 ; while ( p >= 0 && q >= 0 ) { if ( a . get ( p ) + b . get ( q ) > 0 ) { s = s + a . get ( p ) + b . get ( q ) ; } else { break ; } p = p - 1 ; q = q - 1 ; } return s ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 1 , - 2 , 3 , 4 , - 5 , 8 } ; int n1 = arr1 . length ; System . out . print ( findMaxSum ( arr1 , n1 ) + "NEW_LINE"); } }
Print the nodes with a prime degree in given Prufer sequence of a Tree | Java implementation to print the nodes with prime degree from the given prufer sequence ; Function to create Sieve to check primes ; False here indicates that it is not prime ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p , set them to non - prime ; Function to print the nodes with prime degree in the tree whose Prufer sequence is given ; Hash - table to mark the degree of every node ; Initially let all the degrees be 1 ; Increase the count of the degree ; Print the nodes with prime degree ; Driver Code
import java . util . * ; class GFG { static void SieveOfEratosthenes ( boolean prime [ ] , int p_size ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= p_size ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i <= p_size ; i += p ) prime [ i ] = false ; } } } static void PrimeDegreeNodes ( int prufer [ ] , int n ) { int nodes = n + 2 ; boolean [ ] prime = new boolean [ nodes + 1 ] ; Arrays . fill ( prime , true ) ; SieveOfEratosthenes ( prime , nodes + 1 ) ; int [ ] degree = new int [ n + 2 + 1 ] ; for ( int i = 1 ; i <= nodes ; i ++ ) degree [ i ] = 1 ; for ( int i = 0 ; i < n ; i ++ ) degree [ prufer [ i ] ] ++ ; for ( int i = 1 ; i <= nodes ; i ++ ) { if ( prime [ degree [ i ] ] ) { System . out . print ( i + " ▁ " ) ; } } } public static void main ( String [ ] args ) { int a [ ] = { 4 , 1 , 3 , 4 } ; int n = a . length ; PrimeDegreeNodes ( a , n ) ; } }
Nodes with prime degree in an undirected Graph | Java implementation of the approach ; To store Prime Numbers ; Function to find the prime numbers till 10 ^ 5 ; Traverse all multiple of i and make it false ; Function to print the nodes having prime degree ; To store Adjacency List of a Graph ; Make Adjacency List ; To precompute prime numbers till 10 ^ 5 ; Traverse each vertex ; Find size of Adjacency List ; If length of Adj [ i ] is Prime then print it ; Driver code ; Vertices and Edges ; Edges ; Function Call
import java . util . * ; class GFG { static int n = 10005 ; static boolean [ ] Prime = new boolean [ n + 1 ] ; static void SieveOfEratosthenes ( ) { int i , j ; Prime [ 0 ] = Prime [ 1 ] = false ; for ( i = 2 ; i * i <= 10005 ; i ++ ) { if ( Prime [ i ] ) { for ( j = 2 * i ; j < 10005 ; j += i ) { Prime [ j ] = false ; } } } } static void primeDegreeNodes ( int N , int M , int edges [ ] [ ] ) { Vector < Integer > [ ] Adj = new Vector [ N + 1 ] ; for ( int i = 0 ; i < Adj . length ; i ++ ) Adj [ i ] = new Vector < Integer > ( ) ; for ( int i = 0 ; i < M ; i ++ ) { int x = edges [ i ] [ 0 ] ; int y = edges [ i ] [ 1 ] ; Adj [ x ] . add ( y ) ; Adj [ y ] . add ( x ) ; } SieveOfEratosthenes ( ) ; for ( int i = 1 ; i <= N ; i ++ ) { int x = Adj [ i ] . size ( ) ; if ( Prime [ x ] ) System . out . print ( i + " ▁ " ) ; } } public static void main ( String [ ] args ) { int N = 4 , M = 6 ; int edges [ ] [ ] = { { 1 , 2 } , { 1 , 3 } , { 1 , 4 } , { 2 , 3 } , { 2 , 4 } , { 3 , 4 } } ; Arrays . fill ( Prime , true ) ; primeDegreeNodes ( N , M , edges ) ; } }
Number of ways to color N | Java program for the above approach ; Recursive function to count the ways ; Base case ; Initialise answer to 0 ; Color each uncolored block according to the given condition ; If any block is uncolored ; Check if adjacent blocks are colored or not ; Color the block ; recursively iterate for next uncolored block ; Uncolored for the next recursive call ; Return the final count ; Function to count the ways to color block ; Mark which blocks are colored in each recursive step ; Function call to count the ways ; Driver Code ; Number of blocks ; Number of colored blocks ; Function call
import java . util . * ; class GFG { static int mod = 1000000007 ; static int countWays ( int colored [ ] , int count , int n ) { if ( count == n ) { return 1 ; } int answer = 0 ; for ( int i = 1 ; i < n + 1 ; i ++ ) { if ( colored [ i ] == 0 ) { if ( colored [ i - 1 ] == 1 colored [ i + 1 ] == 1 ) { colored [ i ] = 1 ; answer = ( answer + countWays ( colored , count + 1 , n ) ) % mod ; colored [ i ] = 0 ; } } } return answer ; } static int waysToColor ( int arr [ ] , int n , int k ) { int colored [ ] = new int [ n + 2 ] ; for ( int i = 0 ; i < k ; i ++ ) { colored [ arr [ i ] ] = 1 ; } return countWays ( colored , k , n ) ; } public static void main ( String [ ] args ) { int N = 6 ; int K = 3 ; int arr [ ] = { 1 , 2 , 6 } ; System . out . print ( waysToColor ( arr , N , K ) ) ; } }
Shortest Palindromic Substring | Java program to find the shortest palindromic subString ; Function return the shortest palindromic subString ; Finding the smallest character present in the String ; Driver code
class GFG { static char ShortestPalindrome ( String s ) { int n = s . length ( ) ; char ans = s . charAt ( 0 ) ; for ( int i = 1 ; i < n ; i ++ ) { ans = ( char ) Math . min ( ans , s . charAt ( i ) ) ; } return ans ; } public static void main ( String [ ] args ) { String s = " geeksforgeeks " ; System . out . print ( ShortestPalindrome ( s ) ) ; } }
Maximum length Subsequence with alternating sign and maximum Sum | Java implementation to find the subsequence with alternating sign having maximum size and maximum sum . ; Function to find the subsequence with alternating sign having maximum size and maximum sum . ; Find whether each element is positive or negative ; Find the required subsequence ; Find the maximum element in the specified range ; print the result ; Driver code ; array declaration ; size of array
class GFG { static void findSubsequence ( int arr [ ] , int n ) { int sign [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 ) sign [ i ] = 1 ; else sign [ i ] = - 1 ; } int k = 0 ; int result [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { int cur = arr [ i ] ; int j = i ; while ( j < n && sign [ i ] == sign [ j ] ) { cur = Math . max ( cur , arr [ j ] ) ; ++ j ; } result [ k ++ ] = cur ; i = j - 1 ; } for ( int i = 0 ; i < k ; i ++ ) System . out . print ( result [ i ] + " ▁ " ) ; System . out . print ( "NEW_LINE"); } public static void main ( String [ ] args ) { int arr [ ] = { - 4 , 9 , 4 , 11 , - 5 , - 17 , 9 , - 3 , - 5 , 2 } ; int n = arr . length ; findSubsequence ( arr , n ) ; } }
Longest Palindrome in a String formed by concatenating its prefix and suffix | Java program to find the longest palindrome in a String formed by concatenating its prefix and suffix ; Function to check whether the String is a palindrome ; Reverse the String to compare with the original String ; Check if both are same ; Function to find the longest palindrome in a String formed by concatenating its prefix and suffix ; Length of the String ; Finding the length upto which the suffix and prefix forms a palindrome together ; Check whether the String has prefix and suffix subStrings which are palindromes . ; Removing the suffix and prefix subStrings which already forms a palindrome and storing them in separate Strings ; Check all prefix subStrings in the remaining String str ; Check if the prefix subString is a palindrome ; If the prefix subString is a palindrome then check if it is of maximum length so far ; Check all the suffix subStrings in the remaining String str ; Check if the suffix subString is a palindrome ; If the suffix subString is a palindrome then check if it is of maximum length so far ; Combining all the thee parts of the answer ; Driver code
import java . util . * ; class GFG { static boolean isPalindrome ( String r ) { String p = r ; p = reverse ( p ) ; return ( r . equals ( p ) ) ; } static String PrefixSuffixPalindrome ( String str ) { int n = str . length ( ) , len = 0 ; for ( int i = 0 ; i < n / 2 ; i ++ ) { if ( str . charAt ( i ) != str . charAt ( n - i - 1 ) ) { len = i ; break ; } } String prefix = " " , suffix = " " ; String midPal = " " ; prefix = str . substring ( 0 , len ) ; suffix = str . substring ( n - len ) ; str = str . substring ( len , ( n - 2 * len ) + len ) ; for ( int i = 1 ; i <= str . length ( ) ; i ++ ) { String y = str . substring ( 0 , i ) ; if ( isPalindrome ( y ) ) { if ( midPal . length ( ) < y . length ( ) ) { midPal = y ; } } } for ( int i = 1 ; i <= str . length ( ) ; i ++ ) { String y = str . substring ( str . length ( ) - i ) ; if ( isPalindrome ( y ) ) { if ( midPal . length ( ) < y . length ( ) ) { midPal = y ; } } } String answer = prefix + midPal + suffix ; return answer ; } static String reverse ( String input ) { char [ ] a = input . toCharArray ( ) ; int l , r = a . length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . valueOf ( a ) ; } public static void main ( String [ ] args ) { String str = " abcdfdcecba " ; System . out . print ( PrefixSuffixPalindrome ( str ) ) ; } }
Minimize the maximum difference between adjacent elements in an array | Java implementation to find the minimum of the maximum difference of the adjacent elements after removing K elements from the array ; Function to find the minimum of the maximum difference of the adjacent elements after removing K elements from the array ; Initialising the minimum difference ; Traversing over subsets in iterative manner ; Number of elements to be taken in the subset ON bits of i represent elements not to be removed ; If the removed set is of size k ; Creating the new array after removing elements ; Maximum difference of adjacent elements of remaining array ; Driver Code
import java . util . * ; class GFG { static int minimumAdjacentDifference ( int a [ ] , int n , int k ) { int minDiff = Integer . MAX_VALUE ; for ( int i = 0 ; i < ( 1 << n ) ; i ++ ) { int cnt = Integer . bitCount ( i ) ; if ( cnt == n - k ) { Vector < Integer > temp = new Vector < Integer > ( ) ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( i & ( 1 << j ) ) != 0 ) temp . add ( a [ j ] ) ; } int maxDiff = Integer . MIN_VALUE ; for ( int j = 0 ; j < temp . size ( ) - 1 ; j ++ ) { maxDiff = Math . max ( maxDiff , temp . get ( j + 1 ) - temp . get ( j ) ) ; } minDiff = Math . min ( minDiff , maxDiff ) ; } } return minDiff ; } public static void main ( String args [ ] ) { int n = 5 ; int k = 2 ; int a [ ] = { 3 , 7 , 8 , 10 , 14 } ; System . out . println ( minimumAdjacentDifference ( a , n , k ) ) ; } }
Minimize the maximum difference between adjacent elements in an array | Java implementation to find the minimum of the maximum difference of the adjacent elements after removing K elements from the array ; Function to find the minimum of the maximum difference of the adjacent elements after removing K elements from the array ; Initialising the minimum difference ; Iterating over all subarrays of size n - k ; Maximum difference after removing elements ; Minimum Adjacent Difference ; Driver Code
class GFG { static int minimumAdjacentDifference ( int a [ ] , int n , int k ) { int minDiff = Integer . MAX_VALUE ; for ( int i = 0 ; i <= k ; i ++ ) { int maxDiff = Integer . MIN_VALUE ; for ( int j = 0 ; j < n - k - 1 ; j ++ ) { for ( int p = i ; p <= i + j ; p ++ ) { maxDiff = Math . max ( maxDiff , a [ p + 1 ] - a [ p ] ) ; } } minDiff = Math . min ( minDiff , maxDiff ) ; } return minDiff ; } public static void main ( String [ ] args ) { int n = 5 ; int k = 2 ; int [ ] a = { 3 , 7 , 8 , 10 , 14 } ; System . out . println ( minimumAdjacentDifference ( a , n , k ) ) ; } }
Minimize the maximum difference between adjacent elements in an array | Java implementation to find the minimum of the maximum difference of the adjacent elements after removing K elements from the array ; Function to find the minimum different in the subarrays of size K in the array ; Create a Double Ended Queue , Qi that will store indexes of array elements , queue will store indexes of useful elements in every window ; Process first k ( or first window ) elements of array ; For every element , the previous smaller elements are useless so remove them from Qi ; Remove from rear ; Add new element at rear of queue ; Process rest of the elements , i . e . , from arr [ k ] to arr [ n - 1 ] ; The element at the front of the queue is the largest element of previous window ; Remove the elements which are out of this window ; Remove all elements smaller than the currently being added element ( remove useless elements ) ; Add current element at the rear of Qi ; Compare the maximum element of last window ; Function to find the minimum of the maximum difference of the adjacent elements after removing K elements from the array ; Create the difference array ; find minimum of all maximum of subarray sizes n - k - 1 ; Driver code
import java . util . * ; import java . lang . * ; class GFG { static int findKMin ( int arr [ ] , int n , int k ) { Deque < Integer > Qi = new LinkedList < > ( ) ; int i ; for ( i = 0 ; i < k ; ++ i ) { while ( ( ! Qi . isEmpty ( ) ) && arr [ i ] >= arr [ Qi . peekLast ( ) ] ) Qi . pollLast ( ) ; Qi . addLast ( i ) ; } int minDiff = Integer . MAX_VALUE ; for ( ; i < n ; ++ i ) { minDiff = Math . min ( minDiff , arr [ Qi . peekFirst ( ) ] ) ; while ( ( ! Qi . isEmpty ( ) ) && Qi . peekFirst ( ) <= i - k ) Qi . pollFirst ( ) ; while ( ( ! Qi . isEmpty ( ) ) && arr [ i ] >= arr [ Qi . peekLast ( ) ] ) Qi . pollLast ( ) ; Qi . addLast ( i ) ; } minDiff = Math . min ( minDiff , arr [ Qi . peekFirst ( ) ] ) ; return minDiff ; } static int minimumAdjacentDifference ( int a [ ] , int n , int k ) { int [ ] diff = new int [ n - 1 ] ; for ( int i = 0 ; i < n - 1 ; i ++ ) { diff [ i ] = a [ i + 1 ] - a [ i ] ; } int answer = findKMin ( diff , n - 1 , n - k - 1 ) ; return answer ; } public static void main ( String [ ] args ) { int n = 5 ; int k = 2 ; int a [ ] = { 3 , 7 , 8 , 10 , 14 } ; System . out . println ( minimumAdjacentDifference ( a , n , k ) ) ; } }
Minimum elements inserted in a sorted array to form an Arithmetic progression | Java implementation to find the minimum elements required to be inserted into an array to form an arithmetic progression ; Function to find the greatest common divisor of two numbers ; Function to find the minimum the minimum number of elements required to be inserted into array ; Difference array of consecutive elements of the array ; GCD of the difference array ; Loop to calculate the minimum number of elements required ; Driver Code ; Function calling
class GFG { static int gcdFunc ( int a , int b ) { if ( b == 0 ) return a ; return gcdFunc ( b , a % b ) ; } static int findMinimumElements ( int [ ] a , int n ) { int [ ] b = new int [ n - 1 ] ; for ( int i = 1 ; i < n ; i ++ ) { b [ i - 1 ] = a [ i ] - a [ i - 1 ] ; } int gcd = b [ 0 ] ; for ( int i = 0 ; i < n - 1 ; i ++ ) { gcd = gcdFunc ( gcd , b [ i ] ) ; } int ans = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { ans += ( b [ i ] / gcd ) - 1 ; } return ans ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 1 , 6 , 8 , 10 , 14 , 16 } ; int n1 = arr1 . length ; System . out . print ( findMinimumElements ( arr1 , n1 ) + "NEW_LINE"); } }