dhyuti-n/xlcost-variable-roles huggingface dataset:
C++ program:
#include <bits/stdc++.h>
#define N  5
using namespace std ; int after_rev ( vector < int > v ) { int mini = 0 , count = 0 ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) { count += v [ i ] ; if ( count > 0 ) count = 0 ; if ( mini > count ) mini = count ; } return mini ; } void print ( int arr [ N ] ) { int sum = 0 ; for ( int i = 0 ; i < N ; i += 2 ) sum += arr [ i ] ; vector < int > v1 , v2 ; for ( int i = 0 ; i + 1 < N ; i += 2 ) v1 . push_back ( arr [ i + 1 ] - arr [ i ] ) ; for ( int i = 1 ; i + 1 < N ; i += 2 ) v2 . push_back ( arr [ i ] - arr [ i + 1 ] ) ; int change = min ( after_rev ( v1 ) , after_rev ( v2 ) ) ; if ( change < 0 ) sum += change ; cout << sum << endl ; } int main ( ) { int arr [ N ] = { 0 , 1 , 4 , 3 } ; print ( arr ) ; return 0 ; }

Index/key variables: N, i
Accumulator variables: count, i, stdc, sum
Iterator variables: i
C++ program:
#include <bits/stdc++.h>
using namespace std ; int moduloEquality ( int A [ ] , int B [ ] , int n , int m ) { map < int , int > mapA , mapB ; for ( int i = 0 ; i < n ; i ++ ) { mapA [ A [ i ] ] ++ ; mapB [ B [ i ] ] ++ ; } set < int > possibleValues ; int FirstElement = B [ 0 ] ; for ( int i = 0 ; i < n ; i ++ ) { int cur = A [ i ] ; possibleValues . insert ( cur > FirstElement ? m - cur + FirstElement : FirstElement - cur ) ; } int ans = INT_MAX ; for ( auto it : possibleValues ) { bool possible = true ; for ( auto it2 : mapA ) { if ( it2 . second != mapB [ ( it2 . first + it ) % m ] ) { possible = false ; break ; } } if ( possible ) { ans = min ( ans , it ) ; } } return ans ; } int main ( ) { int n = 4 ; int m = 3 ; int A [ ] = { 0 , 0 , 2 , 1 } ; int B [ ] = { 2 , 0 , 1 , 1 } ; cout << moduloEquality ( A , B , n , m ) << endl ; return 0 ; }

Index/key variables: i
Accumulator variables: i, stdc
Iterator variables: i, it, it2
Boolean variables: possible
C++ program:
#include <bits/stdc++.h>
using namespace std ; int countCyclicShifts ( string & S , int n ) { int aux [ n ] = { 0 } ; for ( int i = 0 ; i < n ; ++ i ) { if ( S [ i ] == ' ( ' ) aux [ i ] = 1 ; else aux [ i ] = -1 ; } int mn = aux [ 0 ] ; for ( int i = 1 ; i < n ; ++ i ) { aux [ i ] += aux [ i - 1 ] ; mn = min ( mn , aux [ i ] ) ; } if ( aux [ n - 1 ] != 0 ) return 0 ; int count = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( aux [ i ] == mn ) count ++ ; } return count ; } int main ( ) { string S = " ) ( ) ( " ; int N = S . length ( ) ; cout << countCyclicShifts ( S , N ) ; return 0 ; }

Index/key variables: i, n
Accumulator variables: count, stdc
Iterator variables: i
C++ program:
#include <bits/stdc++.h>
using namespace std ; int maxProfit ( int * prices , int n ) { int profit = 0 , currentDay = n - 1 ; while ( currentDay > 0 ) { int day = currentDay - 1 ; while ( day >= 0 && ( prices [ currentDay ] > prices [ day ] ) ) { profit += ( prices [ currentDay ] - prices [ day ] ) ; day -- ; } currentDay = day ; } return profit ; } int main ( ) { int prices [ ] = { 2 , 3 , 5 } ; int N = sizeof ( prices ) / sizeof ( prices [ 0 ] ) ; cout << maxProfit ( prices , N ) ; return 0 ; }

