dhyuti-n/xlcost-variable-roles huggingface dataset:
PHP program:
< ? php function primeSubarrays ( $ A , $ n ) { $ max_val = pow ( 10 , 5 ) ; $ prime = array_fill ( 0 , $ max_val + 1 , true ) ; $ prime [ 0 ] = false ; $ prime [ 1 ] = false ; for ( $ p = 2 ; $ p * $ p <= $ max_val ; $ p ++ ) { if ( $ prime [ $ p ] == true ) { for ( $ i = $ p * 2 ; $ i <= $ max_val ; $ i += $ p ) $ prime [ $ i ] = false ; } } $ cnt = 0 ; for ( $ i = 0 ; $ i < $ n - 1 ; ++ $ i ) { $ val = $ A [ $ i ] ; for ( $ j = $ i + 1 ; $ j < $ n ; ++ $ j ) { $ val += $ A [ $ j ] ; if ( $ prime [ $ val ] ) ++ $ cnt ; } } return $ cnt ; } $ A = array ( 1 , 2 , 3 , 4 , 5 ) ; $ n = count ( $ A ) ; echo primeSubarrays ( $ A , $ n ) ; ? >

Index/key variables: i, j, p, val
Accumulator variables: i, p, val
Iterator variables: i, j, p
PHP program:
< ? php function repeated_digit ( $ n ) { $ c = 10 ; $ a = array_fill ( 0 , $ c , 0 ) ; while ( $ n > 0 ) { $ d = $ n % 10 ; if ( $ a [ $ d ] > 0 ) { return 0 ; } $ a [ $ d ] ++ ; $ n = ( int ) ( $ n / 10 ) ; } return 1 ; } function calculate ( $ L , $ R ) { $ answer = 0 ; for ( $ i = $ L ; $ i <= $ R ; $ i ++ ) { $ answer += repeated_digit ( $ i ) ; } return $ answer ; } $ L = 1 ; $ R = 100 ; echo calculate ( $ L , $ R ) ; ? >

Index/key variables: d
Accumulator variables: answer, i
Iterator variables: i
PHP program:
< ? php function countMinSwaps ( $ st ) { $ min_swaps = 0 ; $ odd_0 = 0 ; $ even_0 = 0 ; $ odd_1 = 0 ; $ even_1 = 0 ; $ n = strlen ( $ st ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ i % 2 == 0 ) { if ( $ st [ $ i ] == '1' ) { $ even_1 ++ ; } else { $ even_0 ++ ; } } else { if ( $ st [ $ i ] == '1' ) { $ odd_1 ++ ; } else { $ odd_0 ++ ; } } } $ cnt_swaps_1 = min ( $ even_0 , $ odd_1 ) ; $ cnt_swaps_2 = min ( $ even_1 , $ odd_0 ) ; return min ( $ cnt_swaps_1 , $ cnt_swaps_2 ) ; } $ st = "000111" ; echo ( countMinSwaps ( $ st ) ) ; ? >

Index/key variables: i
Accumulator variables: even_0, even_1, i, odd_0, odd_1
Iterator variables: i
PHP program:
< ? php function maxSubArraySum ( $ a , $ size ) { $ max_so_far = PHP_INT_MIN ; $ max_ending_here = 0 ; for ( $ i = 0 ; $ i < $ size ; $ i ++ ) { $ max_ending_here = $ max_ending_here + $ a [ $ i ] ; if ( $ max_so_far < $ max_ending_here ) $ max_so_far = $ max_ending_here ; if ( $ max_ending_here < 0 ) $ max_ending_here = 0 ; } return $ max_so_far ; } $ a = array ( -2 , -3 , 4 , -1 , -2 , 1 , 5 , -3 ) ; $ n = count ( $ a ) ; $ max_sum = maxSubArraySum ( $ a , $ n ) ; echo " Maximum ▁ contiguous ▁ sum ▁ is ▁ " , $ max_sum ; ? >

