codeparrot/xlcost-text-to-code huggingface dataset:
Clockwise rotation of Linked List | C # implementation of the approach ; Link list node ; A utility function to push a node ; allocate node ; put in the data ; link the old list off the new node ; move the head to point to the new node ; A utility function to print linked list ; Function that rotates the given linked list clockwise by k and returns the updated head pointer ; If the linked list is empty ; len is used to store length of the linked list , tmp will point to the last node after this loop ; If k is greater than the size of the linked list ; Subtract from length to convert it into left rotation ; If no rotation needed then return the head node ; current will either point to kth or null after this loop ; If current is null then k is equal to the count of nodes in the list Don 't change the list in this case ; current points to the kth node ; Change next of last node to previous head ; Change head to ( k + 1 ) th node ; Change next of kth node to null ; Return the updated head pointer ; Driver code ; The constructed linked list is : 1.2 . 3.4 . 5 ; Rotate the linked list ; Print the rotated linked list
using System ; class GFG { public class Node { public int data ; public Node next ; } static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = ( head_ref ) ; ( head_ref ) = new_node ; return head_ref ; } static void printList ( Node node ) { while ( node != null ) { Console . Write ( node . data + " ▁ - > ▁ " ) ; node = node . next ; } Console . Write ( " null " ) ; } static Node rightRotate ( Node head , int k ) { if ( head == null ) return head ; Node tmp = head ; int len = 1 ; while ( tmp . next != null ) { tmp = tmp . next ; len ++ ; } if ( k > len ) k = k % len ; k = len - k ; if ( k == 0 k == len ) return head ; Node current = head ; int cnt = 1 ; while ( cnt < k && current != null ) { current = current . next ; cnt ++ ; } if ( current == null ) return head ; Node kthnode = current ; tmp . next = head ; head = kthnode . next ; kthnode . next = null ; return head ; } public static void Main ( String [ ] args ) { Node head = null ; head = push ( head , 5 ) ; head = push ( head , 4 ) ; head = push ( head , 3 ) ; head = push ( head , 2 ) ; head = push ( head , 1 ) ; int k = 2 ; Node updated_head = rightRotate ( head , k ) ; printList ( updated_head ) ; } }
Check if it is possible to make array increasing or decreasing by rotating the array | C # implementation of the approach ; Function that returns true if the array can be made increasing or decreasing after rotating it in any direction ; If size of the array is less than 3 ; Check if the array is already decreasing ; If the array is already decreasing ; Check if the array is already increasing ; If the array is already increasing ; Find the indices of the minimum && the maximum value ; Check if we can make array increasing ; If the array is increasing upto max index && minimum element is right to maximum ; Check if array increasing again or not ; Check if we can make array decreasing ; If the array is decreasing upto min index && minimum element is left to maximum ; Check if array decreasing again or not ; If it is not possible to make the array increasing or decreasing ; Driver code
using System ; class GFG { static bool isPossible ( int [ ] a , int n ) { if ( n <= 2 ) return true ; int flag = 0 ; for ( int i = 0 ; i < n - 2 ; i ++ ) { if ( ! ( a [ i ] > a [ i + 1 ] && a [ i + 1 ] > a [ i + 2 ] ) ) { flag = 1 ; break ; } } if ( flag == 0 ) return true ; flag = 0 ; for ( int i = 0 ; i < n - 2 ; i ++ ) { if ( ! ( a [ i ] < a [ i + 1 ] && a [ i + 1 ] < a [ i + 2 ] ) ) { flag = 1 ; break ; } } if ( flag == 0 ) return true ; int val1 = int . MaxValue , mini = - 1 , val2 = int . MinValue , maxi = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] < val1 ) { mini = i ; val1 = a [ i ] ; } if ( a [ i ] > val2 ) { maxi = i ; val2 = a [ i ] ; } } flag = 1 ; for ( int i = 0 ; i < maxi ; i ++ ) { if ( a [ i ] > a [ i + 1 ] ) { flag = 0 ; break ; } } if ( flag == 1 && maxi + 1 == mini ) { flag = 1 ; for ( int i = mini ; i < n - 1 ; i ++ ) { if ( a [ i ] > a [ i + 1 ] ) { flag = 0 ; break ; } } if ( flag == 1 ) return true ; } flag = 1 ; for ( int i = 0 ; i < mini ; i ++ ) { if ( a [ i ] < a [ i + 1 ] ) { flag = 0 ; break ; } } if ( flag == 1 && maxi - 1 == mini ) { flag = 1 ; for ( int i = maxi ; i < n - 1 ; i ++ ) { if ( a [ i ] < a [ i + 1 ] ) { flag = 0 ; break ; } } if ( flag == 1 ) return true ; } return false ; } public static void Main ( ) { int [ ] a = { 4 , 5 , 6 , 2 , 3 } ; int n = a . Length ; if ( isPossible ( a , n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Count number of rotated strings which have more number of vowels in the first half than second half | C # implementation of the approach ; Function to return the count of rotated Strings which have more number of vowels in the first half than the second half ; Create a new String ; Pre array to store count of all vowels ; Compute the prefix array ; To store the required answer ; Find all rotated Strings ; Right and left index of the String ; x1 stores the number of vowels in the rotated String ; Left stores the number of vowels in the first half of rotated String ; Right stores the number of vowels in the second half of rotated String ; If the count of vowels in the first half is greater than the count in the second half ; Return the required answer ; Driver code
using System ; class GFG { static int cntRotations ( string s , int n ) { string str = s + s ; int [ ] pre = new int [ 2 * n ] ; for ( int i = 0 ; i < 2 * n ; i ++ ) { if ( i != 0 ) pre [ i ] += pre [ i - 1 ] ; if ( str [ i ] == ' a ' str [ i ] == ' e ' str [ i ] == ' i ' str [ i ] == ' o ' str [ i ] == ' u ' ) { pre [ i ] ++ ; } } int ans = 0 ; for ( int i = n - 1 ; i < 2 * n - 1 ; i ++ ) { int r = i , l = i - n ; int x1 = pre [ r ] ; if ( l >= 0 ) x1 -= pre [ l ] ; r = i - n / 2 ; int left = pre [ r ] ; if ( l >= 0 ) left -= pre [ l ] ; int right = x1 - left ; if ( left > right ) { ans ++ ; } } return ans ; } public static void Main ( ) { String s = " abecidft " ; int n = s . Length ; Console . WriteLine ( cntRotations ( s , n ) ) ; } }
Count number of rotated strings which have more number of vowels in the first half than second half | C # implementation of the approach ; Function to return the count of rotated strings which have more number of vowels in the first half than the second half ; Compute the number of vowels in first - half ; Compute the number of vowels in second - half ; Check if first - half has more vowels ; Check for all possible rotations ; Return the answer ; Driver code ; Function call
using System ; class GFG { static int cntRotations ( char [ ] s , int n ) { int lh = 0 , rh = 0 , i , ans = 0 ; for ( i = 0 ; i < n / 2 ; ++ i ) if ( s [ i ] == ' a ' s [ i ] == ' e ' s [ i ] == ' i ' s [ i ] == ' o ' s [ i ] == ' u ' ) { lh ++ ; } for ( i = n / 2 ; i < n ; ++ i ) if ( s [ i ] == ' a ' s [ i ] == ' e ' s [ i ] == ' i ' s [ i ] == ' o ' s [ i ] == ' u ' ) { rh ++ ; } if ( lh > rh ) ans ++ ; for ( i = 1 ; i < n ; ++ i ) { if ( s [ i - 1 ] == ' a ' s [ i - 1 ] == ' e ' s [ i - 1 ] == ' i ' s [ i - 1 ] == ' o ' s [ i - 1 ] == ' u ' ) { rh ++ ; lh -- ; } if ( s [ ( i - 1 + n / 2 ) % n ] == ' a ' || s [ ( i - 1 + n / 2 ) % n ] == ' e ' || s [ ( i - 1 + n / 2 ) % n ] == ' i ' || s [ ( i - 1 + n / 2 ) % n ] == ' o ' || s [ ( i - 1 + n / 2 ) % n ] == ' u ' ) { rh -- ; lh ++ ; } if ( lh > rh ) ans ++ ; } return ans ; } static void Main ( ) { char [ ] s = { ' a ' , ' b ' , ' e ' , ' c ' , ' i ' , ' d ' , ' f ' , ' t ' } ; int n = s . Length ; Console . WriteLine ( cntRotations ( s , n ) ) ; } }
Queries for rotation and Kth character of the given string in constant time | C # implementation of the approach ; Function to perform the required queries on the given string ; Pointer pointing to the current starting character of the string ; For every query ; If the query is to rotate the string ; Update the pointer pointing to the starting character of the string ; Index of the kth character in the current rotation of the string ; Print the kth character ; Driver code
using System ; class GFG { static int size = 2 ; static void performQueries ( String str , int n , int [ , ] queries , int q ) { int ptr = 0 ; for ( int i = 0 ; i < q ; i ++ ) { if ( queries [ i , 0 ] == 1 ) { ptr = ( ptr + queries [ i , 1 ] ) % n ; } else { int k = queries [ i , 1 ] ; int index = ( ptr + k - 1 ) % n ; Console . WriteLine ( str [ index ] ) ; } } } public static void Main ( String [ ] args ) { String str = " abcdefgh " ; int n = str . Length ; int [ , ] queries = { { 1 , 2 } , { 2 , 2 } , { 1 , 4 } , { 2 , 7 } } ; int q = queries . GetLength ( 0 ) ; performQueries ( str , n , queries , q ) ; } }
Count rotations of N which are Odd and Even | CSharp implementation of the above approach ; Function to count of all rotations which are odd and even ; Driver Code
using System ; class Solution { static void countOddRotations ( int n ) { int odd_count = 0 , even_count = 0 ; do { int digit = n % 10 ; if ( digit % 2 == 1 ) odd_count ++ ; else even_count ++ ; n = n / 10 ; } while ( n != 0 ) ; Console . WriteLine ( " Odd ▁ = ▁ " + odd_count ) ; Console . WriteLine ( " Even ▁ = ▁ " + even_count ) ; } public static void Main ( ) { int n = 1234 ; countOddRotations ( n ) ; } }
Generate all rotations of a number | C # implementation of the approach ; Function to return the count of digits of n ; Function to print the left shift numbers ; Formula to calculate left shift from previous number ; Update the original number ; Driver Code
using System ; public class GFG { static int numberOfDigits ( int n ) { int cnt = 0 ; while ( n > 0 ) { cnt ++ ; n /= 10 ; } return cnt ; } static void cal ( int num ) { int digits = numberOfDigits ( num ) ; int powTen = ( int ) Math . Pow ( 10 , digits - 1 ) ; for ( int i = 0 ; i < digits - 1 ; i ++ ) { int firstDigit = num / powTen ; int left = ( ( num * 10 ) + firstDigit ) - ( firstDigit * powTen * 10 ) ; Console . Write ( left + " ▁ " ) ; num = left ; } } static public void Main ( ) { int num = 1445 ; cal ( num ) ; } }
Check whether all the rotations of a given number is greater than or equal to the given number or not | C # implementation of the approach ; Splitting the number at index i and adding to the front ; Checking if the value is greater than or equal to the given value ; Driver code
using System ; class GFG { static void CheckKCycles ( int n , String s ) { bool ff = true ; int x = 0 ; for ( int i = 1 ; i < n ; i ++ ) { x = ( s . Substring ( i ) + s . Substring ( 0 , i ) ) . Length ; if ( x >= s . Length ) { continue ; } ff = false ; break ; } if ( ff ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } public static void Main ( String [ ] args ) { int n = 3 ; String s = "123" ; CheckKCycles ( n , s ) ; } }
Rotate the sub | C # implementation of the above approach ; Definition of node of linkedlist ; This function take head pointer of list , start and end points of sublist that is to be rotated and the number k and rotate the sublist to right by k places . ; If k is greater than size of sublist then we will take its modulo with size of sublist ; If k is zero or k is equal to size or k is a multiple of size of sublist then list remains intact ; m - th node ; This loop will traverse all node till end node of sublist . Current traversed node ; Count of traversed nodes ; Previous of m - th node ; We will save ( m - 1 ) th node and later make it point to ( n - k + 1 ) th node ; That is how we bring ( n - k + 1 ) th node to front of sublist . ; This keeps rest part of list intact . ; Function for creating and linking new nodes ; Driver code
using System ; class GFG { public class ListNode { public int data ; public ListNode next ; } public static void rotateSubList ( ListNode A , int m , int n , int k ) { int size = n - m + 1 ; if ( k > size ) { k = k % size ; } if ( k == 0 k == size ) { ListNode head = A ; while ( head != null ) { Console . Write ( head . data ) ; head = head . next ; } return ; } ListNode link = null ; if ( m == 1 ) { link = A ; } ListNode c = A ; int count = 0 ; ListNode end = null ; ListNode pre = null ; while ( c != null ) { count ++ ; if ( count == m - 1 ) { pre = c ; link = c . next ; } if ( count == n - k ) { if ( m == 1 ) { end = c ; A = c . next ; } else { end = c ; pre . next = c . next ; } } if ( count == n ) { ListNode d = c . next ; c . next = link ; end . next = d ; ListNode head = A ; while ( head != null ) { Console . Write ( head . data + " ▁ " ) ; head = head . next ; } return ; } c = c . next ; } } public static ListNode push ( ListNode head , int val ) { ListNode new_node = new ListNode ( ) ; new_node . data = val ; new_node . next = ( head ) ; ( head ) = new_node ; return head ; } public static void Main ( string [ ] args ) { ListNode head = null ; head = push ( head , 70 ) ; head = push ( head , 60 ) ; head = push ( head , 50 ) ; head = push ( head , 40 ) ; head = push ( head , 30 ) ; head = push ( head , 20 ) ; head = push ( head , 10 ) ; ListNode tmp = head ; Console . Write ( " Given ▁ List : ▁ " ) ; while ( tmp != null ) { Console . Write ( tmp . data + " ▁ " ) ; tmp = tmp . next ; } Console . WriteLine ( ) ; int m = 3 , n = 6 , k = 2 ; Console . Write ( " After ▁ rotation ▁ of ▁ sublist : ▁ " ) ; rotateSubList ( head , m , n , k ) ; } }
Generating numbers that are divisor of their right | C # program to Generating numbers that are divisor of their right - rotations ; Function to check if N is a divisor of its right - rotation ; Function to generate m - digit numbers which are divisor of their right - rotation ; Driver code
using System ; public class GFG { static bool rightRotationDivisor ( int N ) { int lastDigit = N % 10 ; int rightRotation = ( int ) ( lastDigit * Math . Pow ( 10 , ( int ) ( Math . Log10 ( N ) ) ) + Math . Floor ( ( double ) N / 10 ) ) ; return ( rightRotation % N == 0 ) ; } static void generateNumbers ( int m ) { for ( int i = ( int ) Math . Pow ( 10 , ( m - 1 ) ) ; i < Math . Pow ( 10 , m ) ; i ++ ) if ( rightRotationDivisor ( i ) ) Console . WriteLine ( i ) ; } public static void Main ( ) { int m = 3 ; generateNumbers ( m ) ; } }
Generating numbers that are divisor of their right | C # program to Generating numbers that are divisor of their right - rotations ; Function to generate m - digit numbers which are divisor of their right - rotation ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void generateNumbers ( int m ) { List < int > numbers = new List < int > ( ) ; int k_max , x ; for ( int y = 0 ; y < 10 ; y ++ ) { k_max = ( int ) ( Math . Pow ( 10 , m - 2 ) * ( 10 * y + 1 ) ) / ( int ) ( Math . Pow ( 10 , m - 1 ) + y ) ; for ( int k = 1 ; k <= k_max ; k ++ ) { x = ( int ) ( y * ( Math . Pow ( 10 , m - 1 ) - k ) ) / ( 10 * k - 1 ) ; if ( ( int ) ( y * ( Math . Pow ( 10 , m - 1 ) - k ) ) % ( 10 * k - 1 ) == 0 ) numbers . Add ( 10 * x + y ) ; } } numbers . Sort ( ) ; for ( int i = 0 ; i < numbers . Count ; i ++ ) Console . WriteLine ( numbers [ i ] ) ; } public static void Main ( String [ ] args ) { int m = 3 ; generateNumbers ( m ) ; } }
Check if an array is sorted and rotated | C # program to check if an array is sorted and rotated clockwise ; Function to check if an array is sorted and rotated clockwise ; Find the minimum element and it 's index ; Check if all elements before minIndex are in increasing order ; Check if all elements after minIndex are in increasing order ; Check if last element of the array is smaller than the element just before the element at minIndex starting element of the array for arrays like [ 3 , 4 , 6 , 1 , 2 , 5 ] - not circular array ; Driver code ; Function Call
using System ; class GFG { static void checkIfSortRotated ( int [ ] arr , int n ) { int minEle = int . MaxValue ; int maxEle = int . MinValue ; int minIndex = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < minEle ) { minEle = arr [ i ] ; minIndex = i ; } } bool flag1 = true ; for ( int i = 1 ; i < minIndex ; i ++ ) { if ( arr [ i ] < arr [ i - 1 ] ) { flag1 = false ; break ; } } bool flag2 = true ; for ( int i = minIndex + 1 ; i < n ; i ++ ) { if ( arr [ i ] < arr [ i - 1 ] ) { flag2 = false ; break ; } } if ( flag1 && flag2 && ( arr [ n - 1 ] < arr [ 0 ] ) ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } public static void Main ( ) { int [ ] arr = { 3 , 4 , 5 , 1 , 2 } ; int n = arr . Length ; checkIfSortRotated ( arr , n ) ; } }
Rotate a matrix by 90 degree in clockwise direction without using any extra space | C # implementation of above approach ; Function to rotate the matrix 90 degree clockwise ; printing the matrix on the basis of observations made on indices . ; Driver code
using System ; class GFG { static int N = 4 ; static void rotate90Clockwise ( int [ , ] arr ) { for ( int j = 0 ; j < N ; j ++ ) { for ( int i = N - 1 ; i >= 0 ; i -- ) Console . Write ( arr [ i , j ] + " ▁ " ) ; Console . WriteLine ( ) ; } } static void Main ( ) { int [ , ] arr = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 16 } } ; rotate90Clockwise ( arr ) ; } }
Rotate a matrix by 90 degree in clockwise direction without using any extra space |  ; rotate function ; first rotation with respect to main diagonal ; Second rotation with respect to middle column ; to print matrix ; Driver code
using System ; using System . Collections . Generic ; public class GFG { static void rotate ( int [ , ] arr ) { int n = arr . GetLength ( 0 ) ; for ( int i = 0 ; i < n ; ++ i ) { for ( int j = 0 ; j < i ; ++ j ) { int temp = arr [ i , j ] ; arr [ i , j ] = arr [ j , i ] ; arr [ j , i ] = temp ; } } for ( int i = 0 ; i < n ; ++ i ) { for ( int j = 0 ; j < n / 2 ; ++ j ) { int temp = arr [ i , j ] ; arr [ i , j ] = arr [ i , n - j - 1 ] ; arr [ i , n - j - 1 ] = temp ; } } } static void printMatrix ( int [ , ] arr ) { int n = arr . GetLength ( 0 ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) Console . Write ( arr [ i , j ] + " ▁ " ) ; Console . WriteLine ( ) ; } } public static void Main ( String [ ] args ) { int [ , ] arr = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 16 } } ; rotate ( arr ) ; printMatrix ( arr ) ; } }
Rotate a matrix by 90 degree in clockwise direction without using any extra space |  ; rotate function ; First rotation with respect to Secondary diagonal ; Second rotation with respect to middle row ; To print matrix ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void rotate ( int [ , ] arr ) { int n = arr . GetLength ( 0 ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n - i ; j ++ ) { int temp = arr [ i , j ] ; arr [ i , j ] = arr [ n - 1 - j , n - 1 - i ] ; arr [ n - 1 - j , n - 1 - i ] = temp ; } } for ( int i = 0 ; i < n / 2 ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { int temp = arr [ i , j ] ; arr [ i , j ] = arr [ n - 1 - i , j ] ; arr [ n - 1 - i , j ] = temp ; } } } static void printMatrix ( int [ , ] arr ) { int n = arr . GetLength ( 0 ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) Console . Write ( arr [ i , j ] + " ▁ " ) ; Console . WriteLine ( ) ; } } public static void Main ( String [ ] args ) { int [ , ] arr = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 16 } } ; rotate ( arr ) ; printMatrix ( arr ) ; } }
Elements that occurred only once in the array | C # implementation of above approach ; Function to find the elements that appeared only once in the array ; Sort the array ; Check for first element ; Check for all the elements if it is different its adjacent elements ; Check for the last element ; Driver code
using System ; class GFG { static void occurredOnce ( int [ ] arr , int n ) { Array . Sort ( arr ) ; if ( arr [ 0 ] != arr [ 1 ] ) Console . Write ( arr [ 0 ] + " ▁ " ) ; for ( int i = 1 ; i < n - 1 ; i ++ ) if ( arr [ i ] != arr [ i + 1 ] && arr [ i ] != arr [ i - 1 ] ) Console . Write ( arr [ i ] + " ▁ " ) ; if ( arr [ n - 2 ] != arr [ n - 1 ] ) Console . Write ( arr [ n - 1 ] + " ▁ " ) ; } public static void Main ( ) { int [ ] arr = { 7 , 7 , 8 , 8 , 9 , 1 , 1 , 4 , 2 , 2 } ; int n = arr . Length ; occurredOnce ( arr , n ) ; } }
Elements that occurred only once in the array | C # implementation to find elements that appeared only once ; Function to find the elements that appeared only once in the array ; Store all the elements in the map with their occurrence ; Traverse the map and print all the elements with occurrence 1 ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void occurredOnce ( int [ ] arr , int n ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . ContainsKey ( arr [ i ] ) ) mp [ arr [ i ] ] = 1 + mp [ arr [ i ] ] ; else mp . Add ( arr [ i ] , 1 ) ; } foreach ( KeyValuePair < int , int > entry in mp ) { if ( Int32 . Parse ( String . Join ( " " , entry . Value ) ) == 1 ) Console . Write ( entry . Key + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 7 , 7 , 8 , 8 , 9 , 1 , 1 , 4 , 2 , 2 } ; int n = arr . Length ; occurredOnce ( arr , n ) ; } }
Elements that occurred only once in the array | C # implementation to find elements that appeared only once ; Function to find the elements that appeared only once in the array ; Check if the first and last element is equal . If yes , remove those elements ; Start traversing the remaining elements ; Check if current element is equal to the element at immediate previous index If yes , check the same for next element ; Else print the current element ; Check for the last element ; Driver code
using System ; class GFG { static void occurredOnce ( int [ ] arr , int n ) { int i = 1 , len = n ; if ( arr [ 0 ] == arr [ len - 1 ] ) { i = 2 ; len -- ; } for ( ; i < n ; i ++ ) if ( arr [ i ] == arr [ i - 1 ] ) i ++ ; else Console . Write ( arr [ i - 1 ] + " ▁ " ) ; if ( arr [ n - 1 ] != arr [ 0 ] && arr [ n - 1 ] != arr [ n - 2 ] ) Console . Write ( arr [ n - 1 ] ) ; } public static void Main ( ) { int [ ] arr = { 7 , 7 , 8 , 8 , 9 , 1 , 1 , 4 , 2 , 2 } ; int n = arr . Length ; occurredOnce ( arr , n ) ; } }
Split the array and add the first part to the end | Set 2 | C # program to Split the array and add the first part to the end ; Function to reverse arr [ ] from index start to end ; Function to print an array ; Function to left rotate arr [ ] of size n by k ; Driver code ; Function calling
using System ; class GFG { static void rvereseArray ( int [ ] arr , int start , int end ) { while ( start < end ) { int temp = arr [ start ] ; arr [ start ] = arr [ end ] ; arr [ end ] = temp ; start ++ ; end -- ; } } static void printArray ( int [ ] arr , int size ) { for ( int i = 0 ; i < size ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } static void splitArr ( int [ ] arr , int k , int n ) { rvereseArray ( arr , 0 , n - 1 ) ; rvereseArray ( arr , 0 , n - k - 1 ) ; rvereseArray ( arr , n - k , n - 1 ) ; } public static void Main ( ) { int [ ] arr = { 12 , 10 , 5 , 6 , 52 , 36 } ; int n = arr . Length ; int k = 2 ; splitArr ( arr , k , n ) ; printArray ( arr , n ) ; } }
Check if strings are rotations of each other or not | Set 2 | C # program to check if two strings are rotations of each other . ; create lps [ ] that will hold the longest prefix suffix values for pattern ; length of the previous longest prefix suffix ; lps [ 0 ] is always 0 ; the loop calculates lps [ i ] for i = 1 to n - 1 ; match from that rotating point ; Driver code
using System ; class GFG { public static bool isRotation ( string a , string b ) { int n = a . Length ; int m = b . Length ; if ( n != m ) return false ; int [ ] lps = new int [ n ] ; int len = 0 ; int i = 1 ; lps [ 0 ] = 0 ; while ( i < n ) { if ( a [ i ] == b [ len ] ) { lps [ i ] = ++ len ; ++ i ; } else { if ( len == 0 ) { lps [ i ] = 0 ; ++ i ; } else { len = lps [ len - 1 ] ; } } } i = 0 ; for ( int k = lps [ n - 1 ] ; k < m ; ++ k ) { if ( b [ k ] != a [ i ++ ] ) return false ; } return true ; } public static void Main ( ) { string s1 = " ABACD " ; string s2 = " CDABA " ; Console . WriteLine ( isRotation ( s1 , s2 ) ? "1" : "0" ) ; } }