codeparrot/xlcost-text-to-code huggingface dataset:
Maximize boxes required to keep at least one black and one white shirt | C ++ program for the above approach ; Function to find the maximum number of boxes such that each box contains three shirts comprising of at least one white and black shirt ; Stores the low and high pointers for binary search ; Store the required answer ; Loop while low <= high ; Store the mid value ; Check if the mid number of boxes can be used ; Update answer and recur for the right half ; Else , recur for the left half ; Print the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void numberofBoxes ( int W , int B , int O ) { int low = 0 , high = min ( W , B ) ; int ans = 0 ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; if ( ( ( W >= mid ) and ( B >= mid ) ) and ( ( W - mid ) + ( B - mid ) + O ) >= mid ) { ans = mid ; low = mid + 1 ; } else high = mid - 1 ; } cout << ans ; } int main ( ) { int W = 3 , B = 3 , O = 1 ; numberofBoxes ( W , B , O ) ; return 0 ; }
Minimum swaps needed to convert given Binary Matrix A to Binary Matrix B | C ++ program for the above approach ; Function to count the minimum number of swaps required to convert matrix A to matrix B ; Stores number of cells such that matrix A contains 0 and matrix B contains 1 ; Stores number of cells such that matrix A contains 1 and matrix B contains 0 ; Iterate over the range [ 0 , N - 1 ] ; Iterate over the range [ 0 , M - 1 ] ; If A [ i ] [ j ] = 1 and B [ i ] [ j ] = 0 ; If A [ i ] [ j ] = 0 and B [ i ] [ j ] = 1 ; If count01 is equal to count10 ; Otherwise , ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minSwaps ( int N , int M , vector < vector < int > > & A , vector < vector < int > > & B ) { int count01 = 0 ; int count10 = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( A [ i ] [ j ] != B [ i ] [ j ] ) { if ( A [ i ] [ j ] == 1 ) count10 ++ ; else count01 ++ ; } } } if ( count01 == count10 ) return count01 ; else return -1 ; } int main ( ) { vector < vector < int > > A = { { 1 , 1 , 0 } , { 0 , 0 , 1 } , { 0 , 1 , 0 } } ; vector < vector < int > > B = { { 0 , 0 , 1 } , { 0 , 1 , 0 } , { 1 , 1 , 0 } } ; int N = A . size ( ) ; int M = B [ 0 ] . size ( ) ; cout << minSwaps ( N , M , A , B ) ; }
Maximize X such that sum of numbers in range [ 1 , X ] is at most K | C ++ program for the above approach ; Function to count the elements with sum of the first that many natural numbers less than or equal to K ; If K equals to 0 ; Stores sum of first i natural numbers ; Stores the result ; Iterate over the range [ 1 , N ] ; Increment sum by i ; Is sum is less than or equal to K ; Otherwise , ; Return res ; Driver Code ; Input ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Count ( int N , int K ) { if ( K == 0 ) return 0 ; int sum = 0 ; int res = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { sum += i ; if ( sum <= K ) res ++ ; else break ; } return res ; } int main ( ) { int N = 6 , K = 14 ; cout << Count ( N , K ) ; return 0 ; }
Remove last occurrence of a word from a given sentence string | C ++ program for the above approach ; Function to remove last occurrence of W from S ; If M is greater than N ; Iterate while i is greater than or equal to 0 ; Stores if occurrence of W has been found or not ; Iterate over the range [ 0 , M ] ; If S [ j + 1 ] is not equal to W [ j ] ; Mark flag true and break ; If occurrence has been found ; Delete the substring over the range [ i , i + M ] ; Resize the string S ; Return S ; Driver Code ; Input ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; string removeLastOccurrence ( string S , string W , int N , int M ) { if ( M > N ) return S ; for ( int i = N - M ; i >= 0 ; i -- ) { int flag = 0 ; for ( int j = 0 ; j < M ; j ++ ) { if ( S [ j + i ] != W [ j ] ) { flag = 1 ; break ; } } if ( flag == 0 ) { for ( int j = i ; j < N - M ; j ++ ) S [ j ] = S [ j + M ] ; S . resize ( N - M ) ; break ; } } return S ; } int main ( ) { string S = " This ▁ is ▁ GeeksForGeeks " ; string W = " Geeks " ; int N = S . length ( ) ; int M = W . length ( ) ; cout << removeLastOccurrence ( S , W , N , M ) << endl ; return 0 ; }
Rearrange the Array to maximize the elements which is smaller than both its adjacent elements | C ++ program for the above approach ; Function to rearrange array such that count of element that are smaller than their adjacent elements is maximum ; Stores the rearranged array ; Stores the maximum count of elements ; Sort the given array ; Place the smallest ( N - 1 ) / 2 elements at odd indices ; Placing the rest of the elements at remaining indices ; If no element of the array has been placed here ; Print the resultant array ; Driver Code ; Input ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maximumIndices ( int arr [ ] , int N ) { int temp [ N ] = { 0 } ; int maxIndices = ( N - 1 ) / 2 ; sort ( arr , arr + N ) ; for ( int i = 0 ; i < maxIndices ; i ++ ) { temp [ 2 * i + 1 ] = arr [ i ] ; } int j = 0 ; for ( int i = maxIndices ; i < N ; ) { if ( temp [ j ] == 0 ) { temp [ j ] = arr [ i ] ; i ++ ; } j ++ ; } for ( int i = 0 ; i < N ; i ++ ) { cout << temp [ i ] << " ▁ " ; } } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; maximumIndices ( arr , N ) ; return 0 ; }
Check if given Strings can be made equal by inserting at most 1 String | C ++ program for the above approach ; Function to check whether two sentences can be made equal by inserting at most one sentence in one of them ; size of sentence S1 ; size of sentence S2 ; check if S1 and S2 are of equal sizes ; if both sentences are the same , return true ; Otherwise , return false ; Declare 2 deques X and Y ; insert ' ▁ ' at the end of both sentences so that the last word can be identified ; traverse the sentence S1 ; push temp in deque when a space comes in sentence i . e a word has been formed ; temp stores words of the sentence ; traverse the sentence S1 ; push temp in deque when a space comes in sentence i . e a word has been formed ; temp stores words of the sentence ; check for prefixes of both sentences ; pop the prefix from both deques till they are equal ; check for suffixes of both sentences ; pop the suffix from both deques till they are equal ; if any of the deques is empty return true ; if both the deques are not empty return false ; Driver code ; Input ; function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool areSimilar ( string S1 , string S2 ) { int N = S1 . size ( ) ; int M = S2 . size ( ) ; if ( N == M ) { if ( S1 == S2 ) return true ; return false ; } deque < string > X , Y ; S1 . push_back ( ' ▁ ' ) ; S2 . push_back ( ' ▁ ' ) ; string temp = " " ; for ( int i = 0 ; i < N + 1 ; i ++ ) { if ( S1 [ i ] == ' ▁ ' ) { X . push_back ( temp ) ; temp = " " ; } else { temp += S1 [ i ] ; } } for ( int i = 0 ; i < M + 1 ; i ++ ) { if ( S2 [ i ] == ' ▁ ' ) { Y . push_back ( temp ) ; temp = " " ; } else { temp += S2 [ i ] ; } } while ( X . size ( ) > 0 && Y . size ( ) > 0 && X . front ( ) == Y . front ( ) ) { X . pop_front ( ) ; Y . pop_front ( ) ; } while ( X . size ( ) > 0 && Y . size ( ) > 0 && X . back ( ) == Y . back ( ) ) { X . pop_back ( ) ; Y . pop_back ( ) ; } if ( X . size ( ) == 0 || Y . size ( ) == 0 ) return true ; return false ; } int main ( ) { string S1 = " Start ▁ practicing ▁ on ▁ GeeksforGeeks " ; string S2 = " Start ▁ GeeksforGeeks " ; if ( areSimilar ( S1 , S2 ) ) cout << " True " << endl ; else cout << " False " << endl ; return 0 ; }
Find the repeating element in an Array of size N consisting of first M natural numbers | C ++ program for the above approach ; Function to calculate the repeating character in a given permutation ; variables to store maximum element and sum of the array respectively . ; calculate sum of array ; calculate maximum element in the array ; calculating sum of permutation ; calculate required answer ; Driver code ; Input ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int repeatingElement ( int arr [ ] , int N ) { int M = 0 , sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; M = max ( M , arr [ i ] ) ; } int sum1 = M * ( M + 1 ) / 2 ; int ans = ( sum - sum1 ) / ( N - M ) ; return ans ; } int main ( ) { int arr [ ] = { 2 , 6 , 4 , 3 , 1 , 5 , 2 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << repeatingElement ( arr , N ) << endl ; return 0 ; }
Maximize value at Kth index to create N size array with adjacent difference 1 and sum less than M | C ++ program for the above approach ; Function to calculate maximum value that can be placed at the Kth index in a distribution in which difference of adjacent elements is less than 1 and total sum of distribution is M . ; variable to store final answer ; variables for binary search ; Binary search ; variable for binary search ; variable to store total sum of array ; number of indices on the left excluding the Kth index ; number of indices on the left excluding the Kth index ; add mid to final sum ; distribution on left side is possible ; sum of distribution on the left side ; sum of distribution on the left side with ( L - mid ) 1 s ; distribution on right side is possible ; sum of distribution on the right side ; sum of distribution on the left side with ( R - mid ) 1 s ; Distribution is valid ; return answer ; Driver code ; Input ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int calculateMax ( int N , int M , int K ) { int ans = -1 ; int low = 0 , high = M ; while ( low <= high ) { int mid = ( low + high ) / 2 ; int val = 0 ; int L = K - 1 ; int R = N - K ; val += mid ; if ( mid >= L ) { val += ( L ) * ( 2 * mid - L - 1 ) / 2 ; } else { val += mid * ( mid - 1 ) / 2 + ( L - mid ) ; } if ( mid >= R ) { val += ( R ) * ( 2 * mid - R - 1 ) / 2 ; } else { val += mid * ( mid - 1 ) / 2 + ( R - mid ) ; } if ( val <= M ) { ans = max ( ans , mid ) ; low = mid + 1 ; } else high = mid - 1 ; } return ans ; } int main ( ) { int N = 7 , M = 100 , K = 6 ; cout << calculateMax ( N , M , K ) << endl ; return 0 ; }
Minimum time required to color all edges of a Tree | C ++ program for the above approach ; Stores the required answer ; Stores the graph ; Function to add edges ; Function to calculate the minimum time required to color all the edges of a tree ; Starting from time = 0 , for all the child edges ; If the edge is not visited yet . ; Time of coloring of the current edge ; If the parent edge has been colored at the same time ; Update the maximum time ; Recursively call the function to its child node ; Driver Code ; Function call ; Finally , print the answer
#include <bits/stdc++.h> NEW_LINE using namespace std ; int ans = 0 ; vector < int > edges [ 100000 ] ; void Add_edge ( int u , int v ) { edges [ u ] . push_back ( v ) ; edges [ v ] . push_back ( u ) ; } void minTimeToColor ( int node , int parent , int arrival_time ) { int current_time = 0 ; for ( auto x : edges [ node ] ) { if ( x != parent ) { ++ current_time ; if ( current_time == arrival_time ) ++ current_time ; ans = max ( ans , current_time ) ; minTimeToColor ( x , node , current_time ) ; } } } int main ( ) { pair < int , int > A [ ] = { { 1 , 2 } , { 2 , 3 } , { 3 , 4 } } ; for ( auto i : A ) { Add_edge ( i . first , i . second ) ; } minTimeToColor ( 1 , -1 , 0 ) ; cout << ans << " STRNEWLINE " ; }
Largest number having both positive and negative values present in the array | C ++ program for the above approach ; Function to find the largest number k such that both k and - k are present in the array ; Stores the resultant value of K ; Sort the array arr [ ] ; Initialize two variables to use two pointers technique ; Iterate until the value of l is less than r ; Find the value of the sum ; If the sum is 0 , then the resultant element is found ; If the sum is negative ; Otherwise , decrement r ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int largestNum ( vector < int > arr ) { int res = 0 ; sort ( arr . begin ( ) , arr . end ( ) ) ; int l = 0 , r = arr . size ( ) - 1 ; while ( l < r ) { int sum = arr [ l ] + arr [ r ] ; if ( sum == 0 ) { res = max ( res , max ( arr [ l ] , arr [ r ] ) ) ; return res ; } else if ( sum < 0 ) { l ++ ; } else { r -- ; } } return res ; } int main ( ) { vector < int > arr = { 3 , 2 , -2 , 5 , -3 } ; cout << ( largestNum ( arr ) ) ; }
Find elements larger than half of the elements in an array | Set 2 | C ++ program for the above approach ; Function to find the element that are larger than half of elements of the array ; Find the value of mid ; Stores the maximum element ; Stores the frequency of each array element ; Traverse the array in the reverse order ; Decrement the value of count [ i ] and mid ; Print the current element ; Check if the value of mid is equal to 0 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findLarger ( int arr [ ] , int n ) { int mid = ( n + 1 ) / 2 ; int mx = * max_element ( arr , arr + n ) ; int count [ mx + 1 ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) { count [ arr [ i ] ] ++ ; } for ( int i = mx ; i >= 0 ; i -- ) { while ( count [ i ] > 0 ) { count [ i ] -- ; mid -- ; cout << i << ' ▁ ' ; if ( mid == 0 ) break ; } if ( mid == 0 ) break ; } } int main ( ) { int arr [ ] = { 10 , 4 , 2 , 8 , 9 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findLarger ( arr , N ) ; return 0 ; }
Probability of obtaining pairs from two arrays such that element from the first array is smaller than that of the second array | C ++ program for the above approach ; Function to find probability such that x < y and X belongs to arr1 [ ] and Y belongs to arr2 [ ] ; Stores the length of arr1 ; Stores the length of arr2 ; Stores the result ; Traverse the arr1 [ ] ; Stores the count of elements in arr2 that are greater than arr [ i ] ; Traverse the arr2 [ ] ; If arr2 [ j ] is greater than arr1 [ i ] ; Increment res by y ; Update the value of res ; Return resultant probability ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; double probability ( vector < int > arr1 , vector < int > arr2 ) { int N = arr1 . size ( ) ; int M = arr2 . size ( ) ; double res = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int y = 0 ; for ( int j = 0 ; j < M ; j ++ ) { if ( arr2 [ j ] > arr1 [ i ] ) y ++ ; } res += y ; } res = ( double ) res / ( double ) ( N * M ) ; return res ; } int main ( ) { vector < int > arr1 = { 5 , 2 , 6 , 1 } ; vector < int > arr2 = { 1 , 6 , 10 , 1 } ; cout << probability ( arr1 , arr2 ) ; }
Probability of obtaining pairs from two arrays such that element from the first array is smaller than that of the second array | C ++ program for the above approach ; Function to find probability such that x < y and X belongs to arr1 [ ] & Y belongs to arr2 [ ] ; Stores the length of arr1 ; Stores the length of arr2 ; Stores the result ; Sort the arr2 [ ] in the ascending order ; Traverse the arr1 [ ] ; Stores the count of elements in arr2 that are greater than arr [ i ] ; Increment res by y ; Update the resultant probability ; Return the result ; Function to return the count of elements from the array which are greater than k ; Stores the index of the leftmost element from the array which is at least k ; Finds number of elements greater than k ; If mid element is at least K , then update the value of leftGreater and r ; Update leftGreater ; Update r ; If mid element is at most K , then update the value of l ; Return the count of elements greater than k ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countGreater ( int * arr , int k ) ; float probability ( int * arr1 , int * arr2 ) { int N = 4 ; int M = 4 ; float res = 0 ; sort ( arr2 , arr2 + M ) ; for ( int i = 0 ; i < N ; i ++ ) { int y = countGreater ( arr2 , arr1 [ i ] ) ; res += y ; } res = res / ( N * M ) ; return res ; } int countGreater ( int * arr , int k ) { int n = 4 ; int l = 0 ; int r = n - 1 ; int leftGreater = n ; while ( l <= r ) { int m = l + ( r - l ) / 2 ; if ( arr [ m ] > k ) { leftGreater = m ; r = m - 1 ; } else l = m + 1 ; } return ( n - leftGreater ) ; } int main ( ) { int arr1 [ ] = { 5 , 2 , 6 , 1 } ; int arr2 [ ] = { 1 , 6 , 10 , 1 } ; cout << probability ( arr1 , arr2 ) ; return 0 ; }
Minimize cost to cover floor using tiles of dimensions 1 * 1 and 1 * 2 | C ++ program for the above approach ; Function to find the minimum cost of flooring with the given tiles ; Store the size of the 2d array ; Stores the minimum cost of flooring ; Traverse the 2d array row - wise ; If the current character is ' * ' , then skip it ; Choose the 1 * 1 tile if j is m - 1 ; If consecutive ' . ' are present , the greedily choose tile with the minimum cost ; Otherwise choose the 1 * 1 tile ; Print the minimum cost ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minCost ( vector < vector < char > > arr , int A , int B ) { int n = arr . size ( ) ; int m = arr [ 0 ] . size ( ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( arr [ i ] [ j ] == ' * ' ) continue ; if ( j == m - 1 ) ans += A ; else { if ( arr [ i ] [ j + 1 ] == ' . ' ) { ans += min ( 2 * A , B ) ; j ++ ; } else ans += A ; } } } cout << ans ; } int main ( ) { vector < vector < char > > arr = { { ' . ' , ' . ' , ' * ' } , { ' . ' , ' * ' , ' * ' } } ; int A = 2 , B = 10 ; minCost ( arr , A , B ) ; return 0 ; }
Count inversions in a permutation of first N natural numbers | C ++ program for the above approach ; Function to count number of inversions in a permutation of first N natural numbers ; Store array elements in sorted order ; Store the count of inversions ; Traverse the array ; Store the index of first occurrence of arr [ i ] in vector V ; Add count of smaller elements than current element ; Erase current element from vector and go to next index ; Print the result ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countInversions ( int arr [ ] , int n ) { vector < int > v ; for ( int i = 1 ; i <= n ; i ++ ) { v . push_back ( i ) ; } int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { auto itr = lower_bound ( v . begin ( ) , v . end ( ) , arr [ i ] ) ; ans += itr - v . begin ( ) ; v . erase ( itr ) ; } cout << ans ; return 0 ; } int main ( ) { int arr [ ] = { 2 , 3 , 1 , 5 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; countInversions ( arr , n ) ; return 0 ; }
Find all possible pairs with given Bitwise OR and Bitwise XOR values | C ++ code for the above approach ; Function to find pairs with XOR equal to A and OR equal to B ; Iterate from 1 to B ; Check if ( i OR y ) is B ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findPairs ( int A , int B ) { for ( int i = 1 ; i <= B ; i ++ ) { int y = A ^ i ; if ( y > 0 and ( i y ) == B ) { cout << i << " ▁ " << y << endl ; } } } int main ( ) { int A = 8 , B = 10 ; findPairs ( A , B ) ; return 0 ; }
Count distinct elements from a range of a sorted sequence from a given frequency array | C ++ program for the above approach ; Function to find the first index with value is at least element ; Update the value of left ; Binary search for the element ; Find the middle element ; Check if the value lies between the elements at index mid - 1 and mid ; Check in the right subarray ; Update the value of left ; Check in left subarray ; Update the value of right ; Function to count the number of distinct elements over the range [ L , R ] in the sorted sequence ; Stores the count of distinct elements ; Create the prefix sum array ; Update the value of count ; Update the value of pref [ i ] ; Calculating the first index of L and R using binary search ; Print the resultant count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int binarysearch ( int array [ ] , int right , int element ) { int left = 1 ; while ( left <= right ) { int mid = ( left + right / 2 ) ; if ( array [ mid ] == element ) { return mid ; } if ( mid - 1 > 0 && array [ mid ] > element && array [ mid - 1 ] < element ) { return mid ; } else if ( array [ mid ] < element ) { left = mid + 1 ; } else { right = mid - 1 ; } } return 1 ; } void countDistinct ( vector < int > arr , int L , int R ) { int count = 0 ; int pref [ arr . size ( ) + 1 ] ; for ( int i = 1 ; i <= arr . size ( ) ; ++ i ) { count += arr [ i - 1 ] ; pref [ i ] = count ; } int left = binarysearch ( pref , arr . size ( ) + 1 , L ) ; int right = binarysearch ( pref , arr . size ( ) + 1 , R ) ; cout << right - left + 1 ; } int main ( ) { vector < int > arr { 3 , 6 , 7 , 1 , 8 } ; int L = 3 ; int R = 7 ; countDistinct ( arr , L , R ) ; }
Count substrings having frequency of a character exceeding that of another character in a string | C ++ program for the above approach ; Function to find the number of substrings having the frequency of ' a ' greater than frequency of ' c ' ; Stores the size of the string ; Stores the resultant count of substrings ; Traverse the given string ; Store the difference between frequency of ' a ' and ' c ' ; Traverse all substrings beginning at index i ; If the frequency of ' a ' is greater than ' c ' ; Print the answer ; Drive Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countSubstrings ( string & s ) { int n = s . length ( ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int cnt = 0 ; for ( int j = i ; j < n ; j ++ ) { if ( s [ j ] == ' a ' ) cnt ++ ; else if ( s [ j ] == ' c ' ) cnt -- ; if ( cnt > 0 ) { ans ++ ; } } } cout << ans ; } int main ( ) { string S = " abccaab " ; countSubstrings ( S ) ; return 0 ; }
Find Unique ID and Domain Name of a Website from a string | C ++ program for the above approach ; Function to check if a character is alphabet or not ; Function to check if a character is a numeric or not ; Function to find ID and Domain name from a given string ; Stores ID and the domain names ; Stores the words of string S ; Stores the temporary word ; Traverse the string S ; If the current character is space ; Push the curr in words ; Update the curr ; Otherwise ; If curr is not empty ; If length of ss is 10 ; Traverse the string ss ; If j is in the range [ 5 , 9 ) ; If current character is not numeric ; Mark flag 1 ; Otherwise ; If current character is not alphabet ; Mark flag 1 ; If flag is false ; Assign ss to ID ; If substring formed by the first 3 character is " www " and last 3 character is " moc " ; Update the domain name ; Print ID and Domain ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool ischar ( char x ) { if ( ( x >= ' A ' && x <= ' Z ' ) || ( x >= ' a ' && x <= ' z ' ) ) { return 1 ; } return 0 ; } bool isnum ( char x ) { if ( x >= '0' && x <= '9' ) return 1 ; return 0 ; } void findIdandDomain ( string S , int N ) { string ID , Domain ; vector < string > words ; string curr = " " ; for ( int i = 0 ; i < N ; i ++ ) { if ( S [ i ] == ' ▁ ' ) { words . push_back ( curr ) ; curr = " " ; } else { if ( S [ i ] == ' . ' ) { if ( i + 1 == N || ( i + 1 < N && S [ i + 1 ] == ' ▁ ' ) ) continue ; } curr += S [ i ] ; } } if ( curr . length ( ) ) words . push_back ( curr ) ; for ( string ss : words ) { if ( ss . size ( ) == 10 ) { bool flag = 0 ; for ( int j = 0 ; j <= 9 ; j ++ ) { if ( j >= 5 && j < 9 ) { if ( isnum ( ss [ j ] ) == 0 ) flag = 1 ; } else { if ( ischar ( ss [ j ] ) == 0 ) flag = 1 ; } } if ( ! flag ) { ID = ss ; } } if ( ss . substr ( 0 , 3 ) == " www " && ss . substr ( ss . length ( ) - 3 , 3 ) == " com " ) { Domain = ss . substr ( 4 , ss . size ( ) - 4 ) ; } } cout << " ID ▁ = ▁ " << ID << endl ; cout << " Domain ▁ = ▁ " << Domain ; } int main ( ) { string S = " We ▁ thank ▁ ABCDE1234F ▁ for ▁ visiting ▁ " " us ▁ and ▁ buying ▁ " " products ▁ item ▁ AMZrr @ ! k . ▁ For ▁ more ▁ " " offers , ▁ visit ▁ " " us ▁ at ▁ www . amazon . com " ; int N = S . length ( ) ; findIdandDomain ( S , N ) ; return 0 ; }