Index/key variables: i
Accumulator variables: i
Iterator variables: i
PHP program:
< ? php < ? php function maxSubArraySum ( & $ a , $ size ) { $ max_so_far = $ a [ 0 ] ; $ max_ending_here = 0 ; for ( $ i = 0 ; $ i < $ size ; $ i ++ ) { $ max_ending_here = $ max_ending_here + $ a [ $ i ] ; if ( $ max_ending_here < 0 ) $ max_ending_here = 0 ; else if ( $ max_so_far < $ max_ending_here ) $ max_so_far = $ max_ending_here ; } return $ max_so_far ; ? >

Index/key variables: i
Accumulator variables: i
Iterator variables: i
PHP program:
< ? php function findMinRooms ( $ slots , $ n , $ m ) { $ counts = array_fill ( 0 , $ m , 0 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = 0 ; $ j < $ m ; $ j ++ ) if ( $ slots [ $ i ] [ $ j ] == '1' ) $ counts [ $ j ] ++ ; return max ( $ counts ) ; } $ n = 3 ; $ m = 7 ; $ slots = array ( "0101011" , "0011001" , "0110111" ) ; echo findMinRooms ( $ slots , $ n , $ m ) ; ? >

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i, j
PHP program:
< ? php < ? php function maxSubArraySum ( $ a , $ size ) { $ max_so_far = $ a [ 0 ] ; $ curr_max = $ a [ 0 ] ; for ( $ i = 1 ; $ i < $ size ; $ i ++ ) { $ curr_max = max ( $ a [ $ i ] , $ curr_max + $ a [ $ i ] ) ; $ max_so_far = max ( $ max_so_far , $ curr_max ) ; } return $ max_so_far ; } $ a = array ( -2 , -3 , 4 , -1 , -2 , 1 , 5 , -3 ) ; $ n = sizeof ( $ a ) ; $ max_sum = maxSubArraySum ( $ a , $ n ) ; echo " Maximum ▁ contiguous ▁ sum ▁ is ▁ " . $ max_sum ; ? >

Index/key variables: i
Accumulator variables: i
Iterator variables: i
PHP program:
< ? php function maxSubArraySum ( $ a , $ size ) { $ max_so_far = PHP_INT_MIN ; $ max_ending_here = 0 ; $ start = 0 ; $ end = 0 ; $ s = 0 ; for ( $ i = 0 ; $ i < $ size ; $ i ++ ) { $ max_ending_here += $ a [ $ i ] ; if ( $ max_so_far < $ max_ending_here ) { $ max_so_far = $ max_ending_here ; $ start = $ s ; $ end = $ i ; } if ( $ max_ending_here < 0 ) { $ max_ending_here = 0 ; $ s = $ i + 1 ; } } echo " Maximum ▁ contiguous ▁ sum ▁ is ▁ " . $ max_so_far . " STRNEWLINE " ; echo " Starting ▁ index ▁ " . $ start . " " . STRNEWLINE TABSYMBOL TABSYMBOL TABSYMBOL " Ending index " ▁ . ▁ $ end ▁ . ▁ " " } $ a = array ( -2 , -3 , 4 , -1 , -2 , 1 , 5 , -3 ) ; $ n = sizeof ( $ a ) ; $ max_sum = maxSubArraySum ( $ a , $ n ) ; ? >

Index/key variables: i
Accumulator variables: i, max_ending_here
Iterator variables: i
PHP program:
< ? php function countCurrency ( $ amount ) { $ notes = array ( 2000 , 500 , 200 , 100 , 50 , 20 , 10 , 5 , 1 ) ; $ noteCounter = array ( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ) ; for ( $ i = 0 ; $ i < 9 ; $ i ++ ) { if ( $ amount >= $ notes [ $ i ] ) { $ noteCounter [ $ i ] = intval ( $ amount / $ notes [ $ i ] ) ; $ amount = $ amount - $ noteCounter [ $ i ] * $ notes [ $ i ] ; } } echo ( " Currency ▁ Count ▁ - > " . " STRNEWLINE " ) ; for ( $ i = 0 ; $ i < 9 ; $ i ++ ) { if ( $ noteCounter [ $ i ] != 0 ) { echo ( $ notes [ $ i ] . " : " ▁ . ▁ $ noteCounter [ $ i ] ▁ . ▁ " " } } } $ amount = 868 ; countCurrency ( $ amount ) ; ? >

Index/key variables: i
Accumulator variables: i
Iterator variables: i
PHP program:
< ? php function NextFit ( $ blockSize , $ m , $ processSize , $ n ) { $ allocation = array_fill ( 0 , $ n , -1 ) ; $ j = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { while ( $ j < $ m ) { if ( $ blockSize [ $ j ] >= $ processSize [ $ i ] ) { $ allocation [ $ i ] = $ j ; $ blockSize [ $ j ] -= $ processSize [ $ i ] ; break ; } $ j = ( $ j + 1 ) % $ m ; } } echo " Process No . Process Size Block no . " ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { echo " " . ( $ i ▁ + ▁ 1 ) . " " . $ processSize [ $ i ] . " " if ( $ allocation [ $ i ] != -1 ) echo ( $ allocation [ $ i ] + 1 ) ; else echo " Not ▁ Allocated " ; echo " STRNEWLINE " ; } } $ blockSize = array ( 5 , 10 , 20 ) ; $ processSize = array ( 10 , 20 , 5 ) ; $ m = count ( $ blockSize ) ; $ n = count ( $ processSize ) ; NextFit ( $ blockSize , $ m , $ processSize , $ n ) ; ? >

Index/key variables: i, j
Accumulator variables: i
Iterator variables: i
PHP program:
< ? php function findMinAvgSubarray ( $ arr , $ n , $ k ) { if ( $ n < $ k ) return ; $ res_index = 0 ; $ curr_sum = 0 ; for ( $ i = 0 ; $ i < $ k ; $ i ++ ) $ curr_sum += $ arr [ $ i ] ; $ min_sum = $ curr_sum ; for ( $ i = $ k ; $ i < $ n ; $ i ++ ) { $ curr_sum += $ arr [ $ i ] - $ arr [ $ i - $ k ] ; if ( $ curr_sum < $ min_sum ) { $ min_sum = $ curr_sum ; $ res_index = ( $ i - $ k + 1 ) ; } } echo " Subarray between [ " ▁ , $ res _ index ▁ , ▁ " , " ▁ , $ res _ index ▁ + ▁ $ k ▁ - ▁ 1 , ▁ " ] has minimum average " ; } $ arr = array ( 3 , 7 , 90 , 20 , 10 , 50 , 40 ) ; $ k = 3 ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; findMinAvgSubarray ( $ arr , $ n , $ k ) ; return 0 ; ? >

Index/key variables: i
Accumulator variables: curr_sum, i
Iterator variables: i
PHP program:
< ? php $ seg = array ( 6 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 ) ; function computeSegment ( $ x ) { global $ seg ; if ( $ x == 0 ) return $ seg [ 0 ] ; $ count = 0 ; while ( $ x ) { $ count += $ seg [ $ x % 10 ] ; $ x = ( int ) $ x / 10 ; } return $ count ; } function elementMinSegment ( $ arr , $ n ) { $ minseg = computeSegment ( $ arr [ 0 ] ) ; $ minindex = 0 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ temp = computeSegment ( $ arr [ $ i ] ) ; if ( $ temp < $ minseg ) { $ minseg = $ temp ; $ minindex = $ i ; } } return $ arr [ $ minindex ] ; } $ arr = array ( 489 , 206 , 745 , 123 , 756 ) ; $ n = sizeof ( $ arr ) ; echo elementMinSegment ( $ arr , $ n ) , " " ; ? >

Index/key variables: i, minindex
Accumulator variables: count, i
Iterator variables: i
PHP program:
< ? php function findLargest ( $ m , $ s ) { if ( $ s == 0 ) { if ( ( $ m == 1 ) == true ) echo " Largest ▁ number ▁ is ▁ " , 0 ; else echo " Not ▁ possible " ; return ; } if ( $ s > 9 * $ m ) { echo " Not ▁ possible " ; return ; } for ( $ i = 0 ; $ i < $ m ; $ i ++ ) { if ( $ s >= 9 ) { $ res [ $ i ] = 9 ; $ s -= 9 ; } else { $ res [ $ i ] = $ s ; $ s = 0 ; } } echo " Largest ▁ number ▁ is ▁ " ; for ( $ i = 0 ; $ i < $ m ; $ i ++ ) echo $ res [ $ i ] ; } $ s = 9 ; $ m = 2 ; findLargest ( $ m , $ s ) ; ? >

Index/key variables: i
Accumulator variables: i, s
Iterator variables: i
PHP program:
< ? php function minJumps ( $ arr , $ l , $ h ) { if ( $ h == $ l ) return 0 ; if ( $ arr [ $ l ] == 0 ) return INT_MAX ; $ min = 999999 ; for ( $ i = $ l + 1 ; $ i <= $ h && $ i <= $ l + $ arr [ $ l ] ; $ i ++ ) { $ jumps = minJumps ( $ arr , $ i , $ h ) ; if ( $ jumps != 999999 && $ jumps + 1 < $ min ) $ min = $ jumps + 1 ; } return $ min ; } $ arr = array ( 1 , 3 , 6 , 3 , 2 , 3 , 6 , 8 , 9 , 5 ) ; $ n = count ( $ arr ) ; echo " Minimum ▁ number ▁ of ▁ jumps ▁ to ▁ reach ▁ " . " end ▁ is ▁ " . minJumps ( $ arr , 0 , $ n - 1 ) ; ? >

Index/key variables: l
Accumulator variables: i
Iterator variables: i
PHP program:
< ? php function minJumps ( $ arr , $ n ) { $ jumps [ $ n ] = array ( ) ; $ min ; $ jumps [ $ n - 1 ] = array ( 0 ) ; for ( $ i = $ n - 2 ; $ i >= 0 ; $ i -- ) { if ( $ arr [ $ i ] == 0 ) $ jumps [ $ i ] = PHP_INT_MAX ; else if ( $ arr [ $ i ] >= ( $ n - $ i ) - 1 ) $ jumps [ $ i ] = 1 ; else { $ min = PHP_INT_MAX ; for ( $ j = $ i + 1 ; $ j < $ n && $ j <= $ arr [ $ i ] + $ i ; $ j ++ ) { if ( $ min > $ jumps [ $ j ] ) $ min = $ jumps [ $ j ] ; } if ( $ min != PHP_INT_MAX ) $ jumps [ $ i ] = $ min + 1 ; else $ jumps [ $ i ] = $ min ; } } return $ jumps [ 0 ] ; } $ arr = array ( 1 , 3 , 6 , 1 , 0 , 9 ) ; $ size = sizeof ( $ arr ) ; echo " Minimum ▁ number ▁ of ▁ jumps ▁ to ▁ reach " , " ▁ end ▁ is ▁ " , minJumps ( $ arr , $ size ) ; ? >

Index/key variables: i, j, n
Accumulator variables: i, j
Iterator variables: i, j
PHP program:
< ? php function smallestSubWithSum ( $ arr , $ n , $ x ) { $ min_len = $ n + 1 ; for ( $ start = 0 ; $ start < $ n ; $ start ++ ) { $ curr_sum = $ arr [ $ start ] ; if ( $ curr_sum > $ x ) return 1 ; for ( $ end = $ start + 1 ; $ end < $ n ; $ end ++ ) { $ curr_sum += $ arr [ $ end ] ; if ( $ curr_sum > $ x && ( $ end - $ start + 1 ) < $ min_len ) $ min_len = ( $ end - $ start + 1 ) ; } } return $ min_len ; } $ arr1 = array ( 1 , 4 , 45 , 6 , 10 , 19 ) ; $ x = 51 ; $ n1 = sizeof ( $ arr1 ) ; $ res1 = smallestSubWithSum ( $ arr1 , $ n1 , $ x ) ; if ( ( $ res1 == $ n1 + 1 ) == true ) echo " Not ▁ possible STRNEWLINE " ; else echo $ res1 , " STRNEWLINE " ; $ arr2 = array ( 1 , 10 , 5 , 2 , 7 ) ; $ n2 = sizeof ( $ arr2 ) ; $ x = 9 ; $ res2 = smallestSubWithSum ( $ arr2 , $ n2 , $ x ) ; if ( ( $ res2 == $ n2 + 1 ) == true ) echo " Not ▁ possible STRNEWLINE " ; else echo $ res2 , " STRNEWLINE " ; $ arr3 = array ( 1 , 11 , 100 , 1 , 0 , 200 , 3 , 2 , 1 , 250 ) ; $ n3 = sizeof ( $ arr3 ) ; $ x = 280 ; $ res3 = smallestSubWithSum ( $ arr3 , $ n3 , $ x ) ; if ( ( $ res3 == $ n3 + 1 ) == true ) echo " Not ▁ possible STRNEWLINE " ; else echo $ res3 , " STRNEWLINE " ; ? >

Index/key variables: end, start
Accumulator variables: curr_sum, end, start
Iterator variables: end, start
PHP program:
< ? php function smallestSubWithSum ( $ arr , $ n , $ x ) { $ curr_sum = 0 ; $ min_len = $ n + 1 ; $ start = 0 ; $ end = 0 ; while ( $ end < $ n ) { while ( $ curr_sum <= $ x && $ end < $ n ) $ curr_sum += $ arr [ $ end ++ ] ; while ( $ curr_sum > $ x && $ start < $ n ) { if ( $ end - $ start < $ min_len ) $ min_len = $ end - $ start ; $ curr_sum -= $ arr [ $ start ++ ] ; } } return $ min_len ; } $ arr1 = array ( 1 , 4 , 45 , 6 , 10 , 19 ) ; $ x = 51 ; $ n1 = sizeof ( $ arr1 ) ; $ res1 = smallestSubWithSum ( $ arr1 , $ n1 , $ x ) ; if ( $ res1 == $ n1 + 1 ) echo " Not ▁ possible STRNEWLINE " ; else echo $ res1 , " STRNEWLINE " ; $ arr2 = array ( 1 , 10 , 5 , 2 , 7 ) ; $ n2 = sizeof ( $ arr2 ) ; $ x = 9 ; $ res2 = smallestSubWithSum ( $ arr2 , $ n2 , $ x ) ; if ( $ res2 == $ n2 + 1 ) echo " Not ▁ possible STRNEWLINE " ; else echo $ res2 , " STRNEWLINE " ; $ arr3 = array ( 1 , 11 , 100 , 1 , 0 , 200 , 3 , 2 , 1 , 250 ) ; $ n3 = sizeof ( $ arr3 ) ; $ x = 280 ; $ res3 = smallestSubWithSum ( $ arr3 , $ n3 , $ x ) ; if ( $ res3 == $ n3 + 1 ) echo " Not ▁ possible STRNEWLINE " ; else echo $ res3 , " STRNEWLINE " ; ? >

Accumulator variables: curr_sum, end, start
PHP program:
< ? php function countMinOperations ( $ target , $ n ) { $ result = 0 ; while ( 1 ) { $ zero_count = 0 ; $ i = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ target [ $ i ] & 1 ) break ; else if ( $ target [ $ i ] == 0 ) $ zero_count ++ ; } if ( $ zero_count == $ n ) return $ result ; if ( $ i == $ n ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) $ target [ $ j ] = $ target [ $ j ] / 2 ; $ result ++ ; } for ( $ j = $ i ; $ j < $ n ; $ j ++ ) { if ( $ target [ $ j ] & 1 ) { $ target [ $ j ] -- ; $ result ++ ; } } } } $ arr = array ( 16 , 16 , 16 ) ; $ n = sizeof ( $ arr ) ; echo " Minimum ▁ number ▁ of ▁ steps ▁ required ▁ to ▁ STRNEWLINE " . " get ▁ the ▁ given ▁ target ▁ array ▁ is ▁ " . countMinOperations ( $ arr , $ n ) ; ? >

