codeparrot/xlcost-text-to-code huggingface dataset:
Maximize cost of removing all occurrences of substrings " ab " and " ba " | C # program for the above approach : ; Function to find the maximum cost of removing substrings " ab " and " ba " from S ; MaxStr is the substring char array with larger cost ; MinStr is the substring char array with smaller cost ; ; Denotes larger point ; Denotes smaller point ; Stores cost scored ; Stack to keep track of characters ; Traverse the string ; If the substring is maxstr ; Pop from the stack ; Add maxp to cost ; Push the character to the stack ; Remaining string after removing maxstr ; Find remaining string ; Reversing the string retrieved from the stack ; Removing all occurences of minstr ; If the substring is minstr ; Pop from the stack ; Add minp to the cost ; Otherwise ; Return the maximum cost ; Driver Code ; Input String ; Costs
using System ; using System . Collections ; class GFG { static int MaxCollection ( string S , int P , int Q ) { char [ ] maxstr = ( P >= Q ? " ab " : " ba " ) . ToCharArray ( ) ; char [ ] minstr = ( P >= Q ? " ba " : " ab " ) . ToCharArray ( ) ; int maxp = Math . Max ( P , Q ) ; int minp = Math . Min ( P , Q ) ; int cost = 0 ; Stack stack1 = new Stack ( ) ; char [ ] s = S . ToCharArray ( ) ; foreach ( char ch in s ) { if ( stack1 . Count > 0 && ( ( char ) stack1 . Peek ( ) == maxstr [ 0 ] && ch == maxstr [ 1 ] ) ) { stack1 . Pop ( ) ; cost += maxp ; } else { stack1 . Push ( ch ) ; } } string sb = " " ; while ( stack1 . Count > 0 ) { sb = sb + stack1 . Peek ( ) ; stack1 . Pop ( ) ; } char [ ] chars = sb . ToCharArray ( ) ; Array . Reverse ( chars ) ; string remstr = new string ( chars ) ; foreach ( char ch in remstr . ToCharArray ( ) ) { if ( stack1 . Count > 0 && ( ( char ) stack1 . Peek ( ) == minstr [ 0 ] && ch == minstr [ 1 ] ) ) { stack1 . Pop ( ) ; cost += minp ; } else { stack1 . Push ( ch ) ; } } return cost ; } static void Main ( ) { string S = " cbbaabbaab " ; int P = 6 ; int Q = 4 ; Console . Write ( MaxCollection ( S , P , Q ) ) ; } }
Vertical width of Binary tree | Set 2 | C # code to find the vertical width of a binary tree ; A binary tree node has data , pointer to left child and a pointer to right child ; Function to fill hd in set . ; Third parameter is horizontal distance ; Driver Code ; Constructed bunary tree is : 1 / \ 2 3 / \ \ 4 5 8 / \ 6 7
using System ; using System . Collections . Generic ; public class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } public class BinaryTree { Node root ; void fillSet ( Node root , HashSet < int > set , int hd ) { if ( root == null ) return ; fillSet ( root . left , set , hd - 1 ) ; set . Add ( hd ) ; fillSet ( root . right , set , hd + 1 ) ; } int verticalWidth ( Node root ) { HashSet < int > set = new HashSet < int > ( ) ; fillSet ( root , set , 0 ) ; return set . Count ; } public static void Main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; tree . root . right . right = new Node ( 8 ) ; tree . root . right . right . left = new Node ( 6 ) ; tree . root . right . right . right = new Node ( 7 ) ; Console . WriteLine ( tree . verticalWidth ( tree . root ) ) ; } }
Maximum value of ( arr [ i ] * arr [ j ] ) + ( arr [ j ] | C # program for the above approach ; Function to find the value of the expression a * b + ( b - a ) ; Function to find the maximum value of the expression a * b + ( b - a ) possible for any pair ( a , b ) ; Sort the vector in ascending order ; Stores the maximum value ; Update ans by choosing the pair of the minimum and 2 nd minimum ; Update ans by choosing the pair of maximum and 2 nd maximum ; Return the value of ans ; Driver Code ; Given inputs
using System ; using System . Collections . Generic ; class GFG { static int calc ( int a , int b ) { return a * b + ( b - a ) ; } static int findMaximum ( List < int > arr , int N ) { arr . Sort ( ) ; int ans = - 1000000000 ; ans = Math . Max ( ans , calc ( arr [ 0 ] , arr [ 1 ] ) ) ; ans = Math . Max ( ans , calc ( arr [ N - 2 ] , arr [ N - 1 ] ) ) ; return ans ; } public static void Main ( ) { List < int > arr = new List < int > { 0 , - 47 , 12 } ; int N = ( int ) arr . Count ; Console . Write ( findMaximum ( arr , N ) ) ; } }
Minimize flips on K |  ; store previous flip events ; remove an item which is out range of window . ; In a window , if A [ i ] is a even number with even times fliped , it need to be fliped again . On other hand , if A [ i ] is a odd number with odd times fliped , it need to be fliped again . ; flip . Enqueue ( i ) ; insert ; Driver Code
using System ; using System . Collections ; class GFG { static int minKBitFlips ( int [ ] A , int K ) { Queue flip = new Queue ( ) ; int count = 0 ; for ( int i = 0 ; i < A . Length ; i ++ ) { if ( flip . Count > 0 && ( i - ( int ) flip . Peek ( ) >= K ) ) { flip . Dequeue ( ) ; } if ( A [ i ] % 2 == flip . Count % 2 ) { if ( i + K - 1 >= A . Length ) { return - 1 ; } count ++ ; } } return count ; } } static void ( ) { int [ ] A = { 0 , 1 , 0 } ; int K = 1 ; int ans = minKBitFlips ( A , K ) ; Console . WriteLine ( ans ) ; }
Minimize flips on K |  ; We 're flipping the subarray from A[i] to A[i+K-1]  ; If not flip entire array , impossible ; Driver Code
using System ; class GFG { static int minKBitFlips ( int [ ] A , int K ) { int [ ] temp = new int [ A . Length ] ; int count = 0 ; int flip = 0 ; for ( int i = 0 ; i < A . Length ; ++ i ) { flip ^= temp [ i ] ; if ( A [ i ] == flip ) { count ++ ; if ( i + K > A . Length ) { return - 1 ; } flip ^= 1 ; if ( i + K < A . Length ) { temp [ i + K ] ^= 1 ; } } } return count ; } public static void Main ( String [ ] args ) { int [ ] A = { 0 , 1 , 0 } ; int K = 1 ; int ans = minKBitFlips ( A , K ) ; Console . Write ( ans ) ; } }
Generate an N | C # program for the above approach ; Function to calculate GCD of given array ; Utility function to check for all the combinations ; If a sequence of size N is obtained ; If gcd of current combination is K ; If current element from first array is divisible by K ; Recursively proceed further ; If current combination satisfies given condition ; Remove the element from the combination ; If current element from second array is divisible by K ; Recursively proceed further ; If current combination satisfies given condition ; Remove the element from the combination ; Function to check for all possible combinations ; Stores the subsequence ; If GCD of any sequence is not equal to K ; Driver Code ; Given arrays ; size of the array ; Given value of K ; Function call to generate a subsequence whose GCD is K
using System ; using System . Collections . Generic ; class GFG { static int GCD ( int a , int b ) { if ( b == 0 ) return a ; return GCD ( b , a % b ) ; } static int GCDArr ( List < int > a ) { int ans = a [ 0 ] ; foreach ( int val in a ) { ans = GCD ( ans , val ) ; } return ans ; } static bool findSubseqUtil ( int [ ] a , int [ ] b , List < int > ans , int k , int i , int N ) { if ( ans . Count == N ) { if ( GCDArr ( ans ) == k ) { foreach ( int val in ans ) Console . Write ( val + " ▁ " ) ; return true ; } else { return false ; } } if ( a [ i ] % k == 0 ) { ans . Add ( a [ i ] ) ; bool temp = findSubseqUtil ( a , b , ans , k , i + 1 , N ) ; if ( temp == true ) return true ; ans . RemoveAt ( ans . Count - 1 ) ; } if ( b [ i ] % k == 0 ) { ans . Add ( b [ i ] ) ; bool temp = findSubseqUtil ( a , b , ans , k , i + 1 , N ) ; if ( temp == true ) return true ; ans . RemoveAt ( ans . Count - 1 ) ; } return false ; } static void findSubseq ( int [ ] A , int [ ] B , int K , int i , int N ) { List < int > ans = new List < int > ( ) ; bool ret = findSubseqUtil ( A , B , ans , K , i , N ) ; if ( ret == false ) Console . Write ( - 1 ) ; } public static void Main ( ) { int [ ] A = { 5 , 3 , 6 , 2 , 9 } ; int [ ] B = { 21 , 7 , 14 , 12 , 28 } ; int N = A . Length ; int K = 3 ; findSubseq ( A , B , K , 0 , N ) ; } }
Kth element in permutation of first N natural numbers having all even numbers placed before odd numbers in increasing order | C # program for above approach ; Function to find the K - th element in the required permutation ; Stores the required permutation ; Insert all the even numbers less than or equal to N ; Now , insert all odd numbers less than or equal to N ; Print the Kth element ; Driver code ; functions call
using System ; using System . Collections . Generic ; public class GFG { static void findKthElement ( int N , int K ) { List < int > v = new List < int > ( ) ; for ( int i = 1 ; i <= N ; i ++ ) { if ( i % 2 == 0 ) { v . Add ( i ) ; } } for ( int i = 1 ; i <= N ; i ++ ) { if ( i % 2 != 0 ) { v . Add ( i ) ; } } Console . WriteLine ( v [ K - 1 ] ) ; } public static void Main ( String [ ] args ) { int N = 10 , K = 3 ; findKthElement ( N , K ) ; } }
Maximize difference between odd and even indexed array elements by shift operations | C # program for the above approach ; Function to minimize array elements by shift operations ; For checking all the left shift operations ; Left shift ; Consider the minimum possible value ; Function to maximize array elements by shift operations ; For checking all the left shift operations ; Left shift ; Consider the maximum possible value ; Function to maximize the absolute difference between even and odd indexed array elements ; To calculate the difference of odd indexed elements and even indexed elements ; To calculate the difference between odd and even indexed array elements ; Print the maximum value ; Driver Code
using System ; class GFG { static int minimize ( int n ) { int optEle = n ; string strEle = n . ToString ( ) ; for ( int idx = 0 ; idx < strEle . Length ; idx ++ ) { int temp = Int32 . Parse ( strEle . Substring ( idx ) + strEle . Substring ( 0 , idx ) ) ; optEle = Math . Min ( optEle , temp ) ; } return optEle ; } static int maximize ( int n ) { int optEle = n ; string strEle = n . ToString ( ) ; for ( int idx = 0 ; idx < strEle . Length ; idx ++ ) { int temp = Int32 . Parse ( strEle . Substring ( idx ) + strEle . Substring ( 0 , idx ) ) ; optEle = Math . Max ( optEle , temp ) ; } return optEle ; } static void minimumDifference ( int [ ] arr ) { int caseOne = 0 ; int minVal = 0 ; int maxVal = 0 ; for ( int i = 0 ; i < arr . Length ; i ++ ) { if ( i % 2 == 0 ) minVal += minimize ( arr [ i ] ) ; else maxVal += maximize ( arr [ i ] ) ; } caseOne = Math . Abs ( maxVal - minVal ) ; int caseTwo = 0 ; minVal = 0 ; maxVal = 0 ; for ( int i = 0 ; i < arr . Length ; i ++ ) { if ( i % 2 == 0 ) maxVal += maximize ( arr [ i ] ) ; else minVal += minimize ( arr [ i ] ) ; caseTwo = Math . Abs ( maxVal - minVal ) ; } Console . WriteLine ( Math . Max ( caseOne , caseTwo ) ) ; } public static void Main ( ) { int [ ] arr = { 332 , 421 , 215 , 584 , 232 } ; minimumDifference ( arr ) ; } }
Minimize operations of removing 2 i | C # program to implement the above approach ; Function to find minimum count of steps required to remove all the array elements ; Stores minimum count of steps required to remove all the array elements ; Update N ; Traverse each bit of N ; If current bit is set ; Update cntStep ; Driver code
using System ; class GFG { static int minimumStepReqArr ( int [ ] arr , int N ) { int cntStep = 0 ; N += 1 ; for ( int i = 31 ; i >= 0 ; i -- ) { if ( ( N & ( 1 << i ) ) != 0 ) { cntStep += 1 ; } } return cntStep ; } static void Main ( ) { int [ ] arr = { 1 , 2 , 3 } ; int N = arr . Length ; Console . WriteLine ( minimumStepReqArr ( arr , N ) ) ; } }
Minimize removal of alternating subsequences to empty given Binary String | C # program for the above approach ; Function to find the minimum number of operations required to empty the string ; Initialize variables ; Traverse the string ; If current character is 0 ; Update maximum consecutive 0 s and 1 s ; Print the minimum operation ; Driver Code ; input string ; length of string ; Function Call
using System ; class GFG { public static void minOpsToEmptyString ( string S , int N ) { int one = 0 , zero = 0 ; int x0 = 0 , x1 = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( S [ i ] == '0' ) { x0 ++ ; x1 = 0 ; } else { x1 ++ ; x0 = 0 ; } zero = Math . Max ( x0 , zero ) ; one = Math . Max ( x1 , one ) ; } Console . WriteLine ( Math . Max ( one , zero ) ) ; } static public void Main ( ) { int N = S . Length ; string S = "0100100111" ; int N = S . Length ; minOpsToEmptyString ( S , N ) ; } }
Swap upper and lower triangular halves of a given Matrix | C # program for the above approach ; Function to swap laterally inverted images of upper and lower triangular halves of a given matrix ; Store the matrix elements from upper & lower triangular halves ; Traverse the matrix mat [ ] [ ] ; Find the index ; If current element lies on the principal diagonal ; If current element lies below the principal diagonal ; If current element lies above the principal diagonal ; Traverse again to swap values ; Find the index ; Principal diagonal ; Below main diagonal ; Above main diagonal ; Traverse the matrix and print ; Driver Code ; Given Matrix mat [ ] [ ] ; Swap the upper and lower triangular halves
using System ; class GFG { static void ReverseSwap ( int [ , ] mat , int n ) { int [ ] lowerEle = new int [ n ] ; int [ ] upperEle = new int [ n ] ; int index ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { index = Math . Abs ( i - j ) ; if ( i == j ) { continue ; } else if ( j < i ) { lowerEle [ index ] = mat [ i , j ] ; } else { upperEle [ index ] = mat [ i , j ] ; } } } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { index = Math . Abs ( i - j ) ; if ( i == j ) { continue ; } else if ( j < i ) { mat [ i , j ] = upperEle [ index ] ; } else { mat [ i , j ] = lowerEle [ index -- ] ; } } } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { Console . Write ( mat [ i , j ] + " ▁ " ) ; } Console . WriteLine ( ) ; } } static public void Main ( ) { int [ , ] mat = new int [ , ] { { 1 , 2 } , { 4 , 5 } } ; int N = mat . GetLength ( 0 ) ; ReverseSwap ( mat , N ) ; } }
Minimize sum of K positive integers with given LCM | C # program to implement the above approach ; Function to find the prime power of X ; Stores prime powers of X ; Iterate over the range [ 2 , Math . Sqrt ( X ) ] ; If X is divisible by i ; Stores prime power ; Calculate prime power of X ; Update X ; Update p ; Insert prime powers into primePow [ ] ; If X exceeds 1 ; Function to calculate the sum of array elements ; Stores sum of array elements ; Traverse the array ; Update sum ; Function to partition array into K groups such that sum of elements of the K groups is minimum ; If count of prime powers is equal to pos ; Stores minimum sum of of each partition of [ ] arr into K groups ; Traverse the array [ ] arr ; Include primePow [ pos ] into i - th groups ; Update res ; Remove factors [ pos ] from i - th groups ; Utility function to calculate minimum sum of K positive integers whose LCM is X ; Stores all prime powers of X ; Stores count of prime powers ; Stores minimum sum of K positive integers whose LCM is X ; If n is less than or equal to k ; Traverse primePow [ ] array ; Update sum ; Update sum ; arr [ i ] : Stores element in i - th group by partitioning the primePow [ ] array ; Update sum ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static List < int > primePower ( int X ) { List < int > primePow = new List < int > ( ) ; for ( int i = 2 ; i * i <= X ; i ++ ) { if ( X % i == 0 ) { int p = 1 ; while ( X % i == 0 ) { X /= i ; p *= i ; } primePow . Add ( p ) ; } } if ( X > 1 ) { primePow . Add ( X ) ; } return primePow ; } static int getSum ( int [ ] ar ) { int sum = 0 ; foreach ( int i in ar ) { sum += i ; } return sum ; } static int getMinSum ( int pos , int [ ] arr , List < int > primePow ) { if ( pos == primePow . Count ) { return getSum ( arr ) ; } int res = int . MaxValue ; for ( int i = 0 ; i < arr . Length ; i ++ ) { arr [ i ] *= primePow [ pos ] ; res = Math . Min ( res , getMinSum ( pos + 1 , arr , primePow ) ) ; arr [ i ] /= primePow [ pos ] ; } return res ; } static int minimumSumWithGivenLCM ( int k , int x ) { List < int > primePow = primePower ( x ) ; int n = primePow . Count ; int sum = 0 ; if ( n <= k ) { foreach ( int i in primePow ) { sum += i ; } sum += k - n ; } else { int [ ] arr = new int [ k ] ; for ( int l = 0 ; l < arr . Length ; l ++ ) arr [ l ] = 1 ; sum = getMinSum ( 0 , arr , primePow ) ; } return sum ; } public static void Main ( String [ ] args ) { int k = 3 , x = 210 ; Console . Write ( minimumSumWithGivenLCM ( k , x ) ) ; } }
Minimum removal of subsequences of distinct consecutive characters required to empty a given string | C # program to implement the above approach ; Function to count minimum operations required to make the string an empty string ; Stores count of 1 s by removing consecutive distinct subsequence ; Stores count of 0 s by removing consecutive distinct subsequence ; Stores length of str ; Traverse the string ; If current character is 0 ; Update cntOne ; Update cntZero ; If current character is 1 ; Update cntZero ; Update cntOne ; Driver code
using System ; class GFG { static int findMinOperationsReqEmpStr ( String str ) { int cntOne = 0 ; int cntZero = 0 ; int N = str . Length ; for ( int i = 0 ; i < N ; i ++ ) { if ( str [ i ] == '0' ) { if ( cntOne != 0 ) { cntOne -- ; } cntZero ++ ; } else { if ( cntZero != 0 ) { cntZero -- ; } cntOne ++ ; } } return ( cntOne + cntZero ) ; } public static void Main ( String [ ] args ) { String str = "0100100111" ; Console . Write ( findMinOperationsReqEmpStr ( str ) ) ; } }
Minimum number of flips required such that the last cell of matrix can be reached from any other cell | C # program for the above approach ; Function to calculate the minimum number of flips required ; Dimensions of [ , ] mat ; Initialize answer ; Count all ' D ' s in the last row ; Count all ' R ' s in the last column ; Print answer ; Driver Code ; Given matrix ; Function call ; Print answer
using System ; class GFG { public static int countChanges ( char [ , ] mat ) { int n = mat . GetLength ( 0 ) ; int m = mat . GetLength ( 1 ) ; int ans = 0 ; for ( int j = 0 ; j < m - 1 ; j ++ ) { if ( mat [ n - 1 , j ] != ' R ' ) ans ++ ; } for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( mat [ i , m - 1 ] != ' D ' ) ans ++ ; } return ans ; } public static void Main ( String [ ] args ) { char [ , ] arr = { { ' R ' , ' R ' , ' R ' , ' D ' } , { ' D ' , ' D ' , ' D ' , ' R ' } , { ' R ' , ' D ' , ' R ' , ' F ' } } ; int cnt = countChanges ( arr ) ; Console . WriteLine ( cnt ) ; } }
Check if a binary tree is sorted level | C # program to determine whether binary tree is level sorted or not . ; Structure of a tree node . ; Function to create new tree node . ; Function to determine if given binary tree is level sorted or not . ; to store maximum value of previous level . ; to store minimum value of current level . ; to store maximum value of current level . ; to store number of nodes in current level . ; queue to perform level order traversal . ; find number of nodes in current level . ; traverse current level and find minimum and maximum value of this level . ; if minimum value of this level is not greater than maximum value of previous level then given tree is not level sorted . ; maximum value of this level is previous maximum value for next level . ; Driver Code ; 1 / 4 \ 6 / \ 8 9 / \ 12 10
using System ; using System . Collections . Generic ; class GFG { public class Node { public int key ; public Node left , right ; } static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . key = key ; temp . left = null ; temp . right = null ; return temp ; } static int isSorted ( Node root ) { int prevMax = int . MinValue ; int minval ; int maxval ; int levelSize ; Queue < Node > q = new Queue < Node > ( ) ; q . Enqueue ( root ) ; while ( q . Count != 0 ) { levelSize = q . Count ; minval = int . MaxValue ; maxval = int . MinValue ; while ( levelSize > 0 ) { root = q . Peek ( ) ; q . Dequeue ( ) ; levelSize -- ; minval = Math . Min ( minval , root . key ) ; maxval = Math . Max ( maxval , root . key ) ; if ( root . left != null ) q . Enqueue ( root . left ) ; if ( root . right != null ) q . Enqueue ( root . right ) ; } if ( minval <= prevMax ) return 0 ; prevMax = maxval ; } return 1 ; } public static void Main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 4 ) ; root . left . right = newNode ( 6 ) ; root . left . right . left = newNode ( 8 ) ; root . left . right . right = newNode ( 9 ) ; root . left . right . left . left = newNode ( 12 ) ; root . left . right . right . right = newNode ( 10 ) ; if ( isSorted ( root ) == 1 ) Console . WriteLine ( " Sorted " ) ; else Console . WriteLine ( " Not ▁ sorted " ) ; } }
Smallest subsequence having GCD equal to GCD of given array | C # program to implement the above approach ; Function to calculate gcd of two numbers ; Function to print the smallest subsequence that satisfies the condition ; Stores gcd of the array . ; Traverse the given array ; Update gcdArr ; Traverse the given array . ; If current element equal to gcd of array . ; Generate all possible pairs . ; If gcd of current pair equal to gcdArr ; Print current pair of the array ; Driver Code
using System ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static void printSmallSub ( int [ ] arr , int N ) { int gcdArr = 0 ; for ( int i = 0 ; i < N ; i ++ ) { gcdArr = gcd ( gcdArr , arr [ i ] ) ; } for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == gcdArr ) { Console . Write ( arr [ i ] + " ▁ " ) ; return ; } } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { if ( gcd ( arr [ i ] , arr [ j ] ) == gcdArr ) { Console . Write ( arr [ i ] + " ▁ " + arr [ j ] ) ; return ; } } } } public static void Main ( ) { int [ ] arr = { 4 , 6 , 12 } ; int N = arr . Length ; printSmallSub ( arr , N ) ; } }
Count pairs in an array whose product is composite number | C # program to implement the above approach ; Function to get all the prime numbers in the range [ 1 , X ] ; Stores the bool value to check if a number is prime or not ; Mark all non prime numbers as false ; If i is prime number ; Mark j as a composite number ; Function to get the count of pairs whose product is a composite number ; Stores the bool value to check if a number is prime or not ; Stores the count of 1 s ; Stores the count of prime numbers ; Traverse the given array . ; Stores count of pairs whose product is not a composite number ; Stores the count of pairs whose product is composite number ; Driver code
using System ; class GFG { public static int X = 1000000 ; public static bool [ ] getPrimeNum ( ) { bool [ ] isPrime = new bool [ X ] ; for ( int i = 0 ; i < X ; i ++ ) isPrime [ i ] = true ; isPrime [ 0 ] = false ; isPrime [ 1 ] = false ; for ( int i = 2 ; i * i <= X ; i ++ ) { if ( isPrime [ i ] == true ) { for ( int j = i * i ; j < X ; j += i ) { isPrime [ j ] = false ; } } } return isPrime ; } public static int cntPairs ( int [ ] arr , int N ) { bool [ ] isPrime = getPrimeNum ( ) ; int cntOne = 0 ; int cntPrime = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 1 ) { cntOne += 1 ; } else if ( isPrime [ i ] ) { cntPrime += 1 ; } } int cntNonComp = 0 ; cntNonComp = cntPrime * cntOne + cntOne * ( cntOne - 1 ) / 2 ; int res = 0 ; res = N * ( N - 1 ) / 2 - cntNonComp ; return res ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 2 , 2 , 8 } ; int N = arr . Length ; Console . WriteLine ( cntPairs ( arr , N ) ) ; } }