codeparrot/xlcost-text-to-code huggingface dataset:
Partition string into two substrings having maximum number of common non | C ++ program to implement the above approach ; Function to count maximum common non - repeating characters that can be obtained by partitioning the string into two non - empty substrings ; Stores count of non - repeating characters present in both the substrings ; Stores distinct characters in left substring ; Stores distinct characters in right substring ; Traverse left substring ; Insert S [ i ] into ls ; Traverse right substring ; Insert S [ i ] into rs ; Traverse distinct characters of left substring ; If current character is present in right substring ; Update cnt ; Return count ; Function to partition the string into two non - empty substrings in all possible ways ; Stores maximum common distinct characters present in both the substring partitions ; Traverse the string ; Update ans ; Print count of maximum common non - repeating characters ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countCommonChar ( int ind , string & S ) { int cnt = 0 ; set < char > ls ; set < char > rs ; for ( int i = 0 ; i < ind ; ++ i ) { ls . insert ( S [ i ] ) ; } for ( int i = ind ; i < S . length ( ) ; ++ i ) { rs . insert ( S [ i ] ) ; } for ( auto v : ls ) { if ( rs . count ( v ) ) { ++ cnt ; } } return cnt ; } void partitionStringWithMaxCom ( string & S ) { int ans = 0 ; for ( int i = 1 ; i < S . length ( ) ; ++ i ) { ans = max ( ans , countCommonChar ( i , S ) ) ; } cout << ans << " STRNEWLINE " ; } int main ( ) { string str = " aabbca " ; partitionStringWithMaxCom ( str ) ; return 0 ; }
Partition string into two substrings having maximum number of common non | C ++ program to implement the above approach ; Function to count maximum common non - repeating characters that can be obtained by partitioning the string into two non - empty substrings ; Stores distinct characters of S in sorted order ; Stores maximum common distinct characters present in both the partitions ; Stores frequency of each distinct character n the string S ; Traverse the string ; Update frequency of S [ i ] ; Traverse the string ; Decreasing frequency of S [ i ] ; If the frequency of S [ i ] is 0 ; Remove S [ i ] from Q ; Insert S [ i ] into Q ; Stores count of distinct characters in Q ; Update res ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE #include <ext/pb_ds/assoc_container.hpp> NEW_LINE #include <ext/pb_ds/tree_policy.hpp> NEW_LINE using namespace std ; using namespace __gnu_cxx ; using namespace __gnu_pbds ; template < typename T > using ordered_set = tree < T , null_type , less < T > , rb_tree_tag , tree_order_statistics_node_update > ; int countMaxCommonChar ( string & S ) { ordered_set < char > Q ; int res = 0 ; map < char , int > freq ; for ( int i = 0 ; i < S . length ( ) ; i ++ ) { freq [ S [ i ] ] ++ ; } for ( int i = 0 ; i < S . length ( ) ; i ++ ) { freq [ S [ i ] ] -- ; if ( ! freq [ S [ i ] ] ) { Q . erase ( S [ i ] ) ; } else { Q . insert ( S [ i ] ) ; } int curr = Q . size ( ) ; res = max ( res , curr ) ; } cout << res << " STRNEWLINE " ; } int main ( ) { string str = " aabbca " ; countMaxCommonChar ( str ) ; return 0 ; }
Longest Substring of 1 's after removing one character | C ++ Program to implement the above approach ; Function to calculate the length of the longest substring of '1' s that can be obtained by deleting one character ; Add '0' at the end ; Iterator to traverse the string ; Stores maximum length of required substring ; Stores length of substring of '1' preceding the current character ; Stores length of substring of '1' succeeding the current character ; Counts number of '0' s ; Traverse the string S ; If current character is '1' ; Increase curr_one by one ; Otherwise ; Increment numberofZeros by one ; Count length of substring obtained y concatenating preceding and succeeding substrings of '1' ; Store maximum size in res ; Assign curr_one to prev_one ; Reset curr_one ; If string contains only one '0' ; Return the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int longestSubarray ( string s ) { s += '0' ; int i ; int res = 0 ; int prev_one = 0 ; int curr_one = 0 ; int numberOfZeros = 0 ; for ( i = 0 ; i < s . length ( ) ; i ++ ) { if ( s [ i ] == '1' ) { curr_one += 1 ; } else { numberOfZeros += 1 ; prev_one += curr_one ; res = max ( res , prev_one ) ; prev_one = curr_one ; curr_one = 0 ; } } if ( numberOfZeros == 1 ) { res -= 1 ; } return res ; } int main ( ) { string S = "1101" ; cout << longestSubarray ( S ) ; return 0 ; }
Longest Substring of 1 's after removing one character | C ++ Program to implement the above approach ; Function to calculate the length of the longest substring of '1' s that can be obtained by deleting one character ; Initializing i and j as left and right boundaries of sliding window ; If current character is '0' ; Decrement k by one ; If k is less than zero and character at ith index is '0' ; Return result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int longestSubarray ( string s ) { int i = 0 , j = 0 , k = 1 ; for ( j = 0 ; j < s . size ( ) ; ++ j ) { if ( s [ j ] == '0' ) k -- ; if ( k < 0 && s [ i ++ ] == '0' ) k ++ ; } return j - i - 1 ; } int main ( ) { string S = "011101101" ; cout << longestSubarray ( S ) ; return 0 ; }
Lexicographically smallest string possible by inserting given character | C ++ Program to implement the above approach ; Function to obtain lexicographically smallest string possible by inserting character c in the string s ; Traverse the string ; If the current character is greater than the given character ; Insert the character before the greater character ; Return the string ; Append the character at the end ; Return the string ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string SmallestString ( string s , char c ) { for ( int i = 0 ; i < s . size ( ) ; i ++ ) { if ( s [ i ] > c ) { s . insert ( i , 1 , c ) ; return s ; } } s += c ; return s ; } int main ( ) { string S = " acd " ; char C = ' b ' ; cout << SmallestString ( S , C ) << endl ; return 0 ; }
Largest Non | C ++ program to implement the above approach ; Function to find the largest unique element of the array ; Store frequency of each distinct array element ; Traverse the array ; Update frequency of arr [ i ] ; Stores largest non - repeating element present in the array ; Stores index of the largest unique element of the array ; Traverse the array ; If frequency of arr [ i ] is equal to 1 and arr [ i ] exceeds LNRElem ; Update ind ; Update LNRElem ; If no array element is found with frequency equal to 1 ; Print the largest non - repeating element ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void LarUnEl ( int arr [ ] , int N ) { unordered_map < int , int > mp ; for ( int i = 0 ; i < N ; i ++ ) { mp [ arr [ i ] ] ++ ; } int LNRElem = INT_MIN ; int ind = -1 ; for ( int i = 0 ; i < N ; i ++ ) { if ( mp [ arr [ i ] ] == 1 && arr [ i ] > LNRElem ) { ind = i ; LNRElem = arr [ i ] ; } } if ( ind == -1 ) { cout << ind ; return ; } cout << arr [ ind ] ; } int main ( ) { int arr [ ] = { 3 , 1 , 8 , 8 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; LarUnEl ( arr , N ) ; }
Minimum shifts of substrings of 1 s required to group all 1 s together in a given Binary string | C ++ program for the above approach ; Function to count indices substrings of 1 s need to be shifted such that all 1 s in the string are grouped together ; Stores first occurrence of '1' ; Stores last occurrence of '1' ; Count of 0 s between firstOne and lastOne ; Traverse the string to find the first and last occurrences of '1' ; Count number of 0 s present between firstOne and lastOne ; Print minimum operations ; Driver Code ; Given string
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countShifts ( string str ) { int firstOne = -1 ; int lastOne = -1 ; int count = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str [ i ] == '1' ) { if ( firstOne == -1 ) firstOne = i ; lastOne = i ; } } if ( ( firstOne == -1 ) || ( firstOne == lastOne ) ) { cout << 0 ; return ; } for ( int i = firstOne ; i <= lastOne ; i ++ ) { if ( str [ i ] == '0' ) { count ++ ; } } cout << count << endl ; } int main ( ) { string str = "00110111011" ; countShifts ( str ) ; return 0 ; }
Check if a non | C ++ program for the above approach ; Utility Function to check whether a subsequence same as the given subarray exists or not ; Check if first element of the subarray is also present before ; Check if last element of the subarray is also present later ; If above two conditions are not satisfied , then no such subsequence exists ; Function to check and print if a subsequence which is same as the given subarray is present or not ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkSubsequenceUtil ( int arr [ ] , int L , int R , int N ) { for ( int i = 0 ; i < L ; i ++ ) if ( arr [ i ] == arr [ L ] ) return true ; for ( int i = R + 1 ; i < N ; i ++ ) if ( arr [ i ] == arr [ R ] ) return true ; return false ; } void checkSubsequence ( int arr [ ] , int L , int R , int N ) { if ( checkSubsequenceUtil ( arr , L , R , N ) ) { cout << " YES STRNEWLINE " ; } else { cout << " NO STRNEWLINE " ; } } int main ( ) { int arr [ ] = { 1 , 7 , 12 , 1 , 7 , 5 , 10 , 11 , 42 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int L = 3 , R = 6 ; checkSubsequence ( arr , L , R , N ) ; }
Search an element in a Doubly Linked List | C ++ program to implement the above approach ; Structure of a node of the doubly linked list ; Stores data value of a node ; Stores pointer to next node ; Stores pointer to previous node ; Function to insert a node at the beginning of the Doubly Linked List ; Allocate memory for new node ; Insert the data ; Since node is added at the beginning , prev is always NULL ; Link the old list to the new node ; If pointer to head is not NULL ; Change the prev of head node to new node ; Move the head to point to the new node ; Function to find the position of an integer in doubly linked list ; Stores head Node ; Stores position of the integer in the doubly linked list ; Traverse the doubly linked list ; Update pos ; Update temp ; If the integer not present in the doubly linked list ; If the integer present in the doubly linked list ; Driver Code ; Create the doubly linked list 18 < -> 15 < -> 8 < -> 9 < -> 14
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * next ; Node * prev ; } ; void push ( Node * * head_ref , int new_data ) { Node * new_node = ( Node * ) malloc ( sizeof ( struct Node ) ) ; new_node -> data = new_data ; new_node -> prev = NULL ; new_node -> next = ( * head_ref ) ; if ( ( * head_ref ) != NULL ) { ( * head_ref ) -> prev = new_node ; } ( * head_ref ) = new_node ; } int search ( Node * * head_ref , int x ) { Node * temp = * head_ref ; int pos = 0 ; while ( temp -> data != x && temp -> next != NULL ) { pos ++ ; temp = temp -> next ; } if ( temp -> data != x ) return -1 ; return ( pos + 1 ) ; } int main ( ) { Node * head = NULL ; int X = 8 ; push ( & head , 14 ) ; push ( & head , 9 ) ; push ( & head , 8 ) ; push ( & head , 15 ) ; push ( & head , 18 ) ; cout << search ( & head , X ) ; return 0 ; }
Longest subarray in which all elements are smaller than K | C ++ program for the above approach ; Function to find the length of the longest subarray with all elements smaller than K ; Stores the length of maximum consecutive sequence ; Stores maximum length of subarray ; Iterate through the array ; Check if array element smaller than K ; Store the maximum of length and count ; Reset the counter ; Print the maximum length ; Driver Code ; Given array arr [ ] ; Size of the array ; Given K ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int largestSubarray ( int arr [ ] , int N , int K ) { int count = 0 ; int len = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] < K ) { count += 1 ; } else { len = max ( len , count ) ; count = 0 ; } } if ( count ) { len = max ( len , count ) ; } cout << len ; } int main ( ) { int arr [ ] = { 1 , 8 , 3 , 5 , 2 , 2 , 1 , 13 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 6 ; largestSubarray ( arr , N , K ) ; return 0 ; }
Smallest pair of indices with product of subarray co | C ++ program for the above approach ; Function to calculate GCD of two integers ; Recursively calculate GCD ; Function to find the lexicographically smallest pair of indices whose product is co - prime with the product of the subarrays on its left and right ; Stores the suffix product of array elements ; Set 0 / 1 if pair satisfies the given condition or not ; Initialize array right_prod [ ] ; Update the suffix product ; Stores product of all elements ; Stores the product of subarray in between the pair of indices ; Iterate through every pair of indices ( i , j ) ; Store product of A [ i , j ] ; Check if product is co - prime to product of either the left or right subarrays ; If no such pair is found , then print - 1 ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } void findPair ( int A [ ] , int N ) { int right_prod [ N ] ; int flag = 0 ; right_prod [ N - 1 ] = A [ N - 1 ] ; for ( int i = N - 2 ; i >= 0 ; i -- ) right_prod [ i ] = right_prod [ i + 1 ] * A [ i ] ; int total_prod = right_prod [ 0 ] ; int product ; for ( int i = 1 ; i < N - 1 ; i ++ ) { product = 1 ; for ( int j = i ; j < N - 1 ; j ++ ) { product *= A [ j ] ; if ( gcd ( product , right_prod [ j + 1 ] ) == 1 || gcd ( product , total_prod / right_prod [ i ] ) == 1 ) { flag = 1 ; cout << " ( " << i - 1 << " , ▁ " << j + 1 << " ) " ; break ; } } if ( flag == 1 ) break ; } if ( flag == 0 ) cout << -1 ; } int main ( ) { int arr [ ] = { 2 , 4 , 1 , 3 , 7 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findPair ( arr , N ) ; return 0 ; }
Count binary strings of length same as given string after removal of substrings "01" and "00" that consists of at least one '1' | C ++ program for the above approach ; Function to count the strings consisting of at least 1 set bit ; Initialize count ; Iterate through string ; The answer is 2 ^ N - 1 ; Driver Code ; Given string ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countString ( string S ) { long long count = 0 ; for ( auto it : S ) { if ( it == '0' and count > 0 ) { count -- ; } else { count ++ ; } } cout << ( ( 1 << count ) - 1 ) << " STRNEWLINE " ; } int main ( ) { string S = "1001" ; countString ( S ) ; return 0 ; }
Append digits to the end of duplicate strings to make all strings in an array unique | C ++ program for the above approach ; Function to replace duplicate strings by alphanumeric strings to make all strings in the array unique ; Store the frequency of strings ; Iterate over the array ; For the first occurrence , update the frequency count ; Otherwise ; Append frequency count to end of the string ; Print the modified array ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void replaceDuplicates ( vector < string > & names ) { unordered_map < string , int > hash ; for ( int i = 0 ; i < names . size ( ) ; i ++ ) { if ( hash . count ( names [ i ] ) == 0 ) hash [ names [ i ] ] ++ ; else { int count = hash [ names [ i ] ] ++ ; names [ i ] += to_string ( count ) ; } } for ( int i = 0 ; i < names . size ( ) ; i ++ ) { cout << names [ i ] << " ▁ " ; } } int main ( ) { vector < string > str = { " aa " , " bb " , " cc " , " bb " , " aa " , " aa " , " aa " } ; replaceDuplicates ( str ) ; return 0 ; }
Minimum cost of flipping characters required to convert Binary String to 0 s only | C ++ Program to implement the above approach ; Function to find the minimum cost to convert given string to 0 s only ; Length of string ; Stores the index of leftmost '1' in str ; Update the index of leftmost '1' in str ; Stores the index of rightmost '1' in str ; Update the index of rightmost '1' in str ; If str does not contain any '1' s ; No changes required ; Stores minimum cost ; Iterating through str form left_1 to right_1 ; Stores length of consecutive 0 s ; Calculate length of consecutive 0 s ; If a substring of 0 s exists ; Update minimum cost ; Printing the minimum cost ; Driver Code
#include <iostream> NEW_LINE using namespace std ; void convert_to_allzeroes ( string str , int a , int b ) { int len = str . length ( ) ; int left_1 , i = 0 ; while ( i < len && str [ i ] == '0' ) i ++ ; left_1 = i ; int right_1 ; i = len - 1 ; while ( i >= 0 && str [ i ] == '0' ) i -- ; right_1 = i ; if ( left_1 == len && right_1 == -1 ) { cout << 0 ; return ; } int cost = a , zeroes ; for ( i = left_1 ; i <= right_1 ; i ++ ) { zeroes = 0 ; while ( i < len && str [ i ] == '0' ) { zeroes ++ ; i ++ ; } if ( zeroes ) cost += min ( zeroes * b , a ) ; } cout << cost ; } int main ( ) { string str = "01101110" ; int A = 5 , B = 1 ; convert_to_allzeroes ( str , A , B ) ; return 0 ; }
Minimum distance to visit given K points on X | C ++ program to implement the above approach ; Function to find the minimum distance travelled to visit K point ; Stores minimum distance travelled to visit K point ; Stores distance travelled to visit points ; Traverse the array arr [ ] ; If arr [ i ] and arr [ i + K - 1 ] are positive ; Update dist ; Update dist ; Update res ; Driver Code ; initial the array
#include <bits/stdc++.h> NEW_LINE using namespace std ; int MinDistK ( int arr [ ] , int N , int K ) { int res = INT_MAX ; int dist = 0 ; for ( int i = 0 ; i <= ( N - K ) ; i ++ ) { if ( arr [ i ] >= 0 && arr [ i + K - 1 ] >= 0 ) { dist = max ( arr [ i ] , arr [ i + K - 1 ] ) ; } else { dist = abs ( arr [ i ] ) + abs ( arr [ i + K - 1 ] ) + min ( abs ( arr [ i ] ) , abs ( arr [ i + K - 1 ] ) ) ; } res = min ( res , dist ) ; } return res ; } int main ( ) { int K = 3 ; int arr [ ] = { -30 , -10 , 10 , 20 , 50 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << MinDistK ( arr , N , K ) ; }
Maximize the minimum array element by M subarray increments of size S | C ++ program for the above approach ; Function to return index of minimum element in the array ; Initialize a [ 0 ] as minValue ; Traverse the array ; If a [ i ] < existing minValue ; Return the minimum index ; Function that maximize the minimum element of array after incrementing subarray of size S by 1 , M times ; Iterating through the array for M times ; Find minimum element index ; Increment the minimum value ; Storing the left index and right index ; Incrementing S - 1 minimum elements to the left and right of minValue ; Reached extreme left ; Reached extreme right ; Left value is minimum ; Right value is minimum ; Find the minValue in A [ ] after M operations ; Return the minimum value ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int min ( int a [ ] , int n ) { int minIndex = 0 , minValue = a [ 0 ] , i ; for ( i = 1 ; i < n ; i ++ ) { if ( a [ i ] < minValue ) { minValue = a [ i ] ; minIndex = i ; } } return minIndex ; } int maximizeMin ( int A [ ] , int N , int S , int M ) { int minIndex , left , right , i , j ; for ( i = 0 ; i < M ; i ++ ) { minIndex = min ( A , N ) ; A [ minIndex ] ++ ; left = minIndex - 1 ; right = minIndex + 1 ; for ( j = 0 ; j < S - 1 ; j ++ ) { if ( left == -1 ) A [ right ++ ] ++ ; else if ( right == N ) A [ left -- ] ++ ; else { if ( A [ left ] < A [ right ] ) A [ left -- ] ++ ; else A [ right ++ ] ++ ; } } } minIndex = min ( A , N ) ; return A [ minIndex ] ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int S = 2 , M = 3 ; cout << maximizeMin ( arr , N , S , M ) ; return 0 ; }
Sum of nodes in the path from root to N | C ++ program for the above approach ; Function to find sum of all nodes from root to N ; If N is equal to 1 ; If N is equal to 2 or 3 ; Stores the number of nodes at ( i + 1 ) - th level ; Stores the number of nodes ; Stores if the current level is even or odd ; If level is odd ; If level is even ; If level with node N is reached ; Push into vector ; Compute prefix sums of count of nodes in each level ; Stores the level in which node N s present ; Stores the required sum ; Add temp to the sum ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; typedef long long ll ; ll sumOfPathNodes ( ll N ) { if ( N == 1 ) { return 1 ; } else if ( N == 2 N == 3 ) { return N + 1 ; } vector < ll > arr ; arr . push_back ( 1 ) ; ll k = 1 ; bool flag = true ; while ( k < N ) { if ( flag == true ) { k *= 2 ; flag = false ; } else { k *= 4 ; flag = true ; } if ( k > N ) { break ; } arr . push_back ( k ) ; } ll len = arr . size ( ) ; vector < ll > prefix ( len ) ; prefix [ 0 ] = 1 ; for ( ll i = 1 ; i < len ; ++ i ) { prefix [ i ] = arr [ i ] + prefix [ i - 1 ] ; } vector < ll > :: iterator it = lower_bound ( prefix . begin ( ) , prefix . end ( ) , N ) ; ll ind = it - prefix . begin ( ) ; ll final_ans = 0 ; ll temp = N ; while ( ind > 1 ) { ll val = temp - prefix [ ind - 1 ] ; if ( ind % 2 != 0 ) { temp = prefix [ ind - 2 ] + ( val + 1 ) / 2 ; } else { temp = prefix [ ind - 2 ] + ( val + 3 ) / 4 ; } -- ind ; final_ans += temp ; } final_ans += ( N + 1 ) ; return final_ans ; } int main ( ) { ll N = 13 ; cout << sumOfPathNodes ( N ) << endl ; return 0 ; }
Check if given point lies in range of any of the given towers | C ++ program to implement the above approach ; Function to check if the point ( X , Y ) exists in the towers network - range or not ; Traverse the array arr [ ] ; Stores distance of the point ( X , Y ) from i - th tower ; If dist lies within the range of the i - th tower ; If the point ( X , Y ) does not lie in the range of any of the towers ; Driver Code ; If point ( X , Y ) lies in the range of any of the towers ; Otherwise
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkPointRange ( int arr [ ] [ 3 ] , int X , int Y , int N ) { for ( int i = 0 ; i < N ; i ++ ) { double dist = sqrt ( ( arr [ i ] [ 0 ] - X ) * ( arr [ i ] [ 0 ] - X ) + ( arr [ i ] [ 1 ] - Y ) * ( arr [ i ] [ 1 ] - Y ) ) ; if ( dist <= arr [ i ] [ 2 ] ) { return true ; } } return false ; } int main ( ) { int arr [ ] [ 3 ] = { { 1 , 1 , 3 } , { 10 , 10 , 3 } , { 15 , 15 , 15 } } ; int X = 5 , Y = 5 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; if ( checkPointRange ( arr , X , Y , N ) ) { cout << " True " ; } else { cout << " False " ; } }
Find the winner of the Game of removing odd or replacing even array elements | C ++ program for the above approach ; Function to evaluate the winner of the game ; Stores count of odd array elements ; Stores count of even array elements ; Traverse the array ; If array element is odd ; Otherwise ; If count of even is zero ; If count of odd is even ; If count of odd is odd ; If count of odd is odd and count of even is one ; Otherwise ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findWinner ( int arr [ ] , int N ) { int odd = 0 ; int even = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 1 ) { odd ++ ; } else { even ++ ; } } if ( even == 0 ) { if ( odd % 2 == 0 ) { cout << " Player ▁ 2" << endl ; } else if ( odd % 2 == 1 ) { cout << " Player ▁ 1" << endl ; } } else if ( even == 1 && odd % 2 == 1 ) { cout << " Player ▁ 1" << endl ; } else { cout << -1 << endl ; } } int main ( ) { int arr [ ] = { 3 , 1 , 9 , 7 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findWinner ( arr , N ) ; return 0 ; }