Index/key variables: i, j
Accumulator variables: i, j, result, zero_count
Iterator variables: i, j
PHP program:
< ? php function findMinOps ( $ arr , $ n ) { $ ans = 1 ; for ( $ i = 0 , $ j = $ n - 1 ; $ i <= $ j { if ( $ arr [ $ i ] == $ arr [ $ j ] ) { $ i ++ ; $ j -- ; } else if ( $ arr [ $ i ] > $ arr [ $ j ] ) { $ j -- ; $ arr [ $ j ] += $ arr [ $ j + 1 ] ; $ ans ++ ; } else { $ i ++ ; $ arr [ $ i ] += $ arr [ $ i - 1 ] ; $ ans ++ ; } } return $ ans ; } $ arr [ ] = array ( 1 , 4 , 5 , 9 , 1 ) ; $ n = sizeof ( $ arr ) ; echo " Count ▁ of ▁ minimum ▁ operations ▁ is ▁ " , findMinOps ( $ arr , $ n ) ; ? >

Index/key variables: i, j
Accumulator variables: ans, i, j
Iterator variables: i
PHP program:
< ? php function findSmallest ( $ arr , $ n ) { $ res = 1 ; for ( $ i = 0 ; $ i < $ n and $ arr [ $ i ] <= $ res ; $ i ++ ) $ res = $ res + $ arr [ $ i ] ; return $ res ; } $ arr1 = array ( 1 , 3 , 4 , 5 ) ; $ n1 = count ( $ arr1 ) ; echo findSmallest ( $ arr1 , $ n1 ) , " STRNEWLINE " ; $ arr2 = array ( 1 , 2 , 6 , 10 , 11 , 15 ) ; $ n2 = count ( $ arr2 ) ; echo findSmallest ( $ arr2 , $ n2 ) , " STRNEWLINE " ; $ arr3 = array ( 1 , 1 , 1 , 1 ) ; $ n3 = count ( $ arr3 ) ; echo findSmallest ( $ arr3 , $ n3 ) , " STRNEWLINE " ; $ arr4 = array ( 1 , 1 , 3 , 4 ) ; $ n4 = count ( $ arr4 ) ; echo findSmallest ( $ arr4 , $ n4 ) ; ? >

Index/key variables: i
Accumulator variables: i
Iterator variables: i
PHP program:
< ? php function findMinDiff ( $ arr , $ n ) { $ diff = PHP_INT_MAX ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) if ( abs ( $ arr [ $ i ] - $ arr [ $ j ] ) < $ diff ) $ diff = abs ( $ arr [ $ i ] - $ arr [ $ j ] ) ; return $ diff ; } $ arr = array ( 1 , 5 , 3 , 19 , 18 , 25 ) ; $ n = sizeof ( $ arr ) ; echo " Minimum ▁ difference ▁ is ▁ " , findMinDiff ( $ arr , $ n ) ; ? >

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i, j
PHP program:
< ? php function findMinDiff ( $ arr , $ n ) { sort ( $ arr ) ; $ diff = PHP_INT_MAX ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) if ( $ arr [ $ i + 1 ] - $ arr [ $ i ] < $ diff ) $ diff = $ arr [ $ i + 1 ] - $ arr [ $ i ] ; return $ diff ; } $ arr = array ( 1 , 5 , 3 , 19 , 18 , 25 ) ; $ n = sizeof ( $ arr ) ; echo " Minimum ▁ difference ▁ is ▁ " , findMinDiff ( $ arr , $ n ) ; ? >