Index/key variables: currentDay, day
Accumulator variables: day, profit, stdc
C++ program:
#include <bits/stdc++.h>
using namespace std ; bool isOddStringPalindrome ( string str , int n ) { int oddStringSize = n / 2 ; bool lengthOdd = ( ( oddStringSize % 2 == 1 ) ? true : false ) ; stack < char > s ; int i = 1 ; int c = 0 ; while ( i < n && c < oddStringSize / 2 ) { s . push ( str [ i ] ) ; i += 2 ; c ++ ; } if ( lengthOdd ) i = i + 2 ; while ( i < n && s . size ( ) > 0 ) { if ( s . top ( ) == str [ i ] ) s . pop ( ) ; else break ; i = i + 2 ; } if ( s . size ( ) == 0 ) return true ; return false ; } int main ( ) { int N = 10 ; string s = " aeafacafae " ; if ( isOddStringPalindrome ( s , N ) ) cout << " Yes STRNEWLINE " ; else cout << " No STRNEWLINE " ; return 0 ; }

Index/key variables: i
Accumulator variables: c, i, stdc
C++ program:
#include <bits/stdc++.h>
using namespace std ; bool isOddStringPalindrome ( string str , int n ) { int left , right ; if ( n % 2 == 0 ) { left = 1 ; right = n - 1 ; } else { left = 1 ; right = n - 2 ; } while ( left < n && right >= 0 && left < right ) { if ( str [ left ] != str [ right ] ) return false ; left += 2 ; right -= 2 ; } return true ; } int main ( ) { int n = 10 ; string s = " aeafacafae " ; if ( isOddStringPalindrome ( s , n ) ) cout << " Yes STRNEWLINE " ; else cout << " No STRNEWLINE " ; return 0 ; }

Index/key variables: left, right
Accumulator variables: left, right, stdc
C++ program:
#include <bits/stdc++.h>
using namespace std ; void min_remove ( string str ) { int N = str . length ( ) ; int prefix_a [ N + 1 ] ; int prefix_b [ N + 1 ] ; int prefix_c [ N + 1 ] ; prefix_a [ 0 ] = 0 ; prefix_b [ 0 ] = 0 ; prefix_c [ 0 ] = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { prefix_a [ i ] = prefix_a [ i - 1 ] + ( str [ i - 1 ] == ' a ' ) ; prefix_b [ i ] = prefix_b [ i - 1 ] + ( str [ i - 1 ] == ' b ' ) ; prefix_c [ i ] = prefix_c [ i - 1 ] + ( str [ i - 1 ] == ' c ' ) ; } int maxi = INT_MIN ; for ( int i = 0 ; i <= N ; i ++ ) { for ( int j = i ; j <= N ; j ++ ) { maxi = max ( maxi , ( prefix_a [ i ] + ( prefix_b [ j ] - prefix_b [ i ] ) + ( prefix_c [ N ] - prefix_c [ j ] ) ) ) ; } } cout << ( N - maxi ) << endl ; } int main ( ) { string str = " aaaabaaxccac " ; min_remove ( str ) ; return 0 ; }

Index/key variables: N, i, j
Accumulator variables: i, j, stdc
Iterator variables: i, j
C++ program:
#include <bits/stdc++.h>
using namespace std ; void createArray ( int n , int s ) { if ( 2 * n <= s ) { for ( int i = 0 ; i < n - 1 ; i ++ ) { cout << "2" << " ▁ " ; s -= 2 ; } cout << s << endl ; cout << "1" << endl ; } else cout << " - 1" << endl ; } int main ( ) { int N = 1 ; int S = 4 ; createArray ( N , S ) ; }

