codeparrot/xlcost-text-to-code huggingface dataset:
Create loops of even and odd values in a binary tree | Java implementation to create odd and even loops in a binary tree ; structure of a node ; Utility function to create a new node ; preorder traversal to place the node pointer in the respective even_ptrs or odd_ptrs list ; place node ptr in even_ptrs list if node contains even number ; else place node ptr in odd_ptrs list ; function to create the even and odd loops ; forming even loop ; for the last element ; Similarly forming odd loop ; traversing the loop from any random node in the loop ; Driver code ; Binary tree formation ; traversing odd loop from any random odd node ; traversing even loop from any random even node
import java . util . * ; class GFG { static class Node { int data ; Node left , right , abtr ; } ; static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = node . right = node . abtr = null ; return node ; } static Vector < Node > even_ptrs = new Vector < > ( ) ; static Vector < Node > odd_ptrs = new Vector < > ( ) ; static void preorderTraversal ( Node root ) { if ( root == null ) return ; if ( root . data % 2 == 0 ) ( even_ptrs ) . add ( root ) ; else ( odd_ptrs ) . add ( root ) ; preorderTraversal ( root . left ) ; preorderTraversal ( root . right ) ; } static void createLoops ( Node root ) { preorderTraversal ( root ) ; int i ; for ( i = 1 ; i < even_ptrs . size ( ) ; i ++ ) even_ptrs . get ( i - 1 ) . abtr = even_ptrs . get ( i ) ; even_ptrs . get ( i - 1 ) . abtr = even_ptrs . get ( 0 ) ; for ( i = 1 ; i < odd_ptrs . size ( ) ; i ++ ) odd_ptrs . get ( i - 1 ) . abtr = odd_ptrs . get ( i ) ; odd_ptrs . get ( i - 1 ) . abtr = odd_ptrs . get ( 0 ) ; } static void traverseLoop ( Node start ) { Node curr = start ; do { System . out . print ( curr . data + " ▁ " ) ; curr = curr . abtr ; } while ( curr != start ) ; } public static void main ( String [ ] args ) { Node root = null ; root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 5 ) ; root . right . left = newNode ( 6 ) ; root . right . right = newNode ( 7 ) ; createLoops ( root ) ; System . out . print ( " Odd ▁ nodes : ▁ " ) ; traverseLoop ( root . right ) ; System . out . print ( " Even nodes : "); traverseLoop ( root . left ) ; } }
Area of circle which is inscribed in equilateral triangle | Java program to find the area of circle which is inscribed in equilateral triangle ; Function return the area of circle inscribed in equilateral triangle ; Driver code
import java . io . * ; class GFG { static double PI = 3.14 ; static double circle_inscribed ( int a ) { return PI * ( a * a ) / 12 ; } public static void main ( String [ ] args ) { int a = 4 ; System . out . println ( circle_inscribed ( a ) ) ; } }
Program to find the Volume of an irregular tetrahedron | Java implementation of above approach ; Function to find the volume ; Steps to calculate volume of a Tetrahedron using formula ; Driver code ; edge lengths
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static void findVolume ( double u , double v , double w , double U , double V , double W , double b ) { double uPow = Math . pow ( u , 2 ) ; double vPow = Math . pow ( v , 2 ) ; double wPow = Math . pow ( w , 2 ) ; double UPow = Math . pow ( U , 2 ) ; double VPow = Math . pow ( V , 2 ) ; double WPow = Math . pow ( W , 2 ) ; double a = 4 * ( uPow * vPow * wPow ) - uPow * Math . pow ( ( vPow + wPow - UPow ) , 2 ) - vPow * Math . pow ( ( wPow + uPow - VPow ) , 2 ) - wPow * Math . pow ( ( uPow + vPow - WPow ) , 2 ) + ( vPow + wPow - UPow ) * ( wPow + uPow - VPow ) * ( uPow + vPow - WPow ) ; double vol = Math . sqrt ( a ) ; vol /= b ; System . out . printf ( " % .4f " , vol ) ; } public static void main ( String args [ ] ) { double u = 1000 , v = 1000 , w = 1000 ; double U = 3 , V = 4 , W = 5 ; double b = 12 ; findVolume ( u , v , w , U , V , W , b ) ; } }
Check if it is possible to create a polygon with a given angle |  ; Function to check whether it is possible to make a regular polygon with a given angle . ; N denotes the number of sides of polygons possible ; Driver code ; function to print the required answer
class GFG { static void makePolygon ( double a ) { double n = 360 / ( 180 - a ) ; if ( n == ( int ) n ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } public static void main ( String [ ] args ) { double a = 90 ; makePolygon ( a ) ; } }
Finding Quadrant of a Coordinate with respect to a Circle | Java Program to find the quadrant of a given coordinate with respect to the centre of a circle ; Thus function returns the quadrant number ; Coincides with center ; Outside circle ; 1 st quadrant ; 2 nd quadrant ; 3 rd quadrant ; 4 th quadrant ; Driver Code ; Coordinates of centre ; Radius of circle ; Coordinates of the given point
import java . io . * ; class GFG { static int getQuadrant ( int X , int Y , int R , int PX , int PY ) { if ( PX == X && PY == Y ) return 0 ; int val = ( int ) Math . pow ( ( PX - X ) , 2 ) + ( int ) Math . pow ( ( PY - Y ) , 2 ) ; if ( val > Math . pow ( R , 2 ) ) return - 1 ; if ( PX > X && PY >= Y ) return 1 ; if ( PX <= X && PY > Y ) return 2 ; if ( PX < X && PY <= Y ) return 3 ; if ( PX >= X && PY < Y ) return 4 ; return 0 ; } public static void main ( String [ ] args ) { int X = 0 , Y = 3 ; int R = 2 ; int PX = 1 , PY = 4 ; int ans = getQuadrant ( X , Y , R , PX , PY ) ; if ( ans == - 1 ) System . out . println ( " Lies ▁ Outside ▁ the ▁ circle " ) ; else if ( ans == 0 ) System . out . println ( " Coincides ▁ with ▁ centre " ) ; else System . out . println ( ans + " ▁ Quadrant " ) ; } }
Hexadecagonal number | Java program to find Nth hexadecagon number ; Function to calculate hexadecagonal number ; Drivers Code
import java . io . * ; class GFG { static long hexadecagonalNum ( long n ) { return ( ( 14 * n * n ) - 12 * n ) / 2 ; } public static void main ( String [ ] args ) { long n = 5 ; System . out . println ( n + " th ▁ " + " Hexadecagonal ▁ number ▁ : ▁ " + hexadecagonalNum ( n ) ) ; n = 9 ; System . out . println ( n + " th ▁ " + " Hexadecagonal ▁ number ▁ : ▁ " + hexadecagonalNum ( n ) ) ; } }
Find first non matching leaves in two binary trees | Java program to find first leaves that are not same . ; Tree node ; Utility method to create a new node ; Prints the first non - matching leaf node in two trees if it exists , else prints nothing . ; If any of the tree is empty ; Create two stacks for preorder traversals ; If traversal of one tree is over and other tree still has nodes . ; Do iterative traversal of first tree and find first lead node in it as " temp1" ; pushing right childfirst so that left child comes first while popping . ; Do iterative traversal of second tree and find first lead node in it as " temp2" ; If we found leaves in both trees ; Driver code
import java . util . * ; class GfG { static class Node { int data ; Node left , right ; } static Node newNode ( int x ) { Node temp = new Node ( ) ; temp . data = x ; temp . left = null ; temp . right = null ; return temp ; } static boolean isLeaf ( Node t ) { return ( ( t . left == null ) && ( t . right == null ) ) ; } static void findFirstUnmatch ( Node root1 , Node root2 ) { if ( root1 == null root2 == null ) return ; Stack < Node > s1 = new Stack < Node > ( ) ; Stack < Node > s2 = new Stack < Node > ( ) ; s1 . push ( root1 ) ; s2 . push ( root2 ) ; while ( ! s1 . isEmpty ( ) || ! s2 . isEmpty ( ) ) { if ( s1 . isEmpty ( ) || s2 . isEmpty ( ) ) return ; Node temp1 = s1 . peek ( ) ; s1 . pop ( ) ; while ( temp1 != null && isLeaf ( temp1 ) != true ) { s1 . push ( temp1 . right ) ; s1 . push ( temp1 . left ) ; temp1 = s1 . peek ( ) ; s1 . pop ( ) ; } Node temp2 = s2 . peek ( ) ; s2 . pop ( ) ; while ( temp2 != null && isLeaf ( temp2 ) != true ) { s2 . push ( temp2 . right ) ; s2 . push ( temp2 . left ) ; temp2 = s2 . peek ( ) ; s2 . pop ( ) ; } if ( temp1 != null && temp2 != null ) { if ( temp1 . data != temp2 . data ) { System . out . println ( temp1 . data + " ▁ " + temp2 . data ) ; return ; } } } } public static void main ( String [ ] args ) { Node root1 = newNode ( 5 ) ; root1 . left = newNode ( 2 ) ; root1 . right = newNode ( 7 ) ; root1 . left . left = newNode ( 10 ) ; root1 . left . right = newNode ( 11 ) ; Node root2 = newNode ( 6 ) ; root2 . left = newNode ( 10 ) ; root2 . right = newNode ( 15 ) ; findFirstUnmatch ( root1 , root2 ) ; } }
Maximum points of intersection n circles | Java program to find maximum umber of intersections of n circles ; for the calculation of 2 * ( nC2 ) ; Driver code
import java . io . * ; public class GFG { static int intersection ( int n ) { return n * ( n - 1 ) ; } public static void main ( String [ ] args ) throws IOException { System . out . println ( intersection ( 3 ) ) ; } }
Find the perimeter of a cylinder | Java program to find perimeter of cylinder ; Function to calculate perimeter ; Driver program to test above function
import java . io . * ; class GFG { static int perimeter ( int diameter , int height ) { return 2 * ( diameter + height ) ; } public static void main ( String [ ] args ) { int diameter = 5 ; int height = 10 ; System . out . println ( " Perimeter ▁ = ▁ " + perimeter ( diameter , height ) + " unitsNEW_LINE"); } }
Find all possible coordinates of parallelogram | Java program to all possible points of a parallelogram ; Driver code ; coordinates of A ; coordinates of B ; coordinates of C
public class ParallelogramPoints { public static void main ( String [ ] s ) { int ax = 5 , ay = 0 ; int bx = 1 , by = 1 ; int cx = 2 , cy = 5 ; System . out . println ( ax + bx - cx + " , ▁ " + ( ay + by - cy ) ) ; System . out . println ( ax + cx - bx + " , ▁ " + ( ay + cy - by ) ) ; System . out . println ( cx + bx - ax + " , ▁ " + ( cy + by - ax ) ) ; } }
Check whether a given point lies inside a rectangle or not |  ; A utility function to calculate area of triangle formed by ( x1 , y1 ) , ( x2 , y2 ) and ( x3 , y3 ) ; A function to check whether point P ( x , y ) lies inside the rectangle formed by A ( x1 , y1 ) , B ( x2 , y2 ) , C ( x3 , y3 ) and D ( x4 , y4 ) ; Calculate area of rectangle ABCD ; Calculate area of triangle PAB ; Calculate area of triangle PBC ; Calculate area of triangle PCD ; Calculate area of triangle PAD ; Check if sum of A1 , A2 , A3 and A4 is same as A ; Driver code ; Let us check whether the point P ( 10 , 15 ) lies inside the rectangle formed by A ( 0 , 10 ) , B ( 10 , 0 ) C ( 0 , - 10 ) D ( - 10 , 0 )
class GFG { static float area ( int x1 , int y1 , int x2 , int y2 , int x3 , int y3 ) { return ( float ) Math . abs ( ( x1 * ( y2 - y3 ) + x2 * ( y3 - y1 ) + x3 * ( y1 - y2 ) ) / 2.0 ) ; } static boolean check ( int x1 , int y1 , int x2 , int y2 , int x3 , int y3 , int x4 , int y4 , int x , int y ) { float A = area ( x1 , y1 , x2 , y2 , x3 , y3 ) + area ( x1 , y1 , x4 , y4 , x3 , y3 ) ; float A1 = area ( x , y , x1 , y1 , x2 , y2 ) ; float A2 = area ( x , y , x2 , y2 , x3 , y3 ) ; float A3 = area ( x , y , x3 , y3 , x4 , y4 ) ; float A4 = area ( x , y , x1 , y1 , x4 , y4 ) ; return ( A == A1 + A2 + A3 + A4 ) ; } public static void main ( String [ ] args ) { if ( check ( 0 , 10 , 10 , 0 , 0 , - 10 , - 10 , 0 , 10 , 15 ) ) System . out . print ( " yes " ) ; else System . out . print ( " no " ) ; } }
Get maximum left node in binary tree | Java program to print maximum element in left node . ; A Binary Tree Node ; Get max of left element using Inorder traversal ; Return maximum of three values 1 ) Recursive max in left subtree 2 ) Value in left node 3 ) Recursive max in right subtree ; Utility function to create a new tree node ; Driver program to test above functions ; Let us create binary tree shown in above diagram ; 7 / \ 6 5 / \ / \ 4 3 2 1
import java . util . * ; class GfG { static class Node { int data ; Node left , right ; } static int maxOfLeftElement ( Node root ) { int res = Integer . MIN_VALUE ; if ( root == null ) return res ; if ( root . left != null ) res = root . left . data ; return Math . max ( maxOfLeftElement ( root . left ) , Math . max ( res , maxOfLeftElement ( root . right ) ) ) ; } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = null ; temp . right = null ; return temp ; } public static void main ( String [ ] args ) { Node root = newNode ( 7 ) ; root . left = newNode ( 6 ) ; root . right = newNode ( 5 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 3 ) ; root . right . left = newNode ( 2 ) ; root . right . right = newNode ( 1 ) ; System . out . println ( maxOfLeftElement ( root ) ) ; } }
Pizza cut problem ( Or Circle Division by Lines ) | Java program to find maximum no of pieces by given number of cuts ; Function for finding maximum pieces with n cuts . ; Driver Program to test above function
class GFG { static int findMaximumPieces ( int n ) { return 1 + n * ( n + 1 ) / 2 ; } public static void main ( String arg [ ] ) { System . out . print ( findMaximumPieces ( 3 ) ) ; } }
Optimum location of point to minimize total distance | A Java program to find optimum location and total cost ; structure defining a point ; structure defining a line of ax + by + c = 0 form ; method to get distance of point ( x , y ) from point p ; Utility method to compute total distance all points when choose point on given line has x - coordinate value as X ; calculating Y of chosen point by line equation ; Utility method to find minimum total distance ; loop until difference between low and high become less than EPS ; mid1 and mid2 are representative x co - ordiantes of search space ; if mid2 point gives more total distance , skip third part ; if mid1 point gives more total distance , skip first part ; compute optimum distance cost by sending average of low and high as X ; method to find optimum cost ; converting 2D array input to point array ; Driver Code
class GFG { static double sq ( double x ) { return ( ( x ) * ( x ) ) ; } static int EPS = ( int ) ( 1e-6 ) + 1 ; static int N = 5 ; static class point { int x , y ; point ( ) { } public point ( int x , int y ) { this . x = x ; this . y = y ; } } ; static class line { int a , b , c ; public line ( int a , int b , int c ) { this . a = a ; this . b = b ; this . c = c ; } } ; static double dist ( double x , double y , point p ) { return Math . sqrt ( sq ( x - p . x ) + sq ( y - p . y ) ) ; } static double compute ( point p [ ] , int n , line l , double X ) { double res = 0 ; double Y = - 1 * ( l . c + l . a * X ) / l . b ; for ( int i = 0 ; i < n ; i ++ ) res += dist ( X , Y , p [ i ] ) ; return res ; } static double findOptimumCostUtil ( point p [ ] , int n , line l ) { double low = - 1e6 ; double high = 1e6 ; while ( ( high - low ) > EPS ) { double mid1 = low + ( high - low ) / 3 ; double mid2 = high - ( high - low ) / 3 ; double dist1 = compute ( p , n , l , mid1 ) ; double dist2 = compute ( p , n , l , mid2 ) ; if ( dist1 < dist2 ) high = mid2 ; else low = mid1 ; } return compute ( p , n , l , ( low + high ) / 2 ) ; } static double findOptimumCost ( int points [ ] [ ] , line l ) { point [ ] p = new point [ N ] ; for ( int i = 0 ; i < N ; i ++ ) p [ i ] = new point ( points [ i ] [ 0 ] , points [ i ] [ 1 ] ) ; return findOptimumCostUtil ( p , N , l ) ; } public static void main ( String [ ] args ) { line l = new line ( 1 , - 1 , - 3 ) ; int points [ ] [ ] = { { - 3 , - 2 } , { - 1 , 0 } , { - 1 , 2 } , { 1 , 2 } , { 3 , 4 } } ; System . out . println ( findOptimumCost ( points , l ) ) ; } }
Height of Factor Tree for a given number | Java program for the above approach ; Function to find the height of the Factor Tree of the integer N ; Stores the height of Factor Tree ; Loop to iterate over values of N ; Stores if there exist a factor of N or not ; Loop to find the smallest factor of N ; If i is a factor of N ; Increment the height ; If there are no factors of N i . e , N is prime , break loop ; Return Answer ; Driver Code
public class Main { static int factorTree ( int N ) { int height = 0 ; while ( N > 1 ) { boolean flag = false ; for ( int i = 2 ; i <= Math . sqrt ( N ) ; i ++ ) { if ( N % i == 0 ) { N = N / i ; flag = true ; break ; } } height ++ ; if ( ! flag ) { break ; } } return height ; } public static void main ( String [ ] args ) { int N = 48 ; System . out . println ( factorTree ( N ) ) ; } }
Maximize the largest number K such that bitwise and of K till N is 0 | package whatever do not write package name here ; Function to find maximum value of k which makes bitwise AND zero . ; Finding the power less than N ; Driver Code
import java . io . * ; class GFG { static int findMaxK ( int N ) { int p = ( int ) ( Math . log ( N ) / Math . log ( 2 ) ) ; return ( int ) Math . pow ( 2 , p ) ; } public static void main ( String [ ] args ) { int N = 5 ; System . out . println ( findMaxK ( N ) - 1 ) ; } }
Count of Ks in the Array for a given range of indices after array updates for Q queries | Java program for the above approach ; Function to build the segment tree ; Base case ; Since the count of zero is required set leaf node as 1 ; If the value in array is not zero , store 0 in the leaf node ; Find the mid ; Recursive call for left subtree ; Recursive call for right subtree ; Parent nodes contains the count of zero in range tl to tr ; Function to find the count of 0 s in range l to r ; Base Case ; Case when no two segment are combining ; Finding the mid ; When it is required to combine left subtree and right subtree to get the range l to r ; Function that updates the segment tree nodes ; Base Case ; If array element is 0 ; If array element is not 0 ; Otherwise ; Find the mid ; Update the tree or count which is stored in parent node ; Function to solve all the queries ; When query type is 1 ; When query type is 2 ; Driver Code
import java . util . * ; public class Main { static int [ ] a ; static int [ ] seg_tree ; static int [ ] [ ] query ; static void build_tree ( int v , int tl , int tr ) { if ( tl != tr ) { if ( a [ tl ] == 0 ) seg_tree [ v ] = 1 ; else seg_tree [ v ] = 0 ; } else { int tm = ( tl + tr ) / 2 ; build_tree ( v * 2 , tl , tm ) ; build_tree ( v * 2 + 1 , tm + 1 , tr ) ; seg_tree [ v ] = seg_tree [ v * 2 ] + seg_tree [ v * 2 + 1 ] ; } } static int frequency_zero ( int v , int tl , int tr , int l , int r ) { if ( l > r ) return 0 ; if ( l == tl && r == tr ) { return seg_tree [ v ] ; } int tm = ( tl + tr ) / 2 ; return frequency_zero ( v * 2 , tl , tm , l , Math . min ( r , tm ) ) + frequency_zero ( v * 2 + 1 , tm + 1 , tr , Math . max ( l , tm + 1 ) , r ) ; } static void update ( int v , int tl , int tr , int pos , int new_val ) { if ( tl == tr ) { if ( new_val == 0 ) seg_tree [ v ] = 1 ; else seg_tree [ v ] = 0 ; } else { int tm = ( tl + tr ) / 2 ; if ( pos <= tm ) update ( v * 2 , tl , tm , pos , new_val ) ; else update ( v * 2 + 1 , tm + 1 , tr , pos , new_val ) ; seg_tree [ v ] = seg_tree [ v * 2 ] + seg_tree [ v * 2 + 1 ] ; } } static void solve ( int n , int q ) { int [ ] qu = { 5 , 3 , 6 } ; seg_tree = new int [ 4 * n + 1 ] ; Arrays . fill ( seg_tree , 0 ) ; build_tree ( 1 , 0 , n - 1 ) ; for ( int i = 0 ; i < qu . length ; i ++ ) { System . out . println ( qu [ i ] ) ; } for ( int i = q ; i < q ; i ++ ) { if ( query [ i - 1 ] [ 0 ] == 1 ) { int l = query [ i - 1 ] [ 1 ] ; int r = query [ i - 1 ] [ 2 ] ; System . out . println ( frequency_zero ( 1 , 0 , n - 1 , l , r ) ) ; } else { a [ query [ i - 1 ] [ 1 ] ] = query [ i - 1 ] [ 2 ] ; int pos = query [ i - 1 ] [ 1 ] ; int new_val = query [ i - 1 ] [ 2 ] ; update ( 1 , 0 , n - 1 , pos , new_val ) ; } } } public static void main ( String [ ] args ) { a = new int [ ] { 9 , 5 , 7 , 6 , 9 , 0 , 0 , 0 , 0 , 5 , 6 , 7 , 3 , 9 , 0 , 7 , 0 , 9 , 0 } ; int Q = 5 ; query = new int [ ] [ ] { { 1 , 5 , 14 } , { 2 , 6 , 1 } , { 1 , 0 , 8 } , { 2 , 13 , 0 } , { 1 , 6 , 18 } } ; int N = a . length ; solve ( N , Q ) ; } }
Find a number in minimum steps | Java program to find a number in minimum steps ; To represent data of a node in tree ; Prints level of node n ; Create a queue and insert root ; Do level order traversal ; Remove a node from queue ; To astatic void infinite loop ; Check if dequeued number is same as n ; Insert children of dequeued node to queue ; Driver code
import java . util . * ; class GFG { static final int InF = 99999 ; static class number { int no ; int level ; number ( ) { } number ( int n , int l ) { this . no = n ; this . level = l ; } } ; static void findnthnumber ( int n ) { Queue < number > q = new LinkedList < > ( ) ; number r = new number ( 0 , 1 ) ; q . add ( r ) ; while ( ! q . isEmpty ( ) ) { number temp = q . peek ( ) ; q . remove ( ) ; if ( temp . no >= InF temp . no <= - InF ) break ; if ( temp . no == n ) { System . out . print ( " Found ▁ number ▁ n ▁ at ▁ level ▁ " + ( temp . level - 1 ) ) ; break ; } q . add ( new number ( temp . no + temp . level , temp . level + 1 ) ) ; q . add ( new number ( temp . no - temp . level , temp . level + 1 ) ) ; } } public static void main ( String [ ] args ) { findnthnumber ( 13 ) ; } }
Check if all 3 Candy bags can be emptied by removing 2 candies from any one bag and 1 from the other two repeatedly | Java code for the above approach ; If total candies are not multiple of 4 then its not possible to be left with 0 candies ; If minimum candies of three bags are less than number of operations required then the task is not possible ; Driver Code
import java . util . * ; class GFG { static boolean can_empty ( int a , int b , int c ) { if ( ( a + b + c ) % 4 != 0 ) return false ; else { int m = Math . min ( a , Math . min ( b , c ) ) ; if ( m < ( ( a + b + c ) / 4 ) ) return false ; } return true ; } public static void main ( String [ ] args ) { int a = 4 , b = 2 , c = 2 ; System . out . println ( can_empty ( a , b , c ) ? " true " : " false " ) ; a = 3 ; b = 4 ; c = 2 ; System . out . println ( can_empty ( a , b , c ) ? " true " : " false " ) ; } }
Difference between maximum and minimum average of all K | Java implementation of the above approach ; Function to find the difference between averages of the maximum and the minimum subarrays of length k ; Stores min and max sum ; Iterate through starting points ; Sum up next K elements ; Update max and min moving sum ; Return the difference between max and min average ; Driver Code ; Given Input ; Function Call
import java . io . * ; class GFG { static double Avgdifference ( double arr [ ] , int N , int K ) { double min = 1000000 , max = - 1 ; for ( int i = 0 ; i <= N - K ; i ++ ) { double sum = 0 ; for ( int j = 0 ; j < K ; j ++ ) { sum += arr [ i + j ] ; } if ( min > sum ) min = sum ; if ( max < sum ) max = sum ; } return ( max - min ) / K ; } public static void main ( String [ ] args ) { double arr [ ] = { 3 , 8 , 9 , 15 } ; int N = arr . length ; int K = 2 ; System . out . println ( Avgdifference ( arr , N , K ) ) ; } }