codeparrot/xlcost-text-to-code huggingface dataset:
Construct original array starting with K from an array of XOR of all elements except elements at same index | C # program for the above approach ; Function to construct an array with each element equal to XOR of all array elements except the element at the same index ; Original array ; Stores Bitwise XOR of array ; Calculate XOR of all array elements ; Print the original array B [ ] ; Driver Code ; Function Call
using System ; using System . Collections ; class GFG { static void constructArray ( int [ ] A , int N , int K ) { int [ ] B = new int [ N ] ; int totalXOR = A [ 0 ] ^ K ; for ( int i = 0 ; i < N ; i ++ ) B [ i ] = totalXOR ^ A [ i ] ; for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( B [ i ] + " ▁ " ) ; } } static void Main ( ) { int [ ] A = { 13 , 14 , 10 , 6 } ; int K = 2 ; int N = A . Length ; constructArray ( A , N , K ) ; } }
Find extra element in the second array | C # implementation of the approach ; Function to return the extra element in B [ ] ; To store the result ; Find the XOR of all the element of array A [ ] and array B [ ] ; Driver code
using System ; class GFG { static int extraElement ( int [ ] A , int [ ] B , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) ans ^= A [ i ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) ans ^= B [ i ] ; return ans ; } public static void Main ( String [ ] args ) { int [ ] A = { 10 , 15 , 5 } ; int [ ] B = { 10 , 100 , 15 , 5 } ; int n = A . Length ; Console . WriteLine ( extraElement ( A , B , n ) ) ; } }
Hamming distance between two Integers | C # implementation of above approach ; Function to calculate hamming distance ; Driver code
class GFG { static int hammingDistance ( int n1 , int n2 ) { int x = n1 ^ n2 ; int setBits = 0 ; while ( x > 0 ) { setBits += x & 1 ; x >>= 1 ; } return setBits ; } static void Main ( ) { int n1 = 9 , n2 = 14 ; System . Console . WriteLine ( hammingDistance ( n1 , n2 ) ) ; } }
Print bitwise AND set of a number N | C # program to print all bitwise subsets of N ( Naive approach ) ; function to find bitwise subsets Naive approach ; Driver function
using System ; class GFG { static void printSubsets ( int n ) { for ( int i = 0 ; i <= n ; i ++ ) if ( ( n & i ) == i ) Console . Write ( i + " ▁ " ) ; } public static void Main ( ) { int n = 9 ; printSubsets ( n ) ; } }
Find most significant set bit of a number | C # program to find MSB number for given n . ; To find the position of the most significant set bit ; To return the the value of the number with set bit at k - th position ; Driver code
using System ; public class GFG { static int setBitNumber ( int n ) { int k = ( int ) ( Math . Log ( n ) / Math . Log ( 2 ) ) ; return 1 << k ; } static public void Main ( ) { int n = 273 ; Console . WriteLine ( setBitNumber ( n ) ) ; } }
Minimum number of subsets with distinct elements | A sorting based solution to find the minimum number of subsets of a set such that every subset contains distinct elements . ; Function to count subsets such that all subsets have distinct elements . ; Take input and initialize res = 0 ; Sort the array ; Traverse the input array and find maximum frequency ; For each number find its repetition / frequency ; Update res ; Driver function
using System ; public class GfG { public static int subset ( int [ ] ar , int n ) { int res = 0 ; Array . Sort ( ar ) ; for ( int i = 0 ; i < n ; i ++ ) { int count = 1 ; for ( ; i < n - 1 ; i ++ ) { if ( ar [ i ] == ar [ i + 1 ] ) count ++ ; else break ; } res = Math . Max ( res , count ) ; } return res ; } public static void Main ( ) { int [ ] arr = { 5 , 6 , 9 , 3 , 4 , 3 , 4 } ; int n = 7 ; Console . WriteLine ( subset ( arr , n ) ) ; } }
Minimum number of subsets with distinct elements | A hashing based solution to find the minimum number of subsets of a set such that every subset contains distinct elements . ; Function to count subsets such that all subsets have distinct elements . ; Traverse the input array and store frequencies of elements ; Find the maximum value in map . ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int subset ( int [ ] arr , int n ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . ContainsKey ( arr [ i ] ) ) { var val = mp [ arr [ i ] ] ; mp . Remove ( arr [ i ] ) ; mp . Add ( arr [ i ] , val + 1 ) ; } else { mp . Add ( arr [ i ] , 1 ) ; } } int res = 0 ; foreach ( KeyValuePair < int , int > entry in mp ) res = Math . Max ( res , entry . Value ) ; return res ; } public static void Main ( String [ ] args ) { int [ ] arr = { 5 , 6 , 9 , 3 , 4 , 3 , 4 } ; int n = arr . Length ; Console . WriteLine ( subset ( arr , n ) ) ; } }
Count ways to represent a number as sum of perfect squares |  ; Store perfect squares less than or equal to N ; Utility function to calculate perfect squares less than or equal to N ; Function to find the number of ways to represent a number as sum of perfect squares ; Handle the base cases ; Include the i - th index element ; Exclude the i - th index element ; Return the result ; Driver Code ; Given Input ; Precalculate perfect squares <= N ; Function Call
using System . IO ; using System ; using System . Collections ; class GFG { static ArrayList psquare = new ArrayList ( ) ; static void calcPsquare ( int N ) { for ( int i = 1 ; i * i <= N ; i ++ ) psquare . Add ( i * i ) ; } static int countWays ( int index , int target ) { if ( target == 0 ) return 1 ; if ( index < 0 target < 0 ) return 0 ; int inc = countWays ( index , target - ( int ) psquare [ index ] ) ; int exc = countWays ( index - 1 , target ) ; return inc + exc ; } static void Main ( ) { int N = 9 ; calcPsquare ( N ) ; Console . WriteLine ( countWays ( psquare . Count - 1 , N ) ) ; } }
Sum of distances of all nodes from a given node | C # program for the above approach ; Structure of a binary tree node ; Function that allocates a new node with the given data and null to its left and right pointers ; Return newly created node ; Function to count the number of nodes in the left and right subtrees ; Initialize a pair that stores the pair { number of nodes , depth } ; Finding the number of nodes in the left subtree ; Find the number of nodes in the right subtree ; Filling up size field ; Stores the sum of distances of all nodes from the given node ; Function to find the total distance ; If target node matches with the current node ; If root . left is not null ; Update sum ; Recur for the left subtree ; If root . right is not null ; Apply the formula given in the approach ; Recur for the right subtree ; Driver Code ; Input tree ; Total number of nodes ; Print the sum of distances
using System ; public class GFG { class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } class TreeNode { public int data , size ; public TreeNode left ; public TreeNode right ; } ; static TreeNode newNode ( int data ) { TreeNode Node = new TreeNode ( ) ; Node . data = data ; Node . left = null ; Node . right = null ; return ( Node ) ; } static pair sumofsubtree ( TreeNode root ) { pair p = new pair ( 1 , 0 ) ; if ( root . left != null ) { pair ptemp = sumofsubtree ( root . left ) ; p . second += ptemp . first + ptemp . second ; p . first += ptemp . first ; } if ( root . right != null ) { pair ptemp = sumofsubtree ( root . right ) ; p . second += ptemp . first + ptemp . second ; p . first += ptemp . first ; } root . size = p . first ; return p ; } static int sum = 0 ; static void distance ( TreeNode root , int target , int distancesum , int n ) { if ( root . data == target ) { sum = distancesum ; } if ( root . left != null ) { int tempsum = distancesum - root . left . size + ( n - root . left . size ) ; distance ( root . left , target , tempsum , n ) ; } if ( root . right != null ) { int tempsum = distancesum - root . right . size + ( n - root . right . size ) ; distance ( root . right , target , tempsum , n ) ; } } public static void Main ( String [ ] args ) { TreeNode root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 5 ) ; root . right . left = newNode ( 6 ) ; root . right . right = newNode ( 7 ) ; root . left . left . left = newNode ( 8 ) ; root . left . left . right = newNode ( 9 ) ; int target = 3 ; pair p = sumofsubtree ( root ) ; int totalnodes = p . first ; distance ( root , target , p . second , totalnodes ) ; Console . Write ( sum + " STRNEWLINE " ) ; } }
Rearrange array such that sum of same indexed elements is atmost K | C # program for the above approach ; Reverse array ; Function to rearrange array such that sum of similar indexed elements does not exceed K ; Sort the array [ ] B in descending order ; If condition fails ; Print the array ; Driver Code ; Given arrays
using System ; class GFG { static int [ ] reverse ( int [ ] a ) { int i , n = a . Length , t ; for ( i = 0 ; i < n / 2 ; i ++ ) { t = a [ i ] ; a [ i ] = a [ n - i - 1 ] ; a [ n - i - 1 ] = t ; } return a ; } static void rearrangeArray ( int [ ] A , int [ ] B , int N , int K ) { Array . Sort ( B ) ; B = reverse ( B ) ; bool flag = true ; for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] + B [ i ] > K ) { flag = false ; break ; } } if ( ! flag ) { Console . Write ( " - 1" + " STRNEWLINE " ) ; } else { for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( B [ i ] + " ▁ " ) ; } } } public static void Main ( String [ ] args ) { int [ ] A = { 1 , 2 , 3 , 4 , 2 } ; int [ ] B = { 1 , 2 , 3 , 1 , 1 } ; int N = A . Length ; int K = 5 ; rearrangeArray ( A , B , N , K ) ; } }
Count rows with sum exceeding sum of the remaining Matrix | C # program to implement the above approach ; Function to count the number of rows whose sum exceeds the sum of elements of the remaining matrix ; Stores the matrix dimensions ; To store the result ; Stores the total sum of the matrix elements ; Calculate the total sum ; Traverse to check for each row ; Stores the sum of elements of the current row ; Calculate the sum of elements of the current row ; If sum of current row exceeds the sum of rest of the matrix ; Increase count ; Print the result ; Driver Code ; Given matrix ; Function Call
using System ; class GFG { static void countRows ( int [ , ] mat ) { int n = mat . GetLength ( 0 ) ; int m = mat . GetLength ( 1 ) ; int count = 0 ; int totalSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { totalSum += mat [ i , j ] ; } } for ( int i = 0 ; i < n ; i ++ ) { int currSum = 0 ; for ( int j = 0 ; j < m ; j ++ ) { currSum += mat [ i , j ] ; } if ( currSum > totalSum - currSum ) count ++ ; } Console . WriteLine ( count ) ; } public static void Main ( String [ ] args ) { int [ , ] mat = { { 2 , - 1 , 5 } , { - 3 , 0 , - 2 } , { 5 , 1 , 2 } } ; countRows ( mat ) ; } }
Check if array contains contiguous integers with duplicates allowed | Sorting based C # implementation to check whether the array contains a set of contiguous integers ; function to check whether the array contains a set of contiguous integers ; Sort the array ; After sorting , check if current element is either same as previous or is one more . ; Driver program
using System ; class GFG { static bool areElementsContiguous ( int [ ] arr , int n ) { Array . Sort ( arr ) ; for ( int i = 1 ; i < n ; i ++ ) if ( arr [ i ] - arr [ i - 1 ] > 1 ) return false ; return true ; } public static void Main ( ) { int [ ] arr = { 5 , 2 , 3 , 6 , 4 , 4 , 6 , 6 } ; int n = arr . Length ; if ( areElementsContiguous ( arr , n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Check if array contains contiguous integers with duplicates allowed | C # implementation to check whether the array contains a set of contiguous integers ; function to check whether the array contains a set of contiguous integers ; Find maximum and minimum elements . ; There should be at least m elements in aaray to make them contiguous . ; Create a visited array and initialize false . ; Mark elements as true . ; If any element is not marked , all elements are not contiguous . ; Driver program
using System ; class GFG { static bool areElementsContiguous ( int [ ] arr , int n ) { int max = int . MinValue ; int min = int . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { max = Math . Max ( max , arr [ i ] ) ; min = Math . Min ( min , arr [ i ] ) ; } int m = max - min + 1 ; if ( m > n ) return false ; bool [ ] visited = new bool [ n ] ; for ( int i = 0 ; i < n ; i ++ ) visited [ arr [ i ] - min ] = true ; for ( int i = 0 ; i < m ; i ++ ) if ( visited [ i ] == false ) return false ; return true ; } public static void Main ( ) { int [ ] arr = { 5 , 2 , 3 , 6 , 4 , 4 , 6 , 6 } ; int n = arr . Length ; if ( areElementsContiguous ( arr , n ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Check if array contains contiguous integers with duplicates allowed | c # implementation to check whether the array contains a set of contiguous integers ; Function to check whether the array contains a set of contiguous integers ; Storing elements of ' arr [ ] ' in a hash table ' us ' ; As arr [ 0 ] is present in ' us ' ; Starting with previous smaller element of arr [ 0 ] ; If ' curr _ ele ' is present in ' us ' ; increment count ; update 'curr_ele" ; Starting with next greater element of arr [ 0 ] ; If ' curr _ ele ' is present in ' us ' ; increment count ; update 'curr_ele" ; Returns true if array contains a set of contiguous integers else returns false ; Driver Code
using System ; using System . Collections . Generic ; public class GFG { public static bool ? areElementsContiguous ( int [ ] arr , int n ) { HashSet < int > us = new HashSet < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { us . Add ( arr [ i ] ) ; } int count = 1 ; int curr_ele = arr [ 0 ] - 1 ; while ( us . Contains ( curr_ele ) == true ) { count ++ ; curr_ele -- ; } curr_ele = arr [ 0 ] + 1 ; while ( us . Contains ( curr_ele ) == true ) { count ++ ; curr_ele ++ ; } return ( count == ( us . Count ) ) ; } public static void Main ( string [ ] args ) { int [ ] arr = new int [ ] { 5 , 2 , 3 , 6 , 4 , 4 , 6 , 6 } ; int n = arr . Length ; if ( areElementsContiguous ( arr , n ) . Value ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }
Longest subarray not having more than K distinct elements | C # program to find longest subarray with k or less distinct elements . ; function to print the longest sub - array ; mark the element visited ; if its visited first time , then increase the counter of distinct elements by 1 ; When the counter of distinct elements increases from k , then reduce it to k ; from the left , reduce the number of time of visit ; if the reduced visited time element is not present in further segment then decrease the count of distinct elements ; increase the subsegment mark ; check length of longest sub - segment when greater then previous best then change it ; print the longest sub - segment ; Driver code
using System ; class GFG { static void longest ( int [ ] a , int n , int k ) { int [ ] freq = new int [ 7 ] ; int start = 0 , end = 0 , now = 0 , l = 0 ; for ( int i = 0 ; i < n ; i ++ ) { freq [ a [ i ] ] ++ ; if ( freq [ a [ i ] ] == 1 ) now ++ ; while ( now > k ) { freq [ a [ l ] ] -- ; if ( freq [ a [ l ] ] == 0 ) now -- ; l ++ ; } if ( i - l + 1 >= end - start + 1 ) { end = i ; start = l ; } } for ( int i = start ; i <= end ; i ++ ) Console . Write ( a [ i ] + " ▁ " ) ; } public static void Main ( String [ ] args ) { int [ ] a = { 6 , 5 , 1 , 2 , 3 , 2 , 1 , 4 , 5 } ; int n = a . Length ; int k = 3 ; longest ( a , n , k ) ; } }
Check if any K ranges overlap at any point | C # implementation of the approach ; Function that returns true if any k segments overlap at any point ; Vector to store the starting point and the ending point ; Starting points are marked by - 1 and ending points by + 1 ; Sort the vector by first element ; Stack to store the overlaps ; Get the current element ; If it is the starting point ; Push it in the stack ; It is the ending point ; Pop an element from stack ; If more than k ranges overlap ; Driver code
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static bool kOverlap ( List < Tuple < int , int > > pairs , int k ) { List < Tuple < int , int > > vec = new List < Tuple < int , int > > ( ) ; for ( int i = 0 ; i < pairs . Count ; i ++ ) { vec . Add ( new Tuple < int , int > ( pairs [ i ] . Item1 , - 1 ) ) ; vec . Add ( new Tuple < int , int > ( pairs [ i ] . Item2 , 1 ) ) ; } vec . Sort ( ) ; Stack st = new Stack ( ) ; for ( int i = 0 ; i < vec . Count ; i ++ ) { Tuple < int , int > cur = vec [ i ] ; if ( cur . Item2 == - 1 ) { st . Push ( cur ) ; } else { st . Pop ( ) ; } if ( st . Count >= k ) { return true ; } } return false ; } public static void Main ( params string [ ] args ) { List < Tuple < int , int > > pairs = new List < Tuple < int , int > > ( ) ; pairs . Add ( new Tuple < int , int > ( 1 , 3 ) ) ; pairs . Add ( new Tuple < int , int > ( 2 , 4 ) ) ; pairs . Add ( new Tuple < int , int > ( 3 , 5 ) ) ; pairs . Add ( new Tuple < int , int > ( 7 , 10 ) ) ; int n = pairs . Count , k = 3 ; if ( kOverlap ( pairs , k ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Find smallest range containing elements from k lists | C # program to finds out smallest range that includes elements from each of the given sorted lists . ; array for storing the current index of list i ; This function takes an k sorted lists in the form of 2D array as an argument . It finds out smallest range that includes elements from each of the k lists . ; initializing to 0 index ; ; for maintining the index of list containing the minimum element ; iterating over all the list ; if every element of list [ i ] is traversed then break the loop ; find minimum value among all the list elements pointing by the ptr [ ] array ; update the index of the list ; find maximum value among all the list elements pointing by the ptr [ ] array ; if any list exhaust we will not get any better answer , so break the while loop ; updating the minrange ; Driver code
using System ; class GFG { static int N = 5 ; static int [ ] ptr = new int [ 501 ] ; static void findSmallestRange ( int [ , ] arr , int n , int k ) { int i , minval , maxval , minrange , minel = 0 , maxel = 0 , flag , minind ; for ( i = 0 ; i <= k ; i ++ ) { ptr [ i ] = 0 ; } minrange = int . MaxValue ; while ( true ) { minind = - 1 ; minval = int . MaxValue ; maxval = int . MinValue ; flag = 0 ; for ( i = 0 ; i < k ; i ++ ) { if ( ptr [ i ] == n ) { flag = 1 ; break ; } if ( ptr [ i ] < n && arr [ i , ptr [ i ] ] < minval ) { minind = i ; minval = arr [ i , ptr [ i ] ] ; } if ( ptr [ i ] < n && arr [ i , ptr [ i ] ] > maxval ) { maxval = arr [ i , ptr [ i ] ] ; } } if ( flag == 1 ) { break ; } ptr [ minind ] ++ ; if ( ( maxval - minval ) < minrange ) { minel = minval ; maxel = maxval ; minrange = maxel - minel ; } } Console . WriteLine ( " The ▁ smallest ▁ range ▁ is " + " [ { 0 } , ▁ { 1 } ] STRNEWLINE " , minel , maxel ) ; } public static void Main ( String [ ] args ) { int [ , ] arr = { { 4 , 7 , 9 , 12 , 15 } , { 0 , 8 , 10 , 14 , 20 } , { 6 , 12 , 16 , 30 , 50 } } ; int k = arr . GetLength ( 0 ) ; findSmallestRange ( arr , N , k ) ; } }
Find largest d in array such that a + b + c = d | C # Program to find the largest such that d = a + b + c ; function to find largest d ; sort the array in ascending order ; iterating from backwards to find the required largest d ; since all four a , b , c , d should be distinct ; if the current combination of j , k , l in the set is equal to S [ i ] return this value as this would be the largest dsince we are iterating in descending order ; Driver Code
using System ; class GFG { static int findLargestd ( int [ ] S , int n ) { bool found = false ; Array . Sort ( S ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { for ( int j = 0 ; j < n ; j ++ ) { if ( i == j ) continue ; for ( int k = j + 1 ; k < n ; k ++ ) { if ( i == k ) continue ; for ( int l = k + 1 ; l < n ; l ++ ) { if ( i == l ) continue ; if ( S [ i ] == S [ j ] + S [ k ] + S [ l ] ) { found = true ; return S [ i ] ; } } } } } if ( found == false ) return int . MaxValue ; return - 1 ; } public static void Main ( ) { int [ ] S = new int [ ] { 2 , 3 , 5 , 7 , 12 } ; int n = S . Length ; int ans = findLargestd ( S , n ) ; if ( ans == int . MaxValue ) Console . WriteLine ( " No ▁ Solution " ) ; else Console . Write ( " Largest ▁ d ▁ such ▁ that ▁ a ▁ + ▁ " + " b ▁ + ▁ c ▁ = ▁ d ▁ is ▁ " + ans ) ; } }
Find largest d in array such that a + b + c = d | A hashing based C # program to find largest d such that a + b + c = d . ; To store and retrieve indices pair i & j ; The function finds four elements with given sum X ; Store sums ( a + b ) of all pairs ( a , b ) in a hash table ; Traverse through all pairs and find ( d - c ) is present in hash table ; If d - c is present in hash table , ; Making sure that all elements are distinct array elements and an element is not considered more than once . ; Driver code
using System ; using System . Collections . Generic ; public class Indexes { int i , j ; public Indexes ( int i , int j ) { this . i = i ; this . j = j ; } public int getI ( ) { return i ; } public int getJ ( ) { return j ; } } public class GFG { static int findFourElements ( int [ ] arr , int n ) { Dictionary < int , Indexes > map = new Dictionary < int , Indexes > ( ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { map . Add ( arr [ i ] + arr [ j ] , new Indexes ( i , j ) ) ; } } int d = int . MinValue ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int abs_diff = Math . Abs ( arr [ i ] - arr [ j ] ) ; if ( map . ContainsKey ( abs_diff ) ) { Indexes indexes = map [ abs_diff ] ; if ( indexes . getI ( ) != i && indexes . getI ( ) != j && indexes . getJ ( ) != i && indexes . getJ ( ) != j ) { d = Math . Max ( d , Math . Max ( arr [ i ] , arr [ j ] ) ) ; } } } } return d ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 5 , 7 , 12 } ; int n = arr . Length ; int res = findFourElements ( arr , n ) ; if ( res == int . MinValue ) Console . WriteLine ( " No ▁ Solution " ) ; else Console . WriteLine ( res ) ; } }
Maximize count of elements that can be selected having minimum difference between their sum and K | C # implementation of the above approach ; Function to count maximum number of elements that can be selected ; Sorting the array ; Traverse the array ; Add the current element to the sum ; If sum exceeds k ; Increment count ; Returning the count ; Driver code ; Function call
using System ; class GFG { static int CountMaximum ( int [ ] arr , int n , int k ) { Array . Sort ( arr ) ; int sum = 0 , count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; if ( sum > k ) break ; count ++ ; } return count ; } static public void Main ( ) { int [ ] arr = new int [ ] { 30 , 30 , 10 , 10 } ; int n = 4 ; int k = 50 ; Console . WriteLine ( CountMaximum ( arr , n , k ) ) ; } }
Program for array rotation | C # program for array rotation ; Function to left Rotate arr [ ] of size n by 1 ; Function to left rotate arr [ ] of size n by d ; utility function to print an array ; Driver code
using System ; class GFG { static void leftRotatebyOne ( int [ ] arr , int n ) { int i , temp = arr [ 0 ] ; for ( i = 0 ; i < n - 1 ; i ++ ) arr [ i ] = arr [ i + 1 ] ; arr [ n - 1 ] = temp ; } static void leftRotate ( int [ ] arr , int d , int n ) { for ( int i = 0 ; i < d ; i ++ ) leftRotatebyOne ( arr , n ) ; } static void printArray ( int [ ] arr , int size ) { for ( int i = 0 ; i < size ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 , 7 } ; leftRotate ( arr , 2 , 7 ) ; printArray ( arr , 7 ) ; } }