codeparrot/xlcost-text-to-code huggingface dataset:
Length of longest substring to be deleted to make a string equal to another string | Java program to implement the above approach ; Function to print the length of longest substring to be deleted ; Stores the length of string ; Store the position of previous matched character of str1 ; Store the position of first occurrence of str2 in str1 ; Find the position of the first occurrence of str2 ; Store the index of str1 ; If both characters not matched ; Store the length of the longest deleted substring ; Store the position of last occurrence of str2 in str1 ; If both characters not matched ; Update res ; Update res . ; Driver Code ; Given string ; Function call
import java . io . * ; class GFG { static int longDelSub ( String str1 , String str2 ) { int N = str1 . length ( ) ; int M = str2 . length ( ) ; int prev_pos = 0 ; int pos [ ] = new int [ M ] ; for ( int i = 0 ; i < M ; i ++ ) { int index = prev_pos ; while ( index < N && str1 . charAt ( index ) != str2 . charAt ( i ) ) { index ++ ; } pos [ i ] = index ; prev_pos = index + 1 ; } int res = N - prev_pos ; prev_pos = N - 1 ; for ( int i = M - 1 ; i >= 0 ; i -- ) { int index = prev_pos ; while ( index >= 0 && str1 . charAt ( index ) != str2 . charAt ( i ) ) { index -- ; } if ( i != 0 ) { res = Math . max ( res , index - pos [ i - 1 ] - 1 ) ; } prev_pos = index - 1 ; } res = Math . max ( res , prev_pos + 1 ) ; return res ; } public static void main ( String [ ] args ) { String str1 = " GeeksforGeeks " ; String str2 = " forks " ; System . out . print ( longDelSub ( str1 , str2 ) ) ; } }
Maximized partitions of a string such that each character of the string appears in one substring | Java program for the above approach ; Function to print all the substrings ; Stores the substrings ; Stores last index of characters of string s ; Find the last position of each character in the string ; Update the last index ; Iterate the given string ; Get the last index of s [ i ] ; Extend the current partition characters last pos ; If the current pos of character equals the min pos then the end of partition ; Add the respective character first ; Store the partition 's  len and reset variables ; Update the minp and str ; Driver Code ; Input string ; Function call
import java . util . * ; class GFG { public static void print_substring ( String s ) { int n = s . length ( ) ; String str = " " ; Vector < Integer > ans = new Vector < Integer > ( ) ; if ( n == 0 ) { System . out . print ( " - 1" ) ; return ; } int [ ] last_pos = new int [ 26 ] ; Arrays . fill ( last_pos , - 1 ) ; for ( int i = n - 1 ; i >= 0 ; -- i ) { if ( last_pos [ s . charAt ( i ) - ' a ' ] == - 1 ) { last_pos [ s . charAt ( i ) - ' a ' ] = i ; } } int minp = - 1 ; for ( int i = 0 ; i < n ; ++ i ) { int lp = last_pos [ s . charAt ( i ) - ' a ' ] ; minp = Math . max ( minp , lp ) ; if ( i == minp ) { str += s . charAt ( i ) ; System . out . print ( str + ' ▁ ' ) ; minp = - 1 ; str = " " ; } else { str += s . charAt ( i ) ; } } } public static void main ( String [ ] args ) { String S = " ababcbacadefegdehijhklij " ; print_substring ( S ) ; } }
Lengths of maximized partitions of a string such that each character of the string appears in one substring | Java program for the above approach ; Function to find the length of all partitions oof a String such that each characters occurs in a single subString ; Stores last index of String s ; Find the last position of each letter in the String ; Update the last index ; Iterate the given String ; Get the last index of s [ i ] ; Extend the current partition characters last pos ; Increase len of partition ; if the current pos of character equals the min pos then the end of partition ; Store the length ; Print all the partition lengths ; Driver Code ; Given String str ; Function Call
import java . util . * ; class GFG { static void partitionString ( String s ) { int n = s . length ( ) ; Vector < Integer > ans = new Vector < Integer > ( ) ; if ( n == 0 ) { System . out . print ( " - 1" ) ; return ; } int [ ] last_pos = new int [ 26 ] ; Arrays . fill ( last_pos , - 1 ) ; for ( int i = n - 1 ; i >= 0 ; -- i ) { if ( last_pos [ s . charAt ( i ) - ' a ' ] == - 1 ) { last_pos [ s . charAt ( i ) - ' a ' ] = i ; } } int minp = - 1 , plen = 0 ; for ( int i = 0 ; i < n ; ++ i ) { int lp = last_pos [ s . charAt ( i ) - ' a ' ] ; minp = Math . max ( minp , lp ) ; ++ plen ; if ( i == minp ) { ans . add ( plen ) ; minp = - 1 ; plen = 0 ; } } for ( int i = 0 ; i < ( int ) ans . size ( ) ; i ++ ) { System . out . print ( ans . get ( i ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { String str = " acbbcc " ; partitionString ( str ) ; } }
Find the maximum occurring character after performing the given operations | Java program for the above approach ; Function to find the maximum occurring character ; Initialize count of zero and one ; Iterate over the given string ; Count the zeros ; Count the ones ; Iterate over the given string ; Check if character is * then continue ; Check if first character after * is X ; Add all * to the frequency of X ; Set prev to the i - th character ; Check if first character after * is Y ; Set prev to the i - th character ; Check if prev character is 1 and current character is 0 ; Half of the * will be converted to 0 ; Half of the * will be converted to 1 ; Check if prev and current are 1 ; All * will get converted to 1 ; No * can be replaced by either 0 or 1 ; Prev becomes the ith character ; Check if prev and current are 0 ; All * will get converted to 0 ; If frequency of 0 is more ; If frequency of 1 is more ; Driver code ; Given string ; Function call
import java . io . * ; class GFG { static void solve ( String S ) { int count_0 = 0 , count_1 = 0 ; int prev = - 1 ; for ( int i = 0 ; i < S . length ( ) ; i ++ ) { if ( S . charAt ( i ) == '0' ) count_0 ++ ; else if ( S . charAt ( i ) == '1' ) count_1 ++ ; } for ( int i = 0 ; i < S . length ( ) ; i ++ ) { if ( S . charAt ( i ) == ' * ' ) continue ; else if ( S . charAt ( i ) == '0' && prev == - 1 ) { count_0 = count_0 + i ; prev = i ; } else if ( S . charAt ( i ) == '1' && prev == - 1 ) { prev = i ; } else if ( S . charAt ( prev ) == '1' && S . charAt ( i ) == '0' ) { count_0 = count_0 + ( i - prev - 1 ) / 2 ; count_1 = count_1 + ( i - prev - 1 ) / 2 ; prev = i ; } else if ( S . charAt ( prev ) == '1' && S . charAt ( i ) == '1' ) { count_1 = count_1 + ( i - prev - 1 ) ; prev = i ; } else if ( S . charAt ( prev ) == '0' && S . charAt ( i ) == '1' ) prev = i ; else if ( S . charAt ( prev ) == '0' && S . charAt ( i ) == '0' ) { count_0 = count_0 + ( i - prev - 1 ) ; prev = i ; } } if ( count_0 > count_1 ) System . out . print ( "0" ) ; else if ( count_1 > count_0 ) System . out . print ( "1" ) ; else { System . out . print ( " - 1" ) ; } } public static void main ( String [ ] args ) { String str = " * *0 * * 1 * * *0" ; solve ( str ) ; } }
Arithmetic Progression containing X and Y with least possible first term | Java program for the approach ; Function that finds the minimum positive first term including X with given common difference and the number of terms ; Stores the first term ; Initialize the low and high ; Perform binary search ; Find the mid ; Check if first term is greater than 0 ; Store the possible first term ; Search between mid + 1 to high ; Search between low to mid - 1 ; Return the minimum first term ; Function that finds the Arithmetic Progression with minimum possible first term containing X and Y ; Considering X to be smaller than Y always ; Stores the max common difference ; Stores the minimum first term and the corresponding common difference of the resultant AP ; Iterate over all the common difference ; Check if X and Y is included for current common difference ; Store the possible common difference ; Number of terms from X to Y with diff1 common difference ; Number of terms from X to Y with diff2 common difference ; Find the corresponding first terms with diff1 and diff2 ; Store the minimum first term and the corresponding common difference ; Print the resultant AP ; Driver Code ; Given length of AP and the two terms ; Function call
import java . util . * ; class GFG { static int minFirstTerm ( int X , int diff , int N ) { int first_term = Integer . MAX_VALUE ; int low = 0 , high = N ; while ( low <= high ) { int mid = ( low + high ) / 2 ; if ( X - mid * diff > 0 ) { first_term = X - mid * diff ; low = mid + 1 ; } else high = mid - 1 ; } return first_term ; } static void printAP ( int N , int X , int Y ) { if ( X > Y ) { X = X + Y ; Y = X - Y ; X = X - Y ; } int maxDiff = Y - X ; int first_term = Integer . MAX_VALUE , diff = 0 ; for ( int i = 1 ; i * i <= maxDiff ; i ++ ) { if ( maxDiff % i == 0 ) { int diff1 = i ; int diff2 = maxDiff / diff1 ; int terms1 = diff2 + 1 ; int terms2 = diff1 + 1 ; int first_term1 = minFirstTerm ( X , diff1 , N - terms1 ) ; int first_term2 = minFirstTerm ( X , diff2 , N - terms2 ) ; if ( first_term1 < first_term ) { first_term = first_term1 ; diff = diff1 ; } if ( first_term2 < first_term ) { first_term = first_term2 ; diff = diff2 ; } } } for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( first_term + " ▁ " ) ; first_term += diff ; } } public static void main ( String [ ] args ) { int N = 5 , X = 10 , Y = 15 ; printAP ( N , X , Y ) ; } }
Check if a number can be represented as sum of two consecutive perfect cubes | Java program of the above approach ; Function to check if a number can be expressed as the sum of cubes of two consecutive numbers ; Driver Code
import java . util . * ; class GFG { static boolean isCubeSum ( int n ) { for ( int i = 1 ; i * i * i <= n ; i ++ ) { if ( i * i * i + ( i + 1 ) * ( i + 1 ) * ( i + 1 ) == n ) return true ; } return false ; } public static void main ( String [ ] args ) { int n = 35 ; if ( isCubeSum ( n ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
Check if a number can be represented as sum of two consecutive perfect cubes | Java program to implement above approach ; Function to check that a number is the sum of cubes of 2 consecutive numbers or not ; Condition to check if a number is the sum of cubes of 2 consecutive numbers or not ; Driver Code ; Function call
class GFG { static boolean isSumCube ( int N ) { int a = ( int ) Math . cbrt ( N ) ; int b = a - 1 ; return ( ( a * a * a + b * b * b ) == N ) ; } public static void main ( String [ ] args ) { int i = 35 ; if ( isSumCube ( i ) ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } }
Maximize sum of remaining elements after every removal of the array half with greater sum | Java program to implement the above approach ; Function to find the maximum sum ; Base case len of array is 1 ; Stores the final result ; Traverse the array ; Store left prefix sum ; Store right prefix sum ; Compare the left and right ; If both are equal apply the optimal method ; Update with minimum ; Return the final ans ; Function to print maximum sum ; To store prefix sums ; Traversing the array ; Add prefix sum of the array ; Driver code ; Function call
import java . util . * ; class GFG { static int maxweight ( int s , int e , Map < Integer , Integer > pre ) { if ( s == e ) return 0 ; int ans = 0 ; for ( int i = s ; i < e ; i ++ ) { int left = pre . get ( i ) - pre . get ( s - 1 ) ; int right = pre . get ( e ) - pre . get ( i ) ; if ( left < right ) ans = Math . max ( ans , left + maxweight ( s , i , pre ) ) ; if ( left == right ) { ans = Math . max ( ans , Math . max ( left + maxweight ( s , i , pre ) , right + maxweight ( i + 1 , e , pre ) ) ) ; } if ( left > right ) ans = Math . max ( ans , right + maxweight ( i + 1 , e , pre ) ) ; } return ans ; } static void maxSum ( List < Integer > arr ) { Map < Integer , Integer > pre = new HashMap < > ( ) ; pre . put ( - 1 , 0 ) ; pre . put ( 0 , arr . get ( 0 ) ) ; for ( int i = 1 ; i < arr . size ( ) ; i ++ ) { pre . put ( i , pre . getOrDefault ( i - 1 , 0 ) + arr . get ( i ) ) ; } System . out . println ( maxweight ( 0 , arr . size ( ) - 1 , pre ) ) ; } public static void main ( String [ ] args ) { List < Integer > arr = Arrays . asList ( 6 , 2 , 3 , 4 , 5 , 5 ) ; maxSum ( arr ) ; } }
Maximum time in HH : MM : SS format that can be represented by given six digits | Java Program of the above approach ; Function to return the maximum possible time in 24 - Hours format that can be represented by array elements ; Stores the frequency of the array elements ; Maximum possible time ; Iterate to minimum possible time ; Conditions to reduce the the time iteratively ; If all required digits are present in the Map ; Retrieve Original Count ; If seconds is reduced to 0 ; If minutes is reduced to 0 ; Driver Code
import java . util . * ; class GFG { static String largestTimeFromDigits ( int [ ] A ) { HashMap < Integer , Integer > mp1 = new HashMap < Integer , Integer > ( ) ; HashMap < Integer , Integer > mp2 = new HashMap < Integer , Integer > ( ) ; for ( int x : A ) { if ( mp1 . containsKey ( x ) ) mp1 . put ( x , mp1 . get ( x ) + 1 ) ; else mp1 . put ( x , 1 ) ; } mp2 = ( HashMap < Integer , Integer > ) mp1 . clone ( ) ; int hr = 23 , m = 59 , s = 59 ; while ( hr >= 0 ) { int h0 = hr / 10 , h1 = hr % 10 ; int m0 = m / 10 , m1 = m % 10 ; int s0 = s / 10 , s1 = s % 10 ; int p = 0 ; int [ ] arr = { h0 , h1 , m0 , m1 , s0 , s1 } ; for ( int it : arr ) { if ( mp1 . containsKey ( it ) && mp1 . get ( it ) > 0 ) { mp1 . put ( it , mp1 . get ( it ) - 1 ) ; } else { p = 1 ; } } if ( p == 0 ) { String st = " " ; st = String . valueOf ( h0 ) + String . valueOf ( h1 ) ; st += ' : ' + String . valueOf ( m0 ) + String . valueOf ( m1 ) ; st += ' : ' + String . valueOf ( s0 ) + String . valueOf ( s1 ) ; return st ; } mp1 = ( HashMap < Integer , Integer > ) mp2 . clone ( ) ; if ( s == 0 ) { s = 59 ; m -- ; } else if ( m < 0 ) { m = 59 ; hr -- ; } if ( s > 0 ) { s -- ; } } return " - 1" ; } public static void main ( String [ ] args ) { int [ ] v = { 0 , 2 , 1 , 9 , 3 , 2 } ; System . out . print ( largestTimeFromDigits ( v ) ) ; } }
Minimum size substring to be removed to make a given string palindromic | Java program of the above approach ; Function to find palindromic prefix of maximum length ; Finding palindromic prefix of maximum length ; Checking if curr subString is palindrome or not . ; Condition to check if the prefix is a palindrome ; If no palindrome exist ; Function to find the maximum size palindrome such that after removing minimum size subString ; Finding prefix and suffix of same length ; Matching the ranges ; Case 1 : Length is even and whole String is palindrome ; Case 2 : Length is odd and whole String is palindrome ; Adding that mid character ; Add prefix or suffix of the remaining String or suffix , whichever is longer ; Reverse the remaining String to find the palindromic suffix ; Driver Code
import java . util . * ; class GFG { static String palindromePrefix ( String S ) { int n = S . length ( ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { String curr = S . substring ( 0 , i + 1 ) ; int l = 0 , r = curr . length ( ) - 1 ; boolean is_palindrome = true ; while ( l < r ) { if ( curr . charAt ( l ) != curr . charAt ( r ) ) { is_palindrome = false ; break ; } l ++ ; r -- ; } if ( is_palindrome ) return curr ; } return " " ; } static String maxPalindrome ( String S ) { int n = S . length ( ) ; if ( n <= 1 ) { return S ; } String pre = " " , suff = " " ; int i = 0 , j = n - 1 ; while ( S . charAt ( i ) == S . charAt ( j ) && i < j ) { i ++ ; j -- ; } i -- ; j ++ ; pre = S . substring ( 0 , i + 1 ) ; suff = S . substring ( j ) ; if ( j - i == 1 ) { return pre + suff ; } if ( j - i == 2 ) { String mid_char = S . substring ( i + 1 , i + 2 ) ; return pre + mid_char + suff ; } String rem_str = S . substring ( i + 1 , j ) ; String pre_of_rem_str = palindromePrefix ( rem_str ) ; rem_str = reverse ( rem_str ) ; String suff_of_rem_str = palindromePrefix ( rem_str ) ; if ( pre_of_rem_str . length ( ) >= suff_of_rem_str . length ( ) ) { return pre + pre_of_rem_str + suff ; } else { return pre + suff_of_rem_str + suff ; } } 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 S = " geeksforskeeg " ; System . out . print ( maxPalindrome ( S ) ) ; } }
Count numbers from a given range that contains a given number as the suffix | Java Program of the above approach ; Function to count the number ends with given number in range ; Find number of digits in A ; Find the power of 10 ; Incrementing the A ; Driver Code ; Function Call
class GFG { static void countNumEnds ( int A , int L , int R ) { int temp , count = 0 , digits ; int cycle ; digits = ( int ) ( Math . log10 ( A ) + 1 ) ; temp = ( int ) Math . pow ( 10 , digits ) ; cycle = temp ; while ( temp <= R ) { if ( temp >= L ) count ++ ; temp += cycle ; } System . out . print ( count ) ; } public static void main ( String [ ] args ) { int A = 2 , L = 2 , R = 20 ; countNumEnds ( A , L , R ) ; } }
Minimize replacement of characters to its nearest alphabet to make a string palindromic | Java program for the above approach ; Function to find the minimum number of operations required to convert th given string into palindrome ; Iterate till half of the string ; Find the absolute difference between the characters ; Adding the minimum difference of the two result ; Return the result ; Driver code ; Given string ; Function call
class GFG { public static int minOperations ( String s ) { int len = s . length ( ) ; int result = 0 ; for ( int i = 0 ; i < len / 2 ; i ++ ) { int D1 = Math . max ( s . charAt ( i ) , s . charAt ( len - 1 - i ) ) - Math . min ( s . charAt ( i ) , s . charAt ( len - 1 - i ) ) ; int D2 = 26 - D1 ; result += Math . min ( D1 , D2 ) ; } return result ; } public static void main ( String [ ] args ) { String s = " abccdb " ; System . out . println ( minOperations ( s ) ) ; } }
Check if all the pairs of an array are coprime with each other | Java implementation of the above approach ; Function to store and check the factors ; Check if factors are equal ; Check if the factor is already present ; Insert the factor in set ; Check if the factor is already present ; Insert the factors in set ; Function to check if all the pairs of array elements are coprime with each other ; Check if factors of A [ i ] haven 't occurred previously ; Driver Code
import java . util . * ; class GFG { static boolean findFactor ( int value , HashSet < Integer > factors ) { factors . add ( value ) ; for ( int i = 2 ; i * i <= value ; i ++ ) { if ( value % i == 0 ) { if ( value / i == i ) { if ( factors . contains ( i ) ) { return true ; } else { factors . add ( i ) ; } } else { if ( factors . contains ( i ) || factors . contains ( value / i ) ) { return true ; } else { factors . add ( i ) ; factors . add ( value / i ) ; } } } } return false ; } static boolean allCoprime ( int A [ ] , int n ) { boolean all_coprime = true ; HashSet < Integer > factors = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( A [ i ] == 1 ) continue ; if ( findFactor ( A [ i ] , factors ) ) { all_coprime = false ; break ; } } return all_coprime ; } public static void main ( String [ ] args ) { int A [ ] = { 3 , 5 , 11 , 7 , 19 } ; int arr_size = A . length ; if ( allCoprime ( A , arr_size ) ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } }
Count subsequences which contains both the maximum and minimum array element | Java program for the above approach ; Function to calculate the count of subsequences ; Find the maximum from the array ; Find the minimum from the array ; If array contains only one distinct element ; Find the count of maximum ; Find the count of minimum ; Finding the result with given condition ; Driver Code ; Function call
import java . util . Arrays ; class GFG { static double countSubSequence ( int [ ] arr , int n ) { int maximum = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; int minimum = Arrays . stream ( arr ) . min ( ) . getAsInt ( ) ; if ( maximum == minimum ) return Math . pow ( 2 , n ) - 1 ; int i = count ( arr , maximum ) ; int j = count ( arr , minimum ) ; double res = ( Math . pow ( 2 , i ) - 1 ) * ( Math . pow ( 2 , j ) - 1 ) * Math . pow ( 2 , n - i - j ) ; return res ; } static int count ( int [ ] arr , int value ) { int sum = 0 ; for ( int i = 0 ; i < arr . length ; i ++ ) if ( arr [ i ] == value ) sum ++ ; return sum ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int n = arr . length ; System . out . println ( countSubSequence ( arr , n ) ) ; } }
Minimize elements to be added to a given array such that it contains another given array as its subsequence | Java program for the above approach ; Function that finds the minimum number of the element must be added to make A as a subsequence in B ; Base Case ; dp [ i ] [ j ] indicates the length of LCS of A of length i & B of length j ; If there are no elements either in A or B then the length of lcs is 0 ; If the element present at ith and jth index of A and B are equal then include in LCS ; If they are not equal then take the max ; Return difference of length of A and lcs of A and B ; Driver Code ; Given sequence A and B ; Function call
import java . util . * ; class GFG { static int transformSubsequence ( int n , int m , int [ ] A , int [ ] B ) { if ( B . length == 0 ) return n ; int [ ] [ ] dp = new int [ n + 1 ] [ m + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) { for ( int j = 0 ; j < m + 1 ; j ++ ) { if ( i == 0 j == 0 ) dp [ i ] [ j ] = 0 ; else if ( A [ i - 1 ] == B [ j - 1 ] ) dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ] ; else dp [ i ] [ j ] = Math . max ( dp [ i - 1 ] [ j ] , dp [ i ] [ j - 1 ] ) ; } } return n - dp [ n ] [ m ] ; } public static void main ( String [ ] args ) { int N = 5 ; int M = 6 ; int [ ] A = { 1 , 2 , 3 , 4 , 5 } ; int [ ] B = { 2 , 5 , 6 , 4 , 9 , 12 } ; System . out . print ( transformSubsequence ( N , M , A , B ) ) ; } }
Check if the sum of a subarray within a given range is a perfect square or not | Java implementation of the above approach ; Function to calculate the square root of the sum of a subarray in a given range ; Calculate the sum of array elements within a given range ; Finding the square root ; If a perfect square is found ; Reduce the search space if the value is greater than sum ; Reduce the search space if the value if smaller than sum ; Driver Code ; Given Array ; Given range ; Function Call
import java . util . * ; class GFG { static int checkForPerfectSquare ( int [ ] arr , int i , int j ) { int mid , sum = 0 ; for ( int m = i ; m <= j ; m ++ ) { sum += arr [ m ] ; } int low = 0 , high = sum / 2 ; while ( low <= high ) { mid = low + ( high - low ) / 2 ; if ( mid * mid == sum ) { return mid ; } else if ( mid * mid > sum ) { high = mid - 1 ; } else { low = mid + 1 ; } } return - 1 ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 19 , 33 , 48 , 90 , 100 } ; int L = 1 , R = 3 ; System . out . print ( checkForPerfectSquare ( arr , L , R ) ) ; } }
Count of subarrays forming an Arithmetic Progression ( AP ) | Java implementation of the above approach ; Function to find the total count of subarrays ; Iterate over each subarray ; Difference between first two terms of subarray ; Iterate over the subarray from i to j ; Check if the difference of all adjacent elements is same ; Driver Code ; Given array ; Function Call
import java . util . * ; class GFG { static int calcSubarray ( int A [ ] , int N ) { int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { boolean flag = true ; int comm_diff = A [ i + 1 ] - A [ i ] ; for ( int k = i ; k < j ; k ++ ) { if ( A [ k + 1 ] - A [ k ] == comm_diff ) { continue ; } else { flag = false ; break ; } } if ( flag ) { count ++ ; } } } return count ; } public static void main ( String [ ] args ) { int [ ] A = { 8 , 7 , 4 , 1 , 0 } ; int N = A . length ; System . out . print ( calcSubarray ( A , N ) ) ; } }