codeparrot/xlcost-text-to-code huggingface dataset:
Find the largest multiple of 3 from array of digits | Set 2 ( In O ( n ) time and O ( 1 ) space ) | C # program to find the largest number that can be mode from elements of the array and is divisible by 3 ; Number of digits ; function to sort array of digits using counts ; Store count of all elements ; Store ; Remove elements from arr [ ] at indexes ind1 and ind2 ; Returns largest multiple of 3 that can be formed using arr [ ] elements . ; Sum of all array element ; If sum is divisible by 3 , no need to delete an element ; Sort array element in increasing order ; Find reminder ; If remainder is '1' , we have to delete either one element of remainder '1' or two elements of remainder '2' ; Traverse array elements ; Store first element of remainder '1' ; If this is first occurrence of remainder 2 ; If second occurrence ; If remainder is '2' , we have to delete either one element of remainder '2' or two elements of remainder '1' ; traverse array elements ; store first element of remainder '2' ; If this is first occurrence of remainder 1 ; If second occurrence ; Driver code
using System ; class GFG { static int MAX_SIZE = 10 ; static void sortArrayUsingCounts ( int [ ] arr , int n ) { int [ ] count = new int [ MAX_SIZE ] ; for ( int i = 0 ; i < n ; i ++ ) { count [ arr [ i ] ] ++ ; } int index = 0 ; for ( int i = 0 ; i < MAX_SIZE ; i ++ ) { while ( count [ i ] > 0 ) { arr [ index ++ ] = i ; count [ i ] -- ; } } } static void removeAndPrintResult ( int [ ] arr , int n , int ind1 , int ind2 ) { for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( i != ind1 && i != ind2 ) { Console . Write ( arr [ i ] ) ; } } } static Boolean largest3Multiple ( int [ ] arr , int n ) { int sum = accumulate ( arr , 0 , n ) ; if ( sum % 3 == 0 ) { return true ; } sortArrayUsingCounts ( arr , n ) ; int remainder = sum % 3 ; if ( remainder == 1 ) { int [ ] rem_2 = new int [ 2 ] ; rem_2 [ 0 ] = - 1 ; rem_2 [ 1 ] = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 3 == 1 ) { removeAndPrintResult ( arr , n , i , - 1 ) ; return true ; } if ( arr [ i ] % 3 == 2 ) { if ( rem_2 [ 0 ] == - 1 ) { rem_2 [ 0 ] = i ; } else if ( rem_2 [ 1 ] == - 1 ) { rem_2 [ 1 ] = i ; } } } if ( rem_2 [ 0 ] != - 1 && rem_2 [ 1 ] != - 1 ) { removeAndPrintResult ( arr , n , rem_2 [ 0 ] , rem_2 [ 1 ] ) ; return true ; } } else if ( remainder == 2 ) { int [ ] rem_1 = new int [ 2 ] ; rem_1 [ 0 ] = - 1 ; rem_1 [ 1 ] = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 3 == 2 ) { removeAndPrintResult ( arr , n , i , - 1 ) ; return true ; } if ( arr [ i ] % 3 == 1 ) { if ( rem_1 [ 0 ] == - 1 ) { rem_1 [ 0 ] = i ; } else if ( rem_1 [ 1 ] == - 1 ) { rem_1 [ 1 ] = i ; } } } if ( rem_1 [ 0 ] != - 1 && rem_1 [ 1 ] != - 1 ) { removeAndPrintResult ( arr , n , rem_1 [ 0 ] , rem_1 [ 1 ] ) ; return true ; } } Console . Write ( " Not ▁ possible " ) ; return false ; } static int accumulate ( int [ ] arr , int start , int end ) { int sum = 0 ; for ( int i = 0 ; i < arr . Length ; i ++ ) { sum += arr [ i ] ; } return sum ; } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 4 , 1 , 1 , 1 , 3 } ; int n = arr . Length ; largest3Multiple ( arr , n ) ; } }
Minimum swaps to reach permuted array with at most 2 positions left swaps allowed | C # program to find minimum number of swaps to reach a permutation with at most 2 left swaps allowed for every element ; This funt merges two sorted arrays and returns inversion count in the arrays . ; i is index for left subarray ; j is index for right subarray ; k is index for resultant merged subarray ; Copy the remaining elements of left subarray ( if there are any ) to temp ; Copy the remaining elements of right subarray ( if there are any ) to temp ; Copy back the merged elements to original array ; An auxiliary recursive function that sorts the input array and returns the number of inversions in the array . ; Divide the array into two parts and call _mergeSortAndCountInv ( ) for each of the parts ; Inversion count will be sum of inversions in left - part , right - part and number of inversions in merging ; Merge the two parts ; This function sorts the input array and returns the number of inversions in the array ; method returns minimum number of swaps to reach permuted array ' arr ' ; loop over all elements to check Invalid permutation condition ; if an element is at distance more than 2 from its actual position then it is not possible to reach permuted array just by swapping with 2 positions left elements so returning - 1 ; If permuted array is not Invalid , then number of Inversion in array will be our final answer ; Driver code ; change below example
using System ; class GFG { static int merge ( int [ ] arr , int [ ] temp , int left , int mid , int right ) { int inv_count = 0 ; int i = left ; int j = mid ; int k = left ; while ( ( i <= mid - 1 ) && ( j <= right ) ) { if ( arr [ i ] <= arr [ j ] ) { temp [ k ++ ] = arr [ i ++ ] ; } else { temp [ k ++ ] = arr [ j ++ ] ; inv_count = inv_count + ( mid - i ) ; } } while ( i <= mid - 1 ) { temp [ k ++ ] = arr [ i ++ ] ; } while ( j <= right ) { temp [ k ++ ] = arr [ j ++ ] ; } for ( i = left ; i <= right ; i ++ ) { arr [ i ] = temp [ i ] ; } return inv_count ; } static int _mergeSort ( int [ ] arr , int [ ] temp , int left , int right ) { int mid , inv_count = 0 ; if ( right > left ) { mid = ( right + left ) / 2 ; inv_count = _mergeSort ( arr , temp , left , mid ) ; inv_count += _mergeSort ( arr , temp , mid + 1 , right ) ; inv_count += merge ( arr , temp , left , mid + 1 , right ) ; } return inv_count ; } static int mergeSort ( int [ ] arr , int array_size ) { int [ ] temp = new int [ array_size ] ; return _mergeSort ( arr , temp , 0 , array_size - 1 ) ; } static int minSwapToReachArr ( int [ ] arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) { if ( ( arr [ i ] - 1 ) - i > 2 ) { return - 1 ; } } int numOfInversion = mergeSort ( arr , N ) ; return numOfInversion ; } static void Main ( ) { int [ ] arr = { 1 , 2 , 5 , 3 , 4 } ; int N = arr . Length ; int res = minSwapToReachArr ( arr , N ) ; if ( res == - 1 ) Console . WriteLine ( " Not ▁ Possible " ) ; else Console . WriteLine ( res ) ; } }
Sort all even numbers in ascending order and then sort all odd numbers in descending order | Java program sort array in even and odd manner . The odd numbers are to be sorted in descending order and the even numbers in ascending order ; To do two way sort . First sort even numbers in ascending order , then odd numbers in descending order . ; Make all odd numbers negative ; Check for odd ; Sort all numbers ; Retaining original array ; Driver Method
using System ; public class GFG { static void twoWaySort ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) if ( ( arr [ i ] & 1 ) != 0 ) arr [ i ] *= - 1 ; Array . Sort ( arr ) ; for ( int i = 0 ; i < n ; i ++ ) if ( ( arr [ i ] & 1 ) != 0 ) arr [ i ] *= - 1 ; } public static void Main ( ) { int [ ] arr = { 1 , 3 , 2 , 7 , 5 , 4 } ; twoWaySort ( arr , arr . Length ) ; for ( int i = 0 ; i < arr . Length ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } }
Possible to form a triangle from array values | C # program to find if it is possible to form a triangle from array values ; Method prints possible triangle when array values are taken as sides ; If number of elements are less than 3 , then no triangle is possible ; first sort the array ; loop for all 3 consecutive triplets ; If triplet satisfies triangle condition , break ; Driver Code
using System ; class GFG { static bool isPossibleTriangle ( int [ ] arr , int N ) { if ( N < 3 ) return false ; Array . Sort ( arr ) ; for ( int i = 0 ; i < N - 2 ; i ++ ) if ( arr [ i ] + arr [ i + 1 ] > arr [ i + 2 ] ) return true ; return false ; } static public void Main ( ) { int [ ] arr = { 5 , 4 , 3 , 1 , 2 } ; int N = arr . Length ; if ( isPossibleTriangle ( arr , N ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Bucket Sort To Sort an Array with Negative Numbers | C # program to sort an array of positive and negative numbers using bucket sort ; Function to sort [ ] arr of size n using bucket sort ; 1 ) Create n empty buckets ; 2 ) Put array elements in different buckets ; int bi = ( int ) ( n * arr [ i ] ) ; Index in bucket ; 3 ) Sort individual buckets ; 4 ) Concatenate all buckets into [ ] arr ; This function mainly splits array into two and then calls bucketSort ( ) for two arrays . ; traverse array elements ; store - Ve elements by converting into + ve element ; store + ve elements ; First store elements of Neg [ ] array by converting into - ve ; store + ve element ; Driver program to test above function
using System ; using System . Collections . Generic ; public class GFG { static void bucketSort ( List < Double > arr , int n ) { List < Double > [ ] b = new List < Double > [ n ] ; for ( int i = 0 ; i < b . Length ; i ++ ) b [ i ] = new List < Double > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { b [ bi ] . Add ( arr [ i ] ) ; } for ( int i = 0 ; i < n ; i ++ ) b [ i ] . Sort ( ) ; int index = 0 ; arr . Clear ( ) ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < b [ i ] . Count ; j ++ ) arr . Add ( b [ i ] [ j ] ) ; } static void sortMixed ( double [ ] arr , int n ) { List < Double > Neg = new List < Double > ( ) ; List < Double > Pos = new List < Double > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < 0 ) Neg . Add ( - 1 * arr [ i ] ) ; else Pos . Add ( arr [ i ] ) ; } bucketSort ( Neg , ( int ) Neg . Count ) ; bucketSort ( Pos , ( int ) Pos . Count ) ; for ( int i = 0 ; i < Neg . Count ; i ++ ) arr [ i ] = - 1 * Neg [ Neg . Count - 1 - i ] ; for ( int j = Neg . Count ; j < n ; j ++ ) arr [ j ] = Pos [ j - Neg . Count ] ; } public static void Main ( String [ ] args ) { double [ ] arr = { - 0.897 , 0.565 , 0.656 , - 0.1234 , 0 , 0.3434 } ; int n = arr . Length ; sortMixed ( arr , n ) ; Console . Write ( " Sorted ▁ array ▁ is ▁ STRNEWLINE " ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } }
K | C # program to find the K - th smallest element after removing some integers from natural number . ; Return the K - th smallest element . ; Making an array , and mark all number as unmarked . ; Marking the number present in the given array . ; If j is unmarked , reduce k by 1. ; If k is 0 return j . ; Driven code
using System ; class GFG { static int MAX = 1000000 ; static int ksmallest ( int [ ] arr , int n , int k ) { int [ ] b = new int [ MAX ] ; for ( int i = 0 ; i < n ; i ++ ) { b [ arr [ i ] ] = 1 ; } for ( int j = 1 ; j < MAX ; j ++ ) { if ( b [ j ] != 1 ) { k -- ; } if ( k != 1 ) { return j ; } } return int . MaxValue ; } public static void Main ( ) { int k = 1 ; int [ ] arr = { 1 } ; int n = arr . Length ; Console . WriteLine ( ksmallest ( arr , n , k ) ) ; } }
Sort an array when two halves are sorted | C # program to Merge two sorted halves of array Into Single Sorted Array ; Sort the given array using sort STL ; Driver code ; Print sorted Array
using System ; class GFG { static void mergeTwoHalf ( int [ ] A , int n ) { Array . Sort ( A ) ; } static public void Main ( ) { int [ ] A = { 2 , 3 , 8 , - 1 , 7 , 10 } ; int n = A . Length ; mergeTwoHalf ( A , n ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( A [ i ] + " ▁ " ) ; } }
Choose k array elements such that difference of maximum and minimum is minimized | C # program to find minimum difference of maximum and minimum of K number . ; Return minimum difference of maximum and minimum of k elements of arr [ 0. . n - 1 ] . ; Sorting the array . ; Find minimum value among all K size subarray . ; Driver code
using System ; class GFG { static int minDiff ( int [ ] arr , int n , int k ) { int result = int . MaxValue ; Array . Sort ( arr ) ; for ( int i = 0 ; i <= n - k ; i ++ ) result = Math . Min ( result , arr [ i + k - 1 ] - arr [ i ] ) ; return result ; } public static void Main ( ) { int [ ] arr = { 10 , 100 , 300 , 200 , 1000 , 20 , 30 } ; int n = arr . Length ; int k = 3 ; Console . WriteLine ( minDiff ( arr , n , k ) ) ; } }
Sort even | C # Program to separately sort even - placed and odd placed numbers and place them together in sorted array . ; create evenArr [ ] and oddArr [ ] ; Put elements in oddArr [ ] and evenArr [ ] as per their position ; sort evenArr [ ] in ascending order sort oddArr [ ] in descending order ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void bitonicGenerator ( int [ ] arr , int n ) { List < int > evenArr = new List < int > ( ) ; List < int > oddArr = new List < int > ( ) ; int i = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( i % 2 != 1 ) { evenArr . Add ( arr [ i ] ) ; } else { oddArr . Add ( arr [ i ] ) ; } } evenArr . Sort ( ) ; oddArr . Sort ( ) ; oddArr . Reverse ( ) ; i = 0 ; for ( int j = 0 ; j < evenArr . Count ; j ++ ) { arr [ i ++ ] = evenArr [ j ] ; } for ( int j = 0 ; j < oddArr . Count ; j ++ ) { arr [ i ++ ] = oddArr [ j ] ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 5 , 8 , 9 , 6 , 7 , 3 , 4 , 2 , 0 } ; int n = arr . Length ; bitonicGenerator ( arr , n ) ; for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } }
Number of swaps to sort when only adjacent swapping allowed | C # program to count number of swaps required to sort an array when only swapping of adjacent elements is allowed . ; This function merges two sorted arrays and returns inversion count in the arrays . ; i is index for left subarray ; j is index for right subarray ; k is index for resultant merged subarray ; this is tricky -- see above / explanation diagram for merge ( ) ; Copy the remaining elements of left subarray ( if there are any ) to temp ; Copy the remaining elements of right subarray ( if there are any ) to temp ; Copy back the merged elements to original array ; An auxiliary recursive function that sorts the input array and returns the number of inversions in the array . ; Divide the array into two parts and call _mergeSortAndCountInv ( ) for each of the parts ; Inversion count will be sum of inversions in left - part , right - part and number of inversions in merging ; Merge the two parts ; This function sorts the input array and returns the number of inversions in the array ; Driver Code
using System ; class GFG { static int merge ( int [ ] arr , int [ ] temp , int left , int mid , int right ) { int inv_count = 0 ; int i = left ; int j = mid ; int k = left ; while ( ( i <= mid - 1 ) && ( j <= right ) ) { if ( arr [ i ] <= arr [ j ] ) temp [ k ++ ] = arr [ i ++ ] ; else { temp [ k ++ ] = arr [ j ++ ] ; inv_count = inv_count + ( mid - i ) ; } } while ( i <= mid - 1 ) temp [ k ++ ] = arr [ i ++ ] ; while ( j <= right ) temp [ k ++ ] = arr [ j ++ ] ; for ( i = left ; i <= right ; i ++ ) arr [ i ] = temp [ i ] ; return inv_count ; } static int _mergeSort ( int [ ] arr , int [ ] temp , int left , int right ) { int mid , inv_count = 0 ; if ( right > left ) { mid = ( right + left ) / 2 ; inv_count = _mergeSort ( arr , temp , left , mid ) ; inv_count += _mergeSort ( arr , temp , mid + 1 , right ) ; inv_count += merge ( arr , temp , left , mid + 1 , right ) ; } return inv_count ; } static int countSwaps ( int [ ] arr , int n ) { int [ ] temp = new int [ n ] ; return _mergeSort ( arr , temp , 0 , n - 1 ) ; } public static void Main ( ) { int [ ] arr = { 1 , 20 , 6 , 4 , 5 } ; int n = arr . Length ; Console . Write ( " Number ▁ of ▁ swaps ▁ is ▁ " + countSwaps ( arr , n ) ) ; } }
Check whether a given number is even or odd | C # program program to check for even or odd ; Returns true if n is even , else odd ; Driver code
using System ; class GFG { public static bool isEven ( int n ) { return ( n % 2 == 0 ) ; } public static void Main ( ) { int n = 101 ; if ( isEven ( n ) == true ) Console . WriteLine ( " Even " ) ; else Console . WriteLine ( " Odd " ) ; } }
Find Surpasser Count of each element in array | Naive C # program to find surpasser count of each element in array ; Function to find surpasser count of each element in array ; stores surpasser count for element arr [ i ] ; Function to print an array ; Driver program to test above functions
using System ; class GFG { static void findSurpasser ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int count = 0 ; for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ j ] > arr [ i ] ) count ++ ; Console . Write ( count + " ▁ " ) ; } } static void printArray ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; } public static void Main ( ) { int [ ] arr = { 2 , 7 , 5 , 3 , 0 , 8 , 1 } ; int n = arr . Length ; Console . WriteLine ( " Given ▁ array ▁ is ▁ " ) ; printArray ( arr , n ) ; Console . WriteLine ( " Surpasser ▁ Count ▁ of " + " ▁ array ▁ is ▁ " ) ; findSurpasser ( arr , n ) ; } }
Minimum sum of two numbers formed from digits of an array | C # program to find minimum sum of two numbers formed from digits of the array . ; Function to find and return minimum sum of two numbers formed from digits of the array . ; sort the array ; let two numbers be a and b ; fill a and b with every alternate digit of input array ; return the sum ; Driver code
using System ; class GFG { static int solve ( int [ ] arr , int n ) { Array . Sort ( arr ) ; int a = 0 , b = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 != 0 ) a = a * 10 + arr [ i ] ; else b = b * 10 + arr [ i ] ; } return a + b ; } public static void Main ( ) { int [ ] arr = { 6 , 8 , 4 , 5 , 2 , 3 } ; int n = arr . Length ; Console . WriteLine ( " Sum ▁ is ▁ " + solve ( arr , n ) ) ; } }
Maximum product of a triplet ( subsequence of size 3 ) in array | A C # program to find a maximum product of a triplet in array of integers ; Function to find a maximum product of a triplet in array of integers of size n ; if size is less than 3 , no triplet exists ; will contain max product ; Driver Code
using System ; class GFG { static int maxProduct ( int [ ] arr , int n ) { if ( n < 3 ) return - 1 ; int max_product = int . MinValue ; for ( int i = 0 ; i < n - 2 ; i ++ ) for ( int j = i + 1 ; j < n - 1 ; j ++ ) for ( int k = j + 1 ; k < n ; k ++ ) max_product = Math . Max ( max_product , arr [ i ] * arr [ j ] * arr [ k ] ) ; return max_product ; } public static void Main ( ) { int [ ] arr = { 10 , 3 , 5 , 6 , 20 } ; int n = arr . Length ; ; int max = maxProduct ( arr , n ) ; if ( max == - 1 ) Console . WriteLine ( " No ▁ Triplet ▁ Exists " ) ; else Console . WriteLine ( " Maximum ▁ product ▁ is ▁ " + max ) ; } }
Maximum product of a triplet ( subsequence of size 3 ) in array | A C # program to find a maximum product of a triplet in array of integers ; Function to find a maximum product of a triplet in array of integers of size n ; if size is less than 3 , no triplet exists ; Construct four auxiliary vectors of size n and initialize them by - 1 ; will contain max product ; to store maximum element on left of array ; to store minimum element on left of array ; leftMax [ i ] will contain max element on left of arr [ i ] excluding arr [ i ] . leftMin [ i ] will contain min element on left of arr [ i ] excluding arr [ i ] . ; reset max_sum to store maximum element on right of array ; reset min_sum to store minimum element on right of array ; rightMax [ i ] will contain max element on right of arr [ i ] excluding arr [ i ] . rightMin [ i ] will contain min element on right of arr [ i ] excluding arr [ i ] . ; For all array indexes i except first and last , compute maximum of arr [ i ] * x * y where x can be leftMax [ i ] or leftMin [ i ] and y can be rightMax [ i ] or rightMin [ i ] . ; Driver code
using System ; class GFG { static int maxProduct ( int [ ] arr , int n ) { if ( n < 3 ) return - 1 ; int [ ] leftMin = new int [ n ] ; int [ ] rightMin = new int [ n ] ; int [ ] leftMax = new int [ n ] ; int [ ] rightMax = new int [ n ] ; Array . Fill ( leftMin , - 1 ) ; Array . Fill ( leftMax , - 1 ) ; Array . Fill ( rightMax , - 1 ) ; Array . Fill ( rightMin , - 1 ) ; int max_product = int . MinValue ; int max_sum = arr [ 0 ] ; int min_sum = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { leftMax [ i ] = max_sum ; if ( arr [ i ] > max_sum ) max_sum = arr [ i ] ; leftMin [ i ] = min_sum ; if ( arr [ i ] < min_sum ) min_sum = arr [ i ] ; } max_sum = arr [ n - 1 ] ; min_sum = arr [ n - 1 ] ; for ( int j = n - 2 ; j >= 0 ; j -- ) { rightMax [ j ] = max_sum ; if ( arr [ j ] > max_sum ) max_sum = arr [ j ] ; rightMin [ j ] = min_sum ; if ( arr [ j ] < min_sum ) min_sum = arr [ j ] ; } for ( int i = 1 ; i < n - 1 ; i ++ ) { int max1 = Math . Max ( arr [ i ] * leftMax [ i ] * rightMax [ i ] , arr [ i ] * leftMin [ i ] * rightMin [ i ] ) ; int max2 = Math . Max ( arr [ i ] * leftMax [ i ] * rightMin [ i ] , arr [ i ] * leftMin [ i ] * rightMax [ i ] ) ; max_product = Math . Max ( max_product , Math . Max ( max1 , max2 ) ) ; } return max_product ; } static void Main ( ) { int [ ] arr = { 1 , 4 , 3 , - 6 , - 7 , 0 } ; int n = arr . Length ; int max = maxProduct ( arr , n ) ; if ( max == - 1 ) Console . WriteLine ( " No ▁ Triplet ▁ Exists " ) ; else Console . WriteLine ( " Maximum ▁ product ▁ is ▁ " + max ) ; } }
Maximum product of a triplet ( subsequence of size 3 ) in array | C # program to find a maximum product of a triplet in array of integers ; Function to find a maximum product of a triplet in array of integers of size n ; if size is less than 3 , no triplet exists ; Sort the array in ascending order ; Return the maximum of product of last three elements and product of first two elements and last element ; Driver program to test above functions
using System ; public class GFG { static int maxProduct ( int [ ] arr , int n ) { if ( n < 3 ) { return - 1 ; } Array . Sort ( arr ) ; return Math . Max ( arr [ 0 ] * arr [ 1 ] * arr [ n - 1 ] , arr [ n - 1 ] * arr [ n - 2 ] * arr [ n - 3 ] ) ; } public static void Main ( ) { int [ ] arr = { - 10 , - 3 , 5 , 6 , - 20 } ; int n = arr . Length ; int max = maxProduct ( arr , n ) ; if ( max == - 1 ) { Console . WriteLine ( " No ▁ Triplet ▁ Exists " ) ; } else { Console . WriteLine ( " Maximum ▁ product ▁ is ▁ " + max ) ; } } }
Maximum product of a triplet ( subsequence of size 3 ) in array | A O ( n ) C # program to find maximum product pair in an array . ; Function to find a maximum product of a triplet in array of integers of size n ; If size is less than 3 , no triplet exists ; Initialize Maximum , second maximum and third maximum element ; Initialize Minimum and second minimum element ; Update Maximum , second maximum and third maximum element ; Update second maximum and third maximum element ; Update third maximum element ; Update Minimum and second minimum element ; Update second minimum element ; Driver code
using System ; using System . Collections ; class GFG { static int maxProduct ( int [ ] arr , int n ) { if ( n < 3 ) return - 1 ; int maxA = Int32 . MinValue , maxB = Int32 . MinValue , maxC = Int32 . MinValue ; int minA = Int32 . MaxValue , minB = Int32 . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > maxA ) { maxC = maxB ; maxB = maxA ; maxA = arr [ i ] ; } else if ( arr [ i ] > maxB ) { maxC = maxB ; maxB = arr [ i ] ; } else if ( arr [ i ] > maxC ) maxC = arr [ i ] ; if ( arr [ i ] < minA ) { minB = minA ; minA = arr [ i ] ; } else if ( arr [ i ] < minB ) minB = arr [ i ] ; } return Math . Max ( minA * minB * maxA , maxA * maxB * maxC ) ; } public static void Main ( string [ ] args ) { int [ ] arr = { 1 , - 4 , 3 , - 6 , 7 , 0 } ; int n = arr . Length ; int max = maxProduct ( arr , n ) ; if ( max == - 1 ) Console . Write ( " No ▁ Triplet ▁ Exists " ) ; else Console . Write ( " Maximum ▁ product ▁ is ▁ " + max ) ; } }