codeparrot/xlcost-text-to-code huggingface dataset:
Sum of all pair shortest paths in a Tree | C ++ program for the above approach ; Function that performs the Floyd Warshall to find all shortest paths ; Initialize the distance matrix ; Pick all vertices as source one by one ; Pick all vertices as destination for the above picked source ; If vertex k is on the shortest path from i to j then update dist [ i ] [ j ] ; Sum the upper diagonal of the shortest distance matrix ; Traverse the given dist [ ] [ ] ; Add the distance ; Return the final sum ; Function to generate the tree ; Add edges ; Get source and destination with weight ; Add the edges ; Perform Floyd Warshal Algorithm ; Driver code ; Number of Vertices ; Number of Edges ; Given Edges with weight ; Function Call
#include <iostream> NEW_LINE using namespace std ; #define INF  99999 NEW_LINE int floyd_warshall ( int * graph , int V ) { int dist [ V ] [ V ] , i , j , k ; for ( i = 0 ; i < V ; i ++ ) { for ( j = 0 ; j < V ; j ++ ) { dist [ i ] [ j ] = * ( ( graph + i * V ) + j ) ; } } for ( k = 0 ; k < V ; k ++ ) { for ( i = 0 ; i < V ; i ++ ) { for ( j = 0 ; j < V ; j ++ ) { if ( dist [ i ] [ k ] + dist [ k ] [ j ] < dist [ i ] [ j ] ) { dist [ i ] [ j ] = dist [ i ] [ k ] + dist [ k ] [ j ] ; } } } } int sum = 0 ; for ( i = 0 ; i < V ; i ++ ) { for ( j = i + 1 ; j < V ; j ++ ) { sum += dist [ i ] [ j ] ; } } return sum ; } int sumOfshortestPath ( int N , int E , int edges [ ] [ 3 ] ) { int g [ N ] [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { g [ i ] [ j ] = INF ; } } for ( int i = 0 ; i < E ; i ++ ) { int u = edges [ i ] [ 0 ] ; int v = edges [ i ] [ 1 ] ; int w = edges [ i ] [ 2 ] ; g [ u ] [ v ] = w ; g [ v ] [ u ] = w ; } return floyd_warshall ( ( int * ) g , N ) ; } int main ( ) { int N = 4 ; int E = 3 ; int Edges [ ] [ 3 ] = { { 0 , 1 , 1 } , { 1 , 2 , 2 } , { 2 , 3 , 3 } } ; cout << sumOfshortestPath ( N , E , Edges ) ; return 0 ; }
Kth space | C ++ Program for the above approach ; Function to print kth integer in a given string ; Size of the string ; Pointer for iteration ; If space char found decrement K ; If K becomes 1 , the next string is the required one ; Print the required number ; Driver Code ; Given string ; Given K ; Function call
#include <iostream> NEW_LINE using namespace std ; void print_kth_string ( string s , int K ) { int N = s . length ( ) ; int i ; for ( i = 0 ; i < N ; i ++ ) { if ( s [ i ] == ' ▁ ' ) K -- ; if ( K == 1 ) break ; } while ( i ++ < N && s [ i ] != ' ▁ ' ) cout << s [ i ] ; } int main ( ) { string s ( "10 ▁ 20 ▁ 30 ▁ 40" ) ; int K = 4 ; print_kth_string ( s , K ) ; }
Minimum peak elements from an array by their repeated removal at every iteration of the array | C ++ program for the above approach ; Function to return the list of minimum peak elements ; Length of original list ; Initialize resultant list ; Traverse each element of list ; Length of original list after removing the peak element ; Traverse new list after removal of previous min peak element ; Update min and index , if first element of list > next element ; Update min and index , if last element of list > previous one ; Update min and index , if list has single element ; Update min and index , if current element > adjacent elements ; Remove current min peak element from list ; Insert min peak into resultant list ; Print resultant list ; Driver Code ; Given array arr [ ] ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minPeaks ( vector < int > list ) { int n = list . size ( ) ; vector < int > result ; for ( int i = 0 ; i < n ; i ++ ) { int min = INT_MAX ; int index = -1 ; int size = list . size ( ) ; for ( int j = 0 ; j < size ; j ++ ) { if ( j == 0 && j + 1 < size ) { if ( list [ j ] > list [ j + 1 ] && min > list [ j ] ) { min = list [ j ] ; index = j ; } } else if ( j == size - 1 && j - 1 >= 0 ) { if ( list [ j ] > list [ j - 1 ] && min > list [ j ] ) { min = list [ j ] ; index = j ; } } else if ( size == 1 ) { min = list [ j ] ; index = j ; } else if ( list [ j ] > list [ j - 1 ] && list [ j ] > list [ j + 1 ] && min > list [ j ] ) { min = list [ j ] ; index = j ; } } list . erase ( list . begin ( ) + index ) ; result . push_back ( min ) ; } cout << " [ " ; for ( int i = 0 ; i < result . size ( ) ; i ++ ) { cout << result [ i ] << " , ▁ " ; } cout << " ] " ; } int main ( ) { vector < int > arr = { 1 , 9 , 7 , 8 , 2 , 6 } ; minPeaks ( arr ) ; }
Search an element in a reverse sorted array | C ++ program to implement the above approach ; Function to search if element X is present in reverse sorted array ; Store the first index of the subarray in which X lies ; Store the last index of the subarray in which X lies ; Store the middle index of the subarray ; Check if value at middle index of the subarray equal to X ; Element is found ; If X is smaller than the value at middle index of the subarray ; Search in right half of subarray ; Search in left half of subarray ; If X not found ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int binarySearch ( int arr [ ] , int N , int X ) { int start = 0 ; int end = N ; while ( start <= end ) { int mid = start + ( end - start ) / 2 ; if ( X == arr [ mid ] ) { return mid ; } else if ( X < arr [ mid ] ) { start = mid + 1 ; } else { end = mid - 1 ; } } return -1 ; } int main ( ) { int arr [ ] = { 5 , 4 , 3 , 2 , 1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int X = 5 ; cout << binarySearch ( arr , N , X ) ; return 0 ; }
Construct an AP series consisting of A and B having minimum possible Nth term | C ++ program for the above approach ; Function to check if both a and b are present in the AP series or not ; Iterate over the array arr [ ] ; If a is present ; If b is present ; If both are present ; Otherwise ; Function to print all the elements of the Arithmetic Progression ; Function to construct AP series consisting of A and B with minimum Nth term ; Stores the resultant series ; Initialise ans [ i ] as INT_MAX ; Maintain a smaller than b ; swap ( a and b ) ; Difference between a and b ; Check for all possible combination of start and common difference d ; Initialise arr [ 0 ] as start ; Check if both a and b are present or not and the Nth term is the minimum or not ; Update the answer ; Print the resultant array ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check_both_present ( int arr [ ] , int N , int a , int b ) { bool f1 = false , f2 = false ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == a ) { f1 = true ; } if ( arr [ i ] == b ) { f2 = true ; } } if ( f1 && f2 ) { return true ; } else { return false ; } } void print_array ( int ans [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { cout << ans [ i ] << " ▁ " ; } } void build_AP ( int N , int a , int b ) { int arr [ N ] , ans [ N ] ; for ( int i = 0 ; i < N ; i ++ ) ans [ i ] = INT_MAX ; int flag = 0 ; if ( a > b ) { swap ( a , b ) ; } int diff = b - a ; for ( int start = 1 ; start <= a ; start ++ ) { for ( int d = 1 ; d <= diff ; d ++ ) { arr [ 0 ] = start ; for ( int i = 1 ; i < N ; i ++ ) { arr [ i ] = arr [ i - 1 ] + d ; } if ( check_both_present ( arr , N , a , b ) && arr [ N - 1 ] < ans [ N - 1 ] ) { for ( int i = 0 ; i < N ; i ++ ) { ans [ i ] = arr [ i ] ; } } } } print_array ( ans , N ) ; } int main ( ) { int N = 5 , A = 20 , B = 50 ; build_AP ( N , A , B ) ; return 0 ; }
Find the word from a given sentence having given word as prefix | C ++ program for the above approach ; Function to find the position of the string having word as prefix ; Initialize a vector ; Extract words from the sentence ; Traverse each word ; Traverse the characters of word ; If prefix does not match ; Otherwise ; Return the word ; Return - 1 if not present ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string isPrefixOfWord ( string sentence , string Word ) { stringstream ss ( sentence ) ; vector < string > v ; string temp ; while ( ss >> temp ) { v . push_back ( temp ) ; } for ( int i = 0 ; i < v . size ( ) ; i ++ ) { for ( int j = 0 ; j < v [ i ] . size ( ) ; j ++ ) { if ( v [ i ] [ j ] != Word [ j ] ) break ; else if ( j == Word . size ( ) - 1 ) return v [ i ] ; } } return " - 1" ; } int main ( ) { string s = " Welcome ▁ to ▁ Geeksforgeeks " ; string word = " Gee " ; cout << isPrefixOfWord ( s , word ) << endl ; return 0 ; }
Paths requiring minimum number of jumps to reach end of array | C ++ program to implement the above approach ; Pair Struct ; Stores the current index ; Stores the path travelled so far ; Stores the minimum jumps required to reach the last index from current index ; Minimum jumps required to reach end of the array ; Stores the maximum number of steps that can be taken from the current index ; Checking if index stays within bounds ; Stores the minimum number of jumps required to jump from ( i + j ) - th index ; Function to find all possible paths to reach end of array requiring minimum number of steps ; Storing the neighbours of current index element ; Function to find the minimum steps and corresponding paths to reach end of an array ; dp [ ] array stores the minimum jumps from each position to last position ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Pair { int idx ; string psf ; int jmps ; } ; void minJumps ( int arr [ ] , int dp [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) dp [ i ] = INT_MAX ; dp [ n - 1 ] = 0 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { int steps = arr [ i ] ; int min = INT_MAX ; for ( int j = 1 ; j <= steps && i + j < n ; j ++ ) { if ( dp [ i + j ] != INT_MAX && dp [ i + j ] < min ) { min = dp [ i + j ] ; } } if ( min != INT_MAX ) dp [ i ] = min + 1 ; } } void possiblePath ( int arr [ ] , int dp [ ] , int n ) { queue < Pair > Queue ; Pair p1 = { 0 , "0" , dp [ 0 ] } ; Queue . push ( p1 ) ; while ( Queue . size ( ) > 0 ) { Pair tmp = Queue . front ( ) ; Queue . pop ( ) ; if ( tmp . jmps == 0 ) { cout << tmp . psf << " STRNEWLINE " ; continue ; } for ( int step = 1 ; step <= arr [ tmp . idx ] ; step ++ ) { if ( tmp . idx + step < n && tmp . jmps - 1 == dp [ tmp . idx + step ] ) { string s1 = tmp . psf + " ▁ - > ▁ " + to_string ( ( tmp . idx + step ) ) ; Pair p2 = { tmp . idx + step , s1 , tmp . jmps - 1 } ; Queue . push ( p2 ) ; } } } } void Solution ( int arr [ ] , int dp [ ] , int size ) { minJumps ( arr , dp , size ) ; possiblePath ( arr , dp , size ) ; } int main ( ) { int arr [ ] = { 3 , 3 , 0 , 2 , 1 , 2 , 4 , 2 , 0 , 0 } ; int size = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int dp [ size ] ; Solution ( arr , dp , size ) ; }
Length of longest increasing absolute even subsequence | C ++ 14 program for the above approach ; Function to find the longest increasing absolute even subsequence ; Stores length of required subsequence ; Traverse the array ; Traverse prefix of current array element ; Check if the subsequence is LIS and have even absolute difference of adjacent pairs ; Update lis [ ] ; Stores maximum length ; Find the length of longest absolute even subsequence ; Return the maximum length of absolute even subsequence ; Driver code ; Given array arr [ ] and brr [ ] ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void EvenLIS ( int arr [ ] , int n ) { int lis [ n ] ; for ( int i = 0 ; i < n ; i ++ ) lis [ i ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( abs ( arr [ i ] ) > abs ( arr [ j ] ) && abs ( arr [ i ] ) % 2 == 0 && abs ( arr [ j ] ) % 2 == 0 && lis [ i ] < lis [ j ] + 1 ) lis [ i ] = lis [ j ] + 1 ; } } int maxlen = 0 ; for ( int i = 0 ; i < n ; i ++ ) maxlen = max ( maxlen , lis [ i ] ) ; cout << maxlen << endl ; } int main ( ) { int arr [ ] = { 11 , -22 , 43 , -54 , 66 , 5 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; EvenLIS ( arr , N ) ; }
Count balanced nodes present in a binary tree | C ++ program to implement the above approach ; Structure of a Tree Node ; Function to get the sum of left subtree and right subtree ; Base case ; Store the sum of left subtree ; Store the sum of right subtree ; Check if node is balanced or not ; Increase count of balanced nodes ; Return subtree sum ; Driver Code ; Insert nodes in tree ; Store the count of balanced nodes
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * left ; Node * right ; Node ( int val ) { data = val ; left = right = NULL ; } } ; int Sum ( Node * root , int & res ) { if ( root == NULL ) { return 0 ; } int leftSubSum = Sum ( root -> left , res ) ; int rightSubSum = Sum ( root -> right , res ) ; if ( root -> left and root -> right && leftSubSum == rightSubSum ) res += 1 ; return root -> data + leftSubSum + rightSubSum ; } int main ( ) { Node * root = new Node ( 9 ) ; root -> left = new Node ( 2 ) ; root -> left -> left = new Node ( -1 ) ; root -> left -> right = new Node ( 3 ) ; root -> right = new Node ( 4 ) ; root -> right -> right = new Node ( 0 ) ; int res = 0 ; Sum ( root , res ) ; cout << res ; }
Print alternate nodes from all levels of a Binary Tree | C ++ program to implement the above approach ; Structure of a Node ; Print alternate nodes of a binary tree ; Store nodes of each level ; Store count of nodes of current level ; Print alternate nodes of the current level ; If left child exists ; Store left child ; If right child exists ; Store right child ; Driver Code ; Create a tree
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * left ; Node * right ; Node ( int val ) { data = val ; left = right = NULL ; } } ; void PrintAlternate ( Node * root ) { queue < Node * > Q ; Q . push ( root ) ; while ( ! Q . empty ( ) ) { int N = Q . size ( ) ; for ( int i = 0 ; i < N ; i ++ ) { Node * temp = Q . front ( ) ; Q . pop ( ) ; if ( i % 2 == 0 ) { cout << temp -> data << " ▁ " ; } if ( temp -> left ) { Q . push ( temp -> left ) ; } if ( temp -> right ) { Q . push ( temp -> right ) ; } } cout << endl ; } } int main ( ) { Node * root ; root = new Node ( 71 ) ; root -> left = new Node ( 88 ) ; root -> right = new Node ( 99 ) ; root -> left -> left = new Node ( 4 ) ; root -> left -> right = new Node ( 5 ) ; root -> right -> left = new Node ( 6 ) ; root -> right -> right = new Node ( 7 ) ; root -> left -> left -> left = new Node ( 8 ) ; root -> left -> left -> right = new Node ( 9 ) ; root -> left -> right -> left = new Node ( 10 ) ; root -> left -> right -> right = new Node ( 11 ) ; root -> right -> left -> right = new Node ( 13 ) ; root -> right -> right -> left = new Node ( 14 ) ; PrintAlternate ( root ) ; }
Count subarrays for every array element in which they are the minimum | C ++ implementation of the above approach ; Function to required count subarrays ; For storing count of subarrays ; For finding next smaller element left to a element if there is no next smaller element left to it than taking - 1. ; For finding next smaller element right to a element if there is no next smaller element right to it than taking n . ; Taking exact boundaries in which arr [ i ] is minimum ; Similarly for right side ; Driver Code ; Given array arr [ ] ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > countingSubarray ( vector < int > arr , int n ) { vector < int > a ( n ) ; vector < int > nsml ( n , -1 ) ; vector < int > nsmr ( n , n ) ; stack < int > st ; for ( int i = n - 1 ; i >= 0 ; i -- ) { while ( ! st . empty ( ) && arr [ st . top ( ) ] >= arr [ i ] ) st . pop ( ) ; nsmr [ i ] = ( ! st . empty ( ) ) ? st . top ( ) : n ; st . push ( i ) ; } while ( ! st . empty ( ) ) st . pop ( ) ; for ( int i = 0 ; i < n ; i ++ ) { while ( ! st . empty ( ) && arr [ st . top ( ) ] >= arr [ i ] ) st . pop ( ) ; nsml [ i ] = ( ! st . empty ( ) ) ? st . top ( ) : -1 ; st . push ( i ) ; } for ( int i = 0 ; i < n ; i ++ ) { nsml [ i ] ++ ; nsmr [ i ] -- ; int r = nsmr [ i ] - i + 1 ; int l = i - nsml [ i ] + 1 ; a [ i ] = r * l ; } return a ; } int main ( ) { int N = 5 ; vector < int > arr = { 3 , 2 , 4 , 1 , 5 } ; auto a = countingSubarray ( arr , N ) ; cout << " [ " ; int n = a . size ( ) - 1 ; for ( int i = 0 ; i < n ; i ++ ) cout << a [ i ] << " , ▁ " ; cout << a [ n ] << " ] " ; return 0 ; }
Lexicographically smallest permutation of a string that contains all substrings of another string | C ++ program for the above approach ; Function to reorder the string B to contain all the substrings of A ; Find length of strings ; Initialize array to count the frequencies of the character ; Counting frequencies of character in B ; Find remaining character in B ; Declare the reordered string ; Loop until freq [ j ] > 0 ; Decrement the value from freq array ; Check if A [ j ] > A [ 0 ] ; Check if A [ j ] < A [ 0 ] ; Append the remaining characters to the end of the result ; Push all the values from frequency array in the answer ; Return the answer ; Driver Code ; Given strings A and B ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; string reorderString ( string A , string B ) { int size_a = A . length ( ) ; int size_b = B . length ( ) ; int freq [ 300 ] = { 0 } ; for ( int i = 0 ; i < size_b ; i ++ ) freq [ B [ i ] ] ++ ; for ( int i = 0 ; i < size_a ; i ++ ) freq [ A [ i ] ] -- ; for ( int j = ' a ' ; j <= ' z ' ; j ++ ) { if ( freq [ j ] < 0 ) return " - 1" ; } string answer ; for ( int j = ' a ' ; j < A [ 0 ] ; j ++ ) while ( freq [ j ] > 0 ) { answer . push_back ( j ) ; freq [ j ] -- ; } int first = A [ 0 ] ; for ( int j = 0 ; j < size_a ; j ++ ) { if ( A [ j ] > A [ 0 ] ) break ; if ( A [ j ] < A [ 0 ] ) { answer += A ; A . clear ( ) ; break ; } } while ( freq [ first ] > 0 ) { answer . push_back ( first ) ; -- freq [ first ] ; } answer += A ; for ( int j = ' a ' ; j <= ' z ' ; j ++ ) while ( freq [ j ] -- ) answer . push_back ( j ) ; return answer ; } int main ( ) { string A = " aa " ; string B = " ababab " ; cout << reorderString ( A , B ) ; return 0 ; }
Find the interval which contains maximum number of concurrent meetings | C ++ 14 implementation of the above approach ; Function to find time slot of maximum concurrent meeting ; Sort array by start time of meeting ; Declare Minheap ; Insert first meeting end time ; Initialize max_len , max_start and max_end ; Traverse over sorted array to find required slot ; Pop all meetings that end before current meeting ; Push current meeting end time ; Update max_len , max_start and max_end if size of queue is greater than max_len ; Print slot of maximum concurrent meeting ; Driver Code ; Given array of meetings ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool cmp ( vector < int > a , vector < int > b ) { if ( a [ 0 ] != b [ 0 ] ) return a [ 0 ] < b [ 0 ] ; return a [ 1 ] - b [ 1 ] ; } void maxConcurrentMeetingSlot ( vector < vector < int > > meetings ) { sort ( meetings . begin ( ) , meetings . end ( ) , cmp ) ; priority_queue < int , vector < int > , greater < int > > pq ; pq . push ( meetings [ 0 ] [ 1 ] ) ; int max_len = 0 , max_start = 0 ; int max_end = 0 ; for ( auto k : meetings ) { while ( pq . size ( ) > 0 && k [ 0 ] >= pq . top ( ) ) pq . pop ( ) ; pq . push ( k [ 1 ] ) ; if ( pq . size ( ) > max_len ) { max_len = pq . size ( ) ; max_start = k [ 0 ] ; max_end = pq . top ( ) ; } } cout << max_start << " ▁ " << max_end ; } int main ( ) { vector < vector < int > > meetings = { { 100 , 200 } , { 50 , 300 } , { 300 , 400 } } ; maxConcurrentMeetingSlot ( meetings ) ; }
Number from a range [ L , R ] having Kth minimum cost of conversion to 1 by given operations | C ++ 14 implementation of the above approach ; Function to calculate the cost ; Base case ; Even condition ; Odd condition ; Return cost ; Function to find Kth element ; Array to store the costs ; Sort the array based on cost ; Driver Code ; Given range and6 K ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int func ( int n ) { int count = 0 ; if ( n == 2 or n == 1 ) return 1 ; if ( n % 2 == 0 ) count = 1 + func ( n / 2 ) ; if ( n % 2 != 0 ) count = 1 + func ( n * 3 + 1 ) ; return count ; } void findKthElement ( int l , int r , int k ) { vector < int > arr ; for ( int i = l ; i <= r ; i ++ ) arr . push_back ( i ) ; vector < vector < int > > result ; for ( int i : arr ) result . push_back ( { i , func ( i ) } ) ; sort ( result . begin ( ) , result . end ( ) ) ; cout << ( result [ k - 1 ] [ 0 ] ) ; } int main ( ) { int l = 12 ; int r = 15 ; int k = 2 ; findKthElement ( l , r , k ) ; return 0 ; }
Check if a string represents a hexadecimal number or not | C ++ implementation of the above approach ; Function to check if the string represents a hexadecimal number ; Size of string ; Iterate over string ; Check if the character is invalid ; Print true if all characters are valid ; Driver code ; Given string ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void checkHex ( string s ) { int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { char ch = s [ i ] ; if ( ( ch < '0' ch > '9' ) && ( ch < ' A ' ch > ' F ' ) ) { cout << " No " << endl ; return ; } } cout << " Yes " << endl ; } int main ( ) { string s = " BF57C " ; checkHex ( s ) ; return 0 ; }
Longest subsequence forming an Arithmetic Progression ( AP ) | C ++ program for the above approach ; Function that finds the longest arithmetic subsequence having the same absolute difference ; Stores the length of sequences having same difference ; Stores the resultant length ; Iterate over the array ; Update length of subsequence ; Return res ; Driver Code ; Given array arr [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int lenghtOfLongestAP ( int A [ ] , int n ) { unordered_map < int , unordered_map < int , int > > dp ; int res = 2 ; for ( int i = 0 ; i < n ; ++ i ) { for ( int j = i + 1 ; j < n ; ++ j ) { int d = A [ j ] - A [ i ] ; dp [ d ] [ j ] = dp [ d ] . count ( i ) ? dp [ d ] [ i ] + 1 : 2 ; res = max ( res , dp [ d ] [ j ] ) ; } } return res ; } int main ( ) { int arr [ ] = { 20 , 1 , 15 , 3 , 10 , 5 , 8 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << lenghtOfLongestAP ( arr , N ) ; return 0 ; }
Length of longest increasing prime subsequence from a given array | C ++ program for the above approach ; Function to find the prime numbers till 10 ^ 5 using Sieve of Eratosthenes ; False here indicates that it is not prime ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p , set them to non - prime ; Function which computes the length of the LIS of Prime Numbers ; Create an array of size n ; Create boolean array to mark prime numbers ; Initialize all values to true ; Precompute N primes ; Compute optimized LIS having prime numbers in bottom up manner ; Check for LIS and prime ; Return maximum value in lis [ ] ; Driver Code ; Given array ; Size of array ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  100005 NEW_LINE void SieveOfEratosthenes ( bool prime [ ] , int p_size ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= p_size ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i <= p_size ; i += p ) prime [ i ] = false ; } } } int LISPrime ( int arr [ ] , int n ) { int lisp [ n ] ; bool prime [ N + 1 ] ; memset ( prime , true , sizeof ( prime ) ) ; SieveOfEratosthenes ( prime , N ) ; lisp [ 0 ] = prime [ arr [ 0 ] ] ? 1 : 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( ! prime [ arr [ i ] ] ) { lisp [ i ] = 0 ; continue ; } lisp [ i ] = 1 ; for ( int j = 0 ; j < i ; j ++ ) { if ( prime [ arr [ j ] ] && arr [ i ] > arr [ j ] && lisp [ i ] < lisp [ j ] + 1 ) { lisp [ i ] = lisp [ j ] + 1 ; } } } return * max_element ( lisp , lisp + n ) ; } int main ( ) { int arr [ ] = { 1 , 2 , 5 , 3 , 2 , 5 , 1 , 7 } ; int M = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << LISPrime ( arr , M ) ; return 0 ; }