Accumulator variables: i, s, stdc
Iterator variables: i
C++ program:
#include using namespace std ; int maximum_partition ( string str ) { int i = 0 , j = 0 , k = 0 ; int c = 0 , r = 0 ; unordered_map m ; for ( i = str . length ( ) - 1 ; i >= 0 ; i -- ) { if ( m [ str [ i ] ] == 0 ) { m [ str [ i ] ] = i ; } } i = 0 ; k = m [ str [ i ] ] ; for ( i = 0 ; i < str . length ( ) ; i ++ ) { if ( i <= k ) { c = c + 1 ; k = max ( k , m [ str [ i ] ] ) ; } else { r = r + 1 ; c = 1 ; k = max ( k , m [ str [ i ] ] ) ; } } if ( c != 0 ) { r = r + 1 ; } return r ; } int main ( ) { string str = " ababcbacadefegdehijhklij " ; cout << maximum_partition ( str ) ; }

Index/key variables: i
Accumulator variables: i
Iterator variables: i
C++ program:
#include <bits/stdc++.h>
using namespace std ; int findAns ( vector < vector < int > > mat , int x , int y , int n , int m ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( mat [ i ] [ j ] == 1 ) { ans += abs ( x - i ) + abs ( y - j ) ; } } } return ans ; } int main ( ) { vector < vector < int > > mat = { { 1 , 0 , 0 , 0 } , { 0 , 1 , 0 , 1 } , { 1 , 0 , 1 , 1 } } ; int x = 0 , y = 2 ; cout << findAns ( mat , x , y , mat . size ( ) , mat [ 0 ] . size ( ) ) << endl ; return 0 ; }

Index/key variables: i, j
Accumulator variables: ans, i, j, stdc
Iterator variables: i, j
C++ program:
#include <bits/stdc++.h>
using namespace std ; void countReversebitonic ( int arr [ ] , int n ) { int c = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { int temp = arr [ i ] , f = 0 ; if ( j == i ) { c ++ ; continue ; } int k = i + 1 ; while ( temp > arr [ k ] && k <= j ) { temp = arr [ k ] ; k ++ ; } if ( k > j ) { c ++ ; f = 2 ; } while ( temp < arr [ k ] && k <= j && f != 2 ) { temp = arr [ k ] ; k ++ ; } if ( k > j && f != 2 ) { c ++ ; f = 0 ; } } } cout << c << endl ; } int main ( ) { int arr [ ] = { 2 , 3 , 1 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; countReversebitonic ( arr , N ) ; }

Index/key variables: i, k
Accumulator variables: c, i, j, k, stdc
Iterator variables: i, j
C++ program:
#include <bits/stdc++.h>
using namespace std ; int longestSequence ( int n , int arr [ ] ) { int l = 0 , r = n - 1 ; int prev = INT_MIN ; int ans = 0 ; while ( l <= r ) { if ( arr [ l ] > prev && arr [ r ] > prev ) { if ( arr [ l ] < arr [ r ] ) { ans += 1 ; prev = arr [ l ] ; l += 1 ; } else { ans += 1 ; prev = arr [ r ] ; r -= 1 ; } } else if ( arr [ l ] > prev ) { ans += 1 ; prev = arr [ l ] ; l += 1 ; } else if ( arr [ r ] > prev ) { ans += 1 ; prev = arr [ r ] ; r -= 1 ; } else { break ; } } return ans ; } int main ( ) { int arr [ ] = { 3 , 5 , 1 , 4 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << longestSequence ( n , arr ) ; return 0 ; }

Index/key variables: l, r
Accumulator variables: ans, l, r, stdc
C++ program:
#include <bits/stdc++.h>
using namespace std ; int solve ( int n , int m , int x , int y , int d ) { int top = min ( d , x - 1 ) ; int down = min ( d , n - x ) ; int left = min ( d , y - 1 ) ; int right = min ( d , m - y ) ; int quad1 = top * left ; int quad2 = left * down ; int quad3 = down * right ; int quad4 = right * top ; int totalsq = quad1 + quad2 + quad3 + quad4 ; int singleBlocks = top + down + left + right + 1 ; return totalsq + singleBlocks ; } int main ( ) { int n , m , x , y , d ; n = 10 , m = 10 ; x = 7 , y = 8 ; d = 4 ; d -- ; cout << solve ( n , m , x , y , d ) ; }

Accumulator variables: d, stdc
C++ program:
#include <bits/stdc++.h>
using namespace std ; void getSubsequence ( vector < int > & arr , int n ) { vector < int > req ; req . push_back ( arr [ 0 ] ) ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( arr [ i ] > arr [ i + 1 ] && arr [ i ] > arr [ i - 1 ] ) req . push_back ( arr [ i ] ) ; else if ( arr [ i ] < arr [ i + 1 ] && arr [ i ] < arr [ i - 1 ] ) req . push_back ( arr [ i ] ) ; } req . push_back ( arr [ n - 1 ] ) ; cout << req . size ( ) << endl ; for ( auto x : req ) cout << x << " ▁ " ; } int main ( ) { vector < int > arr = { 1 , 2 , 5 , 3 , 6 , 7 , 4 } ; int n = arr . size ( ) ; getSubsequence ( arr , n ) ; return 0 ; }

Index/key variables: i
Accumulator variables: i, stdc
Iterator variables: i, x
C++ program:
#include <bits/stdc++.h>
using namespace std ; #define M  3
#define N  3
int minchanges ( int mat [ N ] [ M ] ) { int count = 0 ; int left = 0 , right = N + M - 2 ; while ( left < right ) { unordered_map < int , int > mp ; int totalsize = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( i + j == left ) { mp [ mat [ i ] [ j ] ] ++ ; totalsize ++ ; } else if ( i + j == right ) { mp [ mat [ i ] [ j ] ] ++ ; totalsize ++ ; } } } unordered_map < int , int > :: iterator itr = mp . begin ( ) ; int changes = 0 ; for ( ; itr != mp . end ( ) ; itr ++ ) changes = max ( changes , itr -> second ) ; count += totalsize - changes ; left ++ ; right -- ; } return count ; } int main ( ) { int mat [ ] [ M ] = { { 1 , 4 , 1 } , { 2 , 5 , 3 } , { 1 , 3 , 1 } } ; cout << minchanges ( mat ) ; return 0 ; }

