codeparrot/xlcost-text-to-code huggingface dataset:
Check if it is possible to form string B from A under the given constraints | Java implementation of the approach ; Function that returns true if it is possible to form B from A satisfying the given conditions ; List to store the frequency of characters in A ; Vector to store the count of characters used from a particular group of characters ; Store the frequency of the characters ; If a character in B is not present in A ; If count of characters used from a particular group of characters exceeds m ; Update low to the starting index of the next group ; If count of characters used from a particular group of characters has not exceeded m ; Driver code
import java . io . * ; import java . util . * ; class GFG { static boolean isPossible ( String A , String B , int b , int m ) { List < List < Integer > > S = new ArrayList < List < Integer > > ( ) ; for ( int i = 0 ; i < 26 ; i ++ ) S . add ( new ArrayList < Integer > ( ) ) ; int [ ] box = new int [ A . length ( ) ] ; for ( int i = 0 ; i < A . length ( ) ; i ++ ) { S . get ( A . charAt ( i ) - ' a ' ) . add ( i ) ; } int low = 0 ; for ( int i = 0 ; i < B . length ( ) ; i ++ ) { List < Integer > indexes = S . get ( B . charAt ( i ) - ' a ' ) ; int it = lower_bound ( indexes , low ) ; if ( it == indexes . size ( ) ) return false ; int count = indexes . get ( it ) / b ; box [ count ] = box [ count ] + 1 ; if ( box [ count ] >= m ) { count ++ ; low = ( count ) * b ; } else low = indexes . get ( it ) + 1 ; } return true ; } static int lower_bound ( List < Integer > indexes , int k ) { int low = 0 , high = indexes . size ( ) - 1 ; while ( low < high ) { int mid = ( low + high ) / 2 ; if ( indexes . get ( mid ) < k ) low = mid + 1 ; else high = mid ; } return ( indexes . get ( low ) < k ) ? low + 1 : low ; } public static void main ( String [ ] args ) { String A = " abcbbcdefxyz " ; String B = " acdxyz " ; int b = 5 ; int m = 2 ; if ( isPossible ( A , B , b , m ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Remove all occurrences of any element for maximum array sum | Java program to convert fractional decimal to binary number ; Find total sum and frequencies of elements ; Find minimum value to be subtracted . ; Find maximum sum after removal ; Drivers code
import java . util . * ; class GFG { static int maxSumArray ( int arr [ ] , int n ) { int sum = 0 ; Map < Integer , Integer > m = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; if ( m . containsKey ( arr [ i ] ) ) { m . put ( arr [ i ] , m . get ( arr [ i ] ) + 1 ) ; } else { m . put ( arr [ i ] , 1 ) ; } } int minimum = Integer . MAX_VALUE ; for ( Map . Entry < Integer , Integer > x : m . entrySet ( ) ) minimum = Math . min ( minimum , x . getValue ( ) * x . getKey ( ) ) ; return ( sum - minimum ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 3 , 3 , 2 , 2 , 1 , 1 , 1 } ; int n = arr . length ; System . out . println ( maxSumArray ( arr , n ) ) ; } }
Given an array and two integers l and r , find the kth largest element in the range [ l , r ] | Java implementation of the approach ; Function to calculate the prefix ; Creating one based indexing ; Initializing and creating prefix array ; Creating a prefix array for every possible value in a given range ; Function to return the kth largest element in the index range [ l , r ] ; Binary searching through the 2d array and only checking the range in which the sub array is a part ; Driver code ; Creating the prefix array for the given array ; Queries ; Perform queries
import java . util . * ; class GFG { static int MAX = 1001 ; static int prefix [ ] [ ] = new int [ MAX ] [ MAX ] ; static int ar [ ] = new int [ MAX ] ; static void cal_prefix ( int n , int arr [ ] ) { int i , j ; for ( i = 0 ; i < n ; i ++ ) ar [ i + 1 ] = arr [ i ] ; for ( i = 1 ; i <= 1000 ; i ++ ) { for ( j = 0 ; j <= n ; j ++ ) prefix [ i ] [ j ] = 0 ; for ( j = 1 ; j <= n ; j ++ ) { prefix [ i ] [ j ] = prefix [ i ] [ j - 1 ] + ( int ) ( ar [ j ] <= i ? 1 : 0 ) ; } } } static int ksub ( int l , int r , int n , int k ) { int lo , hi , mid ; lo = 1 ; hi = 1000 ; while ( lo + 1 < hi ) { mid = ( lo + hi ) / 2 ; if ( prefix [ mid ] [ r ] - prefix [ mid ] [ l - 1 ] >= k ) hi = mid ; else lo = mid + 1 ; } if ( prefix [ lo ] [ r ] - prefix [ lo ] [ l - 1 ] >= k ) hi = lo ; return hi ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 4 , 2 , 3 , 5 , 7 , 6 } ; int n = arr . length ; int k = 4 ; cal_prefix ( n , arr ) ; int queries [ ] [ ] = { { 1 , n , 1 } , { 2 , n - 2 , 2 } , { 3 , n - 1 , 3 } } ; int q = queries . length ; for ( int i = 0 ; i < q ; i ++ ) System . out . println ( ksub ( queries [ i ] [ 0 ] , queries [ i ] [ 1 ] , n , queries [ i ] [ 2 ] ) ) ; } }
Find maximum N such that the sum of square of first N natural numbers is not more than X | Java implementation of the approach ; Function to return the sum of the squares of first N natural numbers ; Function to return the maximum N such that the sum of the squares of first N natural numbers is not more than X ; Iterate through the maxvalue of N and find the ith position ; If the condition fails then return the i - 1 i . e sum of squares till i - 1 ; Driver code
class GFG { static long squareSum ( long N ) { long sum = ( long ) ( N * ( N + 1 ) * ( 2 * N + 1 ) ) / 6 ; return sum ; } static long findMaxN ( long X ) { long N = ( long ) Math . sqrt ( X ) ; for ( long i = 1 ; i <= N ; i ++ ) { if ( squareSum ( i ) > X ) return i - 1 ; } return - 1 ; } public static void main ( String [ ] args ) { long X = 25 ; System . out . println ( findMaxN ( X ) ) ; } }
Remove exactly one element from the array such that max | Java implementation of the above approach ; function to calculate max - min ; There should be at - least two elements ; To store first and second minimums ; To store first and second maximums ; Driver code
class GFG { static int max_min ( int a [ ] , int n ) { if ( n <= 1 ) return Integer . MAX_VALUE ; int f_min = a [ 0 ] , s_min = Integer . MAX_VALUE ; int f_max = a [ 0 ] , s_max = Integer . MIN_VALUE ; for ( int i = 1 ; i < n ; i ++ ) { if ( a [ i ] <= f_min ) { s_min = f_min ; f_min = a [ i ] ; } else if ( a [ i ] < s_min ) { s_min = a [ i ] ; } if ( a [ i ] >= f_max ) { s_max = f_max ; f_max = a [ i ] ; } else if ( a [ i ] > s_max ) { s_max = a [ i ] ; } } return Math . min ( ( f_max - s_min ) , ( s_max - f_min ) ) ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 3 , 3 , 7 } ; int n = a . length ; System . out . println ( max_min ( a , n ) ) ; } }
Minimum in an array which is first decreasing then increasing | Java program to find the smallest number in an array of decrease and increasing numbers ; Function to find the smallest number 's index ; Do a binary search ; Find the mid element ; Check for break point ; Return the index ; Driver Code ; Print the smallest number
class Solution { static int minimal ( int a [ ] , int n ) { int lo = 0 , hi = n - 1 ; while ( lo < hi ) { int mid = ( lo + hi ) >> 1 ; if ( a [ mid ] < a [ mid + 1 ] ) { hi = mid ; } else { lo = mid + 1 ; } } return lo ; } public static void main ( String args [ ] ) { int a [ ] = { 8 , 5 , 4 , 3 , 4 , 10 } ; int n = a . length ; int ind = minimal ( a , n ) ; System . out . println ( a [ ind ] ) ; } }
Leftmost and rightmost indices of the maximum and the minimum element of an array | Java implementation of the approach ; If found new minimum ; If arr [ i ] = min then rightmost index for min will change ; If found new maximum ; If arr [ i ] = max then rightmost index for max will change ; Driver code
public class GFG { public static void findIndices ( int arr [ ] , int n ) { int leftMin = 0 , rightMin = 0 ; int leftMax = 0 , rightMax = 0 ; int min = arr [ 0 ] , max = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] < min ) { leftMin = rightMin = i ; min = arr [ i ] ; } else if ( arr [ i ] == min ) rightMin = i ; if ( arr [ i ] > max ) { leftMax = rightMax = i ; max = arr [ i ] ; } else if ( arr [ i ] == max ) rightMax = i ; } System . out . println ( " Minimum ▁ left ▁ : ▁ " + leftMin ) ; System . out . println ( " Minimum ▁ right ▁ : ▁ " + rightMin ) ; System . out . println ( " Maximum ▁ left ▁ : ▁ " + leftMax ) ; System . out . println ( " Maximum ▁ right ▁ : ▁ " + rightMax ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 1 , 1 , 2 , 1 , 5 , 6 , 5 } ; int n = arr . length ; findIndices ( arr , n ) ; } }
Find smallest and largest element from square matrix diagonals | Java program to find smallest and largest elements of both diagonals ; Function to find smallest and largest element from principal and secondary diagonal ; take length of matrix ; declare and initialize variables with appropriate value ; Condition for principal diagonal ; take new smallest value ; take new largest value ; Condition for secondary diagonal ; take new smallest value ; take new largest value ; Driver code ; Declare and initialize 5 X5 matrix
public class GFG { static void diagonalsMinMax ( int [ ] [ ] mat ) { int n = mat . length ; if ( n == 0 ) return ; int principalMin = mat [ 0 ] [ 0 ] , principalMax = mat [ 0 ] [ 0 ] ; int secondaryMin = mat [ n - 1 ] [ 0 ] , secondaryMax = mat [ n - 1 ] [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 1 ; j < n ; j ++ ) { if ( i == j ) { if ( mat [ i ] [ j ] < principalMin ) { principalMin = mat [ i ] [ j ] ; } if ( mat [ i ] [ j ] > principalMax ) { principalMax = mat [ i ] [ j ] ; } } if ( ( i + j ) == ( n - 1 ) ) { if ( mat [ i ] [ j ] < secondaryMin ) { secondaryMin = mat [ i ] [ j ] ; } if ( mat [ i ] [ j ] > secondaryMax ) { secondaryMax = mat [ i ] [ j ] ; } } } } System . out . println ( " Principal ▁ Diagonal ▁ Smallest ▁ Element : ▁ " + principalMin ) ; System . out . println ( " Principal ▁ Diagonal ▁ Greatest ▁ Element ▁ : ▁ " + principalMax ) ; System . out . println ( " Secondary ▁ Diagonal ▁ Smallest ▁ Element : ▁ " + secondaryMin ) ; System . out . println ( " Secondary ▁ Diagonal ▁ Greatest ▁ Element : ▁ " + secondaryMax ) ; } static public void main ( String [ ] args ) { int [ ] [ ] matrix = { { 1 , 2 , 3 , 4 , - 10 } , { 5 , 6 , 7 , 8 , 6 } , { 1 , 2 , 11 , 3 , 4 } , { 5 , 6 , 70 , 5 , 8 } , { 4 , 9 , 7 , 1 , - 5 } } ; diagonalsMinMax ( matrix ) ; } }
Indexed Sequential Search | Java program for Indexed Sequential Search ; Storing element ; Storing the index ; Driver code ; Element to search ; Function call
import java . io . * ; class GFG { static void indexedSequentialSearch ( int arr [ ] , int n , int k ) { int elements [ ] = new int [ 20 ] ; int indices [ ] = new int [ 20 ] ; int temp , i ; int j = 0 , ind = 0 , start = 0 , end = 0 , set = 0 ; for ( i = 0 ; i < n ; i += 3 ) { elements [ ind ] = arr [ i ] ; indices [ ind ] = i ; ind ++ ; } if ( k < elements [ 0 ] ) { System . out . println ( " Not ▁ found " ) ; return ; } else { for ( i = 1 ; i <= ind ; i ++ ) if ( k <= elements [ i ] ) { start = indices [ i - 1 ] ; set = 1 ; end = indices [ i ] ; break ; } } if ( set == 0 ) { start = indices [ i - 1 ] ; end = n ; } for ( i = start ; i <= end ; i ++ ) { if ( k == arr [ i ] ) { j = 1 ; break ; } } if ( j == 1 ) System . out . println ( " Found ▁ at ▁ index ▁ " + i ) ; else System . out . println ( " Not ▁ found " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 7 , 8 , 9 , 10 } ; int n = arr . length ; int k = 8 ; indexedSequentialSearch ( arr , n , k ) ; } }
Count elements such that there are exactly X elements with values greater than or equal to X | Java implementation of the approach ; Sorting the vector ; Count of numbers which are greater than v [ i ] ; Function to implement upper_bound ( ) ; Driver Code
import java . util . * ; class GFG { static int getCount ( int [ ] v , int n ) { Arrays . sort ( v ) ; int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int tmp = n - 1 - upperBound ( v , n , v [ i ] - 1 ) ; if ( tmp == v [ i ] ) cnt ++ ; } return cnt ; } static int upperBound ( int [ ] array , int length , int value ) { int low = 0 ; int high = length ; while ( low < high ) { final int mid = ( low + high ) / 2 ; if ( value >= array [ mid ] ) { low = mid + 1 ; } else { high = mid ; } } return low ; } public static void main ( String [ ] args ) { int n = 4 ; int [ ] v = { 1 , 2 , 3 , 4 } ; System . out . println ( getCount ( v , n ) ) ; } }
Number of segments where all elements are greater than X | Java program to count the number of segments in which all elements are greater than X ; Function to count number of segments ; Iterate in the array ; check if array element greater then X or not ; if flag is true ; After iteration complete check for the last segment ; Driver Code
import java . io . * ; class GFG { static int countSegments ( int a [ ] , int n , int x ) { boolean flag = false ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] > x ) { flag = true ; } else { if ( flag ) count += 1 ; flag = false ; } } if ( flag ) count += 1 ; return count ; } public static void main ( String [ ] args ) { int a [ ] = { 8 , 25 , 10 , 19 , 19 , 18 , 20 , 11 , 18 } ; int n = a . length ; int x = 13 ; System . out . println ( countSegments ( a , n , x ) ) ; } }
Find array elements with frequencies in range [ l , r ] | Java program to find the elements whose frequency lies in the range [ l , r ] ; Hash map which will store the frequency of the elements of the array . ; Increment the frequency of the element by 1. ; Print the element whose frequency lies in the range [ l , r ] ; Driver code
import java . util . HashMap ; import java . util . Map ; public class GFG { static void findElements ( int arr [ ] , int n , int l , int r ) { Map < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { int a = 0 ; if ( mp . get ( arr [ i ] ) == null ) { a = 1 ; } else { a = mp . get ( arr [ i ] ) + 1 ; } mp . put ( arr [ i ] , a ) ; } for ( int i = 0 ; i < n ; ++ i ) { if ( l <= mp . get ( arr [ i ] ) && ( mp . get ( arr [ i ] ) <= r ) ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 3 , 2 , 2 , 5 } ; int n = arr . length ; int l = 2 , r = 3 ; findElements ( arr , n , l , r ) ; } }
Count triplets ( a , b , c ) such that a + b , b + c and a + c are all divisible by K | Java implementation of the approach ; Function returns the count of the triplets ; iterate for all triples pairs ( i , j , l ) ; if the condition is satisfied ; Driver code
class GFG { static long count_triples ( int n , int k ) { int i = 0 , j = 0 , l = 0 ; int count = 0 ; for ( i = 1 ; i <= n ; i ++ ) { for ( j = 1 ; j <= n ; j ++ ) { for ( l = 1 ; l <= n ; l ++ ) { if ( ( i + j ) % k == 0 && ( i + l ) % k == 0 && ( j + l ) % k == 0 ) count ++ ; } } } return count ; } public static void main ( String [ ] args ) { int n = 3 ; int k = 2 ; long ans = count_triples ( n , k ) ; System . out . println ( ans ) ; } }
kth smallest / largest in a small range unsorted array | Java program of kth smallest / largest in a small range unsorted array ; Storing counts of elements ; Traverse hash array build above until we reach k - th smallest element . ; Driver code
import java . util . Arrays ; class GFG { static int maxs = 1000001 ; static int kthSmallestLargest ( int [ ] arr , int n , int k ) { int max_val = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; int hash [ ] = new int [ max_val + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) { hash [ arr [ i ] ] ++ ; } int count = 0 ; for ( int i = 0 ; i <= max_val ; i ++ ) { while ( hash [ i ] > 0 ) { count ++ ; if ( count == k ) { return i ; } hash [ i ] -- ; } } return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 11 , 6 , 2 , 9 , 4 , 3 , 16 } ; int n = arr . length , k = 3 ; System . out . println ( " kth ▁ smallest ▁ number ▁ is : ▁ " + kthSmallestLargest ( arr , n , k ) ) ; } }
Sum and Product of minimum and maximum element of an Array | Java program for the above approach ; Function to get sum ; Function to get product ; Driver code ; sorting the array ; min element would be the element at 0 th index of array after sorting ; max element would be the element at ( n - 1 ) th index of array after sorting
import java . io . * ; import java . util . Arrays ; class GFG { static int findSum ( int minEle , int maxEle ) { return minEle + maxEle ; } static int findProduct ( int minEle , int maxEle ) { return minEle * maxEle ; } public static void main ( String [ ] args ) { int [ ] arr = { 12 , 1234 , 45 , 67 , 1 } ; int n = arr . length ; Arrays . sort ( arr ) ; int minEle = arr [ 0 ] ; int maxEle = arr [ n - 1 ] ; System . out . println ( " Sum ▁ = ▁ " + findSum ( minEle , maxEle ) ) ; System . out . println ( " Product ▁ = ▁ " + findProduct ( minEle , maxEle ) ) ; } }
Meta Binary Search | One | Java implementation of above approach ; Function to show the working of Meta binary search ; Set number of bits to represent largest array index ; while ( ( 1 << lg ) < n - 1 ) { lg += 1 ; } ; Incrementally construct the index of the target value ; find the element in one direction and update position ; if element found return pos otherwise - 1 ; Driver code
import java . util . Vector ; import com . google . common . math . BigIntegerMath ; import java . math . * ; class GFG { static int bsearch ( Vector < Integer > A , int key_to_search ) { int n = ( int ) A . size ( ) ; int lg = BigIntegerMath . log2 ( BigInteger . valueOf ( n - 1 ) , RoundingMode . UNNECESSARY ) + 1 ; int pos = 0 ; for ( int i = lg - 1 ; i >= 0 ; i -- ) { if ( A . get ( pos ) == key_to_search ) { return pos ; } int new_pos = pos | ( 1 << i ) ; if ( ( new_pos < n ) && ( A . get ( new_pos ) <= key_to_search ) ) { pos = new_pos ; } } return ( ( A . get ( pos ) == key_to_search ) ? pos : - 1 ) ; } static public void main ( String [ ] args ) { Vector < Integer > A = new Vector < Integer > ( ) ; int [ ] arr = { - 2 , 10 , 100 , 250 , 32315 } ; for ( int i = 0 ; i < arr . length ; i ++ ) { A . add ( arr [ i ] ) ; } System . out . println ( bsearch ( A , 10 ) ) ; } }
Queries to check if a number lies in N ranges of L | Java program to check if the number lies in given range ; Function that answers every query ; container to store all range ; hash the L and R ; Push the element to container and hash the L and R ; sort the elements in container ; each query ; get the number same or greater than integer ; if it lies ; check if greater is hashed as 2 ; else check if greater is hashed as 1 ; Lower bound implementation ; Driver Code ; function call to answer queries
import java . io . * ; import java . util . * ; class GFG { static void answerQueries ( int [ ] [ ] a , int n , int [ ] queries , int q ) { Vector < Integer > v = new Vector < > ( ) ; HashMap < Integer , Integer > mpp = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { v . add ( a [ i ] [ 0 ] ) ; mpp . put ( a [ i ] [ 0 ] , 1 ) ; v . add ( a [ i ] [ 1 ] ) ; mpp . put ( a [ i ] [ 1 ] , 2 ) ; } Collections . sort ( v ) ; for ( int i = 0 ; i < q ; i ++ ) { int num = queries [ i ] ; int ind = lowerBound ( v , num ) ; if ( ind >= 0 && v . elementAt ( ind ) == num ) System . out . println ( " Yes " ) ; else { if ( ind >= 0 && mpp . get ( v . elementAt ( ind ) ) == 2 ) System . out . println ( " Yes " ) ; System . out . println ( " No " ) ; } } } static int lowerBound ( Vector < Integer > array , int value ) { int low = 0 ; int high = array . size ( ) ; while ( low < high ) { final int mid = ( low + high ) / 2 ; if ( value <= array . elementAt ( mid ) ) { high = mid ; } else { low = mid + 1 ; } } return low ; } public static void main ( String [ ] args ) { int [ ] [ ] a = { { 5 , 6 } , { 1 , 3 } , { 8 , 10 } } ; int n = 3 ; int [ ] queries = { 2 , 3 , 4 , 7 } ; int q = queries . length ; answerQueries ( a , n , queries , q ) ; } }
Median of two sorted arrays of different sizes | Set 1 ( Linear ) | A Java program to find median of two sorted arrays of unequal sizes ; This function returns median of a [ ] and b [ ] . Assumptions in this function : Both a [ ] and b [ ] are sorted arrays ; Current index of i / p array a [ ] ; Current index of i / p array b [ ] ; Below is to handle the case where all elements of a [ ] are smaller than smallest ( or first ) element of b [ ] or a [ ] is empty ; Below is to handle case where all elements of b [ ] are smaller than smallest ( or first ) element of a [ ] or b [ ] is empty ; Below is to handle the case where sum of number of elements of the arrays is even ; Below is to handle the case where sum of number of elements of the arrays is odd ; Driver program to test above functions
import java . io . * ; class GFG { static float findmedian ( int a [ ] , int n1 , int b [ ] , int n2 ) { int i = 0 ; int j = 0 ; int k ; int m1 = - 1 , m2 = - 1 ; for ( k = 0 ; k <= ( n1 + n2 ) / 2 ; k ++ ) { if ( i < n1 && j < n2 ) { if ( a [ i ] < b [ j ] ) { m2 = m1 ; m1 = a [ i ] ; i ++ ; } else { m2 = m1 ; m1 = b [ j ] ; j ++ ; } } else if ( i == n1 ) { m2 = m1 ; m1 = b [ j ] ; j ++ ; } else if ( j == n2 ) { m2 = m1 ; m1 = a [ i ] ; i ++ ; } } if ( ( n1 + n2 ) % 2 == 0 ) { return ( m1 + m2 ) * ( float ) 1.0 / 2 ; } return m1 ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 12 , 15 , 26 , 38 } ; int b [ ] = { 2 , 13 , 24 } ; int n1 = a . length ; int n2 = b . length ; System . out . println ( findmedian ( a , n1 , b , n2 ) ) ; } }
Next Smaller Element | Simple Java program to print next smaller elements in a given array ; prints element and NSE pair for all elements of arr [ ] of size n ; Driver Code
class Main { static void printNSE ( int arr [ ] , int n ) { int next , i , j ; for ( i = 0 ; i < n ; i ++ ) { next = - 1 ; for ( j = i + 1 ; j < n ; j ++ ) { if ( arr [ i ] > arr [ j ] ) { next = arr [ j ] ; break ; } } System . out . println ( arr [ i ] + " ▁ - - ▁ " + next ) ; } } public static void main ( String args [ ] ) { int arr [ ] = { 11 , 13 , 21 , 3 } ; int n = arr . length ; printNSE ( arr , n ) ; } }
Longest subarray such that the difference of max and min is at | Java code to find longest subarray with difference between max and min as at - most 1. ; longest constant range length ; first number ; If we see same number ; If we see different number , but same as previous . ; If number is neither same as previous nor as current . ; Driver code
public class Demo { public static int longestSubarray ( int [ ] input ) { int prev = - 1 ; int current , next ; int prevCount = 0 , currentCount = 1 ; int longest = 1 ; current = input [ 0 ] ; for ( int i = 1 ; i < input . length ; i ++ ) { next = input [ i ] ; if ( next == current ) { currentCount ++ ; } else if ( next == prev ) { prevCount += currentCount ; prev = current ; current = next ; currentCount = 1 ; } else { longest = Math . max ( longest , currentCount + prevCount ) ; prev = current ; prevCount = currentCount ; current = next ; currentCount = 1 ; } } return Math . max ( longest , currentCount + prevCount ) ; } public static void main ( String [ ] args ) { int [ ] input = { 5 , 5 , 6 , 7 , 6 } ; System . out . println ( longestSubarray ( input ) ) ; } }