codeparrot/xlcost-text-to-code huggingface dataset:
Minimum removals required to make a given array Bitonic | Java program to implement the above approach ; Function to coutnt minimum array elements required to be removed to make an array bitonic ; left [ i ] : Stores the length of LIS up to i - th index ; right [ i ] : Stores the length of decreasing subsequence over the range [ i , N ] ; Calculate the length of LIS up to i - th index ; Traverse the array upto i - th index ; If arr [ j ] is less than arr [ i ] ; Update left [ i ] ; Calculate the length of decreasing subsequence over the range [ i , N ] ; Traverse right [ ] array ; If arr [ i ] is greater than arr [ j ] ; Update right [ i ] ; Stores length of the longest bitonic array ; Traverse left [ ] and right [ ] array ; Update maxLen ; Function to print minimum removals required to make given array bitonic ; Driver Code
class GFG { static void min_element_removal ( int arr [ ] , int N ) { int left [ ] = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) left [ i ] = 1 ; int right [ ] = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) right [ i ] = 1 ; for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( arr [ j ] < arr [ i ] ) { left [ i ] = Math . max ( left [ i ] , left [ j ] + 1 ) ; } } } for ( int i = N - 2 ; i >= 0 ; i -- ) { for ( int j = N - 1 ; j > i ; j -- ) { if ( arr [ i ] > arr [ j ] ) { right [ i ] = Math . max ( right [ i ] , right [ j ] + 1 ) ; } } } int maxLen = 0 ; for ( int i = 1 ; i < N - 1 ; i ++ ) { maxLen = Math . max ( maxLen , left [ i ] + right [ i ] - 1 ) ; } System . out . println ( N - maxLen ) ; } static void makeBitonic ( int arr [ ] , int N ) { if ( N == 1 ) { System . out . println ( "0" ) ; return ; } if ( N == 2 ) { if ( arr [ 0 ] != arr [ 1 ] ) System . out . println ( "0" ) ; else System . out . println ( "1" ) ; return ; } min_element_removal ( arr , N ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 1 , 1 , 5 , 6 , 2 , 3 , 1 } ; int N = arr . length ; makeBitonic ( arr , N ) ; } }
Count subarrays having an equal count of 0 s and 1 s segregated | Java program for the above approach ; Function to count subarrays having equal count of 0 s and 1 s with all 0 s and all 1 s grouped together ; Stores the count of subarrays ; If current element is different from the next array element ; Increment count ; Count the frequency of 1 s and 0 s ; Increment count ; Print the final count ; Driver Code ; Function Call
import java . util . * ; class GFG { static void countSubarrays ( int A [ ] , int N ) { int ans = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( A [ i ] != A [ i + 1 ] ) { ans ++ ; for ( int j = i - 1 , k = i + 2 ; j >= 0 && k < N && A [ j ] == A [ i ] && A [ k ] == A [ i + 1 ] ; j -- , k ++ ) { ans ++ ; } } } System . out . print ( ans + "NEW_LINE"); } public static void main ( String [ ] args ) { int A [ ] = { 1 , 1 , 0 , 0 , 1 , 0 } ; int N = A . length ; countSubarrays ( A , N ) ; } }
Count quadruples of given type from given array | Java program of the above approach ; lcount [ i ] [ j ] : Stores the count of i on left of index j ; rcount [ i ] [ j ] : Stores the count of i on right of index j ; Function to count unique elements on left and right of any index ; Find the maximum array element ; Calculate prefix sum of counts of each value ; Calculate suffix sum of counts of each value ; Function to count quadruples of the required type ; Driver Code
import java . util . * ; class GFG { static int maxN = 2002 ; static int [ ] [ ] lcount = new int [ maxN ] [ maxN ] ; static int [ ] [ ] rcount = new int [ maxN ] [ maxN ] ; static void fill_counts ( int a [ ] , int n ) { int i , j ; int maxA = a [ 0 ] ; for ( i = 0 ; i < n ; i ++ ) { if ( a [ i ] > maxA ) { maxA = a [ i ] ; } } for ( i = 0 ; i < n ; i ++ ) { lcount [ a [ i ] ] [ i ] = 1 ; rcount [ a [ i ] ] [ i ] = 1 ; } for ( i = 0 ; i <= maxA ; i ++ ) { for ( j = 1 ; j < n ; j ++ ) { lcount [ i ] [ j ] = lcount [ i ] [ j - 1 ] + lcount [ i ] [ j ] ; } for ( j = n - 2 ; j >= 0 ; j -- ) { rcount [ i ] [ j ] = rcount [ i ] [ j + 1 ] + rcount [ i ] [ j ] ; } } } static int countSubsequence ( int a [ ] , int n ) { int i , j ; fill_counts ( a , n ) ; int answer = 0 ; for ( i = 1 ; i < n ; i ++ ) { for ( j = i + 1 ; j < n - 1 ; j ++ ) { answer += lcount [ a [ j ] ] [ i - 1 ] * rcount [ a [ i ] ] [ j + 1 ] ; } } return answer ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 3 , 2 , 1 , 3 , 2 } ; System . out . print ( countSubsequence ( a , a . length ) ) ; } }
Reduce string by removing outermost parenthesis from each primitive substring | Java program to implement the above approach ; Function to remove the outermost parentheses of every primitive substring from the given string ; Stores the resultant string ; Stores the count of opened parentheses ; Traverse the string ; If opening parenthesis is encountered and their count exceeds 0 ; Include the character ; If closing parenthesis is encountered and their count is less than count of opening parentheses ; Include the character ; Return the resultant string ; Driver Code
import java . io . * ; class GFG { static String removeOuterParentheses ( String S ) { String res = " " ; int count = 0 ; for ( int c = 0 ; c < S . length ( ) ; c ++ ) { if ( S . charAt ( c ) == ' ( ' && count ++ > 0 ) res += S . charAt ( c ) ; if ( S . charAt ( c ) == ' ) ' && count -- > 1 ) res += S . charAt ( c ) ; } return res ; } public static void main ( String [ ] args ) { String S = " ( ( ) ( ) ) ( ( ) ) ( ) " ; System . out . print ( removeOuterParentheses ( S ) ) ; } }
Length of longest subarray with increasing contiguous elements | Java implementation for the above approach ; Function to find the longest subarray with increasing contiguous elements ; Stores the length of required longest subarray ; Stores the length of length of longest such subarray from ith index ; If consecutive elements are increasing and differ by 1 ; Otherwise ; Update the longest subarray obtained so far ; Return the length obtained ; Driver Code
import java . util . * ; class GFG { public static int maxiConsecutiveSubarray ( int arr [ ] , int N ) { int maxi = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { int cnt = 1 , j ; for ( j = i ; j < N - 1 ; j ++ ) { if ( arr [ j + 1 ] == arr [ j ] + 1 ) { cnt ++ ; } else { break ; } } maxi = Math . max ( maxi , cnt ) ; i = j ; } return maxi ; } public static void main ( String args [ ] ) { int N = 11 ; int arr [ ] = { 1 , 3 , 4 , 2 , 3 , 4 , 2 , 3 , 5 , 6 , 7 } ; System . out . println ( maxiConsecutiveSubarray ( arr , N ) ) ; } }
Length of longest subsequence having sum of digits of each element as a Composite Number | Java implementation of the above approach ; Function to generate prime numbers using Sieve of Eratosthenes ; Set 0 and 1 as non - prime ; If p is a prime ; Set all multiples of p as non - prime ; Function to find the digit sum of a given number ; Stores the sum of digits ; Extract digits and add to the sum ; Return the sum of the digits ; Function to find the longest subsequence with sum of digits of each element equal to a composite number ; Calculate sum of digits of current array element ; If sum of digits equal to 1 ; If sum of digits is a prime ; Driver Code ; Function call
import java . util . * ; class GFG { static int N = 100005 ; static void SieveOfEratosthenes ( boolean [ ] prime , int p_size ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= p_size ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i <= p_size ; i += p ) prime [ i ] = false ; } } } static int digitSum ( int number ) { int sum = 0 ; while ( number > 0 ) { sum += ( number % 10 ) ; number /= 10 ; } return sum ; } static void longestCompositeDigitSumSubsequence ( int [ ] arr , int n ) { int count = 0 ; boolean [ ] prime = new boolean [ N + 1 ] ; for ( int i = 0 ; i <= N ; i ++ ) prime [ i ] = true ; SieveOfEratosthenes ( prime , N ) ; for ( int i = 0 ; i < n ; i ++ ) { int res = digitSum ( arr [ i ] ) ; if ( res == 1 ) { continue ; } if ( prime [ res ] == false ) { count ++ ; } } System . out . println ( count ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 13 , 55 , 7 , 3 , 5 , 1 , 10 , 21 , 233 , 144 , 89 } ; int n = arr . length ; longestCompositeDigitSumSubsequence ( arr , n ) ; } }
Sum of specially balanced nodes from a given Binary Tree | Java program for the above approach ; Structure of Binary Tree ; Function to create a new node ; Return the created node ; Function to insert a node in the tree ; Left insertion ; Right insertion ; Return the root node ; Function to find sum of specially balanced nodes in the Tree ; Base Case ; Find the left subtree sum ; Find the right subtree sum ; Condition of specially balanced node ; Condition of specially balanced node ; Return the sum ; Function to build the binary tree ; Form root node of the tree ; Insert nodes into tree ; Create a new Node ; Insert the node ; Return the root of the Tree ; Function to find the sum of specially balanced nodes ; Build Tree ; Stores the sum of specially balanced node ; Function Call ; Print required sum ; Driver Code ; Given nodes ; Given root ; Given path info of nodes from root ; Given node values ; Function Call
import java . util . * ; class GFG { static int sum ; static class Node { int data ; Node left , right ; } ; static Node newnode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = null ; temp . right = null ; return temp ; } static Node insert ( String s , int i , int N , Node root , Node temp ) { if ( i == N ) return temp ; if ( s . charAt ( i ) == ' L ' ) root . left = insert ( s , i + 1 , N , root . left , temp ) ; else root . right = insert ( s , i + 1 , N , root . right , temp ) ; return root ; } static int SBTUtil ( Node root ) { if ( root == null ) return 0 ; if ( root . left == null && root . right == null ) return root . data ; int left = SBTUtil ( root . left ) ; int right = SBTUtil ( root . right ) ; if ( root . left != null && root . right != null ) { if ( ( left % 2 == 0 && right % 2 != 0 ) || ( left % 2 != 0 && right % 2 == 0 ) ) { sum += root . data ; } } return left + right + root . data ; } static Node build_tree ( int R , int N , String str [ ] , int values [ ] ) { Node root = newnode ( R ) ; int i ; for ( i = 0 ; i < N - 1 ; i ++ ) { String s = str [ i ] ; int x = values [ i ] ; Node temp = newnode ( x ) ; root = insert ( s , 0 , s . length ( ) , root , temp ) ; } return root ; } static void speciallyBalancedNodes ( int R , int N , String str [ ] , int values [ ] ) { Node root = build_tree ( R , N , str , values ) ; sum = 0 ; SBTUtil ( root ) ; System . out . print ( sum + " ▁ " ) ; } public static void main ( String [ ] args ) { int N = 7 ; int R = 12 ; String str [ ] = { " L " , " R " , " RL " , " RR " , " RLL " , " RLR " } ; int values [ ] = { 17 , 16 , 4 , 9 , 2 , 3 } ; speciallyBalancedNodes ( R , N , str , values ) ; } }
Pair having all other given pairs lying between its minimum and maximum | Java program for the above approach ; Function to find the position of the pair that covers every pair in the array arr [ ] [ ] ; Stores the index of the resultant pair ; To count the occurences ; Iterate to check every pair ; Set count to 0 ; Condition to checked for overlapping of pairs ; If that pair can cover all other pairs then store its position ; If position not found ; Otherwise ; Driver Code ; Given array of pairs ; Function Call
import java . util . * ; class GFG { static void position ( int arr [ ] [ ] , int N ) { int pos = - 1 ; int count ; for ( int i = 0 ; i < N ; i ++ ) { count = 0 ; for ( int j = 0 ; j < N ; j ++ ) { if ( arr [ i ] [ 0 ] <= arr [ j ] [ 0 ] && arr [ i ] [ 1 ] >= arr [ j ] [ 1 ] ) { count ++ ; } } if ( count == N ) { pos = i ; } } if ( pos == - 1 ) { System . out . print ( pos ) ; } else { System . out . print ( pos + 1 ) ; } } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 3 , 3 } , { 1 , 3 } , { 2 , 2 } , { 2 , 3 } , { 1 , 2 } } ; int N = arr . length ; position ( arr , N ) ; } }
Pair having all other given pairs lying between its minimum and maximum | Java program for the above approach ; Function to find the position of the pair that covers every pair in the array arr [ ] [ ] ; Position to store the index ; Stores the maximum second value ; Stores the minimum first value ; Iterate over the array of pairs ; Update right maximum ; Update left minimum ; Iterate over the array of pairs ; If any pair exists with value { left , right } then store it ; Print the answer ; Driver Code ; Given array of pairs ; Function Call
import java . util . * ; class GFG { static void position ( int arr [ ] [ ] , int N ) { int pos = - 1 ; int right = Integer . MIN_VALUE ; int left = Integer . MAX_VALUE ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] [ 1 ] > right ) { right = arr [ i ] [ 1 ] ; } if ( arr [ i ] [ 0 ] < left ) { left = arr [ i ] [ 0 ] ; } } for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] [ 0 ] == left && arr [ i ] [ 1 ] == right ) { pos = i + 1 ; } } System . out . print ( pos + "NEW_LINE"); } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 3 , 3 } , { 1 , 3 } , { 2 , 2 } , { 2 , 3 } , { 1 , 2 } } ; int N = arr . length ; position ( arr , N ) ; } }
Minimize count of given operations required to make two given strings permutations of each other | Java program to implement the above approach ; Function to minimize the count of operations to make str1 and str2 permutations of each other ; Store the frequency of each character of str1 ; Store the frequency of each character of str2 ; Traverse the freq1 [ ] and freq2 [ ] ; If frequency of character in str1 is greater than str2 ; Otherwise ; Store sum of freq1 [ ] ; Store sum of freq2 [ ] ; Driver Code
import java . util . * ; import java . io . * ; import java . lang . Math ; class GFG { static int ctMinEdits ( String str1 , String str2 ) { int N1 = str1 . length ( ) ; int N2 = str2 . length ( ) ; int freq1 [ ] = new int [ 256 ] ; Arrays . fill ( freq1 , 0 ) ; for ( int i = 0 ; i < N1 ; i ++ ) { freq1 [ str1 . charAt ( i ) ] ++ ; } int freq2 [ ] = new int [ 256 ] ; Arrays . fill ( freq2 , 0 ) ; for ( int i = 0 ; i < N2 ; i ++ ) { freq2 [ str2 . charAt ( i ) ] ++ ; } for ( int i = 0 ; i < 256 ; i ++ ) { if ( freq1 [ i ] > freq2 [ i ] ) { freq1 [ i ] = freq1 [ i ] - freq2 [ i ] ; freq2 [ i ] = 0 ; } else { freq2 [ i ] = freq2 [ i ] - freq1 [ i ] ; freq1 [ i ] = 0 ; } } int sum1 = 0 ; int sum2 = 0 ; for ( int i = 0 ; i < 256 ; i ++ ) { sum1 += freq1 [ i ] ; sum2 += freq2 [ i ] ; } return Math . max ( sum1 , sum2 ) ; } public static void main ( final String [ ] args ) { String str1 = " geeksforgeeks " ; String str2 = " geeksforcoder " ; System . out . println ( ctMinEdits ( str1 , str2 ) ) ; } }
Count pairs ( i , j ) from arrays arr [ ] & brr [ ] such that arr [ i ] | Java program for the above approach ; Function to find the minimum number needed to be added so that the sum of the digits does not exceed K ; Stores the sum of element at each corresponding index ; Find the sum of each index of both array ; Stores frequency of each element present in sumArr map < int , int > freqCount ; ; Initialize number of pairs ; Add possible vaid pairs ; Return Number of Pairs ; Driver Code ; Given array arr [ ] and brr [ ] ; Size of given array ; Function calling
import java . util . * ; import java . io . * ; class GFG { static void CountPairs ( int a [ ] , int b [ ] , int n ) { int C [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { C [ i ] = a [ i ] + b [ i ] ; } HashMap < Integer , Integer > freqCount = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! freqCount . containsKey ( C [ i ] ) ) freqCount . put ( C [ i ] , 1 ) ; else freqCount . put ( C [ i ] , freqCount . get ( C [ i ] ) + 1 ) ; } int NoOfPairs = 0 ; for ( Map . Entry < Integer , Integer > x : freqCount . entrySet ( ) ) { int y = x . getValue ( ) ; NoOfPairs = NoOfPairs + y * ( y - 1 ) / 2 ; } System . out . println ( NoOfPairs ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 4 , 20 , 3 , 10 , 5 } ; int brr [ ] = { 9 , 6 , 1 , 7 , 11 , 6 } ; int N = arr . length ; CountPairs ( arr , brr , N ) ; } }
Change in Median of given array after deleting given elements | Java program for the above approach ; Function to find the median change after removing elements from arr2 [ ] ; To store the median ; If N is odd ; If N is even ; Find the current element in arr1 ; Erase the element ; Decrement N ; If N is odd ; If N is even ; Print the corresponding difference of median ; Driver Code ; Given arrays ; Function Call
import java . util . * ; class GFG { public static void medianChange ( List < Integer > arr1 , List < Integer > arr2 ) { int N = arr1 . size ( ) ; List < Integer > median = new ArrayList < > ( ) ; if ( ( N & 1 ) != 0 ) median . add ( arr1 . get ( N / 2 ) * 1 ) ; else median . add ( ( arr1 . get ( N / 2 ) + arr1 . get ( ( N - 1 ) / 2 ) ) / 2 ) ; for ( int x = 0 ; x < arr2 . size ( ) ; x ++ ) { int it = arr1 . indexOf ( arr2 . get ( x ) ) ; arr1 . remove ( it ) ; N -- ; if ( ( N & 1 ) != 0 ) { median . add ( arr1 . get ( N / 2 ) * 1 ) ; } else { median . add ( ( arr1 . get ( N / 2 ) + arr1 . get ( ( N - 1 ) / 2 ) ) / 2 ) ; } } for ( int i = 0 ; i < median . size ( ) - 1 ; i ++ ) { System . out . print ( median . get ( i + 1 ) - median . get ( i ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { List < Integer > arr1 = new ArrayList < Integer > ( ) { { add ( 2 ) ; add ( 4 ) ; add ( 6 ) ; add ( 8 ) ; add ( 10 ) ; } } ; List < Integer > arr2 = new ArrayList < Integer > ( ) { { add ( 4 ) ; add ( 6 ) ; } } ; medianChange ( arr1 , arr2 ) ; } }
Count positions such that all elements before it are greater | Java Program to count positions such that all elements before it are greater ; Function to count positions such that all elements before it are greater ; Count is initially 1 for the first element ; Initial Minimum ; Traverse the array ; If current element is new minimum ; Update minimum ; Increment count ; Driver Code
class GFG { static int getPositionCount ( int a [ ] , int n ) { int count = 1 ; int min = a [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( a [ i ] <= min ) { min = a [ i ] ; count ++ ; } } return count ; } public static void main ( String [ ] args ) { int a [ ] = { 5 , 4 , 6 , 1 , 3 , 1 } ; int n = a . length ; System . out . print ( getPositionCount ( a , n ) ) ; } }
Maximum length L such that the sum of all subarrays of length L is less than K | Java implementation of the approach ; Function to return the maximum sum in a subarray of size k ; k must be greater ; Compute sum of first window of size k ; Compute sums of remaining windows by removing first element of previous window and adding last element of current window . ; Function to return the length of subarray Sum of all the subarray of this length is less than or equal to K ; Binary search from l to r as all the array elements are positive so that the maximum subarray sum is monotonically increasing ; Check if the subarray sum is greater than K or not ; Update the maximum length ; Driver code
class GFG { static int maxSum ( int arr [ ] , int n , int k ) { if ( n < k ) { return - 1 ; } int res = 0 ; for ( int i = 0 ; i < k ; i ++ ) res += arr [ i ] ; int curr_sum = res ; for ( int i = k ; i < n ; i ++ ) { curr_sum += arr [ i ] - arr [ i - k ] ; res = Math . max ( res , curr_sum ) ; } return res ; } static int solve ( int arr [ ] , int n , int k ) { int max_len = 0 , l = 0 , r = n , m ; while ( l <= r ) { m = ( l + r ) / 2 ; if ( maxSum ( arr , n , m ) > k ) r = m - 1 ; else { l = m + 1 ; max_len = m ; } } return max_len ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . length ; int k = 10 ; System . out . println ( solve ( arr , n , k ) ) ; } }
Count of triplets in an array that satisfy the given conditions | Java implementation of the approach ; All possible solutions of the equation 1 / a + 1 / b + 1 / c = 1 ; Function to find the triplets ; Storing indices of the elements ; Check if y can act as the middle element of triplet with the given solution of 1 / a + 1 / b + 1 / c = 1 ; Binary search to find the number of possible values of the first element ; Binary search to find the number of possible values of the third element ; Contribution to the answer would be the multiplication of the possible values for the first and the third element ; Driver code
import java . util . * ; class GFG { static int MAX = 100001 ; static int ROW = 10 ; static int COl = 3 ; static Vector < Integer > [ ] indices = new Vector [ MAX ] ; static int test [ ] [ ] = { { 2 , 3 , 6 } , { 2 , 4 , 4 } , { 2 , 6 , 3 } , { 3 , 2 , 6 } , { 3 , 3 , 3 } , { 3 , 6 , 2 } , { 4 , 2 , 4 } , { 4 , 4 , 2 } , { 6 , 2 , 3 } , { 6 , 3 , 2 } } ; static int find_triplet ( int array [ ] , int n ) { int answer = 0 ; for ( int i = 0 ; i < MAX ; i ++ ) { indices [ i ] = new Vector < > ( ) ; } for ( int i = 0 ; i < n ; i ++ ) { indices [ array [ i ] ] . add ( i ) ; } for ( int i = 0 ; i < n ; i ++ ) { int y = array [ i ] ; for ( int j = 0 ; j < ROW ; j ++ ) { int s = test [ j ] [ 1 ] * y ; if ( s % test [ j ] [ 0 ] != 0 ) continue ; if ( s % test [ j ] [ 2 ] != 0 ) continue ; int x = s / test [ j ] [ 0 ] ; int z = s / test [ j ] [ 2 ] ; if ( x > MAX z > MAX ) continue ; int l = 0 ; int r = indices [ x ] . size ( ) - 1 ; int first = - 1 ; while ( l <= r ) { int m = ( l + r ) / 2 ; if ( indices [ x ] . get ( m ) < i ) { first = m ; l = m + 1 ; } else { r = m - 1 ; } } l = 0 ; r = indices [ z ] . size ( ) - 1 ; int third = - 1 ; while ( l <= r ) { int m = ( l + r ) / 2 ; if ( indices [ z ] . get ( m ) > i ) { third = m ; r = m - 1 ; } else { l = m + 1 ; } } if ( first != - 1 && third != - 1 ) { answer += ( first + 1 ) * ( indices [ z ] . size ( ) - third ) ; } } } return answer ; } public static void main ( String [ ] args ) { int array [ ] = { 2 , 4 , 5 , 6 , 7 } ; int n = array . length ; System . out . println ( find_triplet ( array , n ) ) ; } }
Distinct adjacent elements in a binary array | Java implementation of the above approach ; if array has only one element , return 1 ; For first element compare with only next element ; For remaining elements compare with both prev and next elements ; For last element compare with only prev element ; Driver code
class GFG { static int distinct ( int [ ] arr , int n ) { int count = 0 ; if ( n == 1 ) return 1 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( i == 0 ) { if ( arr [ i ] != arr [ i + 1 ] ) count += 1 ; } else { if ( arr [ i ] != arr [ i + 1 ] arr [ i ] != arr [ i - 1 ] ) count += 1 ; } } if ( arr [ n - 1 ] != arr [ n - 2 ] ) count += 1 ; return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 0 , 0 , 0 , 0 , 1 , 0 } ; int n = arr . length ; System . out . println ( distinct ( arr , n ) ) ; } }
Check if an array of pairs can be sorted by swapping pairs with different first elements | java program for the above approach ; Function to check if an array is sorted or not ; Traverse the array arr [ ] ; Return true ; Function to check if it is possible to sort the array w . r . t . first element ; Stores the ID of the first element ; Traverse the array arr [ ] ; If arr [ i ] . second is not equal to that of the group ; If array is sorted ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static boolean isSorted ( int [ ] [ ] arr , int N ) { for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ i ] [ 0 ] > arr [ i - 1 ] [ 0 ] ) { return false ; } } return true ; } static String isPossibleToSort ( int [ ] [ ] arr , int N ) { int group = arr [ 0 ] [ 1 ] ; for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ i ] [ 1 ] != group ) { return " Yes " ; } } if ( isSorted ( arr , N ) ) { return " Yes " ; } else { return " No " ; } } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 340000 , 2 } , { 45000 , 1 } , { 30000 , 2 } , { 50000 , 4 } } ; int N = arr . length ; System . out . print ( isPossibleToSort ( arr , N ) ) ; } }