Index/key variables: i
Accumulator variables: i
Iterator variables: i
PHP program:
< ? php $ a = 2 ; $ b = 10 ; $ size = abs ( $ b - $ a ) + 1 ; $ array = array_fill ( 0 , $ size , 0 ) ; for ( $ i = $ a ; $ i <= $ b ; $ i ++ ) if ( $ i % 2 == 0 $ i % 5 == 0 ) $ array [ $ i - $ a ] = 1 ; echo " MULTIPLES ▁ of ▁ 2 ▁ and ▁ 5 : STRNEWLINE " ; for ( $ i = $ a ; $ i <= $ b ; $ i ++ ) if ( $ array [ $ i - $ a ] == 1 ) echo $ i . " " ; ? >

Accumulator variables: i
Iterator variables: i
PHP program:
< ? php function longestCommonSum ( $ arr1 , $ arr2 , $ n ) { $ maxLen = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ sum1 = 0 ; $ sum2 = 0 ; for ( $ j = $ i ; $ j < $ n ; $ j ++ ) { $ sum1 += $ arr1 [ $ j ] ; $ sum2 += $ arr2 [ $ j ] ; if ( $ sum1 == $ sum2 ) { $ len = $ j - $ i + 1 ; if ( $ len > $ maxLen ) $ maxLen = $ len ; } } } return $ maxLen ; } $ arr1 = array ( 0 , 1 , 0 , 1 , 1 , 1 , 1 ) ; $ arr2 = array ( 1 , 1 , 1 , 1 , 1 , 0 , 1 ) ; $ n = sizeof ( $ arr1 ) ; echo " Length ▁ of ▁ the ▁ longest ▁ common ▁ span ▁ " . " with ▁ same ▁ " , " sum ▁ is ▁ " , longestCommonSum ( $ arr1 , $ arr2 , $ n ) ; ? >

