codeparrot/xlcost-text-to-code huggingface dataset:
Find the Alpha Score of the Given Steps ( Using BST ) | Java Program to implement the above approach ; Structure of a node ; Function to calculate and return the Alpha Score of the journey ; Traverse left subtree ; Calculate the alpha score of the current step ; Update alpha score of the journey ; Traverse right subtree ; Return ; Function to construct a BST from the sorted array arr [ ] ; Insert root ; Construct left subtree ; Construct right subtree ; Return root ; Driver Code ; Sort the array ; Construct BST from the sorted array
import java . lang . * ; import java . util . * ; class Node { Node left , right ; int data ; public Node ( int data ) { this . data = data ; left = null ; right = null ; } } class AlphaScore { Node root ; AlphaScore ( ) { root = null ; } static long sum = 0 , total_sum = 0 ; static long mod = 1000000007 ; public static long getAlphaScore ( Node node ) { if ( node . left != null ) getAlphaScore ( node . left ) ; sum = ( sum + node . data ) % mod ; total_sum = ( total_sum + sum ) % mod ; if ( node . right != null ) getAlphaScore ( node . right ) ; return total_sum ; } public static Node constructBST ( int [ ] arr , int start , int end , Node root ) { if ( start > end ) return null ; int mid = ( start + end ) / 2 ; if ( root == null ) root = new Node ( arr [ mid ] ) ; root . left = constructBST ( arr , start , mid - 1 , root . left ) ; root . right = constructBST ( arr , mid + 1 , end , root . right ) ; return root ; } public static void main ( String args [ ] ) { int arr [ ] = { 10 , 11 , 12 } ; int length = arr . length ; Arrays . sort ( arr ) ; Node root = null ; root = constructBST ( arr , 0 , length - 1 , root ) ; System . out . println ( getAlphaScore ( root ) ) ; } }
Sorting element of an array by frequency in decreasing order | Java program to sort an array in decreasing order of their frequency ; Function that return the index upto all the array elements are updated . ; Initialise maxE = - 1 ; Find the maximum element of arr [ ] ; Create frequency array freq [ ] ; Update the frequency array as per the occurrence of element in arr [ ] ; Initialise cnt to 0 ; Traversing freq [ ] ; If freq of an element is greater than 0 update the value of arr [ ] at index cnt & increment cnt ; Return cnt ; Function that print array arr [ ] elements in sorted order ; Traversing arr [ ] till index cnt ; Find frequency of elements ; Find value at index i ; Traversing till frequency to print value at index i ; Driver code ; Size of array arr [ ] ; Function call to get cnt ; Sort the arr [ ] in decreasing order ; Function that prints elements in decreasing order
import java . util . * ; class GFG { static int sortByFreq ( Integer [ ] arr , int n ) { int maxE = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { maxE = Math . max ( maxE , arr [ i ] ) ; } int freq [ ] = new int [ maxE + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) { freq [ arr [ i ] ] ++ ; } int cnt = 0 ; for ( int i = 0 ; i <= maxE ; i ++ ) { if ( freq [ i ] > 0 ) { int value = 100000 - i ; arr [ cnt ] = 100000 * freq [ i ] + value ; cnt ++ ; } } return cnt ; } static void printSortedArray ( Integer [ ] arr , int cnt ) { for ( int i = 0 ; i < cnt ; i ++ ) { int frequency = arr [ i ] / 100000 ; int value = 100000 - ( arr [ i ] % 100000 ) ; for ( int j = 0 ; j < frequency ; j ++ ) { System . out . print ( value + " ▁ " ) ; } } } public static void main ( String [ ] args ) { Integer arr [ ] = { 4 , 4 , 5 , 6 , 4 , 2 , 2 , 8 , 5 } ; int n = arr . length ; int cnt = sortByFreq ( arr , n ) ; Arrays . sort ( arr , Collections . reverseOrder ( ) ) ; printSortedArray ( arr , cnt ) ; } }
Check if N rectangles of equal area can be formed from ( 4 * N ) integers | Java implementation of the approach ; Function to check whether we can make n rectangles of equal area ; Sort the array ; Find the area of any one rectangle ; Check whether we have two equal sides for each rectangle and that area of each rectangle formed is the same ; Update the answer to false if any condition fails ; If possible ; Driver code
import java . util . * ; class GFG { static boolean checkRectangles ( int [ ] arr , int n ) { boolean ans = true ; Arrays . sort ( arr ) ; int area = arr [ 0 ] * arr [ 4 * n - 1 ] ; for ( int i = 0 ; i < 2 * n ; i = i + 2 ) { if ( arr [ i ] != arr [ i + 1 ] arr [ 4 * n - i - 1 ] != arr [ 4 * n - i - 2 ] arr [ i ] * arr [ 4 * n - i - 1 ] != area ) { ans = false ; break ; } } if ( ans ) return true ; return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 8 , 2 , 1 , 2 , 4 , 4 , 8 } ; int n = 2 ; if ( checkRectangles ( arr , n ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
Count of elements which are not at the correct position | Java implementation of the approach ; Function to return the count of elements which are not in the correct position when sorted ; To store a copy of the original array ; Copy the elements of the given array to the new array ; To store the required count ; Sort the original array ; If current element was not at the right position ; Driver code
import java . util . * ; class GFG { static int cntElements ( int arr [ ] , int n ) { int copy_arr [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) copy_arr [ i ] = arr [ i ] ; int count = 0 ; Arrays . sort ( arr ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] != copy_arr [ i ] ) { count ++ ; } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 6 , 2 , 4 , 5 } ; int n = arr . length ; System . out . println ( cntElements ( arr , n ) ) ; } }
Find k ordered pairs in array with minimum difference d | Java implementation of the approach ; Function to find the required pairs ; There has to be atleast 2 * k elements ; To store the pairs ; Sort the given array ; For every possible pair ; If the current pair is valid ; Insert it into the pair vector ; If k pairs are not possible ; Print the pairs ; Driver code
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void findPairs ( int arr [ ] , int n , int k , int d ) { if ( n < 2 * k ) { System . out . print ( - 1 ) ; return ; } Vector < pair > pairs = new Vector < pair > ( ) ; Arrays . sort ( arr ) ; for ( int i = 0 ; i < k ; i ++ ) { if ( arr [ n - k + i ] - arr [ i ] >= d ) { pair p = new pair ( arr [ i ] , arr [ n - k + i ] ) ; pairs . add ( p ) ; } } if ( pairs . size ( ) < k ) { System . out . print ( - 1 ) ; return ; } for ( pair v : pairs ) { System . out . println ( " ( " + v . first + " , ▁ " + v . second + " ) " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 6 , 10 , 23 , 14 , 7 , 2 , 20 , 9 } ; int n = arr . length ; int k = 4 , d = 3 ; findPairs ( arr , n , k , d ) ; } }
Count pairs with given sum | Set 2 | Java implementation of the approach ; Function to return the count of pairs from arr [ ] with the given sum ; To store the count of pairs ; Sort the given array ; Take two pointers ; If sum is greater ; If sum is lesser ; If sum is equal ; Find the frequency of arr [ i ] ; Find the frequency of arr [ j ] ; If arr [ i ] and arr [ j ] are same then remove the extra number counted ; Return the required answer ; Driver code
import java . util . Arrays ; import java . io . * ; class GFG { static int pairs_count ( int arr [ ] , int n , int sum ) { int ans = 0 ; Arrays . sort ( arr ) ; int i = 0 , j = n - 1 ; while ( i < j ) { if ( arr [ i ] + arr [ j ] < sum ) i ++ ; else if ( arr [ i ] + arr [ j ] > sum ) j -- ; else { int x = arr [ i ] , xx = i ; while ( ( i < j ) && ( arr [ i ] == x ) ) i ++ ; int y = arr [ j ] , yy = j ; while ( ( j >= i ) && ( arr [ j ] == y ) ) j -- ; if ( x == y ) { int temp = i - xx + yy - j - 1 ; ans += ( temp * ( temp + 1 ) ) / 2 ; } else ans += ( i - xx ) * ( yy - j ) ; } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 7 , 5 , - 1 } ; int n = arr . length ; int sum = 6 ; System . out . println ( pairs_count ( arr , n , sum ) ) ; } }
Check if the string contains consecutive letters and each letter occurs exactly once | Java program to implement the above approach ; for all the characters of the string ; find the ascii value of the character ; check if if its a valid character , if not then return false ; calculate sum of all the characters ascii values ; find minimum ascii value from the string ; find maximum ascii value from the string ; To get the previous element of the minimum ASCII value ; take the expected sum from the above equation ; check if the expected sum is equals to the calculated sum or not ; Driver code ; 1 st example ; 2 nd example
public class GFG { public static boolean check ( String str ) { int min = Integer . MAX_VALUE ; int max = Integer . MIN_VALUE ; int sum = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { int ascii = ( int ) str . charAt ( i ) ; if ( ascii < 96 ascii > 122 ) return false ; sum += ascii ; if ( min > ascii ) min = ascii ; if ( max < ascii ) max = ascii ; } min -= 1 ; int eSum = ( ( max * ( max + 1 ) ) / 2 ) - ( ( min * ( min + 1 ) ) / 2 ) ; return sum == eSum ; } public static void main ( String [ ] args ) { String str = " dcef " ; if ( check ( str ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; String str1 = " xyza " ; if ( check ( str1 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
k | Java implementation of the above approach ; Function to find the sum of minimum of all subarrays ; Insert all the elements in a set ; Find the maximum and minimum element ; Traverse from the minimum to maximum element ; Check if " i " is missing ; Check if it is kth missing ; If no kth element is missing ; Driver code
import java . util . * ; class GFG { static int findKth ( int arr [ ] , int n , int k ) { HashSet < Integer > missing = new HashSet < > ( ) ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { missing . add ( arr [ i ] ) ; } int maxm = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; int minm = Arrays . stream ( arr ) . min ( ) . getAsInt ( ) ; for ( int i = minm + 1 ; i < maxm ; i ++ ) { if ( ! missing . contains ( i ) ) { count ++ ; } if ( count == k ) { return i ; } } return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 10 , 9 , 4 } ; int n = arr . length ; int k = 5 ; System . out . println ( findKth ( arr , n , k ) ) ; } }
Sort Linked List containing values from 1 to N | Java program to sort linked list containing values from 1 to N ; Link list node ; Function to sort linked list ; Function to add a node at the beginning of Linked List ; allocate node ; put in the data ; link the old list off the new node ; move the head to point to the new node ; This function prints contents of linked list starting from the given node ; Driver Code ; The constructed linked list is : 3 -> 5 -> 4 -> 6 -> 1 -> 2
import java . util . * ; class GFG { static class Node { int data ; Node next ; } ; static Node start ; static void sortList ( Node head ) { int startVal = 1 ; while ( head != null ) { head . data = startVal ; startVal ++ ; head = head . next ; } } static void push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head_ref ; head_ref = new_node ; start = head_ref ; } static void printList ( Node node ) { while ( node != null ) { System . out . print ( node . data + " ▁ " ) ; node = node . next ; } } public static void main ( String [ ] args ) { start = null ; push ( start , 2 ) ; push ( start , 1 ) ; push ( start , 6 ) ; push ( start , 4 ) ; push ( start , 5 ) ; push ( start , 3 ) ; sortList ( start ) ; printList ( start ) ; } }
Check if linked list is sorted ( Iterative and Recursive ) | Java program to recursively check Linked List is sorted in descending order or not ; Linked list node ; function to Check Linked List is sorted in descending order or not ; Base cases ; Check first two nodes and recursively check remaining . ; Driver program to test above
class GfG { static class Node { int data ; Node next ; } static boolean isSortedDesc ( Node head ) { if ( head == null head . next == null ) return true ; return ( head . data > head . next . data && isSortedDesc ( head . next ) ) ; } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . next = null ; temp . data = data ; return temp ; } public static void main ( String [ ] args ) { Node head = newNode ( 7 ) ; head . next = newNode ( 5 ) ; head . next . next = newNode ( 4 ) ; head . next . next . next = newNode ( 3 ) ; if ( isSortedDesc ( head ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Rearrange an array to minimize sum of product of consecutive pair elements | Java program to sort an array such that sum of product of alternate element is minimum . ; create evenArr [ ] and oddArr [ ] ; sort main array in ascending order ; Put elements in oddArr [ ] and evenArr [ ] as per desired value . ; sort evenArr [ ] in descending order ; merge both sub - array and calculate minimum sum of product of alternate elements ; Driver program
import java . util . Arrays ; import java . util . Collections ; import java . util . Comparator ; import java . util . Vector ; class GFG { static int minSum ( int arr [ ] , int n ) { Vector < Integer > evenArr = new Vector < > ( ) ; Vector < Integer > oddArr = new Vector < > ( ) ; Arrays . sort ( arr ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( i < n / 2 ) { oddArr . add ( arr [ i ] ) ; } else { evenArr . add ( arr [ i ] ) ; } } Comparator comparator = Collections . reverseOrder ( ) ; Collections . sort ( evenArr , comparator ) ; int i = 0 , sum = 0 ; for ( int j = 0 ; j < evenArr . size ( ) ; j ++ ) { arr [ i ++ ] = evenArr . get ( j ) ; arr [ i ++ ] = oddArr . get ( j ) ; sum += evenArr . get ( j ) * oddArr . get ( j ) ; } return sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 8 , 9 , 6 , 7 , 3 , 4 , 2 , 0 } ; int n = arr . length ; System . out . println ( " Minimum ▁ required ▁ sum ▁ = ▁ " + minSum ( arr , n ) ) ; System . out . println ( " Sorted ▁ array ▁ in ▁ required ▁ format ▁ : ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } }
Minimum time required to print given string from a circular container based on given conditions | Java implementation for the above approach ; Function to calculate minimum time to print all characters in the string ; Current element where the pointer is pointing ; Find index of that element ; Calculate absolute difference between pointer index and character index as clockwise distance ; Subtract clockwise time from 26 to get anti - clockwise time ; Add minimum of both times to the answer ; Add one unit time to print the character ; Print the final answer ; Driver code ; Given string word ; Function call
class GFG { static void minTime ( String word ) { int ans = 0 ; int curr = 0 ; for ( int i = 0 ; i < word . length ( ) ; i ++ ) { int k = ( int ) word . charAt ( i ) - 97 ; int a = Math . abs ( curr - k ) ; int b = 26 - Math . abs ( curr - k ) ; ans += Math . min ( a , b ) ; ans ++ ; curr = ( int ) word . charAt ( i ) - 97 ; } System . out . print ( ans ) ; } public static void main ( String [ ] args ) { String str = " zjpc " ; minTime ( str ) ; } }
Minimum decrements or division by a proper divisor required to reduce N to 1 | Java program for the above approach ; Function to find the minimum number of steps required to reduce N to 1 ; Stores the number of steps required ; If the value of N is equal to 2 or N is odd ; Decrement N by 1 ; Increment cnt by 1 ; If N is even ; Update N ; Increment cnt by 1 ; Return the number of steps obtained ; Driver Code
import java . io . * ; class GFG { static int reduceToOne ( long N ) { int cnt = 0 ; while ( N != 1 ) { if ( N == 2 || ( N % 2 == 1 ) ) { N = N - 1 ; cnt ++ ; } else if ( N % 2 == 0 ) { N = N / ( N / 2 ) ; cnt ++ ; } } return cnt ; } public static void main ( String [ ] args ) { long N = 35 ; System . out . println ( reduceToOne ( N ) ) ; } }
Maximum number of diamonds that can be gained in K minutes | Java program for the above approach ; Function to find the maximum number of diamonds that can be gained in exactly K minutes ; Stores all the array elements ; Push all the elements to the priority queue ; Stores the required result ; Loop while the queue is not empty and K is positive ; Store the top element from the pq ; Pop it from the pq ; Add it to the answer ; Divide it by 2 and push it back to the pq ; Print the answer ; Driver Code
import java . util . * ; class GFG { static void maxDiamonds ( int A [ ] , int N , int K ) { PriorityQueue < Integer > pq = new PriorityQueue < > ( ( a , b ) -> b - a ) ; for ( int i = 0 ; i < N ; i ++ ) { pq . add ( A [ i ] ) ; } int ans = 0 ; while ( ! pq . isEmpty ( ) && K -- > 0 ) { int top = pq . peek ( ) ; pq . remove ( ) ; ans += top ; top = top / 2 ; pq . add ( top ) ; } System . out . print ( ans ) ; } public static void main ( String [ ] args ) { int A [ ] = { 2 , 1 , 7 , 4 , 2 } ; int K = 3 ; int N = A . length ; maxDiamonds ( A , N , K ) ; } }
Minimize cost of increments or decrements such that same indexed elements become multiple of each other | Java program for the above approach ; Function to find the minimum cost to make A [ i ] multiple of B [ i ] or vice - versa for every array element ; Stores the minimum cost ; Traverse the array ; Case 1 : Update A [ i ] ; Case 2 : Update B [ i ] ; Add the minimum of the above two cases ; Return the resultant cost ; Driver Code
import java . io . * ; class GFG { static int MinimumCost ( int A [ ] , int B [ ] , int N ) { int totalCost = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int mod_A = B [ i ] % A [ i ] ; int totalCost_A = Math . min ( mod_A , A [ i ] - mod_A ) ; int mod_B = A [ i ] % B [ i ] ; int totalCost_B = Math . min ( mod_B , B [ i ] - mod_B ) ; totalCost += Math . min ( totalCost_A , totalCost_B ) ; } return totalCost ; } public static void main ( String [ ] args ) { int A [ ] = { 3 , 6 , 3 } ; int B [ ] = { 4 , 8 , 13 } ; int N = A . length ; System . out . print ( MinimumCost ( A , B , N ) ) ; } }
Largest number divisible by 50 that can be formed from a given set of N digits consisting of 0 s and 7 s only | Java program of the above approach ; Print the largest number divisible by 50 ; Counting number of 0 s and 7 s ; If count of 7 is divisible by 50 ; If count of 7 is less than 5 ; If count of 7 is not divisible by 50 ; Count of groups of 5 in which count of 7 s can be grouped ; Driver Code ; Given array ; Size of the array
import java . io . * ; class GFG { static void printLargestDivisible ( int arr [ ] , int N ) { int i , count0 = 0 , count7 = 0 ; for ( i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 0 ) count0 ++ ; else count7 ++ ; } if ( count7 % 50 == 0 ) { while ( count7 != 0 ) { System . out . print ( 7 ) ; count7 -= 1 ; } while ( count0 != 0 ) { System . out . print ( 0 ) ; count0 -= 1 ; } } else if ( count7 < 5 ) { if ( count0 == 0 ) System . out . print ( " No " ) ; else System . out . print ( "0" ) ; } else { count7 = count7 - count7 % 5 ; while ( count7 != 0 ) { System . out . print ( 7 ) ; count7 -= 1 ; } while ( count0 != 0 ) { System . out . print ( 0 ) ; count0 -= 1 ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 7 , 0 , 7 , 7 , 7 , 7 , 0 , 0 , 0 , 0 , 0 , 0 , 7 , 7 , 7 } ; int N = arr . length ; printLargestDivisible ( arr , N ) ; } }
Rearrange array to maximize sum of GCD of array elements with their respective indices | Java program to implement the above approach ; Function to find the maximum sum of GCD ( arr [ i ] , i ) by rearranging the array ; Sort the array in ascending order ; Stores maximum sum of GCD ( arr [ i ] , i ) by rearranging the array elements ; Generate all possible permutations of the array ; Stores sum of GCD ( arr [ i ] , i ) ; Traverse the array ; Update sum ; Update res ; Driver Code
import java . util . * ; class GFG { static int findMaxValByRearrArr ( int arr [ ] , int N ) { Arrays . sort ( arr ) ; int res = 0 ; do { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += __gcd ( i + 1 , arr [ i ] ) ; } res = Math . max ( res , sum ) ; } while ( next_permutation ( arr ) ) ; return res ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } static boolean next_permutation ( int [ ] p ) { for ( int a = p . length - 2 ; a >= 0 ; -- a ) if ( p [ a ] < p [ a + 1 ] ) for ( int b = p . length - 1 ; ; -- b ) if ( p [ b ] > p [ a ] ) { int t = p [ a ] ; p [ a ] = p [ b ] ; p [ b ] = t ; for ( ++ a , b = p . length - 1 ; a < b ; ++ a , -- b ) { t = p [ a ] ; p [ a ] = p [ b ] ; p [ b ] = t ; } return true ; } return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 1 } ; int N = arr . length ; System . out . print ( findMaxValByRearrArr ( arr , N ) ) ; } }
Minimum removals required to make frequency of each array element equal to its value | Java program to implement the above approach ; Function to find the minimum count of elements required to be removed such that frequency of arr [ i ] equal to arr [ i ] ; Stores frequency of each element of the array ; Traverse the array ; Update frequency of arr [ i ] ; Stores minimum count of removals ; Traverse the map ; Stores key value of the map ; If frequency of i is less than i ; Update cntMinRem ; If frequency of i is greater than i ; Update cntMinRem ; Driver Code
import java . util . * ; class GFG { public static int min_elements ( int arr [ ] , int N ) { Map < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { mp . put ( arr [ i ] , mp . getOrDefault ( arr [ i ] , 0 ) + 1 ) ; } int cntMinRem = 0 ; for ( int key : mp . keySet ( ) ) { int i = key ; int val = mp . get ( i ) ; if ( val < i ) { cntMinRem += val ; } else if ( val > i ) { cntMinRem += ( val - i ) ; } } return cntMinRem ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 1 , 4 , 2 } ; System . out . println ( min_elements ( arr , arr . length ) ) ; } }
Minimum increments to make all array elements equal with sum same as the given array after exactly one removal | Java program to implement the above approach ; Function to check if an array of equal elements with sum equal to the given array can be obtained or not ; Base case ; Stores sum of array elements ; Stores second largest array element ; Stores the largest array element ; Traverse the array ; Update secMax ; Update Max ; Update secMax ; Update totalSum ; If totalSum is less than secMax * ( N - 1 ) ) ; If totalSum is not divisible by ( N - 1 ) ; Driver Code
import java . util . * ; class GFG { static boolean CheckAllarrayEqual ( int [ ] arr , int N ) { if ( N == 1 ) { return true ; } int totalSum = arr [ 0 ] ; int secMax = Integer . MIN_VALUE ; int Max = arr [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ i ] >= Max ) { secMax = Max ; Max = arr [ i ] ; } else if ( arr [ i ] > secMax ) { secMax = arr [ i ] ; } totalSum += arr [ i ] ; } if ( ( secMax * ( N - 1 ) ) > totalSum ) { return false ; } if ( totalSum % ( N - 1 ) != 0 ) { return false ; } return true ; } public static void main ( String [ ] args ) { int [ ] arr = { 6 , 2 , 2 , 2 } ; int N = arr . length ; if ( CheckAllarrayEqual ( arr , N ) ) { System . out . print ( " YES " ) ; } else { System . out . print ( " NO " ) ; } } }