codeparrot/xlcost-text-to-code huggingface dataset:
Maximum difference between two elements such that larger element appears after the smaller number | Java program to find Maximum difference between two elements such that larger element appears after the smaller number ; The function assumes that there are at least two elements in array . The function returns a negative value if the array is sorted in decreasing order . Returns 0 if elements are equal ; Driver program to test above functions ; Function calling
class MaximumDiffrence { int maxDiff ( int arr [ ] , int arr_size ) { int max_diff = arr [ 1 ] - arr [ 0 ] ; int i , j ; for ( i = 0 ; i < arr_size ; i ++ ) { for ( j = i + 1 ; j < arr_size ; j ++ ) { if ( arr [ j ] - arr [ i ] > max_diff ) max_diff = arr [ j ] - arr [ i ] ; } } return max_diff ; } public static void main ( String [ ] args ) { MaximumDifference maxdif = new MaximumDifference ( ) ; int arr [ ] = { 1 , 2 , 90 , 10 , 110 } ; System . out . println ( " Maximum ▁ difference ▁ is ▁ " + maxdif . maxDiff ( arr , 5 ) ) ; } }
Maximum difference between two elements such that larger element appears after the smaller number | Java program to find Maximum difference between two elements such that larger element appears after the smaller number ; The function assumes that there are at least two elements in array . The function returns a negative value if the array is sorted in decreasing order and returns 0 if elements are equal ; Initialize Result ; Initialize max element from right side ; Driver program to test above function ; Function calling
import java . io . * ; class GFG { static int maxDiff ( int arr [ ] , int n ) { int maxDiff = - 1 ; int maxRight = arr [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] > maxRight ) maxRight = arr [ i ] ; else { int diff = maxRight - arr [ i ] ; if ( diff > maxDiff ) { maxDiff = diff ; } } } return maxDiff ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 90 , 10 , 110 } ; int n = arr . length ; System . out . println ( " Maximum ▁ difference ▁ is ▁ " + maxDiff ( arr , n ) ) ; } }
Maximum difference between two elements such that larger element appears after the smaller number | Java program to find Maximum difference between two elements such that larger element appears after the smaller number ; The function assumes that there are at least two elements in array . The function returns a negative value if the array is sorted in decreasing order and returns 0 if elements are equal ; Initialize diff , current sum and max sum ; Calculate current diff ; Calculate current sum ; Update max sum , if needed ; Driver Code ; Function calling
class GFG { static int maxDiff ( int arr [ ] , int n ) { int diff = arr [ 1 ] - arr [ 0 ] ; int curr_sum = diff ; int max_sum = curr_sum ; for ( int i = 1 ; i < n - 1 ; i ++ ) { diff = arr [ i + 1 ] - arr [ i ] ; if ( curr_sum > 0 ) curr_sum += diff ; else curr_sum = diff ; if ( curr_sum > max_sum ) max_sum = curr_sum ; } return max_sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 80 , 2 , 6 , 3 , 100 } ; int n = arr . length ; System . out . print ( " Maximum ▁ difference ▁ is ▁ " + maxDiff ( arr , n ) ) ; } }
Find the maximum element in an array which is first increasing and then decreasing | java program to find maximum element ; function to find the maximum element ; main function
class Main { static int findMaximum ( int arr [ ] , int low , int high ) { int max = arr [ low ] ; int i ; for ( i = low ; i <= high ; i ++ ) { if ( arr [ i ] > max ) max = arr [ i ] ; } return max ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 30 , 40 , 50 , 60 , 70 , 23 , 20 } ; int n = arr . length ; System . out . println ( " The ▁ maximum ▁ element ▁ is ▁ " + findMaximum ( arr , 0 , n - 1 ) ) ; } }
Find the maximum element in an array which is first increasing and then decreasing | java program to find maximum element ; Base Case : Only one element is present in arr [ low . . high ] ; If there are two elements and first is greater then the first element is maximum ; If there are two elements and second is greater then the second element is maximum ; If we reach a point where arr [ mid ] is greater than both of its adjacent elements arr [ mid - 1 ] and arr [ mid + 1 ] , then arr [ mid ] is the maximum element ; If arr [ mid ] is greater than the next element and smaller than the previous element then maximum lies on left side of mid ; when arr [ mid ] is greater than arr [ mid - 1 ] and smaller than arr [ mid + 1 ] ; main function
class Main { static int findMaximum ( int arr [ ] , int low , int high ) { if ( low == high ) return arr [ low ] ; if ( ( high == low + 1 ) && arr [ low ] >= arr [ high ] ) return arr [ low ] ; if ( ( high == low + 1 ) && arr [ low ] < arr [ high ] ) return arr [ high ] ; int mid = ( low + high ) / 2 ; if ( arr [ mid ] > arr [ mid + 1 ] && arr [ mid ] > arr [ mid - 1 ] ) return arr [ mid ] ; if ( arr [ mid ] > arr [ mid + 1 ] && arr [ mid ] < arr [ mid - 1 ] ) return findMaximum ( arr , low , mid - 1 ) ; else return findMaximum ( arr , mid + 1 , high ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 50 , 10 , 9 , 7 , 6 } ; int n = arr . length ; System . out . println ( " The ▁ maximum ▁ element ▁ is ▁ " + findMaximum ( arr , 0 , n - 1 ) ) ; } }
Count smaller elements on right side |  ; initialize all the counts in countSmaller array as 0 ; Utility function that prints out an array on a line ; Driver program to test above functions
class CountSmaller { void constructLowerArray ( int arr [ ] , int countSmaller [ ] , int n ) { int i , j ; for ( i = 0 ; i < n ; i ++ ) countSmaller [ i ] = 0 ; for ( i = 0 ; i < n ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { if ( arr [ j ] < arr [ i ] ) countSmaller [ i ] ++ ; } } } void printArray ( int arr [ ] , int size ) { int i ; for ( i = 0 ; i < size ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( " " ) ; } public static void main ( String [ ] args ) { CountSmaller small = new CountSmaller ( ) ; int arr [ ] = { 12 , 10 , 5 , 4 , 2 , 20 , 6 , 1 , 0 , 2 } ; int n = arr . length ; int low [ ] = new int [ n ] ; small . constructLowerArray ( arr , low , n ) ; small . printArray ( low , n ) ; } }
Find the smallest positive number missing from an unsorted array | Set 1 | Java program to find the smallest positive missing number ; Utility function that puts all non - positive ( 0 and negative ) numbers on left side of arr [ ] and return count of such numbers ; increment count of non - positive integers ; Find the smallest positive missing number in an array that contains all positive integers ; Mark arr [ i ] as visited by making arr [ arr [ i ] - 1 ] negative . Note that 1 is subtracted because index start from 0 and positive numbers start from 1 ; Return the first index value at which is positive ; 1 is added becuase indexes ; Find the smallest positive missing number in an array that contains both positive and negative integers ; First separate positive and negative numbers ; Shift the array and call findMissingPositive for positive part ; Driver code
import java . util . * ; class Main { static int segregate ( int arr [ ] , int size ) { int j = 0 , i ; for ( i = 0 ; i < size ; i ++ ) { if ( arr [ i ] <= 0 ) { int temp ; temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; j ++ ; } } return j ; } static int findMissingPositive ( int arr [ ] , int size ) { int i ; for ( i = 0 ; i < size ; i ++ ) { int x = Math . abs ( arr [ i ] ) ; if ( x - 1 < size && arr [ x - 1 ] > 0 ) arr [ x - 1 ] = - arr [ x - 1 ] ; } for ( i = 0 ; i < size ; i ++ ) if ( arr [ i ] > 0 ) return i + 1 ; return size + 1 ; } static int findMissing ( int arr [ ] , int size ) { int shift = segregate ( arr , size ) ; int arr2 [ ] = new int [ size - shift ] ; int j = 0 ; for ( int i = shift ; i < size ; i ++ ) { arr2 [ j ] = arr [ i ] ; j ++ ; } return findMissingPositive ( arr2 , j ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 10 , 2 , - 10 , - 20 } ; int arr_size = arr . length ; int missing = findMissing ( arr , arr_size ) ; System . out . println ( " The ▁ smallest ▁ positive ▁ missing ▁ number ▁ is ▁ " + missing ) ; } }
Find the smallest positive number missing from an unsorted array | Set 1 | Java program for the above approach ; Function for finding the first missing positive number ; Check if 1 is present in array or not ; Loop to check boundary condition and for swapping ; Finding which index has value less than n ; If array has values from 1 to n ; Driver Code
import java . util . Arrays ; class GFG { static int firstMissingPositive ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { while ( arr [ i ] >= 1 && arr [ i ] <= n && arr [ i ] != arr [ arr [ i ] - 1 ] ) { int temp = arr [ arr [ i ] - 1 ] ; arr [ arr [ i ] - 1 ] = arr [ i ] ; arr [ i ] = temp ; } } for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] != i + 1 ) return ( i + 1 ) ; return ( n + 1 ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , - 7 , 6 , 8 , 1 , - 10 , 15 } ; int n = arr . length ; int ans = firstMissingPositive ( arr , n ) ; System . out . println ( ans ) ; } }
Maximum Sum Path in Two Arrays | JAVA program to find maximum sum path ; Utility function to find maximum of two integers ; This function returns the sum of elements on maximum path from beginning to end ; initialize indexes for ar1 [ ] and ar2 [ ] ; Initialize result and current sum through ar1 [ ] and ar2 [ ] . ; Below 3 loops are similar to merge in merge sort ; Add elements of ar1 [ ] to sum1 ; Add elements of ar2 [ ] to sum2 ; we reached a common point ; Take the maximum of two sums and add to result Also add the common element of array , once ; Update sum1 and sum2 for elements after this intersection point ; update i and j to move to next element of each array ; Add remaining elements of ar1 [ ] ; Add remaining elements of ar2 [ ] ; Add maximum of two sums of remaining elements ; Driver code ; Function call
class MaximumSumPath { int max ( int x , int y ) { return ( x > y ) ? x : y ; } int maxPathSum ( int ar1 [ ] , int ar2 [ ] , int m , int n ) { int i = 0 , j = 0 ; int result = 0 , sum1 = 0 , sum2 = 0 ; while ( i < m && j < n ) { if ( ar1 [ i ] < ar2 [ j ] ) sum1 += ar1 [ i ++ ] ; else if ( ar1 [ i ] > ar2 [ j ] ) sum2 += ar2 [ j ++ ] ; else { result += max ( sum1 , sum2 ) + ar1 [ i ] ; sum1 = 0 ; sum2 = 0 ; i ++ ; j ++ ; } } while ( i < m ) sum1 += ar1 [ i ++ ] ; while ( j < n ) sum2 += ar2 [ j ++ ] ; result += max ( sum1 , sum2 ) ; return result ; } public static void main ( String [ ] args ) { MaximumSumPath sumpath = new MaximumSumPath ( ) ; int ar1 [ ] = { 2 , 3 , 7 , 10 , 12 , 15 , 30 , 34 } ; int ar2 [ ] = { 1 , 5 , 7 , 8 , 10 , 15 , 16 , 19 } ; int m = ar1 . length ; int n = ar2 . length ; System . out . println ( " Maximum ▁ sum ▁ path ▁ is ▁ : " + sumpath . maxPathSum ( ar1 , ar2 , m , n ) ) ; } }
Smallest greater elements in whole array | Simple Java program to find smallest greater element in whole array for every element . ; Find the closest greater element for arr [ j ] in the entire array . ; Check if arr [ i ] is largest ; Driver code
import java . io . * ; class GFG { static void smallestGreater ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int diff = Integer . MAX_VALUE ; int closest = - 1 ; for ( int j = 0 ; j < n ; j ++ ) { if ( arr [ i ] < arr [ j ] && arr [ j ] - arr [ i ] < diff ) { diff = arr [ j ] - arr [ i ] ; closest = j ; } } if ( closest == - 1 ) System . out . print ( " _ ▁ " ) ; else System . out . print ( arr [ closest ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int ar [ ] = { 6 , 3 , 9 , 8 , 10 , 2 , 1 , 15 , 7 } ; int n = ar . length ; smallestGreater ( ar , n ) ; } }
Smallest greater elements in whole array | Efficient Java program to find smallest greater element in whole array for every element . ; lowerbound function ; Driver code
import java . util . * ; class GFG { static void smallestGreater ( int arr [ ] , int n ) { HashSet < Integer > s = new HashSet < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) s . add ( arr [ i ] ) ; Vector < Integer > newAr = new Vector < > ( ) ; for ( int p : s ) { newAr . add ( p ) ; } for ( int i = 0 ; i < n ; i ++ ) { int temp = lowerBound ( newAr , 0 , newAr . size ( ) , arr [ i ] ) ; if ( temp < n ) System . out . print ( newAr . get ( temp ) + " ▁ " ) ; else System . out . print ( " _ ▁ " ) ; } } static int lowerBound ( Vector < Integer > vec , int low , int high , int element ) { int [ ] array = new int [ vec . size ( ) ] ; int k = 0 ; for ( Integer val : vec ) { array [ k ] = val ; k ++ ; } while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( element > array [ middle ] ) { low = middle + 1 ; } else { high = middle ; } } return low + 1 ; } public static void main ( String [ ] args ) { int ar [ ] = { 6 , 3 , 9 , 8 , 10 , 2 , 1 , 15 , 7 } ; int n = ar . length ; smallestGreater ( ar , n ) ; } }
Online algorithm for checking palindrome in a stream | Java program for online algorithm for palindrome checking ; d is the number of characters in input alphabet ; q is a prime number used for evaluating Rabin Karp 's Rolling hash ; Length of input string ; A single character is always a palindrome ; Return if string has only one character ; Initialize first half reverse and second half for as firstr and second characters ; Now check for palindromes from second character onward ; If the hash values of ' firstr ' and ' second ' match , then only check individual characters ; Check if str [ 0. . i ] is palindrome using simple character by character match ; Calculate hash values for next iteration . Don 't calculate hash for next characters  if this is the last character of string ; If i is even ( next i is odd ) ; Add next character after first half at beginning of ' firstr ' ; Add next character after second half at the end of second half . ; If next i is odd ( next i is even ) then we need not to change firstr , we need to remove first character of second and append a character to it . ; Driver program to test above function
public class GFG { static final int d = 256 ; static final int q = 103 ; static void checkPalindromes ( String str ) { int N = str . length ( ) ; System . out . println ( str . charAt ( 0 ) + " ▁ Yes " ) ; if ( N == 1 ) return ; int firstr = str . charAt ( 0 ) % q ; int second = str . charAt ( 1 ) % q ; int h = 1 , i , j ; for ( i = 1 ; i < N ; i ++ ) { if ( firstr == second ) { for ( j = 0 ; j < i / 2 ; j ++ ) { if ( str . charAt ( j ) != str . charAt ( i - j ) ) break ; } System . out . println ( ( j == i / 2 ) ? str . charAt ( i ) + " ▁ Yes " : str . charAt ( i ) + " ▁ No " ) ; } else System . out . println ( str . charAt ( i ) + " ▁ No " ) ; if ( i != N - 1 ) { if ( i % 2 == 0 ) { h = ( h * d ) % q ; firstr = ( firstr + h * str . charAt ( i / 2 ) ) % q ; second = ( second * d + str . charAt ( i + 1 ) ) % q ; } else { second = ( d * ( second + q - str . charAt ( ( i + 1 ) / 2 ) * h ) % q + str . charAt ( i + 1 ) ) % q ; } } } } public static void main ( String args [ ] ) { String txt = " aabaacaabaa " ; checkPalindromes ( txt ) ; } }
Find zeroes to be flipped so that number of consecutive 1 's is maximized | Java to find positions of zeroes flipping which produces maximum number of consecutive 1 's ; m is maximum of number zeroes allowed to flip ; Left and right indexes of current window ; Left index and size of the widest window ; Count of zeroes in current window ; While right boundary of current window doesn 't cross  right end ; If zero count of current window is less than m , widen the window toward right ; If zero count of current window is more than m , reduce the window from left ; Update widest window if this window size is more ; Print positions of zeroes in the widest window ; Driver method to test the above function
class Test { static int arr [ ] = new int [ ] { 1 , 0 , 0 , 1 , 1 , 0 , 1 , 0 , 1 , 1 } ; static void findZeroes ( int m ) { int wL = 0 , wR = 0 ; int bestL = 0 , bestWindow = 0 ; int zeroCount = 0 ; while ( wR < arr . length ) { if ( zeroCount <= m ) { if ( arr [ wR ] == 0 ) zeroCount ++ ; wR ++ ; } if ( zeroCount > m ) { if ( arr [ wL ] == 0 ) zeroCount -- ; wL ++ ; } if ( ( wR - wL > bestWindow ) && ( zeroCount <= m ) ) { bestWindow = wR - wL ; bestL = wL ; } } for ( int i = 0 ; i < bestWindow ; i ++ ) { if ( arr [ bestL + i ] == 0 ) System . out . print ( bestL + i + " ▁ " ) ; } } public static void main ( String [ ] args ) { int m = 2 ; System . out . println ( " Indexes ▁ of ▁ zeroes ▁ to ▁ be ▁ flipped ▁ are ▁ " ) ; findZeroes ( m ) ; } }
Count Strictly Increasing Subarrays | Java program to count number of strictly increasing subarrays ; Initialize count of subarrays as 0 ; Pick starting point ; Pick ending point ; If subarray arr [ i . . j ] is not strictly increasing , then subarrays after it , i . e . , arr [ i . . j + 1 ] , arr [ i . . j + 2 ] , ... . cannot be strictly increasing ; Driver method to test the above function
class Test { static int arr [ ] = new int [ ] { 1 , 2 , 2 , 4 } ; static int countIncreasing ( int n ) { int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( arr [ j ] > arr [ j - 1 ] ) cnt ++ ; else break ; } } return cnt ; } public static void main ( String [ ] args ) { System . out . println ( " Count ▁ of ▁ strictly ▁ increasing ▁ subarrays ▁ is ▁ " + countIncreasing ( arr . length ) ) ; } }
Count Strictly Increasing Subarrays | Java program to count number of strictly increasing subarrays ; Initialize result ; Initialize length of current increasing subarray ; Traverse through the array ; If arr [ i + 1 ] is greater than arr [ i ] , then increment length ; Else Update count and reset length ; If last length is more than 1 ; Driver method to test the above function
class Test { static int arr [ ] = new int [ ] { 1 , 2 , 2 , 4 } ; static int countIncreasing ( int n ) { int cnt = 0 ; int len = 1 ; for ( int i = 0 ; i < n - 1 ; ++ i ) { if ( arr [ i + 1 ] > arr [ i ] ) len ++ ; else { cnt += ( ( ( len - 1 ) * len ) / 2 ) ; len = 1 ; } } if ( len > 1 ) cnt += ( ( ( len - 1 ) * len ) / 2 ) ; return cnt ; } public static void main ( String [ ] args ) { System . out . println ( " Count ▁ of ▁ strictly ▁ increasing ▁ subarrays ▁ is ▁ " + countIncreasing ( arr . length ) ) ; } }
Maximum difference between group of k | java to find maximum group difference ; utility function for array sum ; function for finding maximum group difference of array ; sort the array ; find array sum ; difference for k - smallest diff1 = ( arraysum - k_smallest ) - k_smallest ; reverse array for finding sum of 1 st k - largest ; difference for k - largest diff2 = ( arraysum - k_largest ) - k_largest ; return maximum difference value ; driver program
import java . util . Arrays ; public class GFG { static long arraySum ( int arr [ ] , int n ) { long sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum = sum + arr [ i ] ; return sum ; } static long maxDiff ( int arr [ ] , int n , int k ) { Arrays . sort ( arr ) ; long arraysum = arraySum ( arr , n ) ; long diff1 = Math . abs ( arraysum - 2 * arraySum ( arr , k ) ) ; int end = arr . length - 1 ; int start = 0 ; while ( start < end ) { int temp = arr [ start ] ; arr [ start ] = arr [ end ] ; arr [ end ] = temp ; start ++ ; end -- ; } long diff2 = Math . abs ( arraysum - 2 * arraySum ( arr , k ) ) ; return ( Math . max ( diff1 , diff2 ) ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 7 , 4 , 8 , - 1 , 5 , 2 , 1 } ; int n = arr . length ; int k = 3 ; System . out . println ( " Maximum ▁ Difference ▁ = ▁ " + maxDiff ( arr , n , k ) ) ; } }
Minimum number of elements to add to make median equals x | Java program to find minimum number of elements needs to add to the array so that its median equals x . ; Returns count of elements to be added to make median x . This function assumes that a [ ] has enough extra space . ; to sort the array in increasing order . ; Driver code
import java . util . Arrays ; class GFG { static int minNumber ( int a [ ] , int n , int x ) { Arrays . sort ( a ) ; int k ; for ( k = 0 ; a [ ( n ) / 2 ] != x ; k ++ ) { a [ n ++ ] = x ; Arrays . sort ( a ) ; } return k ; } public static void main ( String [ ] args ) { int x = 10 ; int a [ ] = { 10 , 20 , 30 } ; int n = 3 ; System . out . println ( minNumber ( a , n - 1 , x ) ) ; } }
Minimum number of elements to add to make median equals x | Java program to find minimum number of elements to add so that its median equals x . ; no . of elements equals to x , that is , e . ; no . of elements greater than x , that is , h . ; no . of elements smaller than x , that is , l . ; subtract the no . of elements that are equal to x . ; Driven Program
import java . util . * ; import java . lang . * ; class GFG { public static int minNumber ( int a [ ] , int n , int x ) { int l = 0 , h = 0 , e = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == x ) e ++ ; else if ( a [ i ] > x ) h ++ ; else if ( a [ i ] < x ) l ++ ; } int ans = 0 ; if ( l > h ) ans = l - h ; else if ( l < h ) ans = h - l - 1 ; return ans + 1 - e ; } public static void main ( String [ ] args ) { int x = 10 ; int a [ ] = { 10 , 20 , 30 } ; int n = a . length ; System . out . println ( minNumber ( a , n , x ) ) ; } }
Find whether a subarray is in form of a mountain or not | Java program to check whether a subarray is in mountain form or not ; Utility method to construct left and right array ; initialize first left index as that index only ; if current value is greater than previous , update last increasing ; initialize last right index as that index only ; if current value is greater than next , update first decreasing ; method returns true if arr [ L . . R ] is in mountain form ; return true only if right at starting range is greater than left at ending range ; Driver Code
class SubArray { static void preprocess ( int arr [ ] , int N , int left [ ] , int right [ ] ) { left [ 0 ] = 0 ; int lastIncr = 0 ; for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ i ] > arr [ i - 1 ] ) lastIncr = i ; left [ i ] = lastIncr ; } right [ N - 1 ] = N - 1 ; int firstDecr = N - 1 ; for ( int i = N - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] > arr [ i + 1 ] ) firstDecr = i ; right [ i ] = firstDecr ; } } static boolean isSubarrayMountainForm ( int arr [ ] , int left [ ] , int right [ ] , int L , int R ) { return ( right [ L ] >= left [ R ] ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 2 , 4 , 4 , 6 , 3 , 2 } ; int N = arr . length ; int left [ ] = new int [ N ] ; int right [ ] = new int [ N ] ; preprocess ( arr , N , left , right ) ; int L = 0 ; int R = 2 ; if ( isSubarrayMountainForm ( arr , left , right , L , R ) ) System . out . println ( " Subarray ▁ is ▁ in ▁ mountain ▁ form " ) ; else System . out . println ( " Subarray ▁ is ▁ not ▁ in ▁ mountain ▁ form " ) ; L = 1 ; R = 3 ; if ( isSubarrayMountainForm ( arr , left , right , L , R ) ) System . out . println ( " Subarray ▁ is ▁ in ▁ mountain ▁ form " ) ; else System . out . println ( " Subarray ▁ is ▁ not ▁ in ▁ mountain ▁ form " ) ; } }
Range sum queries without updates | Java program to find sum between two indexes when there is no update . ; Returns sum of elements in arr [ i . . j ] It is assumed that i <= j ; Driver code ; Function call
import java . util . * ; import java . lang . * ; class GFG { public static void preCompute ( int arr [ ] , int n , int pre [ ] ) { pre [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) pre [ i ] = arr [ i ] + pre [ i - 1 ] ; } public static int rangeSum ( int i , int j , int pre [ ] ) { if ( i == 0 ) return pre [ j ] ; return pre [ j ] - pre [ i - 1 ] ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . length ; int pre [ ] = new int [ n ] ; preCompute ( arr , n , pre ) ; System . out . println ( rangeSum ( 1 , 3 , pre ) ) ; System . out . println ( rangeSum ( 2 , 4 , pre ) ) ; } }