Index/key variables: j
Accumulator variables: i, j, sum1, sum2
Iterator variables: i, j
PHP program:
< ? php $ NA = -1 ; function moveToEnd ( & $ mPlusN , $ size ) { global $ NA ; $ j = $ size - 1 ; for ( $ i = $ size - 1 ; $ i >= 0 ; $ i -- ) if ( $ mPlusN [ $ i ] != $ NA ) { $ mPlusN [ $ j ] = $ mPlusN [ $ i ] ; $ j -- ; } } function merge ( & $ mPlusN , & $ N , $ m , $ n ) { $ i = $ n ; $ j = 0 ; $ k = 0 ; while ( $ k < ( $ m + $ n ) ) { if ( ( $ j == $ n ) || ( $ i < ( $ m + $ n ) && $ mPlusN [ $ i ] <= $ N [ $ j ] ) ) { $ mPlusN [ $ k ] = $ mPlusN [ $ i ] ; $ k ++ ; $ i ++ ; } else { $ mPlusN [ $ k ] = $ N [ $ j ] ; $ k ++ ; $ j ++ ; } } } function printArray ( & $ arr , $ size ) { for ( $ i = 0 ; $ i < $ size ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; echo " STRNEWLINE " ; } $ mPlusN = array ( 2 , 8 , $ NA , $ NA , $ NA , 13 , $ NA , 15 , 20 ) ; $ N = array ( 5 , 7 , 9 , 25 ) ; $ n = sizeof ( $ N ) ; $ m = sizeof ( $ mPlusN ) - $ n ; moveToEnd ( $ mPlusN , $ m + $ n ) ; merge ( $ mPlusN , $ N , $ m , $ n ) ; printArray ( $ mPlusN , $ m + $ n ) ; ? >

Index/key variables: i, j, k
Accumulator variables: i, j, k
Iterator variables: i
PHP program:
< ? php function sortedAfterSwap ( $ A , $ B , $ n ) { for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) { if ( $ B [ $ i ] ) { $ j = $ i ; while ( $ B [ $ j ] ) $ j ++ ; sort ( $ A ) ; $ i = $ j ; } } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ A [ $ i ] != $ i + 1 ) return false ; } return true ; } $ A = array ( 1 , 2 , 5 , 3 , 4 , 6 ) ; $ B = array ( 0 , 1 , 1 , 1 , 0 ) ; $ n = count ( $ A ) ; if ( sortedAfterSwap ( $ A , $ B , $ n ) ) echo " A ▁ can ▁ be ▁ sorted STRNEWLINE " ; else echo " A ▁ can ▁ not ▁ be ▁ sorted STRNEWLINE " ; ? >