Index/key variables: M, N, i, j
Accumulator variables: count, i, itr, j, left, right, stdc, totalsize
Iterator variables: i, j
C++ program:
#include <bits/stdc++.h>
using namespace std ; int CountLongestSubarrays ( int arr [ ] , int n , int k ) { int i , s = 0 ; for ( i = 0 ; i < n ; ++ i ) { s += arr [ i ] ; } if ( s % k ) { return 1 ; } else { int ini = 0 ; while ( ini < n && arr [ ini ] % k == 0 ) { ++ ini ; } int final = n - 1 ; while ( final >= 0 && arr [ final ] % k == 0 ) { -- final ; } int len , sum = 0 , count = 0 ; if ( ini == n ) { return -1 ; } else { len = max ( n - 1 - ini , final ) ; } for ( i = 0 ; i < len ; i ++ ) { sum += arr [ i ] ; } if ( sum % k != 0 ) { count ++ ; } for ( i = len ; i < n ; i ++ ) { sum = sum + arr [ i ] ; sum = sum - arr [ i - len ] ; if ( sum % k != 0 ) { count ++ ; } } return count ; } } int main ( ) { int arr [ ] = { 3 , 2 , 2 , 2 , 3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 3 ; cout << CountLongestSubarrays ( arr , n , k ) ; return 0 ; }

Index/key variables: final, i, ini
Accumulator variables: count, i, s, stdc, sum
Iterator variables: i
C++ program:
#include <bits/stdc++.h>
using namespace std ; const int MAXN = 1e6 + 1 ; int freq [ MAXN ] = { 0 } ; int mul [ MAXN ] = { 0 } ; int cnt [ MAXN ] = { 0 } ; int nC3 ( int n ) { if ( n < 3 ) return 0 ; return ( n * ( n - 1 ) * ( n - 2 ) ) / 6 ; } void count_triplet ( vector < int > arr , int N , int K ) { for ( int i = 0 ; i < N ; i ++ ) { freq [ arr [ i ] ] ++ ; } for ( int i = 1 ; i <= 1000000 ; i ++ ) { for ( int j = i ; j <= 1000000 ; j += i ) { mul [ i ] += freq [ j ] ; } cnt [ i ] = nC3 ( mul [ i ] ) ; } for ( int i = 1000000 ; i >= 1 ; i -- ) { for ( int j = 2 * i ; j <= 1000000 ; j += i ) { cnt [ i ] -= cnt [ j ] ; } } cout << " Number ▁ of ▁ triplets ▁ " << " with ▁ GCD ▁ " << K ; cout << " ▁ are ▁ " << cnt [ K ] ; } int main ( ) { vector < int > arr = { 1 , 7 , 12 , 6 , 15 , 9 } ; int N = 6 , K = 3 ; count_triplet ( arr , N , K ) ; return 0 ; }

Index/key variables: K, MAXN, i, j
Accumulator variables: i, j, stdc
Iterator variables: i, j
C++ program:
#include <bits/stdc++.h>
using namespace std ; bool isSubseqPossible ( int arr [ ] , int N , int K ) { int i ; int odd = 0 , even = 0 ; for ( i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 1 ) odd ++ ; else even ++ ; } if ( odd == 0 || ( even == 0 && K % 2 == 0 ) ) return false ; return true ; } int main ( ) { int arr [ ] = { 2 , 3 , 5 , 7 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 3 ; cout << ( isSubseqPossible ( arr , N , K ) ? " Yes " : " No " ) ; return 0 ; }

Index/key variables: i
Accumulator variables: even, i, odd, stdc
Iterator variables: i
C++ program:
#include <bits/stdc++.h>
using namespace std ; void print_path ( int N , int jump , int coin ) { if ( jump > coin || jump * ( N - 1 ) < coin ) { cout << " - 1" << endl ; } else { int pos = 1 ; while ( jump > 0 ) { int tmp = min ( N - 1 , coin - ( jump - 1 ) ) ; if ( pos + tmp <= N ) { pos += tmp ; } else { pos -= tmp ; } cout << pos << " ▁ " ; coin -= tmp ; jump -= 1 ; } } } int main ( ) { int N = 5 , K = 4 , M = 12 ; print_path ( N , K , M ) ; return 0 ; }

Accumulator variables: coin, jump, pos, stdc
C++ program:
#include <bits/stdc++.h>
using namespace std ; #define INF  1e9
void findMinHike ( vector < int > arr , int n ) { arr . insert ( arr . begin ( ) , INF ) ; arr . push_back ( INF ) ; vector < int > hike ( n + 2 , 0 ) ; for ( int i = 1 ; i <= n ; i ++ ) { if ( arr [ i - 1 ] >= arr [ i ] && arr [ i ] <= arr [ i + 1 ] ) { hike [ i ] = 1 ; } } for ( int i = 1 ; i <= n ; i ++ ) { if ( arr [ i - 1 ] < arr [ i ] && arr [ i ] <= arr [ i + 1 ] ) { hike [ i ] = hike [ i - 1 ] + 1 ; } } for ( int i = 1 ; i <= n ; i ++ ) { if ( arr [ i - 1 ] >= arr [ i ] && arr [ i ] > arr [ i + 1 ] ) { hike [ i ] = hike [ i + 1 ] + 1 ; } } for ( int i = 1 ; i <= n ; i ++ ) { if ( arr [ i - 1 ] < arr [ i ] && arr [ i ] > arr [ i + 1 ] ) { hike [ i ] = max ( hike [ i - 1 ] , hike [ i + 1 ] ) + 1 ; } } for ( int i = 1 ; i <= n ; i ++ ) { cout << hike [ i ] << " ▁ " ; } } int main ( ) { vector < int > arr = { 5 , 3 , 4 , 2 , 1 , 6 } ; findMinHike ( arr , arr . size ( ) ) ; return 0 ; }

Index/key variables: i
Accumulator variables: i, stdc
Iterator variables: i
C++ program:
#include <bits/stdc++.h>
using namespace std ; void minimumCost ( int arr [ ] , int cost [ ] , int N ) { vector < vector < int > > dp ( N , vector < int > ( 3 ) ) ; dp [ 0 ] [ 0 ] = 0 ; dp [ 0 ] [ 1 ] = cost [ 0 ] ; dp [ 0 ] [ 2 ] = cost [ 0 ] * 2 ; for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 0 ; j < 3 ; j ++ ) { int minimum = 1e6 ; if ( j + arr [ i ] != arr [ i - 1 ] ) minimum = min ( minimum , dp [ i - 1 ] [ 0 ] ) ; if ( j + arr [ i ] != arr [ i - 1 ] + 1 ) minimum = min ( minimum , dp [ i - 1 ] [ 1 ] ) ; if ( j + arr [ i ] != arr [ i - 1 ] + 2 ) minimum = min ( minimum , dp [ i - 1 ] [ 2 ] ) ; dp [ i ] [ j ] = j * cost [ i ] + minimum ; } } int ans = 1e6 ; for ( int i = 0 ; i < 3 ; i ++ ) ans = min ( ans , dp [ N - 1 ] [ i ] ) ; cout << ans << " STRNEWLINE " ; } int main ( ) { int arr [ ] = { 1 , 1 , 2 , 2 , 3 , 4 } ; int cost [ ] = { 3 , 2 , 5 , 4 , 2 , 1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; minimumCost ( arr , cost , N ) ; return 0 ; }

Index/key variables: i, j
Accumulator variables: i, j, stdc
Iterator variables: i, j
C++ program:
#include <bits/stdc++.h>
using namespace std ; void maximumSumSubarray ( int arr [ ] , int n ) { unordered_map < int , int > mp ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] >= 0 && mp . find ( arr [ i ] ) == mp . end ( ) ) mp [ arr [ i ] ] = 1 ; } int first = 0 ; int last = 0 ; int ans = 0 ; int INF = 1e6 ; for ( auto i : mp ) { int mx = i . first ; int curr = 0 ; int curr_start ; for ( int j = 0 ; j < n ; j ++ ) { if ( curr == 0 ) curr_start = j ; int val = arr [ j ] > mx ? - INF : arr [ j ] ; curr += val ; if ( curr < 0 ) curr = 0 ; if ( curr > ans ) { ans = curr ; first = curr_start ; last = j ; } } } cout << first + 1 << " ▁ " << last + 1 ; } int main ( ) { int arr [ ] = { 5 , -2 , 10 , -1 , 4 } ; int size = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; maximumSumSubarray ( arr , size ) ; return 0 ; }

