codeparrot/xlcost-text-to-code huggingface dataset:
Closest perfect square and its distance | CPP program to find the closest perfect square taking minimum steps to reach from a number ; Function to check if a number is perfect square or not ; Function to find the closest perfect square taking minimum steps to reach from a number ; Variables to store first perfect square number above and below N ; Finding first perfect square number greater than N ; Finding first perfect square number less than N ; Variables to store the differences ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPerfect ( int N ) { if ( ( sqrt ( N ) - floor ( sqrt ( N ) ) ) != 0 ) return false ; return true ; } void getClosestPerfectSquare ( int N ) { if ( isPerfect ( N ) ) { cout << N << " ▁ " << "0" << endl ; return ; } int aboveN = -1 , belowN = -1 ; int n1 ; n1 = N + 1 ; while ( true ) { if ( isPerfect ( n1 ) ) { aboveN = n1 ; break ; } else n1 ++ ; } n1 = N - 1 ; while ( true ) { if ( isPerfect ( n1 ) ) { belowN = n1 ; break ; } else n1 -- ; } int diff1 = aboveN - N ; int diff2 = N - belowN ; if ( diff1 > diff2 ) cout << belowN << " ▁ " << diff2 ; else cout << aboveN << " ▁ " << diff1 ; } int main ( ) { int N = 1500 ; getClosestPerfectSquare ( N ) ; }
Fraction | C ++ program to add 2 fractions ; Function to return gcd of a and b ; Function to convert the obtained fraction into it 's simplest form ; Finding gcd of both terms ; Converting both terms into simpler terms by dividing them by common factor ; Function to add two fractions ; Finding gcd of den1 and den2 ; Denominator of final fraction obtained finding LCM of den1 and den2 LCM * GCD = a * b ; Changing the fractions to have same denominator Numerator of the final fraction obtained ; Calling function to convert final fraction into it 's simplest form ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } void lowest ( int & den3 , int & num3 ) { int common_factor = gcd ( num3 , den3 ) ; den3 = den3 / common_factor ; num3 = num3 / common_factor ; } void addFraction ( int num1 , int den1 , int num2 , int den2 , int & num3 , int & den3 ) { den3 = gcd ( den1 , den2 ) ; den3 = ( den1 * den2 ) / den3 ; num3 = ( num1 ) * ( den3 / den1 ) + ( num2 ) * ( den3 / den2 ) ; lowest ( den3 , num3 ) ; } int main ( ) { int num1 = 1 , den1 = 500 , num2 = 2 , den2 = 1500 , den3 , num3 ; addFraction ( num1 , den1 , num2 , den2 , num3 , den3 ) ; printf ( " % d / % d ▁ + ▁ % d / % d ▁ is ▁ equal ▁ to ▁ % d / % d STRNEWLINE " , num1 , den1 , num2 , den2 , num3 , den3 ) ; return 0 ; }
Largest Divisor of a Number not divisible by a perfect square | Efficient C ++ Program to find the largest divisor not divisible by any perfect square greater than 1 ; Function to find the largest divisor not divisible by any perfect square greater than 1 ; If the number is divisible by i * i , then remove one i ; Now all squares are removed from n ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findLargestDivisor ( int n ) { for ( int i = 2 ; i < sqrt ( n ) + 1 ; i ++ ) { while ( n % ( i * i ) == 0 ) { n = n / i ; } } return n ; } int main ( ) { int n = 12 ; cout << findLargestDivisor ( n ) << endl ; n = 97 ; cout << findLargestDivisor ( n ) << endl ; return 0 ; }
Arithmetic Progression | C ++ program to check if a given array can form arithmetic progression ; Returns true if a permutation of arr [ 0. . n - 1 ] can form arithmetic progression ; Sort array ; After sorting , difference between consecutive elements must be same . ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkIsAP ( int arr [ ] , int n ) { if ( n == 1 ) return true ; sort ( arr , arr + n ) ; int d = arr [ 1 ] - arr [ 0 ] ; for ( int i = 2 ; i < n ; i ++ ) if ( arr [ i ] - arr [ i - 1 ] != d ) return false ; return true ; } int main ( ) { int arr [ ] = { 20 , 15 , 5 , 0 , 10 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; ( checkIsAP ( arr , n ) ) ? ( cout << " Yes " << endl ) : ( cout << " No " << endl ) ; return 0 ; }
Check if a number is Triperfect Number | CPP code to check if a given number is Triperfect or not ; Returns true if n is Triperfect ; To store sum of divisors . Adding 1 and n since they are divisors of n . ; Find all divisors and add them ; If sum of divisors is equal to 3 * n , then n is a Triperfect number ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isTriPerfect ( int n ) { int sum = 1 + n ; int i = 2 ; while ( i * i <= n ) { if ( n % i == 0 ) { if ( n / i == i ) sum = sum + i ; else sum = sum + i + n / i ; } i += 1 ; } if ( sum == 3 * n and n != 1 ) return true ; else false ; } int main ( ) { int n = 120 ; if ( isTriPerfect ( n ) ) cout << n << " ▁ is ▁ a ▁ Triperfect ▁ number " ; }
Sum of first N natural numbers which are divisible by X or Y | C ++ program to find sum of numbers from 1 to N which are divisible by X or Y ; Function to calculate the sum of numbers divisible by X or Y ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sum ( int N , int X , int Y ) { int S1 , S2 , S3 ; S1 = ( ( N / X ) ) * ( 2 * X + ( N / X - 1 ) * X ) / 2 ; S2 = ( ( N / Y ) ) * ( 2 * Y + ( N / Y - 1 ) * Y ) / 2 ; S3 = ( ( N / ( X * Y ) ) ) * ( 2 * ( X * Y ) + ( N / ( X * Y ) - 1 ) * ( X * Y ) ) / 2 ; return S1 + S2 - S3 ; } int main ( ) { int N = 14 ; int X = 3 , Y = 5 ; cout << sum ( N , X , Y ) ; return 0 ; }
Count numbers from range whose prime factors are only 2 and 3 | C ++ program to count the numbers within a range whose prime factors are only 2 and 3 ; Function to count the number within a range whose prime factors are only 2 and 3 ; Start with 2 so that 1 doesn 't get counted ; While num is divisible by 2 , divide it by 2 ; While num is divisible by 3 , divide it by 3 ; If num got reduced to 1 then it has only 2 and 3 as prime factors ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findTwoThreePrime ( int l , int r ) { if ( l == 1 ) l ++ ; int count = 0 ; for ( int i = l ; i <= r ; i ++ ) { int num = i ; while ( num % 2 == 0 ) num /= 2 ; while ( num % 3 == 0 ) num /= 3 ; if ( num == 1 ) count ++ ; } return count ; } int main ( ) { int l = 1 , r = 10 ; cout << findTwoThreePrime ( l , r ) ; return 0 ; }
Maximum number with same digit factorial product | C ++ implementation of the approach ; Function to return the required number ; Count the frequency of each digit ; 4 ! can be expressed as 2 ! * 2 ! * 3 ! ; 6 ! can be expressed as 5 ! * 3 ! ; 8 ! can be expressed as 7 ! * 2 ! * 2 ! * 2 ! ; 9 ! can be expressed as 7 ! * 3 ! * 3 ! * 2 ! ; To store the required number ; If number has only either 1 and 0 as its digits ; Generate the greatest number possible ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string getNumber ( string s ) { int number_of_digits = s . length ( ) ; int freq [ 10 ] = { 0 } ; for ( int i = 0 ; i < number_of_digits ; i ++ ) { if ( s [ i ] == '1' s [ i ] == '2' s [ i ] == '3' s [ i ] == '5' s [ i ] == '7' ) { freq [ s [ i ] - 48 ] += 1 ; } if ( s [ i ] == '4' ) { freq [ 2 ] += 2 ; freq [ 3 ] ++ ; } if ( s [ i ] == '6' ) { freq [ 5 ] ++ ; freq [ 3 ] ++ ; } if ( s [ i ] == '8' ) { freq [ 7 ] ++ ; freq [ 2 ] += 3 ; } if ( s [ i ] == '9' ) { freq [ 7 ] ++ ; freq [ 3 ] += 2 ; freq [ 2 ] ++ ; } } string t = " " ; if ( freq [ 1 ] == number_of_digits || freq [ 0 ] == number_of_digits || ( freq [ 0 ] + freq [ 1 ] ) == number_of_digits ) { return s ; } else { for ( int i = 9 ; i >= 2 ; i -- ) { int ctr = freq [ i ] ; while ( ctr -- ) { t += ( char ) ( i + 48 ) ; } } return t ; } } int main ( ) { string s = "1280" ; cout << getNumber ( s ) ; return 0 ; }
Program to find first N Iccanobif Numbers | C ++ program to find first N Icanobif numbers ; Iterative function to reverse digits of num ; Function to print first N Icanobif Numbers ; Initialize first , second numbers ; Print first two numbers ; Reversing digit of previous two terms and adding them ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int reversDigits ( int num ) { int rev_num = 0 ; while ( num > 0 ) { rev_num = rev_num * 10 + num % 10 ; num = num / 10 ; } return rev_num ; } void icanobifNumbers ( int N ) { int first = 0 , second = 1 ; if ( N == 1 ) cout << first ; else if ( N == 2 ) cout << first << " ▁ " << second ; else { cout << first << " ▁ " << second << " ▁ " ; for ( int i = 3 ; i <= N ; i ++ ) { int x = reversDigits ( first ) ; int y = reversDigits ( second ) ; cout << x + y << " ▁ " ; int temp = second ; second = x + y ; first = temp ; } } } int main ( ) { int N = 12 ; icanobifNumbers ( N ) ; return 0 ; }
Add N digits to A such that it is divisible by B after each addition | C ++ implementation of the approach ; Try all digits from ( 0 to 9 ) ; Fails in the first move itself ; Add ( n - 1 ) 0 's ; Driver Program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int addNDigits ( int a , int b , int n ) { int num = a ; for ( int i = 0 ; i <= 9 ; i ++ ) { int tmp = a * 10 + i ; if ( tmp % b == 0 ) { a = tmp ; break ; } } if ( num == a ) return -1 ; for ( int j = 0 ; j < n - 1 ; j ++ ) a *= 10 ; return a ; } int main ( ) { int a = 5 , b = 3 , n = 3 ; cout << addNDigits ( a , b , 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 ++ program to Find number of Triplets 1 <= a <= b <= c <= n , Such that a ^ 2 + b ^ 2 = c ^ 2 ; function to ind number of Triplets 1 <= a <= b <= c <= n , Such that a ^ 2 + b ^ 2 = c ^ 2 ; to store required answer ; run nested loops for first two numbers . ; third number ; check if third number is perfect square and less than n ; Driver code ; function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Triplets ( int n ) { int ans = 0 ; for ( int i = 1 ; i <= n ; ++ i ) { for ( int j = i ; j <= n ; ++ j ) { int x = i * i + j * j ; int y = sqrt ( x ) ; if ( y * y == x && y <= n ) ++ ans ; } } return ans ; } int main ( ) { int n = 10 ; cout << Triplets ( n ) ; return 0 ; }
Sum of the digits of a number N written in all bases from 2 to N / 2 | C ++ implementation of the approach ; Function to calculate the sum of the digits of n in the given base ; Sum of digits ; Digit of n in the given base ; Add the digit ; Function to calculate the sum of digits of n in bases from 2 to n / 2 ; to store digit sum in all bases ; function call for multiple bases ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int solve ( int n , int base ) { int sum = 0 ; while ( n > 0 ) { int remainder = n % base ; sum += remainder ; n = n / base ; } return sum ; } void SumsOfDigits ( int n ) { int sum = 0 ; for ( int base = 2 ; base <= n / 2 ; ++ base ) sum += solve ( n , base ) ; cout << sum ; } int main ( ) { int n = 8 ; SumsOfDigits ( n ) ; return 0 ; }
Largest number in an array that is not a perfect cube | CPP program to find the largest non - perfect cube number among n numbers ; Function to check if a number is perfect cube number or not ; takes the sqrt of the number ; checks if it is a perfect cube number ; Function to find the largest non perfect cube number in the array ; stores the maximum of all perfect cube numbers ; Traverse all elements in the array ; store the maximum if current element is a non perfect cube ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkPerfectcube ( int n ) { int d = cbrt ( n ) ; if ( d * d * d == n ) return true ; return false ; } int largestNonPerfectcubeNumber ( int a [ ] , int n ) { int maxi = -1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! checkPerfectcube ( a [ i ] ) ) maxi = max ( a [ i ] , maxi ) ; } return maxi ; } int main ( ) { int a [ ] = { 16 , 64 , 25 , 2 , 3 , 10 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << largestNonPerfectcubeNumber ( a , n ) ; return 0 ; }
Check if N can be represented as sum of integers chosen from set { A , B } | CPP program to find if number N can be represented as sum of a ' s ▁ and ▁ b ' s ; Function to find if number N can be represented as sum of a ' s ▁ and ▁ b ' s ; base condition ; if x is already visited ; set x as possible ; recursive call ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void checkIfPossibleRec ( int x , int a , int b , bool isPossible [ ] , int n ) { if ( x > n ) return ; if ( isPossible [ x ] ) return ; isPossible [ x ] = true ; checkIfPossibleRec ( x + a , a , b , isPossible , n ) ; checkIfPossibleRec ( x + b , a , b , isPossible , n ) ; } bool checkPossible ( int n , int a , int b ) { bool isPossible [ n + 1 ] = { false } ; checkIfPossibleRec ( 0 , a , b , isPossible , n ) ; return isPossible [ n ] ; } int main ( ) { int a = 3 , b = 7 , n = 8 ; if ( checkPossible ( a , b , n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Sum of all odd natural numbers in range L and R | C ++ program to print the sum of all numbers in range L and R ; Function to return the sum of all odd natural numbers ; Function to return the sum of all odd numbers in range L and R ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumOdd ( int n ) { int terms = ( n + 1 ) / 2 ; int sum = terms * terms ; return sum ; } int suminRange ( int l , int r ) { return sumOdd ( r ) - sumOdd ( l - 1 ) ; } int main ( ) { int l = 2 , r = 5 ; cout << " Sum ▁ of ▁ odd ▁ natural ▁ numbers ▁ from ▁ L ▁ to ▁ R ▁ is ▁ " << suminRange ( l , r ) ; return 0 ; }
Sum of common divisors of two numbers A and B | C ++ implementation of above approach ; Function to calculate gcd of two numbers ; Function to calculate all common divisors of two given numbers a , b -- > input integer numbers ; find gcd of a , b ; Find the sum of divisors of n . ; if ' i ' is factor of n ; check if divisors are equal ; Driver program to run the case
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } int sumcommDiv ( int a , int b ) { int n = gcd ( a , b ) ; int sum = 0 ; for ( int i = 1 ; i <= sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) sum += i ; else sum += ( n / i ) + i ; } } return sum ; } int main ( ) { int a = 10 , b = 15 ; cout << " Sum ▁ = ▁ " << sumcommDiv ( a , b ) ; return 0 ; }
Check if a number is formed by Concatenation of 1 , 14 or 144 only | C ++ program to check if a number is formed by Concatenation of 1 , 14 or 144 only ; Function to check if a number is formed by Concatenation of 1 , 14 or 144 only ; check for each possible digit if given number consist other then 1 , 14 , 144 print NO else print YES ; Driver Code
#include <iostream> NEW_LINE using namespace std ; string checkNumber ( int N ) { int temp = N ; while ( temp > 0 ) { if ( temp % 1000 == 144 ) temp /= 1000 ; else if ( temp % 100 == 14 ) temp /= 100 ; else if ( temp % 10 == 1 ) temp /= 10 ; else { return " NO " ; } } return " YES " ; } int main ( ) { int N = 1414 ; cout << checkNumber ( N ) ; return 0 ; }
Fibonacci problem ( Value of Fib ( N ) * Fib ( N ) | C ++ implementation of the approach ; Driver code
#include <iostream> NEW_LINE using namespace std ; int getResult ( int n ) { if ( n & 1 ) return 1 ; return -1 ; } int main ( ) { int n = 3 ; cout << getResult ( n ) ; }
Find two numbers with sum and product both same as N | C ++ program to find a and b such that a * b = N and a + b = N ; Function to return the smallest string ; Not possible ; find a and b ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findAandB ( double N ) { double val = N * N - 4.0 * N ; if ( val < 0 ) { cout << " NO " ; return ; } double a = ( N + sqrt ( val ) ) / 2.0 ; double b = ( N - sqrt ( val ) ) / 2.0 ; cout << " a ▁ = ▁ " << a << endl ; cout << " b ▁ = ▁ " << b << endl ; } int main ( ) { double N = 69.0 ; findAandB ( N ) ; return 0 ; }
Find minimum operations needed to make an Array beautiful | CPP implementation of above approach ; Function to find minimum operations required to make array beautiful ; counting consecutive zeros . ; check that start and end are same ; check is zero and one are equal ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minOperations ( int A [ ] , int n ) { if ( n & 1 ) return -1 ; int zeros = 0 , consZeros = 0 , ones = 0 ; for ( int i = 0 ; i < n ; ++ i ) { A [ i ] == 0 ? zeros ++ : ones ++ ; if ( i + 1 < n ) { if ( A [ i ] == 0 && A [ i + 1 ] == 0 ) consZeros ++ ; } } if ( A [ 0 ] == A [ n - 1 ] && A [ 0 ] == 0 ) consZeros ++ ; if ( zeros == ones ) return consZeros ; else return -1 ; } int main ( ) { int A [ ] = { 1 , 1 , 0 , 0 } ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << minOperations ( A , n ) ; return 0 ; }
Steps to reduce N to zero by subtracting its most significant digit at every step | C ++ program to find the count of Steps to reduce N to zero by subtracting its most significant digit at every step ; Function to count the number of digits in a number m ; Function to count the number of steps to reach 0 ; count the total number of stesp ; iterate till we reach 0 ; count the digits in last ; decrease it by 1 ; find the number on whose division , we get the first digit ; first digit in last ; find the first number less than last where the first digit changes ; find the number of numbers with same first digit that are jumped ; count the steps ; the next number with a different first digit ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countdig ( int m ) { if ( m == 0 ) return 0 ; else return 1 + countdig ( m / 10 ) ; } int countSteps ( int x ) { int c = 0 ; int last = x ; while ( last ) { int digits = countdig ( last ) ; digits -= 1 ; int divisor = pow ( 10 , digits ) ; int first = last / divisor ; int lastnumber = first * divisor ; int skipped = ( last - lastnumber ) / first ; skipped += 1 ; c += skipped ; last = last - ( first * skipped ) ; } return c ; } int main ( ) { int n = 14 ; cout << countSteps ( n ) ; return 0 ; }
GCD of a number raised to some power and another number | C ++ program of the above approach ; Calculates modular exponentiation , i . e . , ( x ^ y ) % p in O ( log y ) ; x = x % p ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now y = y >> 1 ; y = y / 2 ; Returns GCD of a ^ n and b ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; typedef long long int ll ; ll power ( ll x , ll y , ll p ) { while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } ll gcd ( ll a , ll b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } ll powerGCD ( ll a , ll b , ll n ) { ll e = power ( a , n , b ) ; return gcd ( e , b ) ; } int main ( ) { ll a = 5 , b = 4 , n = 2 ; cout << powerGCD ( a , b , n ) ; return 0 ; }
Largest number not greater than N all the digits of which are odd | CPP program to print the largest integer not greater than N with all odd digits ; Function to check if all digits of a number are odd ; iterate for all digits ; if digit is even ; all digits are odd ; function to return the largest number with all digits odd ; iterate till we find a number with all digits odd ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool allOddDigits ( int n ) { while ( n ) { if ( ( n % 10 ) % 2 == 0 ) return false ; n /= 10 ; } return true ; } int largestNumber ( int n ) { if ( n % 2 == 0 ) n -- ; for ( int i = n ; ; i -= 2 ) if ( allOddDigits ( i ) ) return i ; } int main ( ) { int N = 23 ; cout << largestNumber ( N ) ; return 0 ; }
Largest number not greater than N all the digits of which are odd | CPP program to print the largest integer not greater than N with all odd digits ; function to return the largest number with all digits odd ; convert the number to a string for easy operations ; find first even digit ; if no even digit , then N is the answer ; till first even digit , add all odd numbers ; decrease 1 from the even digit ; add 9 in the rest of the digits ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int largestNumber ( int n ) { string s = " " ; int duplicate = n ; while ( n ) { s = char ( n % 10 + 48 ) + s ; n /= 10 ; } int index = -1 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( ( ( s [ i ] - '0' ) % 2 & 1 ) == 0 ) { index = i ; break ; } } if ( index == -1 ) return duplicate ; int num = 0 ; for ( int i = 0 ; i < index ; i ++ ) num = num * 10 + ( s [ i ] - '0' ) ; num = num * 10 + ( s [ index ] - '0' - 1 ) ; for ( int i = index + 1 ; i < s . length ( ) ; i ++ ) num = num * 10 + 9 ; return num ; } int main ( ) { int N = 24578 ; cout << largestNumber ( N ) ; return 0 ; }
Count number less than N which are product of perfect squares | C ++ program to count number less than N which are product of any two perfect squares ; Function to count number less than N which are product of any two perfect squares ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countNumbers ( int N ) { return int ( sqrt ( N ) ) - 1 ; } int main ( ) { int N = 36 ; cout << countNumbers ( N ) ; return 0 ; }
Count ordered pairs with product less than N | C ++ implementation of above approach ; Function to return count of Ordered pairs whose product are less than N ; Initialize count to 0 ; count total pairs ; multiply by 2 to get ordered_pairs ; subtract redundant pairs ( a , b ) where a == b . ; return answer ; Driver code ; function call to print required answer
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countOrderedPairs ( int N ) { int count_pairs = 0 ; for ( int i = 1 ; i <= sqrt ( N - 1 ) ; ++ i ) { for ( int j = i ; j * i < N ; ++ j ) ++ count_pairs ; } count_pairs *= 2 ; count_pairs -= int ( sqrt ( N - 1 ) ) ; return count_pairs ; } int main ( ) { int N = 5 ; cout << countOrderedPairs ( N ) ; return 0 ; }
Absolute Difference of all pairwise consecutive elements in an array | C ++ program to print the absolute difference of the consecutive elements ; Function to print pairwise absolute difference of consecutive elements ; absolute difference between consecutive numbers ; Driver Code
#include <iostream> NEW_LINE using namespace std ; void pairwiseDifference ( int arr [ ] , int n ) { int diff ; for ( int i = 0 ; i < n - 1 ; i ++ ) { diff = abs ( arr [ i ] - arr [ i + 1 ] ) ; cout << diff << " ▁ " ; } } int main ( ) { int arr [ ] = { 4 , 10 , 15 , 5 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; pairwiseDifference ( arr , n ) ; return 0 ; }
Find the sum of all multiples of 2 and 5 below N | CPP program to find the sum of all multiples of 2 and 5 below N ; Function to find sum of AP series ; Number of terms ; Function to find the sum of all multiples of 2 and 5 below N ; Since , we need the sum of multiples less than N ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long sumAP ( long long n , long long d ) { n /= d ; return ( n ) * ( 1 + n ) * d / 2 ; } long long sumMultiples ( long long n ) { n -- ; return sumAP ( n , 2 ) + sumAP ( n , 5 ) - sumAP ( n , 10 ) ; } int main ( ) { long long n = 20 ; cout << sumMultiples ( n ) ; return 0 ; }
Find the total marks obtained according to given marking scheme | C ++ implementation of above approach ; Function that calculates marks . ; for not attempt score + 0 ; for each correct answer score + 3 ; for each wrong answer score - 1 ; calculate total marks ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int markingScheme ( int N , int answerKey [ ] , int studentAnswer [ ] ) { int positive = 0 , negative = 0 , notattempt = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( studentAnswer [ i ] == 0 ) notattempt ++ ; else if ( answerKey [ i ] == studentAnswer [ i ] ) positive ++ ; else if ( answerKey [ i ] != studentAnswer [ i ] ) negative ++ ; } return ( positive * 3 ) + ( negative * -1 ) ; } int main ( ) { int answerKey [ ] = { 1 , 2 , 3 , 4 , 1 } ; int studentAnswer [ ] = { 1 , 2 , 3 , 4 , 0 } ; int N = sizeof ( answerKey ) / sizeof ( answerKey [ 0 ] ) ; cout << markingScheme ( N , answerKey , studentAnswer ) ; return 0 ; }