Index/key variables: i, j
Accumulator variables: i, j
Iterator variables: i
PHP program:
< ? php function sortedAfterSwap ( & $ A , & $ B , $ n ) { for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) { if ( $ B [ $ i ] ) { if ( $ A [ $ i ] != $ i + 1 ) { $ t = $ A [ $ i ] ; $ A [ $ i ] = $ A [ $ i + 1 ] ; $ A [ $ i + 1 ] = $ t ; } } } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ A [ $ i ] != $ i + 1 ) return false ; } return true ; } $ A = array ( 1 , 2 , 5 , 3 , 4 , 6 ) ; $ B = array ( 0 , 1 , 1 , 1 , 0 ) ; $ n = sizeof ( $ A ) ; if ( sortedAfterSwap ( $ A , $ B , $ n ) ) echo " A ▁ can ▁ be ▁ sorted STRNEWLINE " ; else echo " A ▁ can ▁ not ▁ be ▁ sorted STRNEWLINE " ; ? >

Index/key variables: i
Accumulator variables: i
Iterator variables: i
PHP program:
< ? php function segregate0and1 ( $ arr , $ n ) { $ type0 = 0 ; $ type1 = $ n - 1 ; while ( $ type0 < $ type1 ) { if ( $ arr [ $ type0 ] == 1 ) { $ temp = $ arr [ $ type0 ] ; $ arr [ $ type0 ] = $ arr [ $ type1 ] ; $ arr [ $ type1 ] = $ temp ; $ type1 -- ; } else { $ type0 ++ ; } } return $ arr ; } $ arr = array ( 1 , 1 , 1 , 0 , 1 , 0 , 0 , 1 , 1 , 1 , 1 ) ; $ n = count ( $ arr ) ; $ arr1 = segregate0and1 ( $ arr , $ n ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr1 [ $ i ] . " ▁ " ; ? >

Index/key variables: i, type0, type1
Accumulator variables: i, type0, type1
Iterator variables: i
PHP program:
< ? php function getInvCount ( & $ arr , $ n ) { $ inv_count = 0 ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) if ( $ arr [ $ i ] > $ arr [ $ j ] ) $ inv_count ++ ; return $ inv_count ; } $ arr = array ( 1 , 20 , 6 , 4 , 5 ) ; $ n = sizeof ( $ arr ) ; echo " Number ▁ of ▁ inversions ▁ are ▁ " , getInvCount ( $ arr , $ n ) ; ? >