Index/key variables: i, j
Accumulator variables: curr, i, j, stdc
Iterator variables: i, j
C++ program:
#include <bits/stdc++.h>
using namespace std ; int dp [ 100 ] [ 100 ] [ 100 ] ; int maxProfit ( int profit [ ] , int weight [ ] , int n , int max_W , int max_E ) { for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= max_W ; j ++ ) { for ( int k = 1 ; k <= max_E ; k ++ ) { if ( j >= weight [ i - 1 ] ) { dp [ i ] [ j ] [ k ] = max ( dp [ i - 1 ] [ j ] [ k ] , dp [ i - 1 ] [ j - weight [ i - 1 ] ] [ k - 1 ] + profit [ i - 1 ] ) ; } else { dp [ i ] [ j ] [ k ] = dp [ i - 1 ] [ j ] [ k ] ; } } } } return dp [ n ] [ max_W ] [ max_E ] ; } int main ( ) { memset ( dp , 0 , sizeof ( dp ) ) ; int n = 5 ; int profit [ ] = { 2 , 7 , 1 , 5 , 3 } ; int weight [ ] = { 2 , 5 , 2 , 3 , 4 } ; int max_weight = 8 ; int max_element = 2 ; cout << maxProfit ( profit , weight , n , max_weight , max_element ) << " STRNEWLINE " ; return 0 ; }

