codeparrot/xlcost-text-to-code huggingface dataset:
Median of two sorted arrays with different sizes in O ( log ( min ( n , m ) ) ) | Java code for finding median of the given two sorted arrays that exists in the merged array ( ( n + m ) / 2 - 1 position ) ; Function to find median of given two sorted arrays ; If i = n , it means that Elements from a [ ] in the second half is an empty set . If j = 0 , it means that Elements from b [ ] in the first half is an empty set . so it is necessary to check that , because we compare elements from these two groups . searching on right ; If i = 0 , it means that Elements from a [ ] in the first half is an empty set and if j = m , it means that Elements from b [ ] in the second half is an empty set . so it is necessary to check that , because we compare elements from these two groups . searching on left ; We have found the desired halves . ; This condition happens when we don 't have any   elements in the first   half from a[] so we   returning the last   element in b[] from  the first half. ; And this condition happens when we don 't have any   elements in the first half   from b[] so we returning the  last element in a[] from the  first half. ; Function to find maximum ; Driver code ; We need to define the smaller array as the first parameter to make sure that the time complexity will be O ( log ( min ( n , m ) ) )
import java . io . * ; class GFG { static int findMedianSortedArrays ( int [ ] a , int n , int [ ] b , int m ) { int min_index = 0 , max_index = n , i , j ; while ( min_index <= max_index ) { i = ( min_index + max_index ) / 2 ; j = ( ( n + m + 1 ) / 2 ) - i ; if ( i < n && j > 0 && b [ j - 1 ] > a [ i ] ) min_index = i + 1 ; else if ( i > 0 && j < m && b [ j ] < a [ i - 1 ] ) max_index = i - 1 ; else { if ( i == 0 ) return b [ j - 1 ] ; if ( j == 0 ) return a [ i - 1 ] ; else return maximum ( a [ i - 1 ] , b [ j - 1 ] ) ; } } System . out . print ( " ERROR ! ! ! ▁ " + "returningNEW_LINE");  return 0 ; } static int maximum ( int a , int b ) { return a > b ? a : b ; } public static void main ( String [ ] args ) { int [ ] a = { 900 } ; int [ ] b = { 10 , 13 , 14 } ; int n = a . length ; int m = b . length ; if ( n < m ) System . out . print ( " The ▁ median ▁ is : ▁ " + findMedianSortedArrays ( a , n , b , m ) ) ; else System . out . print ( " The ▁ median ▁ is : ▁ " + findMedianSortedArrays ( b , m , a , n ) ) ; } }
Print uncommon elements from two sorted arrays | Java program to find uncommon elements of two sorted arrays ; If not common , print smaller ; Skip common element ; printing remaining elements ; Driver code
import java . io . * ; class GFG { static void printUncommon ( int arr1 [ ] , int arr2 [ ] , int n1 , int n2 ) { int i = 0 , j = 0 , k = 0 ; while ( i < n1 && j < n2 ) { if ( arr1 [ i ] < arr2 [ j ] ) { System . out . print ( arr1 [ i ] + " ▁ " ) ; i ++ ; k ++ ; } else if ( arr2 [ j ] < arr1 [ i ] ) { System . out . print ( arr2 [ j ] + " ▁ " ) ; k ++ ; j ++ ; } else { i ++ ; j ++ ; } } while ( i < n1 ) { System . out . print ( arr1 [ i ] + " ▁ " ) ; i ++ ; k ++ ; } while ( j < n2 ) { System . out . print ( arr2 [ j ] + " ▁ " ) ; j ++ ; k ++ ; } } public static void main ( String [ ] args ) { int arr1 [ ] = { 10 , 20 , 30 } ; int arr2 [ ] = { 20 , 25 , 30 , 40 , 50 } ; int n1 = arr1 . length ; int n2 = arr2 . length ; printUncommon ( arr1 , arr2 , n1 , n2 ) ; } }
Least frequent element in an array | Java program to find the least frequent element in an array . ; Sort the array ; find the min frequency using linear traversal ; If last element is least frequent ; driver program
import java . io . * ; import java . util . * ; class GFG { static int leastFrequent ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int min_count = n + 1 , res = - 1 ; int curr_count = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == arr [ i - 1 ] ) curr_count ++ ; else { if ( curr_count < min_count ) { min_count = curr_count ; res = arr [ i - 1 ] ; } curr_count = 1 ; } } if ( curr_count < min_count ) { min_count = curr_count ; res = arr [ n - 1 ] ; } return res ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 3 , 2 , 1 , 2 , 2 , 3 , 1 } ; int n = arr . length ; System . out . print ( leastFrequent ( arr , n ) ) ; } }
Least frequent element in an array | Java program to find the least frequent element in an array ; Insert all elements in hash . ; find min frequency . ; driver program
import java . util . HashMap ; import java . util . Map ; import java . util . Map . Entry ; class GFG { static int leastFrequent ( int arr [ ] , int n ) { Map < Integer , Integer > count = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int key = arr [ i ] ; if ( count . containsKey ( key ) ) { int freq = count . get ( key ) ; freq ++ ; count . put ( key , freq ) ; } else count . put ( key , 1 ) ; } int min_count = n + 1 , res = - 1 ; for ( Entry < Integer , Integer > val : count . entrySet ( ) ) { if ( min_count >= val . getValue ( ) ) { res = val . getKey ( ) ; min_count = val . getValue ( ) ; } } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 2 , 1 , 2 , 2 , 3 , 1 } ; int n = arr . length ; System . out . println ( leastFrequent ( arr , n ) ) ; } }
Maximum sum of increasing order elements from n arrays | Java program to find maximum sum by selecting a element from n arrays ; To calculate maximum sum by selecting element from each array ; Sort each array ; Store maximum element of last array ; Selecting maximum element from previoulsy selected element ; j = - 1 means no element is found in a [ i ] so return 0 ; Driver Code
import java . io . * ; class GFG { static int M = 4 ; static int arr [ ] [ ] = { { 1 , 7 , 3 , 4 } , { 4 , 2 , 5 , 1 } , { 9 , 5 , 1 , 8 } } ; static void sort ( int a [ ] [ ] , int row , int n ) { for ( int i = 0 ; i < M - 1 ; i ++ ) { if ( a [ row ] [ i ] > a [ row ] [ i + 1 ] ) { int temp = a [ row ] [ i ] ; a [ row ] [ i ] = a [ row ] [ i + 1 ] ; a [ row ] [ i + 1 ] = temp ; } } } static int maximumSum ( int a [ ] [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) sort ( a , i , n ) ; int sum = a [ n - 1 ] [ M - 1 ] ; int prev = a [ n - 1 ] [ M - 1 ] ; int i , j ; for ( i = n - 2 ; i >= 0 ; i -- ) { for ( j = M - 1 ; j >= 0 ; j -- ) { if ( a [ i ] [ j ] < prev ) { prev = a [ i ] [ j ] ; sum += prev ; break ; } } if ( j == - 1 ) return 0 ; } return sum ; } public static void main ( String args [ ] ) { int n = arr . length ; System . out . print ( maximumSum ( arr , n ) ) ; } }
Maximum sum of increasing order elements from n arrays | Java program to find maximum sum by selecting a element from n arrays ; To calculate maximum sum by selecting element from each array ; Store maximum element of last array ; Selecting maximum element from previoulsy selected element ; max_smaller equals to INT_MIN means no element is found in a [ i ] so return 0 ; Driver code
import java . util . * ; class GFG { static int M = 4 ; static int maximumSum ( int a [ ] [ ] , int n ) { int prev = Math . max ( a [ n - 1 ] [ 0 ] , a [ n - 1 ] [ M - 1 ] + 1 ) ; int sum = prev ; for ( int i = n - 2 ; i >= 0 ; i -- ) { int max_smaller = Integer . MIN_VALUE ; for ( int j = M - 1 ; j >= 0 ; j -- ) { if ( a [ i ] [ j ] < prev && a [ i ] [ j ] > max_smaller ) max_smaller = a [ i ] [ j ] ; } if ( max_smaller == Integer . MIN_VALUE ) return 0 ; prev = max_smaller ; sum += max_smaller ; } return sum ; } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 1 , 7 , 3 , 4 } , { 4 , 2 , 5 , 1 } , { 9 , 5 , 1 , 8 } } ; int n = arr . length ; System . out . print ( maximumSum ( arr , n ) ) ; } }
Pairs such that one is a power multiple of other | Java program to find pairs count ; function to count the required pairs ; sort the given array ; for each A [ i ] traverse rest array ; count Aj such that Ai * k ^ x = Aj ; increase x till Ai * k ^ x <= largest element ; Driver program
import java . io . * ; import java . util . * ; class GFG { static int countPairs ( int A [ ] , int n , int k ) { int ans = 0 ; Arrays . sort ( A ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int x = 0 ; while ( ( A [ i ] * Math . pow ( k , x ) ) <= A [ j ] ) { if ( ( A [ i ] * Math . pow ( k , x ) ) == A [ j ] ) { ans ++ ; break ; } x ++ ; } } } return ans ; } public static void main ( String [ ] args ) { int A [ ] = { 3 , 8 , 9 , 12 , 18 , 4 , 24 , 2 , 6 } ; int n = A . length ; int k = 3 ; System . out . println ( countPairs ( A , n , k ) ) ; } }
Minimum distance between two occurrences of maximum | Java program to find Min distance of maximum element ; function to return min distance ; case a ; case b ; case c ; Driver code
class GFG { static int minDistance ( int arr [ ] , int n ) { int maximum_element = arr [ 0 ] ; int min_dis = n ; int index = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( maximum_element == arr [ i ] ) { min_dis = Math . min ( min_dis , ( i - index ) ) ; index = i ; } else if ( maximum_element < arr [ i ] ) { maximum_element = arr [ i ] ; min_dis = n ; index = i ; } else continue ; } return min_dis ; } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 3 , 1 , 3 , 6 , 4 , 6 } ; int n = arr . length ; System . out . print ( " Minimum ▁ distance ▁ = ▁ " + minDistance ( arr , n ) ) ; } }
Find final value if we double after every successful search in array | Java program to find value if we double the value after every successful search ; Function to Find the value of k ; Search for k . After every successful search , double k . ; Driver Code
class GFG { static int findValue ( int arr [ ] , int n , int k ) { for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] == k ) k *= 2 ; return k ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 4 , 10 , 8 , 1 } , k = 2 ; int n = arr . length ; System . out . print ( findValue ( arr , n , k ) ) ; } }
Last duplicate element in a sorted array | Java code to print last duplicate element and its index in a sorted array ; if array is null or size is less than equal to 0 return ; compare elements and return last duplicate and its index ; If we reach here , then no duplicate found . ; Driver code
import java . io . * ; class GFG { static void dupLastIndex ( int arr [ ] , int n ) { if ( arr == null n <= 0 ) return ; for ( int i = n - 1 ; i > 0 ; i -- ) { if ( arr [ i ] == arr [ i - 1 ] ) { System . out . println ( " Last ▁ index : " + i ) ; System . out . println ( " Last ▁ duplicate ▁ item : ▁ " + arr [ i ] ) ; return ; } } System . out . print ( " no ▁ duplicate ▁ found " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 5 , 6 , 6 , 7 , 9 } ; int n = arr . length ; dupLastIndex ( arr , n ) ; } }
Find an array element such that all elements are divisible by it | Java program to find an array element that divides all numbers in the array using naive approach ; function to find smallest num ; traverse for all elements ; stores the minimum if it divides all ; driver code
import java . io . * ; class GFG { static int findSmallest ( int a [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int j ; for ( j = 0 ; j < n ; j ++ ) if ( a [ j ] % a [ i ] >= 1 ) break ; if ( j == n ) return a [ i ] ; } return - 1 ; } public static void main ( String args [ ] ) { int a [ ] = { 25 , 20 , 5 , 10 , 100 } ; int n = a . length ; System . out . println ( findSmallest ( a , n ) ) ; } }
Find an array element such that all elements are divisible by it | Java Program to find the smallest number that divides all numbers in an array ; function to find the smallest element ; function to find smallest num ; Find the smallest element ; Check if all array elements are divisible by smallest . ; Driver code
import java . io . * ; class GFG { static int min_element ( int a [ ] ) { int min = Integer . MAX_VALUE , i ; for ( i = 0 ; i < a . length ; i ++ ) { if ( a [ i ] < min ) min = a [ i ] ; } return min ; } static int findSmallest ( int a [ ] , int n ) { int smallest = min_element ( a ) ; for ( int i = 1 ; i < n ; i ++ ) if ( a [ i ] % smallest >= 1 ) return - 1 ; return smallest ; } public static void main ( String args [ ] ) { int a [ ] = { 25 , 20 , 5 , 10 , 100 } ; int n = a . length ; System . out . println ( findSmallest ( a , n ) ) ; } }
Maximum in array which is at | Java program for Maximum of the array which is at least twice of other elements of the array . ; Function to find the index of Max element that satisfies the condition ; Finding index of max of the array ; Returns - 1 if the max element is not twice of the i - th element . ; Driver function
import java . util . * ; import java . lang . * ; class GfG { public static int findIndex ( int [ ] arr ) { int maxIndex = 0 ; for ( int i = 0 ; i < arr . length ; ++ i ) if ( arr [ i ] > arr [ maxIndex ] ) maxIndex = i ; for ( int i = 0 ; i < arr . length ; ++ i ) if ( maxIndex != i && arr [ maxIndex ] < 2 * arr [ i ] ) return - 1 ; return maxIndex ; } public static void main ( String argc [ ] ) { int [ ] arr = new int [ ] { 3 , 6 , 1 , 0 } ; System . out . println ( findIndex ( arr ) ) ; } }
Consecutive steps to roof top | Java code to find maximum number of consecutive steps . ; Function to count consecutive steps ; count the number of consecutive increasing height building ; Driver code
import java . io . * ; class GFG { static int find_consecutive_steps ( int arr [ ] , int len ) { int count = 0 ; int maximum = 0 ; for ( int index = 1 ; index < len ; index ++ ) { if ( arr [ index ] > arr [ index - 1 ] ) count ++ ; else { maximum = Math . max ( maximum , count ) ; count = 0 ; } } return Math . max ( maximum , count ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int len = arr . length ; System . out . println ( find_consecutive_steps ( arr , len ) ) ; } }
Maximum difference between groups of size two | Java program to find minimum difference between groups of highest and lowest sums . ; Sorting the whole array . ; Driver code
import java . util . Arrays ; import java . io . * ; class GFG { static int CalculateMax ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int min_sum = arr [ 0 ] + arr [ 1 ] ; int max_sum = arr [ n - 1 ] + arr [ n - 2 ] ; return ( Math . abs ( max_sum - min_sum ) ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 7 , 1 , 11 } ; int n = arr . length ; System . out . println ( CalculateMax ( arr , n ) ) ; } }
Minimum difference between groups of size two | Java program to find minimum difference between groups of highest and lowest sums . ; Sorting the whole array . ; Generating sum groups . ; Driver code
import java . util . Arrays ; import java . util . Collections ; import java . util . Vector ; class GFG { static long calculate ( long a [ ] , int n ) { Arrays . sort ( a ) ; int i , j ; Vector < Long > s = new Vector < > ( ) ; for ( i = 0 , j = n - 1 ; i < j ; i ++ , j -- ) s . add ( ( a [ i ] + a [ j ] ) ) ; long mini = Collections . min ( s ) ; long maxi = Collections . max ( s ) ; return Math . abs ( maxi - mini ) ; } public static void main ( String [ ] args ) { long a [ ] = { 2 , 6 , 4 , 3 } ; int n = a . length ; System . out . println ( calculate ( a , n ) ) ; } }
Closest numbers from a list of unsorted integers | Java program to find minimum difference an unsorted array . ; Returns minimum difference between any two pair in arr [ 0. . n - 1 ] ; Sort array elements ; Compare differences of adjacent pairs to find the minimum difference . ; Traverse array again and print all pairs with difference as minDiff . ; Driver code
import java . util . * ; class GFG { static void printMinDiffPairs ( int arr [ ] , int n ) { if ( n <= 1 ) return ; Arrays . sort ( arr ) ; int minDiff = arr [ 1 ] - arr [ 0 ] ; for ( int i = 2 ; i < n ; i ++ ) minDiff = Math . min ( minDiff , arr [ i ] - arr [ i - 1 ] ) ; for ( int i = 1 ; i < n ; i ++ ) { if ( ( arr [ i ] - arr [ i - 1 ] ) == minDiff ) { System . out . print ( " ( " + arr [ i - 1 ] + " , ▁ " + arr [ i ] + " ) , " ) ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 3 , 2 , 4 , 1 } ; int n = arr . length ; printMinDiffPairs ( arr , n ) ; } }
Maximum absolute difference of value and index sums | Java program to calculate the maximum absolute difference of an array . ; Utility function to calculate the value of absolute difference for the pair ( i , j ) . ; Function to return maximum absolute difference in brute force . ; Variable for storing the maximum absolute distance throughout the traversal of loops . ; Iterate through all pairs . ; If the absolute difference of current pair ( i , j ) is greater than the maximum difference calculated till now , update the value of result . ; Driver program to test above function
public class MaximumAbsoluteDifference { private static int calculateDiff ( int i , int j , int [ ] array ) { return Math . abs ( array [ i ] - array [ j ] ) + Math . abs ( i - j ) ; } private static int maxDistance ( int [ ] array ) { int result = 0 ; for ( int i = 0 ; i < array . length ; i ++ ) { for ( int j = i ; j < array . length ; j ++ ) { result = Math . max ( result , calculateDiff ( i , j , array ) ) ; } } return result ; } public static void main ( String [ ] args ) { int [ ] array = { - 70 , - 64 , - 6 , - 56 , 64 , 61 , - 57 , 16 , 48 , - 98 } ; System . out . println ( maxDistance ( array ) ) ; } }
Maximum absolute difference of value and index sums | Java program to calculate the maximum absolute difference of an array . ; Function to return maximum absolute difference in linear time . ; max and min variables as described in algorithm . ; Updating max and min variables as described in algorithm . ; Calculating maximum absolute difference . ; Driver program to test above function
public class MaximumAbsoluteDifference { private static int maxDistance ( int [ ] array ) { int max1 = Integer . MIN_VALUE ; int min1 = Integer . MAX_VALUE ; int max2 = Integer . MIN_VALUE ; int min2 = Integer . MAX_VALUE ; for ( int i = 0 ; i < array . length ; i ++ ) { max1 = Math . max ( max1 , array [ i ] + i ) ; min1 = Math . min ( min1 , array [ i ] + i ) ; max2 = Math . max ( max2 , array [ i ] - i ) ; min2 = Math . min ( min2 , array [ i ] - i ) ; } return Math . max ( max1 - min1 , max2 - min2 ) ; } public static void main ( String [ ] args ) { int [ ] array = { - 70 , - 64 , - 6 , - 56 , 64 , 61 , - 57 , 16 , 48 , - 98 } ; System . out . println ( maxDistance ( array ) ) ; } }
Number of local extrema in an array | Java to find number of extrema ; function to find local extremum ; start loop from position 1 till n - 1 ; check if a [ i ] is greater than both its neighbours then add 1 to x ; check if a [ i ] is less than both its neighbours , then add 1 to x ; driver program
import java . io . * ; class GFG { static int extrema ( int a [ ] , int n ) { int count = 0 ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( a [ i ] > a [ i - 1 ] && a [ i ] > a [ i + 1 ] ) count += 1 ; if ( a [ i ] < a [ i - 1 ] && a [ i ] < a [ i + 1 ] ) count += 1 ; } return count ; } public static void main ( String args [ ] ) throws IOException { int a [ ] = { 1 , 0 , 2 , 1 } ; int n = a . length ; System . out . println ( extrema ( a , n ) ) ; } }
Find closest number in array | Java program to find element closet to given target . ; Returns element closest to target in arr [ ] ; Corner cases ; Doing binary search ; If target is less than array element , then search in left ; If target is greater than previous to mid , return closest of two ; Repeat for left half ; If target is greater than mid ; update i ; Only single element left after search ; Method to compare which one is the more close We find the closest by taking the difference between the target and both values . It assumes that val2 is greater than val1 and target lies between these two . ; Driver code
import java . util . * ; import java . lang . * ; import java . io . * ; class FindClosestNumber { public static int findClosest ( int arr [ ] , int target ) { int n = arr . length ; if ( target <= arr [ 0 ] ) return arr [ 0 ] ; if ( target >= arr [ n - 1 ] ) return arr [ n - 1 ] ; int i = 0 , j = n , mid = 0 ; while ( i < j ) { mid = ( i + j ) / 2 ; if ( arr [ mid ] == target ) return arr [ mid ] ; if ( target < arr [ mid ] ) { if ( mid > 0 && target > arr [ mid - 1 ] ) return getClosest ( arr [ mid - 1 ] , arr [ mid ] , target ) ; j = mid ; } else { if ( mid < n - 1 && target < arr [ mid + 1 ] ) return getClosest ( arr [ mid ] , arr [ mid + 1 ] , target ) ; i = mid + 1 ; } } return arr [ mid ] ; } public static int getClosest ( int val1 , int val2 , int target ) { if ( target - val1 >= val2 - target ) return val2 ; else return val1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 4 , 5 , 6 , 6 , 8 , 9 } ; int target = 11 ; System . out . println ( findClosest ( arr , target ) ) ; } }
Number of pairs with maximum sum | Java program to count pairs with maximum sum . ; function to find the number of maximum pair sums ; traverse through all the pairs ; traverse through all pairs and keep a count of the number of maximum pairs ; driver program to test the above function
class GFG { static int sum ( int a [ ] , int n ) { int maxSum = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) maxSum = Math . max ( maxSum , a [ i ] + a [ j ] ) ; int c = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( a [ i ] + a [ j ] == maxSum ) c ++ ; return c ; } public static void main ( String [ ] args ) { int array [ ] = { 1 , 1 , 1 , 2 , 2 , 2 } ; int n = array . length ; System . out . println ( sum ( array , n ) ) ; } }
Number of pairs with maximum sum | Java program to count pairs with maximum sum . ; function to find the number of maximum pair sums ; Find maximum and second maximum elements . Also find their counts . ; If maximum element appears more than once . ; If maximum element appears only once . ; driver program
import java . io . * ; class GFG { static int sum ( int a [ ] , int n ) { int maxVal = a [ 0 ] , maxCount = 1 ; int secondMax = Integer . MIN_VALUE , secondMaxCount = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( a [ i ] == maxVal ) maxCount ++ ; else if ( a [ i ] > maxVal ) { secondMax = maxVal ; secondMaxCount = maxCount ; maxVal = a [ i ] ; maxCount = 1 ; } else if ( a [ i ] == secondMax ) { secondMax = a [ i ] ; secondMaxCount ++ ; } else if ( a [ i ] > secondMax ) { secondMax = a [ i ] ; secondMaxCount = 1 ; } } if ( maxCount > 1 ) return maxCount * ( maxCount - 1 ) / 2 ; return secondMaxCount ; } public static void main ( String [ ] args ) { int array [ ] = { 1 , 1 , 1 , 2 , 2 , 2 , 3 } ; int n = array . length ; System . out . println ( sum ( array , n ) ) ; } }
Find first k natural numbers missing in given array | Java program to find missing k numbers in an array . ; Prints first k natural numbers in arr [ 0. . n - 1 ] ; Find first positive number ; Now find missing numbers between array elements ; Find missing numbers after maximum . ; Driver code
import java . util . Arrays ; class GFG { static void printKMissing ( int [ ] arr , int n , int k ) { Arrays . sort ( arr ) ; int i = 0 ; while ( i < n && arr [ i ] <= 0 ) i ++ ; int count = 0 , curr = 1 ; while ( count < k && i < n ) { if ( arr [ i ] != curr ) { System . out . print ( curr + " ▁ " ) ; count ++ ; } else i ++ ; curr ++ ; } while ( count < k ) { System . out . print ( curr + " ▁ " ) ; curr ++ ; count ++ ; } } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 4 } ; int n = arr . length ; int k = 3 ; printKMissing ( arr , n , k ) ; } }
Find first k natural numbers missing in given array | Java code for the above approach ; Program to print first k missing number ; Creating a hashmap ; Iterate over array ; Iterate to find missing element ; Driver Code
import java . io . * ; import java . util . HashMap ; class GFG { static void printmissingk ( int arr [ ] , int n , int k ) { HashMap < Integer , Integer > d = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) d . put ( arr [ i ] , arr [ i ] ) ; int cnt = 1 ; int fl = 0 ; for ( int i = 0 ; i < ( n + k ) ; i ++ ) { if ( ! d . containsKey ( cnt ) ) { fl += 1 ; System . out . print ( cnt + " ▁ " ) ; if ( fl == k ) break ; } cnt += 1 ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 4 , 3 } ; int n = arr . length ; int k = 3 ; printmissingk ( arr , n , k ) ; } }