Index/key variables: i, j
Accumulator variables: i, inv_count, j
Iterator variables: i, j
PHP program:
< ? php function minAbsSumPair ( $ arr , $ arr_size ) { $ inv_count = 0 ; if ( $ arr_size < 2 ) { echo " Invalid ▁ Input " ; return ; } $ min_l = 0 ; $ min_r = 1 ; $ min_sum = $ arr [ 0 ] + $ arr [ 1 ] ; for ( $ l = 0 ; $ l < $ arr_size - 1 ; $ l ++ ) { for ( $ r = $ l + 1 ; $ r < $ arr_size ; $ r ++ ) { $ sum = $ arr [ $ l ] + $ arr [ $ r ] ; if ( abs ( $ min_sum ) > abs ( $ sum ) ) { $ min_sum = $ sum ; $ min_l = $ l ; $ min_r = $ r ; } } } echo " The ▁ two ▁ elements ▁ whose ▁ sum ▁ is ▁ minimum ▁ are ▁ " . $ arr [ $ min_l ] . " ▁ and ▁ " . $ arr [ $ min_r ] ; } $ arr = array ( 1 , 60 , -10 , 70 , -80 , 85 ) ; minAbsSumPair ( $ arr , 6 ) ; ? >

Index/key variables: l, min_l, min_r, r
Accumulator variables: l, r
Iterator variables: l, r
PHP program:
< ? php function increasing ( $ a , $ n ) { for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) if ( $ a [ $ i ] >= $ a [ $ i + 1 ] ) return false ; return true ; } function decreasing ( $ a , $ n ) { for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) if ( $ a [ $ i ] < $ a [ $ i + 1 ] ) return false ; return true ; } function shortestUnsorted ( $ a , $ n ) { if ( increasing ( $ a , $ n ) == true || decreasing ( $ a , $ n ) == true ) return 0 ; else return 3 ; } $ ar = array ( 7 , 9 , 10 , 8 , 11 ) ; $ n = sizeof ( $ ar ) ; echo shortestUnsorted ( $ ar , $ n ) ; ? >