Index/key variables: i, j, k, max_E, max_W, n
Accumulator variables: i, j, k, stdc
Iterator variables: i, j, k
C++ program:
#include <bits/stdc++.h>
using namespace std ; const int MAXX = 10000 , INF = 1e9 ; vector < vector < pair < int , int > > > adj ( MAXX * 10 + 3 ) ; vector < int > dist ( MAXX * 10 + 3 , INF ) ; int even ( int x ) { return x * 10 + 2 ; } int odd ( int x ) { return x * 10 + 1 ; } void addEdge ( int a , int b , int cost ) { adj [ even ( a ) ] . push_back ( { odd ( b ) , cost } ) ; adj [ odd ( a ) ] . push_back ( { even ( b ) , cost } ) ; adj [ odd ( b ) ] . push_back ( { even ( a ) , cost } ) ; adj [ even ( b ) ] . push_back ( { odd ( a ) , cost } ) ; } int dijkstra ( int source , int destination ) { priority_queue < pair < int , int > , vector < pair < int , int > > , greater < pair < int , int > > > pq ; pq . push ( { 0 , even ( source ) } ) ; dist [ even ( source ) ] = 0 ; while ( ! pq . empty ( ) ) { int u = pq . top ( ) . second ; pq . pop ( ) ; for ( pair < int , int > p : adj [ u ] ) { int v = p . first ; int c = p . second ; if ( dist [ u ] + c < dist [ v ] ) { dist [ v ] = dist [ u ] + c ; pq . push ( { dist [ v ] , v } ) ; } } } return dist [ even ( destination ) ] ; } int main ( ) { int n = 5 , m = 6 ; addEdge ( 1 , 2 , 1 ) ; addEdge ( 2 , 3 , 2 ) ; addEdge ( 2 , 5 , 15 ) ; addEdge ( 3 , 5 , 1 ) ; addEdge ( 3 , 4 , 4 ) ; addEdge ( 5 , 4 , 3 ) ; int source = 1 ; int destination = n ; int ans = dijkstra ( source , destination ) ; if ( ans == INF ) cout << " - 1" << " STRNEWLINE " ; else cout << ans << " STRNEWLINE " ; return 0 ; }

