codeparrot/xlcost-text-to-code huggingface dataset:
Koko Eating Bananas | Java implementation for the above approach ; to get the ceil value ; in case of odd number ; in case of even number ; check if time is less than or equals to given hour ; as minimum speed of eating must be 1 ; Maximum speed of eating is the maximum bananas in given piles ; Check if the mid ( hours ) is valid ; If valid continue to search lower speed ; If cant finish bananas in given hours , then increase the speed ; Driver code
import java . util . * ; class GFG { static boolean check ( int [ ] bananas , int mid_val , int H ) { int time = 0 ; for ( int i = 0 ; i < bananas . length ; i ++ ) { if ( bananas [ i ] % mid_val != 0 ) { time += ( ( bananas [ i ] / mid_val ) + 1 ) ; } else { time += ( bananas [ i ] / mid_val ) ; } } if ( time <= H ) { return true ; } else { return false ; } } static int minEatingSpeed ( int [ ] piles , int H ) { int start = 1 ; int end = Arrays . stream ( piles ) . max ( ) . getAsInt ( ) ; while ( start < end ) { int mid = start + ( end - start ) / 2 ; if ( ( check ( piles , mid , H ) ) == true ) { end = mid ; } else { start = mid + 1 ; } } return end ; } public static void main ( String [ ] args ) { int [ ] piles = { 30 , 11 , 23 , 4 , 20 } ; int H = 6 ; System . out . print ( minEatingSpeed ( piles , H ) ) ; } }
Minimize the sum of pair which upon removing divides the Array into 3 subarrays | Java implementation of the above approach ; Function to find minimum possible sum of pair which breaks the array into 3 non - empty subarrays ; prefixMin [ i ] contains minimum element till i ; Driver Code ; Given array
class GFG { static int minSumPair ( int arr [ ] , int N ) { if ( N < 5 ) { return - 1 ; } int [ ] prefixMin = new int [ N ] ; prefixMin [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < N - 1 ; i ++ ) { prefixMin [ i ] = Math . min ( arr [ i ] , prefixMin [ i - 1 ] ) ; } int ans = Integer . MAX_VALUE ; for ( int i = 3 ; i < N - 1 ; i ++ ) { ans = Math . min ( ans , arr [ i ] + prefixMin [ i - 2 ] ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 2 , 4 , 6 , 3 , 7 } ; int N = arr . length ; System . out . print ( minSumPair ( arr , N ) + "NEW_LINE"); } }
Minimum time remaining for safety alarm to start | Java program for the above approach ; Function to check if the value of mid as the minimum number of hours satisfies the condition ; Stores the sum of speed ; Iterate over the range [ 0 , N ] ; Find the value of speed ; If the bike is considered to be fast add it in sum ; Return the resultant sum ; Function to find the minimum number of time required ; Stores the range of Binary Search ; Stores the minimum number of time required ; Find the value of mid ; If the mid is the resultant speed required ; Update the ans and high ; Otherwise ; Return the minimum number of hours ; Driver Code
import java . io . * ; class GFG { static long check ( long H [ ] , long A [ ] , long mid , long N , long M , long L ) { long sum = 0 ; for ( long i = 0 ; i < N ; i ++ ) { long speed = mid * A [ ( int ) i ] + H [ ( int ) i ] ; if ( speed >= L ) { sum += speed ; } } return sum ; } static long buzzTime ( long N , long M , long L , long H [ ] , long A [ ] ) { long low = 0 , high = 100000000 ; long ans = 0 ; while ( high >= low ) { long mid = low + ( high - low ) / 2 ; if ( check ( H , A , mid , N , M , L ) >= M ) { ans = mid ; high = mid - 1 ; } else low = mid + 1 ; } return ans ; } public static void main ( String [ ] args ) { long M = 400 , L = 120 ; long H [ ] = { 20 , 50 , 20 } ; long A [ ] = { 20 , 70 , 90 } ; long N = A . length ; System . out . println ( buzzTime ( N , M , L , H , A ) ) ; } }
Count of different numbers divisible by 3 that can be obtained by changing at most one digit | Java program fo the above approach ; Function to count the number of possible numbers divisible by 3 ; Calculate the sum ; Store the answer ; Iterate over the range ; Decreasing the sum ; Iterate over the range ; Checking if the new sum is divisible by 3 or not ; If yes increment the value of the count ; Driver Code ; Given number
import java . io . * ; import java . util . * ; class GFG { public static void findCount ( String number ) { int sum = 0 ; for ( int i = 0 ; i < number . length ( ) ; ++ i ) { sum += number . charAt ( i ) - 48 ; } int count = 0 ; for ( int i = 0 ; i < number . length ( ) ; ++ i ) { int remaining_sum = sum - ( number . charAt ( i ) - 48 ) ; for ( int j = 0 ; j <= 9 ; ++ j ) { if ( ( remaining_sum + j ) % 3 == 0 && j != number . charAt ( i ) - 48 ) { ++ count ; } } } System . out . println ( count ) ; } public static void main ( String [ ] args ) { String number = "235" ; findCount ( number ) ; } }
Maximum number of teams of size K possible with each player from different country | Java program for the above approach ; Function to find if T number of teams can be formed or not ; Store the sum of array elements ; Traverse the array teams [ ] ; Required Condition ; Function to find the maximum number of teams possible ; Lower and Upper part of the range ; Perform the Binary Search ; Find the value of mid ; Perform the Binary Search ; Otherwise , update the search range ; Otherwise , update the search range ; Driver Code
class GFG { public static boolean is_possible ( int [ ] teams , int T , int k ) { int sum = 0 ; for ( int i = 0 ; i < teams . length ; i ++ ) { sum += Math . min ( T , teams [ i ] ) ; } return ( sum >= ( T * k ) ) ; } public static int countOfTeams ( int [ ] teams_list , int N , int K ) { int lb = 0 ; double ub = 1e9 ; while ( lb <= ub ) { int mid = lb + ( int ) ( ub - lb ) / 2 ; if ( is_possible ( teams_list , mid , K ) ) { if ( ! is_possible ( teams_list , mid + 1 , K ) ) { return mid ; } else { lb = mid + 1 ; } } else { ub = mid - 1 ; } } return 0 ; } public static void main ( String args [ ] ) { int [ ] arr = { 2 , 3 , 4 } ; int K = 2 ; int N = arr . length ; System . out . println ( countOfTeams ( arr , N , K ) ) ; } }
Minimum number of decrements by 1 required to reduce all elements of a circular array to 0 | Java program for the above approach ; Function to find minimum operation require to make all elements 0 ; Stores the maximum element and its position in the array ; Traverse the array ; Update the maximum element and its index ; Print the minimum number of operations required ; Driver Code
import java . io . * ; class GFG { static void minimumOperations ( int arr [ ] , int N ) { int mx = 0 , pos = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] >= mx ) { mx = arr [ i ] ; pos = i ; } } System . out . println ( ( mx - 1 ) * N + pos + 1 ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 0 , 2 } ; int N = arr . length ; minimumOperations ( arr , N ) ; } }
Maximize the smallest array element by incrementing all elements in a K | Java program for above approach ; Function to check if the smallest value of v is achievable or not ; Create array to store previous moves ; Remove previous moves ; Add balance to ans ; Update contiguous subarray of length k ; Number of moves should not exceed m ; Function to find the maximum value of the smallest array element that can be obtained ; Perform Binary search ; Driver Code ; Given Input ; Function Call
class GFG { static long n , m , k , l , r , i ; static boolean check ( long v , long [ ] a ) { long tec = 0 , ans = 0 ; long [ ] b = new long [ ( int ) ( n + k + 1 ) ] ; for ( int i = 0 ; i < n ; i ++ ) { tec -= b [ i ] ; if ( a [ i ] + tec < v ) { long mov = v - a [ i ] - tec ; ans = ans + mov ; tec += mov ; b [ i + ( int ) k ] = mov ; } } return ans <= m ; } static long FindLargest ( long [ ] a ) { l = 1 ; r = ( long ) Math . pow ( 10 , 10 ) ; while ( r - l > 0 ) { long tm = ( l + r + 1 ) / 2 ; if ( check ( tm , a ) ) l = tm ; else r = tm - 1 ; } return l ; } public static void main ( String [ ] args ) { long [ ] a = { 2 , 2 , 2 , 2 , 1 , 1 } ; m = 2 ; k = 3 ; n = a . length ; System . out . println ( FindLargest ( a ) ) ; } }
Find value K in given Complete Binary Tree with values indexed from 1 to N | Java program for the above approach ; Class containing left and right child of current node and key value ; Function to store the list of the directions ; While the root is not found ; If it is the right child of its parent ; Add right step ( 1 ) ; Add left step ( - 1 ) ; Repeat the same for its parent ; Reverse the steps list ; Return the steps ; Function to find if the key is present or not ; Get the steps to be followed ; Taking one step at a time ; Go left ; If left child does not exist return false ; Go right ; If right child does not exist return false ; If the entire array of steps has been successfully traversed ; Consider the following complete binary tree 1 / \ 2 3 / \ / \ 4 5 6 7 / \ / 8 9 10 ; Function Call
import java . util . * ; import java . io . * ; class Node { int data ; Node left , right ; public Node ( int item ) { data = item ; left = null ; right = null ; } } class Solution { public static ArrayList < Integer > findSteps ( Node root , int data ) { ArrayList < Integer > steps = new ArrayList < Integer > ( ) ; while ( data != 1 ) { if ( data / 2.0 > data / 2 ) { steps . add ( 1 ) ; } else { steps . add ( - 1 ) ; } int parent = data / 2 ; data = parent ; } Collections . reverse ( steps ) ; return steps ; } public static boolean findKey ( Node root , int data ) { ArrayList < Integer > steps = findSteps ( root , data ) ; for ( Integer cur_step : steps ) { if ( cur_step == - 1 ) { if ( root . left == null ) return false ; root = root . left ; } else { if ( root . right == null ) return false ; root = root . right ; } } return true ; } public static void main ( String [ ] args ) { Node root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 5 ) ; root . right . left = new Node ( 6 ) ; root . right . right = new Node ( 7 ) ; root . left . left . left = new Node ( 8 ) ; root . left . left . right = new Node ( 9 ) ; root . left . right . left = new Node ( 10 ) ; System . out . println ( findKey ( root , 7 ) ) ; } }
Altitude of largest Triangle that can be inscribed in a Rectangle | Java program for the above approach ; Function to find the greatest altitude of the largest triangle triangle that can be inscribed in the rectangle ; If L is greater than B ; Variables to perform binary search ; Stores the maximum altitude possible ; Iterate until low is less than high ; Stores the mid value ; If mide is less than or equal to the B / 2 ; Update res ; Update low ; Update high ; Print the result ; Driver Code ; Given Input ; Function call
import java . io . * ; class GFG { static int largestAltitude ( int L , int B ) { if ( L > B ) { int t = L ; L = B ; B = t ; } int low = 0 , high = L ; int res = 0 ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; if ( mid <= ( B / 2 ) ) { res = mid ; low = mid + 1 ; } else high = mid - 1 ; } return res ; } public static void main ( String [ ] args ) { int L = 3 ; int B = 4 ; System . out . print ( largestAltitude ( L , B ) ) ; } }
Find the frequency of each element in a sorted array | Java program for the above approach ; Function to print the frequency of each element of the sorted array ; Stores the frequency of an element ; Traverse the array arr [ ] ; If the current element is equal to the previous element ; Increment the freq by 1 ; Otherwise , ; Update freq ; Print the frequency of the last element ; Driver Code ; Given Input ; Function Call
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void printFreq ( int arr [ ] , int N ) { int freq = 1 ; for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ i ] == arr [ i - 1 ] ) { freq ++ ; } else { System . out . println ( " Frequency ▁ of ▁ " + arr [ i - 1 ] + " ▁ is : ▁ " + freq ) ; freq = 1 ; } } System . out . println ( " Frequency ▁ of ▁ " + arr [ N - 1 ] + " ▁ is : ▁ " + freq ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 1 , 1 , 2 , 3 , 3 , 5 , 5 , 8 , 8 , 8 , 9 , 9 , 10 } ; int N = arr . length ; printFreq ( arr , N ) ; } }
Maximize median of a KxK sub | Java program for the above approach ; Function to determine if a given value can be median ; Stores the prefix sum array ; Traverse the matrix arr [ ] [ ] ; Update Pre [ i + 1 ] [ j + 1 ] ; If arr [ i ] [ j ] is less than or equal to mid ; Stores the count of elements should be less than mid ; Stores if the median mid can be possible or not ; Iterate over the range [ K , N ] ; Iterate over the range [ K , N ] ; Stores count of elements less than or equal to the value mid in submatrix with bottom right vertices at ( i , j ) ; If X is less than or equal to required ; Return flag ; Function to find the maximum median of a subsquare of the given size ; Stores the range of the search space ; Iterate until low is less than high ; Stores the mid value of the range [ low , high ] ; If the current median can be possible ; Update the value of low ; Update the value of high ; Return value stored in low as answer ; Driver Code
public class GFG { static boolean isMaximumMedian ( int arr [ ] [ ] , int N , int K , int mid ) { int [ ] [ ] Pre = new int [ N + 5 ] [ N + 5 ] ; for ( int i = 0 ; i < N ; ++ i ) { for ( int j = 0 ; j < N ; ++ j ) { Pre [ i + 1 ] [ j + 1 ] = Pre [ i + 1 ] [ j ] + Pre [ i ] [ j + 1 ] - Pre [ i ] [ j ] ; if ( arr [ i ] [ j ] <= mid ) Pre [ i + 1 ] [ j + 1 ] ++ ; } } int required = ( K * K + 1 ) / 2 ; boolean flag = false ; for ( int i = K ; i <= N ; ++ i ) { for ( int j = K ; j <= N ; ++ j ) { int X = Pre [ i ] [ j ] - Pre [ i - K ] [ j ] - Pre [ i ] [ j - K ] + Pre [ i - K ] [ j - K ] ; if ( X < required ) flag = true ; } } return flag ; } static int maximumMedian ( int arr [ ] [ ] , int N , int K ) { int low = 0 , high = ( int ) 1e9 ; while ( low < high ) { int mid = low + ( high - low ) / 2 ; if ( isMaximumMedian ( arr , N , K , mid ) ) { low = mid + 1 ; } else { high = mid ; } } return low ; } public static void main ( String args [ ] ) { int [ ] [ ] arr = { { 1 , 5 , 12 } , { 6 , 7 , 11 } , { 8 , 9 , 10 } } ; int N = arr . length ; int K = 2 ; System . out . print ( maximumMedian ( arr , N , K ) ) ; } }
Minimum days to make Array elements with value at least K sum at least X | Java program for the above approach ; Function to find the minimum number of days such that the sum of array elements >= K is at least X ; Initialize the boundaries of search space ; Perform the binary search ; Find the value of mid ; Traverse the array , arr [ ] ; Find the value of arr [ i ] after mid number of days ; Check if temp is not less than K ; Update the value of sum ; Check if the value of sum is greater than X ; Update value of high ; Update the value of low ; Print the minimum number of days ; Driver Code
import java . io . * ; class GFG { static void findMinDays ( int arr [ ] , int R [ ] , int N , int X , int K ) { int low = 0 , high = X ; int minDays = - 1 ; while ( low <= high ) { int mid = ( low + high ) / 2 ; int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int temp = arr [ i ] + R [ i ] * mid ; if ( temp >= K ) { sum += temp ; } } if ( sum >= X ) { minDays = mid ; high = mid - 1 ; } else { low = mid + 1 ; } } System . out . println ( minDays ) ; } public static void main ( String [ ] args ) { int X = 100 , K = 45 ; int arr [ ] = { 2 , 5 , 2 , 6 } ; int R [ ] = { 10 , 13 , 15 , 12 } ; int N = arr . length ; findMinDays ( arr , R , N , X , K ) ; } }
Count of indices pairs such that product of elements at these indices is equal to absolute difference of indices | Java program for the above approach ; Function to count the number of pairs ( i , j ) such that arr [ i ] * arr [ j ] is equal to abs ( i - j ) ; Stores the resultant number of pairs ; Generate all possible pairs from the array arr [ ] ; If the given condition satisfy then increment the value of count ; Return the resultant count ; Driver Code
import java . util . * ; class GFG { static int getPairsCount ( int a [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( ( a [ i ] * a [ j ] ) == Math . abs ( i - j ) ) count ++ ; } } return count ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 1 , 2 , 4 } ; int N = arr . length ; System . out . print ( getPairsCount ( arr , N ) ) ; } }
Lexicographically smallest string formed repeatedly deleting character from substring 10 | Java program for the above approach ; Function to find smallest lexicogra - phically smallest string ; Stores the index of last occuring 0 ; Stores the lexicographically smallest string ; Traverse the string S ; If str [ i ] is 0 ; Assign i to lastZe ; Traverse the string str ; If i is less than or equal to lastZe and str [ i ] is 0 ; If i is greater than lastZe ; Return ans ; Driver code ; Input ; Function Call
import java . lang . * ; import java . util . * ; class GFG { static String lexicographicallySmallestString ( String S , int N ) { int LastZe = - 1 ; String ans = " " ; for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( S . charAt ( i ) == '0' ) { LastZe = i ; break ; } } for ( int i = 0 ; i < N ; i ++ ) { if ( i <= LastZe && S . charAt ( i ) == '0' ) ans += S . charAt ( i ) ; else if ( i > LastZe ) ans += S . charAt ( i ) ; } return ans ; } public static void main ( String [ ] args ) { String S = "11001101" ; int N = S . length ( ) ; System . out . println ( lexicographicallySmallestString ( S , N ) ) ; } }
Maximize boxes required to keep at least one black and one white shirt | Java program for the above approach ; Function to find the maximum number of boxes such that each box contains three shirts comprising of at least one white and black shirt ; Stores the low and high pointers for binary search ; Store the required answer ; Loop while low <= high ; Store the mid value ; Check if the mid number of boxes can be used ; Update answer and recur for the right half ; Else , recur for the left half ; Print the result ; Driver Code
import java . util . * ; class GFG { static void numberofBoxes ( int W , int B , int O ) { int low = 0 , high = Math . min ( W , B ) ; int ans = 0 ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; if ( ( ( W >= mid ) && ( B >= mid ) ) && ( ( W - mid ) + ( B - mid ) + O ) >= mid ) { ans = mid ; low = mid + 1 ; } else high = mid - 1 ; } System . out . println ( ans ) ; } public static void main ( String args [ ] ) { int W = 3 , B = 3 , O = 1 ; numberofBoxes ( W , B , O ) ; } }
Minimum swaps needed to convert given Binary Matrix A to Binary Matrix B | Java program for the above approach ; Function to count the minimum number of swaps required to convert matrix A to matrix B ; Stores number of cells such that matrix A contains 0 and matrix B contains 1 ; Stores number of cells such that matrix A contains 1 and matrix B contains 0 ; Iterate over the range [ 0 , N - 1 ] ; Iterate over the range [ 0 , M - 1 ] ; If A [ i ] [ j ] = 1 and B [ i ] [ j ] = 0 ; If A [ i ] [ j ] = 0 and B [ i ] [ j ] = 1 ; If count01 is equal to count10 ; Otherwise , ; Driver Code
public class MyClass { public static int minSwaps ( int N , int M , int A [ ] [ ] , int B [ ] [ ] ) { int count01 = 0 ; int count10 = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( A [ i ] [ j ] != B [ i ] [ j ] ) { if ( A [ i ] [ j ] == 1 ) count10 ++ ; else count01 ++ ; } } } if ( count01 == count10 ) return count01 ; else return - 1 ; } public static void main ( String args [ ] ) { int [ ] [ ] A = { { 1 , 1 , 0 } , { 0 , 0 , 1 } , { 0 , 1 , 0 } } ; int [ ] [ ] B = { { 0 , 0 , 1 } , { 0 , 1 , 0 } , { 1 , 1 , 0 } } ; int N = A . length ; int M = B [ 0 ] . length ; System . out . println ( minSwaps ( N , M , A , B ) ) ; } }
Maximize X such that sum of numbers in range [ 1 , X ] is at most K | Java program for the above approach ; Function to count the elements with sum of the first that many natural numbers less than or equal to K ; If K equals to 0 ; Stores sum of first i natural numbers ; Stores the result ; Iterate over the range [ 1 , N ] ; Increment sum by i ; Is sum is less than or equal to K ; Otherwise , ; Return res ; Driver Code ; Input ; Function call
public class GFG { static int Count ( int N , int K ) { if ( K == 0 ) return 0 ; int sum = 0 ; int res = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { sum += i ; if ( sum <= K ) res ++ ; else break ; } return res ; } public static void main ( String args [ ] ) { int N = 6 , K = 14 ; System . out . println ( Count ( N , K ) ) ; } }
Remove last occurrence of a word from a given sentence string | Java program for the above approach ; Function to remove last occurrence of W from S ; If M is greater than N ; Iterate while i is greater than or equal to 0 ; Stores if occurrence of W has been found or not ; Iterate over the range [ 0 , M ] ; If S [ j + 1 ] is not equal to W [ j ] ; Mark flag true and break ; If occurrence has been found ; Delete the substring over the range [ i , i + M ] ; Resize the string S ; Return S ; Driver Code ; Input ; Function call
import java . util . * ; class GFG { static String removeLastOccurrence ( String S , String W , int N , int M ) { char [ ] ch = S . toCharArray ( ) ; if ( M > N ) return S ; for ( int i = N - M ; i >= 0 ; i -- ) { int flag = 0 ; for ( int j = 0 ; j < M ; j ++ ) { if ( ch [ j + i ] != W . charAt ( j ) ) { flag = 1 ; break ; } } if ( flag == 0 ) { for ( int j = i ; j < N - M ; j ++ ) ch [ j ] = ch [ j + M ] ; break ; } } char [ ] chh = new char [ N - M ] ; for ( int i = 0 ; i < N - M ; i ++ ) { chh [ i ] = ch [ i ] ; } return String . valueOf ( chh ) ; } public static void main ( String [ ] args ) { String S = " This ▁ is ▁ GeeksForGeeks " ; String W = " Geeks " ; int N = S . length ( ) ; int M = W . length ( ) ; System . out . print ( removeLastOccurrence ( S , W , N , M ) ) ; } }
Rearrange the Array to maximize the elements which is smaller than both its adjacent elements | Java program for the above approach ; Function to rearrange array such that count of element that are smaller than their adjacent elements is maximum ; Stores the rearranged array ; Stores the maximum count of elements ; Sort the given array ; Place the smallest ( N - 1 ) / 2 elements at odd indices ; Placing the rest of the elements at remaining indices ; If no element of the array has been placed here ; Print the resultant array ; Driver Code ; Input ; Function call
import java . io . * ; import java . util . * ; class GFG { public static void maximumIndices ( int arr [ ] , int N ) { int [ ] temp = new int [ N ] ; int maxIndices = ( N - 1 ) / 2 ; Arrays . sort ( arr ) ; for ( int i = 0 ; i < maxIndices ; i ++ ) { temp [ 2 * i + 1 ] = arr [ i ] ; } int j = 0 ; for ( int i = maxIndices ; i < N ; ) { if ( temp [ j ] == 0 ) { temp [ j ] = arr [ i ] ; i ++ ; } j ++ ; } for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( temp [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int N = 4 ; maximumIndices ( arr , N ) ; } }
Find the repeating element in an Array of size N consisting of first M natural numbers | Java Program for the above approach ; Function to calculate the repeating character in a given permutation ; variables to store maximum element and sum of the array respectively . ; calculate sum of array ; calculate maximum element in the array ; calculating sum of permutation ; calculate required answer ; Driver code ; Input ; Function call
import java . io . * ; class GFG { public static int repeatingElement ( int arr [ ] , int N ) { int M = 0 , sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; M = Math . max ( M , arr [ i ] ) ; } int sum1 = M * ( M + 1 ) / 2 ; int ans = ( sum - sum1 ) / ( N - M ) ; return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 6 , 4 , 3 , 1 , 5 , 2 } ; int N = arr . length ; System . out . println ( repeatingElement ( arr , N ) ) ; } }