Index/key variables: i
Accumulator variables: i
Iterator variables: i
PHP program:
< ? php function printUnion ( $ arr1 , $ arr2 , $ m , $ n ) { $ i = 0 ; $ j = 0 ; while ( $ i < $ m && $ j < $ n ) { if ( $ arr1 [ $ i ] < $ arr2 [ $ j ] ) echo ( $ arr1 [ $ i ++ ] . " ▁ " ) ; else if ( $ arr2 [ $ j ] < $ arr1 [ $ i ] ) echo ( $ arr2 [ $ j ++ ] . " ▁ " ) ; else { echo ( $ arr2 [ $ j ++ ] . " " ) ; $ i ++ ; } } while ( $ i < $ m ) echo ( $ arr1 [ $ i ++ ] . " ▁ " ) ; while ( $ j < $ n ) echo ( $ arr2 [ $ j ++ ] . " ▁ " ) ; } $ arr1 = array ( 1 , 2 , 4 , 5 , 6 ) ; $ arr2 = array ( 2 , 3 , 5 , 7 ) ; $ m = sizeof ( $ arr1 ) ; $ n = sizeof ( $ arr2 ) ; printUnion ( $ arr1 , $ arr2 , $ m , $ n ) ; ? >

Index/key variables: i, j
Accumulator variables: i, j
PHP program:
< ? php function printIntersection ( $ arr1 , $ arr2 , $ m , $ n ) { $ i = 0 ; $ j = 0 ; while ( $ i < $ m && $ j < $ n ) { if ( $ arr1 [ $ i ] < $ arr2 [ $ j ] ) $ i ++ ; else if ( $ arr2 [ $ j ] < $ arr1 [ $ i ] ) $ j ++ ; else { echo $ arr2 [ $ j ] , " " ; $ i ++ ; $ j ++ ; } } } $ arr1 = array ( 1 , 2 , 4 , 5 , 6 ) ; $ arr2 = array ( 2 , 3 , 5 , 7 ) ; $ m = count ( $ arr1 ) ; $ n = count ( $ arr2 ) ; printIntersection ( $ arr1 , $ arr2 , $ m , $ n ) ; ? >

Index/key variables: i, j
Accumulator variables: i, j
PHP program:
< ? php function printUnion ( $ arr1 , $ arr2 , $ m , $ n ) { if ( $ m > $ n ) { $ tempp = $ arr1 ; $ arr1 = $ arr2 ; $ arr2 = $ tempp ; $ temp = $ m ; $ m = $ n ; $ n = $ temp ; } sort ( $ arr1 ) ; for ( $ i = 0 ; $ i < $ m ; $ i ++ ) echo $ arr1 [ $ i ] . " ▁ " ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( binarySearch ( $ arr1 , 0 , $ m - 1 , $ arr2 [ $ i ] ) == -1 ) echo $ arr2 [ $ i ] . " ▁ " ; } function printIntersection ( $ arr1 , $ arr2 , $ m , $ n ) { if ( $ m > $ n ) { $ tempp = $ arr1 ; $ arr1 = $ arr2 ; $ arr2 = $ tempp ; $ temp = $ m ; $ m = $ n ; $ n = $ temp ; } sort ( $ arr1 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( binarySearch ( $ arr1 , 0 , $ m - 1 , $ arr2 [ $ i ] ) != -1 ) echo $ arr2 [ $ i ] . " ▁ " ; } function binarySearch ( $ arr , $ l , $ r , $ x ) { if ( $ r >= $ l ) { $ mid = ( int ) ( $ l + ( $ r - $ l ) / 2 ) ; if ( $ arr [ $ mid ] == $ x ) return $ mid ; if ( $ arr [ $ mid ] > $ x ) return binarySearch ( $ arr , $ l , $ mid - 1 , $ x ) ; return binarySearch ( $ arr , $ mid + 1 , $ r , $ x ) ; } return -1 ; } $ arr1 = array ( 7 , 1 , 5 , 2 , 3 , 6 ) ; $ arr2 = array ( 3 , 8 , 6 , 20 , 7 ) ; $ m = count ( $ arr1 ) ; $ n = count ( $ arr2 ) ; echo " Union ▁ of ▁ two ▁ arrays ▁ is ▁ STRNEWLINE " ; printUnion ( $ arr1 , $ arr2 , $ m , $ n ) ; echo " Intersection of two arrays is " ; printIntersection ( $ arr1 , $ arr2 , $ m , $ n ) ; ? >

Index/key variables: i, mid
Accumulator variables: i
Iterator variables: i