Index/key variables: u, v
Accumulator variables: stdc
Iterator variables: p
C++ program:
#include <bits/stdc++.h>
using namespace std ; int make_xor_half ( vector < int > & arr ) { int sum = 0 , xr = 0 ; for ( int a : arr ) { sum += a ; xr ^= a ; } if ( 2 * xr == sum ) return -1 ; if ( xr == 0 ) { arr . push_back ( sum ) ; return 1 ; } arr . push_back ( xr ) ; arr . push_back ( sum + xr ) ; return 2 ; } int main ( ) { int N = 7 ; vector < int > nums = { 3 , 4 , 7 , 1 , 2 , 5 , 6 } ; int count = make_xor_half ( nums ) ; if ( count == -1 ) cout << " - 1" << endl ; else if ( count == 1 ) cout << nums [ N ] << endl ; else cout << nums [ N ] << " ▁ " << nums [ N + 1 ] << endl ; return 0 ; }

Index/key variables: N
Accumulator variables: stdc, sum, xr
Iterator variables: a
C++ program:
#include <bits/stdc++.h>
using namespace std ; int count_triangles ( int a , int b , int c , int d ) { int ans = 0 ; for ( int x = a ; x <= b ; ++ x ) { int num_greater_than_d = max ( d , c + x ) - max ( d , b + x - 1 ) ; ans += num_greater_than_d * ( d - c + 1 ) ; int r = min ( max ( c , c + x ) , d ) - c ; int l = min ( max ( c , b + x - 1 ) , d ) - c ; int x1 = ( r * ( r + 1 ) ) / 2 ; int x2 = ( l * ( l + 1 ) ) / 2 ; ans += x1 - x2 ; } return ans ; } int main ( ) { int a = 2 , b = 3 , c = 4 , d = 5 ; cout << count_triangles ( a , b , c , d ) << endl ; return 0 ; }

