codeparrot/xlcost-text-to-code huggingface dataset:
Problem of 8 Neighbours of an element in a 2 | C ++ implementation of the approach ; Dimension of Array ; Count of 1 s ; Counting all neighbouring 1 s ; Comparing the number of neighbouring 1 s with given ranges ; Copying changes to the main matrix ; Driver code ; Function call to calculate the resultant matrix after ' K ' iterations . ; Printing Result
#include <iostream> NEW_LINE using namespace std ; #define N  4 NEW_LINE void predictMatrix ( int arr [ N ] [ N ] , int range1a , int range1b , int range0a , int range0b , int K , int b [ N ] [ N ] ) { int c = 0 ; while ( K -- ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { c = 0 ; if ( i > 0 && arr [ i - 1 ] [ j ] == 1 ) c ++ ; if ( j > 0 && arr [ i ] [ j - 1 ] == 1 ) c ++ ; if ( i > 0 && j > 0 && arr [ i - 1 ] [ j - 1 ] == 1 ) c ++ ; if ( i < N - 1 && arr [ i + 1 ] [ j ] == 1 ) c ++ ; if ( j < N - 1 && arr [ i ] [ j + 1 ] == 1 ) c ++ ; if ( i < N - 1 && j < N - 1 && arr [ i + 1 ] [ j + 1 ] == 1 ) c ++ ; if ( i < N - 1 && j > 0 && arr [ i + 1 ] [ j - 1 ] == 1 ) c ++ ; if ( i > 0 && j < N - 1 && arr [ i - 1 ] [ j + 1 ] == 1 ) c ++ ; if ( arr [ i ] [ j ] == 1 ) { if ( c >= range1a && c <= range1b ) b [ i ] [ j ] = 1 ; else b [ i ] [ j ] = 0 ; } if ( arr [ i ] [ j ] == 0 ) { if ( c >= range0a && c <= range0b ) b [ i ] [ j ] = 1 ; else b [ i ] [ j ] = 0 ; } } } for ( int k = 0 ; k < N ; k ++ ) for ( int m = 0 ; m < N ; m ++ ) arr [ k ] [ m ] = b [ k ] [ m ] ; } } int main ( ) { int arr [ N ] [ N ] = { 0 , 0 , 0 , 0 , 0 , 1 , 1 , 0 , 0 , 0 , 1 , 0 , 0 , 1 , 0 , 1 } ; int range1a = 2 , range1b = 2 ; int range0a = 2 , range0b = 3 ; int K = 3 , b [ N ] [ N ] = { 0 } ; predictMatrix ( arr , range1a , range1b , range0a , range0b , K , b ) ; for ( int i = 0 ; i < N ; i ++ ) { cout << endl ; for ( int j = 0 ; j < N ; j ++ ) cout << b [ i ] [ j ] << " ▁ " ; } return 0 ; }
Number of moves required to guess a permutation . | C ++ implementation of the approach ; Function that returns the required moves ; Final move ; Driver Program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countMoves ( int n ) { int ct = 0 ; for ( int i = 1 ; i <= n ; i ++ ) ct += i * ( n - i ) ; ct += n ; return ct ; } int main ( ) { int n = 3 ; cout << countMoves ( n ) ; return 0 ; }
Count number of triplets ( a , b , c ) such that a ^ 2 + b ^ 2 = c ^ 2 and 1 <= a <= b <= c <= n | C ++ implementation of the approach ; Function to return an Array containing all the perfect squares upto n ; While current perfect square is less than or equal to n ; Function to return the count of triplet ( a , b , c ) pairs such that a ^ 2 + b ^ 2 = c ^ 2 and 1 <= a <= b <= c <= n ; Vector of perfect squares upto n ^ 2 ; Since , a ^ 2 + b ^ 2 = c ^ 2 ; If c < a or bSquare is not a perfect square ; If triplet pair ( a , b , c ) satisfy the given condition ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > getPerfectSquares ( int n ) { vector < int > perfectSquares ; int current = 1 , i = 1 ; while ( current <= n ) { perfectSquares . push_back ( current ) ; current = pow ( ++ i , 2 ) ; } return perfectSquares ; } int countTriplets ( int n ) { vector < int > perfectSquares = getPerfectSquares ( pow ( n , 2 ) ) ; int count = 0 ; for ( int a = 1 ; a <= n ; a ++ ) { int aSquare = pow ( a , 2 ) ; for ( int i = 0 ; i < perfectSquares . size ( ) ; i ++ ) { int cSquare = perfectSquares [ i ] ; int bSquare = abs ( cSquare - aSquare ) ; int b = sqrt ( bSquare ) ; int c = sqrt ( cSquare ) ; if ( c < a || ( find ( perfectSquares . begin ( ) , perfectSquares . end ( ) , bSquare ) == perfectSquares . end ( ) ) ) continue ; if ( ( b >= a ) && ( b <= c ) && ( aSquare + bSquare == cSquare ) ) count ++ ; } } return count ; } int main ( ) { int n = 10 ; cout << countTriplets ( n ) ; return 0 ; }
Count Numbers with N digits which consists of even number of 0 ’ s | C ++ program to count numbers with N digits which consists of odd number of 0 's ; Function to count Numbers with N digits which consists of odd number of 0 's ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countNumbers ( int N ) { return ( pow ( 10 , N ) - 1 ) - ( pow ( 10 , N ) - pow ( 8 , N ) ) / 2 ; } int main ( ) { int n = 2 ; cout << countNumbers ( n ) << endl ; return 0 ; }
Find determinant of matrix generated by array rotation | C ++ program for finding determinant of generated matrix ; Function to calculate determinant ; Driver code
#include <bits/stdc++.h> NEW_LINE #define N  3 NEW_LINE using namespace std ; int calcDeterminant ( int arr [ ] ) { int determinant = 0 ; for ( int i = 0 ; i < N ; i ++ ) { determinant += pow ( arr [ i ] , 3 ) ; } determinant -= 3 * arr [ 0 ] * arr [ 1 ] * arr [ 2 ] ; return determinant ; } int main ( ) { int arr [ ] = { 4 , 5 , 3 } ; cout << calcDeterminant ( arr ) ; return 0 ; }
Minimum elements to be added in a range so that count of elements is divisible by K | C ++ implementation of the approach ; Total elements in the range ; If total elements are already divisible by k ; Value that must be added to count in order to make it divisible by k ; Driver Program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumMoves ( int k , int l , int r ) { int count = r - l + 1 ; if ( count % k == 0 ) return 0 ; return ( k - ( count % k ) ) ; } int main ( ) { int k = 3 , l = 10 , r = 10 ; cout << minimumMoves ( k , l , r ) ; return 0 ; }
Sum of all even numbers in range L and R | C ++ program to print the sum of all even numbers in range L and R ; Function to return the sum of all natural numbers ; Function to return sum of even numbers in range L and R ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumNatural ( int n ) { int sum = ( n * ( n + 1 ) ) ; return sum ; } int sumEven ( int l , int r ) { return sumNatural ( r / 2 ) - sumNatural ( ( l - 1 ) / 2 ) ; } int main ( ) { int l = 2 , r = 5 ; cout << " Sum ▁ of ▁ Natural ▁ numbers ▁ from ▁ L ▁ to ▁ R ▁ is ▁ " << sumEven ( l , r ) ; return 0 ; }
Check if N is divisible by a number which is composed of the digits from the set { A , B } | C ++ program to find if number N is divisible by a number that contains only a and b as it 's digits ; Function to check whether n is divisible by a number whose digits are either a or b ; base condition ; recursive call ; Check for all numbers beginning with ' a ' or ' b ' ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isDivisibleRec ( int x , int a , int b , int n ) { if ( x > n ) return false ; if ( n % x == 0 ) return true ; return ( isDivisibleRec ( x * 10 + a , a , b , n ) || isDivisibleRec ( x * 10 + b , a , b , n ) ) ; } bool isDivisible ( int a , int b , int n ) { return isDivisibleRec ( a , a , b , n ) || isDivisibleRec ( b , a , b , n ) ; } int main ( ) { int a = 3 , b = 5 , n = 53 ; if ( isDivisible ( a , b , n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Minimum number of moves required to reach the destination by the king in a chess board | C ++ program to Find the minimum number of moves required to reach the destination by the king in a chess board ; function to Find the minimum number of moves required to reach the destination by the king in a chess board ; minimum number of steps ; while the king is not in the same row or column as the destination ; Go up ; Go down ; Go left ; Go right ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void MinSteps ( int SourceX , int SourceY , int DestX , int DestY ) { cout << max ( abs ( SourceX - DestX ) , abs ( SourceY - DestY ) ) << endl ; while ( ( SourceX != DestX ) || ( SourceY != DestY ) ) { if ( SourceX < DestX ) { cout << ' U ' ; SourceX ++ ; } if ( SourceX > DestX ) { cout << ' D ' ; SourceX -- ; } if ( SourceY > DestY ) { cout << ' L ' ; SourceY -- ; } if ( SourceY < DestY ) { cout << ' R ' ; SourceY ++ ; } cout << endl ; } } int main ( ) { int sourceX = 4 , sourceY = 4 ; int destinationX = 7 , destinationY = 0 ; MinSteps ( sourceX , sourceY , destinationX , destinationY ) ; return 0 ; }
Count of pairs in an array whose sum is a perfect square | CPP implementation of the approach ; Function to return an ArrayList containing all the perfect squares upto n ; while current perfect square is less than or equal to n ; Function to print the sum of maximum two elements from the array ; Function to return the count of numbers that when added with n give a perfect square ; temp > n is checked so that pairs ( x , y ) and ( y , x ) don 't get counted twice  ; Function to count the pairs whose sum is a perfect square ; Sum of the maximum two elements from the array ; List of perfect squares upto max ; Contains all the array elements ; Add count of the elements that when added with arr [ i ] give a perfect square ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > getPerfectSquares ( int n ) { vector < int > perfectSquares ; int current = 1 , i = 1 ; while ( current <= n ) { perfectSquares . push_back ( current ) ; current = static_cast < int > ( pow ( ++ i , 2 ) ) ; } return perfectSquares ; } int maxPairSum ( vector < int > & arr ) { int n = arr . size ( ) ; int max , secondMax ; if ( arr [ 0 ] > arr [ 1 ] ) { max = arr [ 0 ] ; secondMax = arr [ 1 ] ; } else { max = arr [ 1 ] ; secondMax = arr [ 0 ] ; } for ( int i = 2 ; i < n ; i ++ ) { if ( arr [ i ] > max ) { secondMax = max ; max = arr [ i ] ; } else if ( arr [ i ] > secondMax ) { secondMax = arr [ i ] ; } } return ( max + secondMax ) ; } int countPairsWith ( int n , vector < int > & perfectSquares , unordered_set < int > & nums ) { int count = 0 ; for ( int i = 0 ; i < perfectSquares . size ( ) ; i ++ ) { int temp = perfectSquares [ i ] - n ; if ( temp > n && find ( nums . begin ( ) , nums . end ( ) , temp ) != nums . end ( ) ) { count ++ ; } } return count ; } int countPairs ( vector < int > & arr ) { int i , n = arr . size ( ) ; int max = maxPairSum ( arr ) ; vector < int > perfectSquares = getPerfectSquares ( max ) ; unordered_set < int > nums ; for ( i = 0 ; i < n ; i ++ ) { nums . insert ( arr [ i ] ) ; } int count = 0 ; for ( i = 0 ; i < n ; i ++ ) { count += countPairsWith ( arr [ i ] , perfectSquares , nums ) ; } return count ; } int main ( ) { vector < int > arr = { 2 , 3 , 6 , 9 , 10 , 20 } ; cout << countPairs ( arr ) << endl ; return 0 ; }
Element equal to the sum of all the remaining elements | C ++ implementation of the above approach ; Function to find the element ; sum is use to store sum of all elements of array ; iterate over all elements ; Driver code
#include <bits/stdc++.h> NEW_LINE #define ll  long long int NEW_LINE using namespace std ; int findEle ( int arr [ ] , int n ) { ll sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] == sum - arr [ i ] ) return arr [ i ] ; return -1 ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findEle ( arr , n ) ; return 0 ; }
Sum of all natural numbers in range L to R | C ++ program to print the sum of all numbers in range L and R ; Function to return the sum of all natural numbers ; Function to return the sum of all numbers in range L and R ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumNatural ( int n ) { int sum = ( n * ( n + 1 ) ) / 2 ; return sum ; } int suminRange ( int l , int r ) { return sumNatural ( r ) - sumNatural ( l - 1 ) ; } int main ( ) { int l = 2 , r = 5 ; cout << " Sum ▁ of ▁ Natural ▁ numbers ▁ from ▁ L ▁ to ▁ R ▁ is ▁ " << suminRange ( l , r ) ; return 0 ; }
Check if a large number is divisible by 75 or not | C ++ implementation to check the number is divisible by 75 or not ; check divisibleBy3 ; to store sum of Digit ; traversing through each digit ; summing up Digit ; check if sumOfDigit is divisibleBy3 ; check divisibleBy25 ; if a single digit number ; length of the number ; taking the last two digit ; checking if the lastTwo digit is divisibleBy25 ; Function to check divisibleBy75 or not ; check if divisibleBy3 and divisibleBy25 ; Drivers code ; divisible ; if divisibleBy75
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool divisibleBy3 ( string number ) { int sumOfDigit = 0 ; for ( int i = 0 ; i < number . length ( ) ; i ++ ) sumOfDigit += number [ i ] - '0' ; if ( sumOfDigit % 3 == 0 ) return true ; return false ; } bool divisibleBy25 ( string number ) { if ( number . length ( ) < 2 ) return false ; int length = number . length ( ) ; int lastTwo = ( number [ length - 2 ] - '0' ) * 10 + ( number [ length - 1 ] - '0' ) ; if ( lastTwo % 25 == 0 ) return true ; return false ; } bool divisibleBy75 ( string number ) { if ( divisibleBy3 ( number ) && divisibleBy25 ( number ) ) return true ; return false ; } int main ( ) { string number = "754586672150" ; bool divisible = divisibleBy75 ( number ) ; if ( divisible ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Find the other number when LCM and HCF given | CPP program to find other number from given HCF and LCM ; Function that will calculates the zeroes at the end ; Driver code ; Calling function .
#include <bits/stdc++.h> NEW_LINE using namespace std ; int otherNumber ( int A , int Lcm , int Hcf ) { return ( Lcm * Hcf ) / A ; } int main ( ) { int A = 8 , Lcm = 8 , Hcf = 1 ; int result = otherNumber ( A , Lcm , Hcf ) ; cout << " B ▁ = ▁ " << result ; return 0 ; }
Overall percentage change from successive changes | C ++ implementation of above approach ; Calculate successive change of 1 st 2 change ; Calculate successive change for rest of the value ; Driver code ; Calling function
#include <bits/stdc++.h> NEW_LINE using namespace std ; float successiveChange ( int arr [ ] , int N ) { float var1 , var2 , result = 0 ; var1 = arr [ 0 ] ; var2 = arr [ 1 ] ; result = var1 + var2 + ( float ( var1 * var2 ) / 100 ) ; for ( int i = 2 ; i < N ; i ++ ) result = result + arr [ i ] + ( float ( result * arr [ i ] ) / 100 ) ; return result ; } int main ( ) { int arr [ ] = { 10 , 20 , 30 , 10 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; float result = successiveChange ( arr , N ) ; cout << " Percentage ▁ change ▁ is ▁ = ▁ " << result << " ▁ % " ; return 0 ; }
Minimum numbers ( smaller than or equal to N ) with sum S | C ++ program to find the minimum numbers required to get to S ; Function to find the minimum numbers required to get to S ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumNumbers ( int n , int s ) { if ( s % n ) return s / n + 1 ; else return s / n ; } int main ( ) { int n = 5 ; int s = 11 ; cout << minimumNumbers ( n , s ) ; return 0 ; }
Sum of multiples of A and B less than N | CPP program to find the sum of all multiples of A and B below N ; Function to find sum of AP series ; Number of terms ; Function to find the sum of all multiples of A and B below N ; Since , we need the sum of multiples less than N ; common factors of A and B ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long int NEW_LINE ll sumAP ( ll n , ll d ) { n /= d ; return ( n ) * ( 1 + n ) * d / 2 ; } ll sumMultiples ( ll A , ll B , ll n ) { n -- ; ll common = ( A * B ) / __gcd ( A , B ) ; return sumAP ( n , A ) + sumAP ( n , B ) - sumAP ( n , common ) ; } int main ( ) { ll n = 100 , A = 5 , B = 10 ; cout << " Sum ▁ = ▁ " << sumMultiples ( A , B , n ) ; return 0 ; }
Check if a prime number can be expressed as sum of two Prime Numbers | C ++ program to check if a prime number can be expressed as sum of two Prime Numbers ; Function to check whether a number is prime or not ; Function to check if a prime number can be expressed as sum of two Prime Numbers ; if the number is prime , and number - 2 is also prime ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPrime ( int n ) { if ( n <= 1 ) return false ; for ( int i = 2 ; i <= sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) return false ; } return true ; } bool isPossible ( int N ) { if ( isPrime ( N ) && isPrime ( N - 2 ) ) return true ; else return false ; } int main ( ) { int n = 13 ; if ( isPossible ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Count pairs from two arrays whose modulo operation yields K | C ++ implementation of above approach ; Function to return the total pairs of elements whose modulo yield K ; set is used to avoid duplicate pairs ; check which element is greater and proceed according to it ; check if modulo is equal to K ; return size of the set ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int totalPairs ( int arr1 [ ] , int arr2 [ ] , int K , int n , int m ) { set < pair < int , int > > s ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( arr1 [ i ] > arr2 [ j ] ) { if ( arr1 [ i ] % arr2 [ j ] == K ) s . insert ( make_pair ( arr1 [ i ] , arr2 [ j ] ) ) ; } else { if ( arr2 [ j ] % arr1 [ i ] == K ) s . insert ( make_pair ( arr2 [ j ] , arr1 [ i ] ) ) ; } } } return s . size ( ) ; } int main ( ) { int arr1 [ ] = { 8 , 3 , 7 , 50 } ; int arr2 [ ] = { 5 , 1 , 10 , 4 } ; int K = 3 ; int n = sizeof ( arr1 ) / sizeof ( arr1 [ 0 ] ) ; int m = sizeof ( arr2 ) / sizeof ( arr2 [ 0 ] ) ; cout << totalPairs ( arr1 , arr2 , K , n , m ) ; return 0 ; }
Largest sub | C ++ program to find the length of the largest sub - array of an array every element of whose is a perfect square ; function to return the length of the largest sub - array of an array every element of whose is a perfect square ; if both a and b are equal then arr [ i ] is a perfect square ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int contiguousPerfectSquare ( int arr [ ] , int n ) { int a ; float b ; int current_length = 0 ; int max_length = 0 ; for ( int i = 0 ; i < n ; i ++ ) { b = sqrt ( arr [ i ] ) ; a = b ; if ( a == b ) current_length ++ ; else current_length = 0 ; max_length = max ( max_length , current_length ) ; } return max_length ; } int main ( ) { int arr [ ] = { 9 , 75 , 4 , 64 , 121 , 25 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << contiguousPerfectSquare ( arr , n ) ; return 0 ; }
Count pairs of numbers from 1 to N with Product divisible by their Sum | C ++ program to count pairs of numbers from 1 to N with Product divisible by their Sum ; Function to count pairs ; variable to store count ; Generate all possible pairs such that 1 <= x < y < n ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countPairs ( int n ) { int count = 0 ; for ( int x = 1 ; x < n ; x ++ ) { for ( int y = x + 1 ; y <= n ; y ++ ) { if ( ( y * x ) % ( y + x ) == 0 ) count ++ ; } } return count ; } int main ( ) { int n = 15 ; cout << countPairs ( n ) ; return 0 ; }
Find the index of the left pointer after possible moves in the array | C ++ program to find the index of the left pointer ; Function that returns the index of the left pointer ; there 's only one element in the array ; initially both are at end ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getIndex ( int a [ ] , int n ) { if ( n == 1 ) return 0 ; int ptrL = 0 , ptrR = n - 1 , sumL = a [ 0 ] , sumR = a [ n - 1 ] ; while ( ptrR - ptrL > 1 ) { if ( sumL < sumR ) { ptrL ++ ; sumL += a [ ptrL ] ; } else if ( sumL > sumR ) { ptrR -- ; sumR += a [ ptrR ] ; } else { break ; } } return ptrL ; } int main ( ) { int a [ ] = { 2 , 7 , 9 , 8 , 7 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << getIndex ( a , n ) ; return 0 ; }
Find the position of the last removed element from the array | C ++ program to find the position of the last removed element from the array ; Function to find the original position of the element which will be removed last ; take ceil of every number ; Since position is index + 1 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getPosition ( int a [ ] , int n , int m ) { for ( int i = 0 ; i < n ; i ++ ) { a [ i ] = ( a [ i ] / m + ( a [ i ] % m != 0 ) ) ; } int ans = -1 , max = -1 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( max < a [ i ] ) { max = a [ i ] ; ans = i ; } } return ans + 1 ; } int main ( ) { int a [ ] = { 2 , 5 , 4 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int m = 2 ; cout << getPosition ( a , n , m ) ; return 0 ; }
Find the value of f ( n ) / f ( r ) * f ( n | CPP to find the value of f ( n ) / f ( r ) * f ( n - r ) ; Function to find value of given F ( n ) ; iterate over n ; calculate result ; return the result ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int calcFunction ( int n , int r ) { int finalDenominator = 1 ; int mx = max ( r , n - r ) ; for ( int i = mx + 1 ; i <= n ; i ++ ) { int denominator = ( int ) pow ( i , i ) ; int numerator = ( int ) pow ( i - mx , i - mx ) ; finalDenominator = ( finalDenominator * denominator ) / numerator ; } return finalDenominator ; } int main ( ) { int n = 6 , r = 2 ; cout << "1 / " << calcFunction ( n , r ) << endl ; return 0 ; }
Check if the array has an element which is equal to sum of all the remaining elements | C ++ program to Check if the array has an element which is equal to sum of all the remaining elements ; Function to check if such element exists or not ; Storing frequency in map ; Stores the sum ; Traverse the array and count the array elements ; Only possible if sum is even ; If half sum is available ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isExists ( int a [ ] , int n ) { unordered_map < int , int > freq ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { freq [ a [ i ] ] ++ ; sum += a [ i ] ; } if ( sum % 2 == 0 ) { if ( freq [ sum / 2 ] ) return true ; } return false ; } int main ( ) { int a [ ] = { 5 , 1 , 2 , 2 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; if ( isExists ( a , n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Print all numbers less than N with at | C ++ program to print all the numbers less than N which have at most 2 unique digits ; Function to generate all possible numbers ; If the number is less than n ; If the number exceeds ; Check if it is not the same number ; Function to print all numbers ; All combination of digits ; Print all numbers ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; set < int > st ; void generateNumbers ( int n , int num , int a , int b ) { if ( num > 0 && num < n ) st . insert ( num ) ; if ( num >= n ) return ; if ( num * 10 + a > num ) generateNumbers ( n , num * 10 + a , a , b ) ; generateNumbers ( n , num * 10 + b , a , b ) ; } void printNumbers ( int n ) { for ( int i = 0 ; i <= 9 ; i ++ ) for ( int j = i + 1 ; j <= 9 ; j ++ ) generateNumbers ( n , 0 , i , j ) ; cout << " The ▁ numbers ▁ are : ▁ " ; while ( ! st . empty ( ) ) { cout << * st . begin ( ) << " ▁ " ; st . erase ( st . begin ( ) ) ; } } int main ( ) { int n = 12 ; printNumbers ( n ) ; return 0 ; }
Smallest prime number missing in an array | C ++ implementation of the above approach ; this store all prime number upto 10 ^ 5 this function find all prime ; use sieve to find prime ; if number is prime then store it in prime vector ; Function to find the smallest prime missing ; first of all find all prime ; now store all number as index of freq arr so that we can improve searching ; now check for each prime ; Driver code ; find smallest prime which is not present
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long int NEW_LINE vector < ll > findPrime ( int MAX ) { bool pm [ MAX + 1 ] ; memset ( pm , true , sizeof ( pm ) ) ; pm [ 0 ] = pm [ 1 ] = false ; for ( int i = 2 ; i <= MAX ; i ++ ) if ( pm [ i ] ) for ( int j = 2 * i ; j <= MAX ; j += i ) pm [ j ] = false ; vector < ll > prime ; for ( int i = 0 ; i <= MAX ; i ++ ) if ( pm [ i ] ) prime . push_back ( i ) ; return prime ; } int findSmallest ( int arr [ ] , int n ) { int MAX = * max_element ( arr , arr + n ) ; vector < ll > prime = findPrime ( MAX ) ; unordered_set < int > s ; for ( int i = 0 ; i < n ; i ++ ) s . insert ( arr [ i ] ) ; int ans = -1 ; for ( int i = 0 ; i < prime . size ( ) ; i ++ ) if ( s . find ( prime [ i ] ) == s . end ( ) ) { ans = prime [ i ] ; break ; } return ans ; } int main ( ) { int arr [ ] = { 3 , 0 , 1 , 2 , 7 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; if ( findSmallest ( arr , n ) == -1 ) cout << " No ▁ prime ▁ number ▁ missing " ; else cout << findSmallest ( arr , n ) ; return 0 ; }