codeparrot/xlcost-text-to-code huggingface dataset:
Count number of triangles possible for the given sides range | Java implementation to count the number of possible triangles for the given sides ranges ; Function to count the number of possible triangles for the given sides ranges ; Iterate for every possible of x ; Range of y is [ b , c ] From this range First we will find the number of x + y greater than d ; For x + y greater than d we can choose all z from [ c , d ] Total permutation will be ; Now we will find the number of x + y in between the [ c , d ] ; [ l , r ] will be the range from total [ c , d ] x + y belongs For any r such that r = x + y We can choose z , in the range [ c , d ] only less than r , Thus total permutation be ; Driver Code
import java . util . Scanner ; import java . util . Arrays ; class GFG { public static int count_triangles ( int a , int b , int c , int d ) { int ans = 0 ; for ( int x = a ; x <= b ; ++ x ) { int num_greater_than_d = Math . max ( d , c + x ) - Math . max ( d , b + x - 1 ) ; ans += num_greater_than_d * ( d - c + 1 ) ; int r = Math . min ( Math . max ( c , c + x ) , d ) - c ; int l = Math . min ( Math . max ( c , b + x - 1 ) , d ) - c ; int x1 = ( r * ( r + 1 ) ) / 2 ; int x2 = ( l * ( l + 1 ) ) / 2 ; ans += x1 - x2 ; } return ans ; } public static void main ( String args [ ] ) { int a = 2 , b = 3 , c = 4 , d = 5 ; System . out . println ( count_triangles ( a , b , c , d ) ) ; } }
Minimum number of swaps required to make the string K periodic | Java code to find the minimum number of swaps required to make the String K periodic ; Mark all allowed characters as true ; Initialize the freq array to 0 ; Increase the frequency of each character ; Total number of periods of size K in the String ; Check if the current character is present in allowed and whether the current frequency is greater than all previous frequencies for this position ; Update the answer by subtracting the maxfrequency from total positions if there exist extra character at the end of the String apart from the n / k characters then add 1. ; Driver code
import java . util . * ; class GFG { static void minFlip ( String s , int n , int k , char a [ ] , int p ) { boolean allowed [ ] = new boolean [ 26 ] ; for ( int i = 0 ; i < p ; i ++ ) { allowed [ a [ i ] - ' a ' ] = true ; } char [ ] [ ] freq = new char [ k ] [ 26 ] ; for ( int i = 0 ; i < k ; i ++ ) for ( int j = 0 ; j < 26 ; j ++ ) freq [ i ] [ j ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) { freq [ i % k ] [ s . charAt ( i ) - ' a ' ] += 1 ; } int ans = 0 ; int totalpositions = n / k ; for ( int i = 0 ; i < k ; i ++ ) { int maxfrequency = 0 ; for ( int j = 0 ; j < 26 ; j ++ ) { if ( freq [ i ] [ j ] > maxfrequency && allowed [ j ] == true ) maxfrequency = freq [ i ] [ j ] ; } ans += ( totalpositions - maxfrequency + ( ( i % k < n % k ) ? 1 : 0 ) ) ; } System . out . print ( ans + "NEW_LINE"); } public static void main ( String [ ] args ) { String S = " nihsiakyt " ; int n = S . length ( ) ; int K = 3 ; char [ ] A = { ' n ' , ' i ' , ' p ' , ' s ' , ' q ' } ; int p = A . length ; minFlip ( S , n , K , A , p ) ; } }
Minimum number of colors required to color a Circular Array | Java implementation of above approach ; Function that finds minimum number of colors required ; To check that if all the elements are same or not ; To check if only one adjacent exist ; Traverse the array ; If adjacent elements found different means all are not same ; If two adjacent elements found to be same then make one_adjacent_same true ; If all elements are same then print 1 ; If total number of elements are even or there exist two adjacent elements that are same then print 2 ; Else 3 type of colors are required ; Driver Code ; Function call
import java . util . * ; class GFG { static void colorRequired ( int arr [ ] , int n ) { boolean all_same = true ; boolean one_adjacent_same = false ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] != arr [ i + 1 ] ) { all_same = false ; } if ( arr [ i ] == arr [ i + 1 ] ) { one_adjacent_same = true ; } } if ( all_same == true ) { System . out . print ( 1 + "NEW_LINE"); return ; } if ( n % 2 == 0 one_adjacent_same == true ) { System . out . print ( 2 + "NEW_LINE"); return ; } System . out . print ( 3 + "NEW_LINE"); } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 1 , 1 , 2 } ; int n = arr . length ; colorRequired ( arr , n ) ; } }
Maximize the sum of sum of the Array by removing end elements | Java program to maximize the sum of sum of the Array by removing end elements ; Function to find the maximum sum of sum ; Compute the sum of whole array ; Traverse and remove the minimum value from an end to maximum the sum value ; If the left end element is smaller than right end ; Remove the left end element ; If the right end element is smaller than left end ; Remove the right end element ; Add the remaining element sum in the result ; Return the maximum sum of sum ; Driver code
import java . util . * ; class GFG { static int maxRemainingSum ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; int i = 0 ; int j = n - 1 ; int result = 0 ; while ( i < j ) { if ( arr [ i ] < arr [ j ] ) { sum -= arr [ i ] ; i ++ ; } else { sum -= arr [ j ] ; j -- ; } result += sum ; } return result ; } public static void main ( String args [ ] ) { int arr [ ] = { 3 , 1 , 7 , 2 , 1 } ; int N = arr . length ; System . out . println ( maxRemainingSum ( arr , N ) ) ; } }
Split a number as sum of K numbers which are not divisible by K | Java program to split a number as sum of K numbers which are not divisible by K ; Function to split into K parts and print them ; Print 1 K - 1 times ; Print N - K + 1 ; Print 1 K - 2 times ; Print 2 and N - K ; Driver code
class GFG { static void printKParts ( int N , int K ) { if ( N % K == 0 ) { for ( int i = 1 ; i < K ; i ++ ) System . out . print ( "1 , ▁ " ) ; System . out . print ( N - ( K - 1 ) + "NEW_LINE"); } else { if ( K == 2 ) { System . out . print ( " Not ▁ Possible " + "NEW_LINE"); return ; } for ( int i = 1 ; i < K - 1 ; i ++ ) System . out . print ( 1 + " , ▁ " ) ; System . out . print ( 2 + " , ▁ " + ( N - K ) + "NEW_LINE"); } } public static void main ( String [ ] args ) { int N = 18 , K = 5 ; printKParts ( N , K ) ; } }
Minimum operation required to make a balanced sequence | Java implementation of the approach ; Function to return the minimum operations required ; Condition where we got only one closing bracket instead of 2 , here we have to add one more closing bracket to make the sequence balanced ; Add closing bracket that costs us one operation ; Remove the top opening bracket because we got the 1 opening and 2 continuous closing brackets ; Inserting the opening bracket to stack ; After making the sequence balanced closing is now set to 0 ; Case when there is no opening bracket the sequence starts with a closing bracket and one opening bracket is required Now we have one opening and one closing bracket ; Add opening bracket that costs us one operation ; Assigning 1 to cntClosing because we have one closing bracket ; Case where we got two continuous closing brackets need to pop one opening bracket from stack top ; Condition where stack is not empty This is the case where we have only opening brackets ( st . size ( ) * 2 ) will give us the total closing bracket needed cntClosing is the count of closing bracket that we already have ; Driver code
import java . util . * ; class GFG { static int minOperations ( String s , int len ) { int operationCnt = 0 ; Stack < Character > st = new Stack < Character > ( ) ; int cntClosing = 0 ; for ( int i = 0 ; i < len ; i ++ ) { if ( s . charAt ( i ) == ' { ' ) { if ( cntClosing > 0 ) { operationCnt ++ ; st . pop ( ) ; } st . add ( s . charAt ( i ) ) ; cntClosing = 0 ; } else if ( st . isEmpty ( ) ) { st . add ( ' { ' ) ; operationCnt ++ ; cntClosing = 1 ; } else { cntClosing = ( cntClosing + 1 ) % 2 ; if ( cntClosing == 0 ) { st . pop ( ) ; } } } operationCnt += st . size ( ) * 2 - cntClosing ; return operationCnt ; } public static void main ( String [ ] args ) { String str = " } } { " ; int len = str . length ( ) ; System . out . print ( minOperations ( str , len ) ) ; } }
Longest subsequence with different adjacent characters | Java program for the above approach ; Function to find the longest subsequence with different adjacent character ; Length of the String s ; Previously picked character ; If the current character is different from the previous then include this character and update previous character ; Driver Code ; Function call
import java . util . * ; class GFG { static int longestSubsequence ( String s ) { int n = s . length ( ) ; int answer = 0 ; char prev = ' - ' ; for ( int i = 0 ; i < n ; i ++ ) { if ( prev != s . charAt ( i ) ) { prev = s . charAt ( i ) ; answer ++ ; } } return answer ; } public static void main ( String [ ] args ) { String str = " ababa " ; System . out . print ( longestSubsequence ( str ) ) ; } }
Length of longest subarray of length at least 2 with maximum GCD | Java program for the above approach ; Function to calculate GCD of two numbers ; Function to find maximum size subarray having maximum GCD ; Base case ; Let the maximum GCD be 1 initially ; Loop through array to find maximum GCD of subarray with size 2 ; Traverse the array ; Is a multiple of k , increase cnt ; Else update maximum length with consecutive element divisible by k Set cnt to 0 ; Update the maxLength ; Return the maxLength ; Driver Code ; Function call
class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static int maximumGcdSubarray ( int arr [ ] , int n ) { if ( n == 1 ) return 0 ; int k = 1 ; for ( int i = 1 ; i < n ; i ++ ) { k = Math . max ( k , gcd ( arr [ i ] , arr [ i - 1 ] ) ) ; } int cnt = 0 ; int maxLength = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % k == 0 ) { cnt ++ ; } else { maxLength = Math . max ( maxLength , cnt ) ; cnt = 0 ; } } maxLength = Math . max ( maxLength , cnt ) ; return maxLength ; } public static void main ( String args [ ] ) { int arr [ ] = { 18 , 3 , 6 , 9 } ; int n = arr . length ; System . out . println ( maximumGcdSubarray ( arr , n ) ) ; } }
Split N powers of 2 into two subsets such that their difference of sum is minimum | Java program to find the minimum difference possible by splitting all powers of 2 up to N into two sets of equal size ; Store the largest ; Form two separate groups ; Initialize the sum for two groups as 0 ; Insert 2 ^ n in the first group ; Calculate the sum of least n / 2 - 1 elements added to the first set ; Sum of remaining n / 2 - 1 elements ; Min difference between the two groups ; Driver Code
import java . lang . Math ; class GFG { public static void MinDiff ( int n ) { int val = ( int ) Math . pow ( 2 , n ) ; int sep = n / 2 ; int grp1 = 0 ; int grp2 = 0 ; grp1 = grp1 + val ; for ( int i = 1 ; i < sep ; i ++ ) grp1 = grp1 + ( int ) Math . pow ( 2 , i ) ; for ( int i = sep ; i < n ; i ++ ) grp2 = grp2 + ( int ) Math . pow ( 2 , i ) ; System . out . println ( Math . abs ( grp1 - grp2 ) ) ; } public static void main ( String args [ ] ) { int n = 4 ; MinDiff ( n ) ; } }
Count elements in first Array with absolute difference greater than K with an element in second Array | Java program to count elements in first Array with absolute difference greater than K with an element in second Array ; Function to count the such elements ; Store count of required elements in arr1 ; Initialise the smallest and the largest value from the second array arr2 [ ] ; Find the smallest and the largest element in arr2 ; Check if absolute difference of smallest and arr1 [ i ] or largest and arr1 [ i ] is > K then arr [ i ] is a required element ; Print the final result ; Driver code
class GFG { static void countDist ( int arr1 [ ] , int n , int arr2 [ ] , int m , int k ) { int count = 0 ; int smallest = arr2 [ 0 ] ; int largest = arr2 [ 0 ] ; for ( int i = 0 ; i < m ; i ++ ) { smallest = Math . max ( smallest , arr2 [ i ] ) ; largest = Math . min ( largest , arr1 [ i ] ) ; } for ( int i = 0 ; i < n ; i ++ ) { if ( Math . abs ( arr1 [ i ] - smallest ) > k || Math . abs ( arr1 [ i ] - largest ) > k ) count ++ ; } System . out . print ( count ) ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 3 , 1 , 4 } ; int n = arr1 . length ; int arr2 [ ] = { 5 , 1 , 2 } ; int m = arr2 . length ; int k = 2 ; countDist ( arr1 , n , arr2 , m , k ) ; } }
Make sum of all subarrays of length K equal by only inserting elements | Java implementation of above approach ; Function that prints another array whose all subarray of length k have an equal sum ; Store all distinct elements in the unordered map ; Condition if the number of distinct elements is greater than k ; Push all distinct elements in a vector ; Push 1 while the size of vector not equal to k ; Print the vector 2 times ; Driver Code
import java . util . * ; class GFG { static void MakeArray ( int a [ ] , int n , int k ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! mp . containsKey ( a [ i ] ) ) mp . put ( a [ i ] , 1 ) ; } if ( mp . size ( ) > k ) { System . out . print ( "Not possibleNEW_LINE"); return ; } Vector < Integer > ans = new Vector < Integer > ( ) ; for ( Map . Entry < Integer , Integer > i : mp . entrySet ( ) ) { ans . add ( i . getKey ( ) ) ; } while ( ans . size ( ) < k ) { ans . add ( 1 ) ; } for ( int i = 0 ; i < 2 ; i ++ ) { for ( int j = 0 ; j < k ; j ++ ) System . out . print ( ans . get ( j ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 2 , 1 } ; int K = 2 ; int size = arr . length ; MakeArray ( arr , size , K ) ; } }
Longest substring with atmost K characters from the given set of characters | Java implementation to find the longest substring in the string which contains atmost K characters from the given set of characters ; Function to find the longest substring in the string which contains atmost K characters from the given set of characters ; Base Condition ; Count for Characters from set in substring ; Two pointers ; Loop to iterate until right pointer is not equal to N ; Loop to increase the substring length until the characters from set are at most K ; Check if current pointer points a character from set ; If the count of the char is exceeding the limit ; update answer with substring length ; Increment the left pointer until the count is less than or equal to K ; If the character which comes out then decrement the count by 1 ; Driver code ; Construction of set ; Output result
import java . util . * ; class GFG { static int maxNormalSubstring ( String P , Set < Character > Q , int K , int N ) { if ( K == 0 ) return 0 ; int count = 0 ; int left = 0 , right = 0 ; int ans = 0 ; while ( right < N ) { while ( right < N && count <= K ) { if ( Q . contains ( P . charAt ( right ) ) ) { if ( count + 1 > K ) break ; else count ++ ; } right ++ ; if ( count <= K ) ans = Math . max ( ans , right - left ) ; } while ( left < right ) { left ++ ; if ( Q . contains ( P . charAt ( left - 1 ) ) ) count -- ; if ( count < K ) break ; } } return ans ; } public static void main ( String [ ] args ) { String P = " giraffe " ; Set < Character > Q = new HashSet < > ( ) ; Q . add ( ' a ' ) ; Q . add ( ' f ' ) ; Q . add ( ' g ' ) ; Q . add ( ' r ' ) ; int K = 2 ; int N = P . length ( ) ; System . out . println ( maxNormalSubstring ( P , Q , K , N ) ) ; } }
Find the minimum value of the given expression over all pairs of the array | Java program to find the minimum value of the given expression over all pairs of the array ; Function to find the minimum value of the expression ; Iterate over all the pairs and find the minimum value ; Driver code
import java . io . * ; import java . util . Arrays ; class GFG { static int MinimumValue ( int a [ ] , int n ) { int answer = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { answer = Math . min ( answer , ( ( a [ i ] & a [ j ] ) ^ ( a [ i ] a [ j ] ) ) ) ; } } return answer ; } public static void main ( String [ ] args ) { int N = 6 ; int [ ] A = new int [ ] { 12 , 3 , 14 , 5 , 9 , 8 } ; System . out . print ( MinimumValue ( A , N ) ) ; } }
Number of substrings with length divisible by the number of 1 's in it | Java program to count number of subString under given condition ; Function return count of such substring ; Mark 1 at those indices where '1' appears ; Take prefix sum ; Iterate through all the subStrings ; Driver Code
import java . util . * ; class GFG { static int countOfSubStrings ( String s ) { int n = s . length ( ) ; int [ ] prefix_sum = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . charAt ( i ) == '1' ) prefix_sum [ i ] = 1 ; } for ( int i = 1 ; i < n ; i ++ ) prefix_sum [ i ] += prefix_sum [ i - 1 ] ; int answer = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { int countOfOnes = prefix_sum [ j ] - ( i - 1 >= 0 ? prefix_sum [ i - 1 ] : 0 ) ; int length = j - i + 1 ; if ( countOfOnes > 0 && length % countOfOnes == 0 ) answer ++ ; } } return answer ; } public static void main ( String [ ] args ) { String S = "1111100000" ; System . out . print ( countOfSubStrings ( S ) ) ; } }
Find the number of elements X such that X + K also exists in the array | Java implementation of find number of elements x in this array such x + k also present in this array . ; Function to return the count of element x such that x + k also lies in this array ; Key in map will store elements and value will store the frequency of the elements ; Find if i . first + K is present in this map or not ; If we find i . first or key + K in this map then we have to increase in answer the frequency of this element ; Driver code ; array initialisation ; size of array ; initialise k
import java . util . * ; class GFG { static int count_element ( int N , int K , int [ ] arr ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < N ; ++ i ) if ( mp . containsKey ( arr [ i ] ) ) { mp . put ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . put ( arr [ i ] , 1 ) ; } int answer = 0 ; for ( Map . Entry < Integer , Integer > i : mp . entrySet ( ) ) { if ( mp . containsKey ( i . getKey ( ) + K ) ) answer += i . getValue ( ) ; } return answer ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 6 , 2 , 8 , 7 , 6 , 5 , 9 } ; int N = arr . length ; int K = 2 ; System . out . print ( count_element ( N , K , arr ) ) ; } }
Find the size of largest group where groups are according to the xor of digits | Java implementation to Find the size of largest group , where groups are according to the xor of its digits . ; Function to find out xor of digit ; calculate xor digitwise ; return xor ; Function to find the size of largest group ; hash map for counting frequency ; counting freq of each element ; find the maximum ; Driver code ; initialise N
import java . util . * ; class GFG { static int digit_xor ( int x ) { int xorr = 0 ; while ( x > 0 ) { xorr ^= x % 10 ; x = x / 10 ; } return xorr ; } static int find_count ( int n ) { HashMap < Integer , Integer > mpp = new HashMap < Integer , Integer > ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { if ( mpp . containsKey ( digit_xor ( i ) ) ) mpp . put ( digit_xor ( i ) , mpp . get ( digit_xor ( i ) ) + 1 ) ; else mpp . put ( digit_xor ( i ) , 1 ) ; } int maxm = 0 ; for ( Map . Entry < Integer , Integer > x : mpp . entrySet ( ) ) { if ( x . getValue ( ) > maxm ) maxm = x . getValue ( ) ; } return maxm ; } public static void main ( String [ ] args ) { int N = 13 ; System . out . print ( find_count ( N ) ) ; } }
Count of largest sized groups while grouping according to product of digits | Java implementation to Count the groups having largest size while grouping is according to the product of its digits ; Function to find out product of digit ; Calculate product ; Return the product of digits ; Function to find the count ; Hash map for counting frequency ; Counting freq of each element ; Find the maximum ; Count the number of groups having size of equal to largest group . ; Driver Code ; Initialise N
import java . io . * ; import java . util . * ; class GFG { static int digit_prod ( int x ) { int prod = 1 ; while ( x != 0 ) { prod *= x % 10 ; x = x / 10 ; } return prod ; } static int find_count ( int n ) { Map < Integer , Integer > mpp = new HashMap < > ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { int t = digit_prod ( i ) ; mpp . put ( t , mpp . getOrDefault ( t , 0 ) + 1 ) ; } int ans = 1 ; int maxm = 0 ; for ( Integer x : mpp . values ( ) ) { if ( x > maxm ) { maxm = x ; ans = 1 ; } else if ( x == maxm ) { ans ++ ; } } return ans ; } public static void main ( String args [ ] ) { int N = 13 ; System . out . print ( find_count ( N ) ) ; } }
Count of subarrays having exactly K prime numbers | Java program for the above approach ; A utility function to check if the number n is prime or not ; Base Cases ; Check to skip middle five numbers in below loop ; If n is divisible by i & i + 2 then it is not prime ; Function to find number of subarrays with sum exactly equal to k ; STL map to store number of subarrays starting from index zero having particular value of sum . ; To store the sum of element traverse so far ; Add current element to currsum ; If currsum = K , then a new subarray is found ; If currsum > K then find the no . of subarrays with sum currsum - K and exclude those subarrays ; Add currsum to count of different values of sum ; Return the final result ; Function to count the subarray with K primes ; Update the array element ; If current element is prime then update the arr [ i ] to 1 ; Else change arr [ i ] to 0 ; Function Call ; Driver Code ; Function Call
import java . util . * ; class GFG { static boolean isPrime ( int n ) { int i ; if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) { return false ; } for ( i = 5 ; i * i <= n ; i += 6 ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) { return false ; } } return true ; } static int findSubarraySum ( int arr [ ] , int n , int K ) { HashMap < Integer , Integer > prevSum = new HashMap < Integer , Integer > ( ) ; int res = 0 ; int currsum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { currsum += arr [ i ] ; if ( currsum == K ) { res ++ ; } if ( prevSum . containsKey ( currsum - K ) ) { res += ( prevSum . get ( currsum - K ) ) ; } if ( prevSum . containsKey ( currsum ) ) prevSum . put ( currsum , prevSum . get ( currsum ) + 1 ) ; else prevSum . put ( currsum , 1 ) ; } return res ; } static void countSubarray ( int arr [ ] , int n , int K ) { for ( int i = 0 ; i < n ; i ++ ) { if ( isPrime ( arr [ i ] ) ) { arr [ i ] = 1 ; } else { arr [ i ] = 0 ; } } System . out . print ( findSubarraySum ( arr , n , K ) ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int K = 2 ; int N = arr . length ; countSubarray ( arr , N , K ) ; } }
Count the elements having frequency equals to its value | Java program to count the elements having frequency equals to its value ; Function to find the count ; Hash map for counting frequency ; Counting freq of each element ; Check if value equals to frequency and increment the count ; Driver code ; Function call
import java . util . * ; class GFG { static int find_maxm ( int arr [ ] , int n ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . containsKey ( arr [ i ] ) ) { mp . put ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . put ( arr [ i ] , 1 ) ; } } int ans = 0 ; for ( Map . Entry < Integer , Integer > x : mp . entrySet ( ) ) { int value = x . getKey ( ) ; int freq = x . getValue ( ) ; if ( value == freq ) { ans ++ ; } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 2 , 3 , 4 , 3 } ; int n = arr . length ; System . out . print ( find_maxm ( arr , n ) ) ; } }