Accumulator variables: ans, stdc
Iterator variables: x
C++ program:
#include <bits/stdc++.h>
using namespace std ; int minFlip ( string s , int n , int k , char a [ ] , int p ) { bool allowed [ 26 ] = { 0 } ; for ( int i = 0 ; i < p ; i ++ ) { allowed [ a [ i ] - ' a ' ] = true ; } char freq [ k ] [ 26 ] ; for ( int i = 0 ; i < k ; i ++ ) for ( int j = 0 ; j < 26 ; j ++ ) freq [ i ] [ j ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) { freq [ i % k ] [ s [ i ] - ' a ' ] += 1 ; } int ans = 0 ; int totalpositions = n / k ; for ( int i = 0 ; i < k ; i ++ ) { int maxfrequency = 0 ; for ( int j = 0 ; j < 26 ; j ++ ) { if ( freq [ i ] [ j ] > maxfrequency and allowed [ j ] == true ) maxfrequency = freq [ i ] [ j ] ; } ans += ( totalpositions - maxfrequency + ( ( i % k < n % k ) ? 1 : 0 ) ) ; } cout << ans << endl ; } int main ( ) { string S = " nihsiakyt " ; int n = S . length ( ) ; int K = 3 ; char A [ 5 ] = { ' n ' , ' i ' , ' p ' , ' s ' , ' q ' } ; int p = sizeof ( A ) / sizeof ( A [ 0 ] ) ; minFlip ( S , n , K , A , p ) ; return 0 ; }

Index/key variables: i, j, k
Accumulator variables: ans, i, j, stdc
Iterator variables: i, j
C++ program:
#include <bits/stdc++.h>
using namespace std ; void colorRequired ( int arr [ ] , int n ) { bool all_same = true ; bool one_adjacent_same = false ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] != arr [ i + 1 ] ) { all_same = false ; } if ( arr [ i ] == arr [ i + 1 ] ) { one_adjacent_same = true ; } } if ( all_same == true ) { cout << 1 << endl ; return ; } if ( n % 2 == 0 one_adjacent_same == true ) { cout << 2 << endl ; return ; } cout << 3 << endl ; } int main ( ) { int arr [ ] = { 1 , 2 , 1 , 1 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; colorRequired ( arr , n ) ; return 0 ; }

Index/key variables: i
Accumulator variables: i, stdc
Iterator variables: i
Boolean variables: all_same, one_adjacent_same
C++ program:
#include <iostream>
using namespace std ; int maxRemainingSum ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; int i = 0 ; int j = n - 1 ; int result = 0 ; while ( i < j ) { if ( arr [ i ] < arr [ j ] ) { sum -= arr [ i ] ; i ++ ; } else { sum -= arr [ j ] ; j -- ; } result += sum ; } return result ; } int main ( ) { int arr [ ] = { 3 , 1 , 7 , 2 , 1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maxRemainingSum ( arr , N ) ; return 0 ; }

Index/key variables: i, j
Accumulator variables: i, j, result, sum
Iterator variables: i
C++ program:
#include <iostream>
using namespace std ; void printKParts ( int N , int K ) { if ( N % K == 0 ) { for ( int i = 1 ; i < K ; i ++ ) cout << "1 , ▁ " ; cout << N - ( K - 1 ) << endl ; } else { if ( K == 2 ) { cout << " Not ▁ Possible " << endl ; return ; } for ( int i = 1 ; i < K - 1 ; i ++ ) cout << 1 << " , ▁ " ; cout << 2 << " , ▁ " << N - K << endl ; } } int main ( ) { int N = 18 , K = 5 ; printKParts ( N , K ) ; return 0 ; }

Accumulator variables: i
Iterator variables: i