codeparrot/xlcost-text-to-code huggingface dataset:
Lower case to upper case | Converts a string to uppercase ; Driver code
function to_upper ( input ) { var result = new Array ( input . length ) ; for ( var i = 0 ; i < input . length ; i ++ ) if ( " " . charCodeAt ( 0 ) <= input [ i ] . charCodeAt ( 0 ) <= " " . charCodeAt ( 0 ) ) result [ i ] = String . fromCharCode ( input [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) + " " . charCodeAt ( 0 ) ) ; return result . join ( " " ) . toString ( ) ; } var str = " " ; document . write ( to_upper ( str ) ) ;
Longest Common Prefix using Character by Character Matching | A Function to find the string having the minimum length and returns that length ; A Function that returns the longest common prefix from the array of strings ; Our resultant string let current ; The current character ; Current character ( must be same in all strings to be a part of result ) ; Append to result ; Driver program to test above function
function findMinLength ( arr , n ) { let min = arr [ 0 ] . length ; for ( let i = 1 ; i < n ; i ++ ) { if ( arr [ i ] . length < min ) { min = arr [ i ] . length ; } } return ( min ) ; } function commonPrefix ( arr , n ) { let minlen = findMinLength ( arr , n ) ; let result = " " ; for ( let i = 0 ; i < minlen ; i ++ ) { current = arr [ 0 ] [ i ] ; for ( let j = 1 ; j < n ; j ++ ) { if ( arr [ j ] [ i ] != current ) { return result ; } } result += ( current ) ; } return ( result ) ; } let arr = [ " " , " " , " " , " " ] let n = arr . length ; let ans = commonPrefix ( arr , n ) ; if ( ans . length > 0 ) { document . write ( " " + ans ) ; } else { document . write ( " " ) ; }
Print Concatenation of Zig | Prints concatenation of all rows of str 's  Zig-Zag fashion ; Corner Case ( Only one row ) ; Find length of string ; Create an array of strings for all n rows ; Initialize index for array of strings arr [ ] ; True if we are moving down in rows , else false ; Traverse through given string ; append current character to current row ; If last row is reached , change direction to ' up ' ; If 1 st row is reached , change direction to ' down ' ; If direction is down , increment , else decrement ; Print concatenation of all rows ; Driver Code
function printZigZagConcat ( str , n ) { if ( n == 1 ) { document . write ( str ) ; return ; } let str1 = str . split ( " " ) ; let len = str . length ; let arr = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { arr [ i ] = " " ; } let row = 0 ; let down = true ; for ( let i = 0 ; i < len ; ++ i ) { arr [ row ] += ( str1 [ i ] ) ; if ( row == n - 1 ) { down = false ; } else if ( row == 0 ) { down = true ; } if ( down ) { row ++ ; } else { row -- ; } } for ( let i = 0 ; i < n ; ++ i ) { document . write ( arr [ i ] ) ; } } let str = " " ; let n = 3 ; printZigZagConcat ( str , n ) ;
Check if two given strings are isomorphic to each other | Javascript program to check if two strings are isomorphic ; Function returns true if str1 and str2 are isomorphic ; Length of both strings must be same for one to one corresponance ; To mark visited characters in str2 ; To store mapping of every character from str1 to that of str2 and Initialize all entries of map as - 1. ; Process all characters one by on ; If current character of str1 is seen first time in it . ; If current character of str2 is already seen , one to one mapping not possible ; Mark current character of str2 as visited ; Store mapping of current characters ; If this is not first appearance of current character in str1 , then check if previous appearance mapped to same character of str2 ; Driver code
let size = 256 ; function areIsomorphic ( str1 , str2 ) { let m = str1 . length ; let n = str2 . length ; if ( m != n ) return false ; let marked = new Array ( size ) ; for ( let i = 0 ; i < size ; i ++ ) marked [ i ] = false ; let map = new Array ( size ) ; map . fill ( 0 ) ; for ( let i = 0 ; i < size ; i ++ ) map [ i ] = - 1 ; for ( let i = 0 ; i < n ; i ++ ) { if ( map [ str1 [ i ] . charCodeAt ( ) ] == - 1 ) { if ( marked [ str2 [ i ] . charCodeAt ( ) ] == true ) return false ; marked [ str2 [ i ] . charCodeAt ( ) ] = true ; map [ str1 [ i ] . charCodeAt ( ) ] = str2 [ i ] . charCodeAt ( ) ; } else if ( map [ str1 [ i ] . charCodeAt ( ) ] != str2 [ i ] . charCodeAt ( ) ) return 0 ; } return 1 ; } let res = areIsomorphic ( " " , " " ) ; document . write ( res + " " ) ; res = areIsomorphic ( " " , " " ) ; document . write ( res ) ;
Check if two given strings are isomorphic to each other | Javascript program for the above approach ; This function returns true if str1 and str2 are isomorphic ; Length of both strings must be same for one to one correspondence ; For counting the previous appearances of character in both the strings ; Process all characters one by one ; For string to be isomorphic the previous counts of appearances of current character in both string must be same if it is not same we return false . ; Driver Code
let CHAR = 26 ; function isoMorphic ( str1 , str2 ) { let n = str1 . length ; let m = str2 . length ; if ( n != m ) return false ; let countChars1 = new Array ( CHAR ) ; countChars1 . fill ( 0 ) ; let countChars2 = new Array ( CHAR ) ; countChars2 . fill ( 0 ) ; for ( let i = 0 ; i < n ; i ++ ) { countChars1 [ str1 [ i ] . charCodeAt ( ) - ' ' ] ++ ; countChars2 [ str2 [ i ] . charCodeAt ( ) - ' ' ] ++ ; if ( countChars1 [ str1 [ i ] . charCodeAt ( ) - ' ' ] != countChars2 [ str2 [ i ] . charCodeAt ( ) - ' ' ] ) { return false ; } } return true ; } document . write ( isoMorphic ( " " , " " ) ? 1 + " " : 0 + " " ) ; document . write ( isoMorphic ( " " , " " ) ? 1 : 0 ) ;
Minimum insertions to form shortest palindrome | Returns true if a string str [ st . . end ] is palindrome ; Returns count of insertions on left side to make str [ ] a palindrome ; Find the largest prefix of given string that is palindrome . ; Characters after the palindromic prefix must be added at the beginning also to make the complete string palindrome ; Driver Code
function isPalin ( str , st , end ) { while ( st < end ) { if ( str [ st ] != str [ end ] ) return false ; st ++ ; end -- ; } return true ; } function findMinInsert ( str , n ) { for ( let i = n - 1 ; i >= 0 ; i -- ) { if ( isPalin ( str , 0 , i ) ) return ( n - i - 1 ) ; } } let Input = " " ; document . write ( findMinInsert ( Input , Input . length ) ) ;
Remove repeated digits in a given number | Javascript program to remove repeated digits ; Store first digits as previous digit ; Initialize power ; Iterate through all digits of n , note that the digits are processed from least significant digit to most significant digit . ; Store current digit ; Add the current digit to the beginning of result ; Update previous result and power ; Remove last digit from n ; Driver program
function removeRecur ( n ) { let prev_digit = n % 10 ; let pow = 10 ; let res = prev_digit ; while ( n > 0 ) { let curr_digit = n % 10 ; if ( curr_digit != prev_digit ) { res += curr_digit * pow ; prev_digit = curr_digit ; pow *= 10 ; } n = parseInt ( n / 10 , 10 ) ; } return res ; } let n = 12224 ; document . write ( removeRecur ( n ) ) ;
Write your own atoi ( ) | A simple JavaScript program for implementation of atoi ; if whitespaces then ignore . ; sign of number ; checking for valid input ; handling overflow test case ; Driver code ; Function call
function myAtoi ( str ) { var sign = 1 , base = 0 , i = 0 ; while ( str [ i ] == ' ' ) { i ++ ; } if ( str [ i ] == ' ' str [ i ] == ' ' ) { sign = 1 - 2 * ( str [ i ++ ] == ' ' ) ; } while ( str [ i ] >= ' ' && str [ i ] <= ' ' ) { if ( base > Number . MAX_VALUE / 10 || ( base == Number . MAX_VALUE / 10 && str [ i ] - ' ' > 7 ) ) { if ( sign == 1 ) return Number . MAX_VALUE ; else return Number . MAX_VALUE ; } base = 10 * base + ( str [ i ++ ] - ' ' ) ; } return base * sign ; } var str = " " ; var val = myAtoi ( str ) ; document . write ( " " , val ) ;
Check whether two strings are anagram of each other | Javascript program to check if two strings are anagrams of each other ; function to check if two strings are anagrams of each other ; Create a count array and initialize all values as 0 ; For each character in input strings , increment count in the corresponding count array ; If both strings are of different length . Removing this condition will make the program fail for strings like " aaca " and " aca " ; See if there is any non - zero value in count array ; Driver code ; Function call
let NO_OF_CHARS = 256 ; function areAnagram ( str1 , str2 ) { let count = new Array ( NO_OF_CHARS ) ; for ( let i = 0 ; i < NO_OF_CHARS ; i ++ ) { count [ i ] = 0 ; } let i ; for ( i = 0 ; i < str1 . length ; i ++ ) { count [ str1 [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; count [ str2 [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] -- ; } if ( str1 . length != str2 . length ) return false ; for ( i = 0 ; i < NO_OF_CHARS ; i ++ ) if ( count [ i ] != 0 ) { return false ; } return true ; } let str1 = " " . split ( " " ) ; let str2 = " " . split ( " " ) ; if ( areAnagram ( str1 , str2 ) ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ;
Length of the longest substring without repeating characters | This function returns true if all characters in str [ i . . j ] are distinct , otherwise returns false ; Note : Default values in visited are false ; Returns length of the longest substring with all distinct characters . ; Result ; Driver code
function areDistinct ( str , i , j ) { var visited = new [ 26 ] ; for ( var k = i ; k <= j ; k ++ ) { if ( visited [ str . charAt ( k ) - ' ' ] == true ) return false ; visited [ str . charAt ( k ) - ' ' ] = true ; } return true ; } function longestUniqueSubsttr ( str ) { var n = str . length ( ) ; var res = 0 ; for ( var i = 0 ; i < n ; i ++ ) for ( var j = i ; j < n ; j ++ ) if ( areDistinct ( str , i , j ) ) res = Math . max ( res , j - i + 1 ) ; return res ; } var str = " " ; document . write ( " " + str ) ; var len = longestUniqueSubsttr ( str ) ; document . write ( " " + " " + " " + len ) ;
Given a string , find its first non | this function return the index of first non - repeating character if found , or else it returns - 1 ; initializing all elements to - 1 ; sets all repeating characters to - 2 and non - repeating characters contain the index where they occur ; If this character is not - 1 or - 2 then it means that this character occurred only once so find the min index of all characters that occur only once , that 's our first index ; if res remains INT_MAX , it means there are no characters that repeat only once or the string is empty
function firstNonRepeating ( str ) { var fi = new Array ( 256 ) ; fi . fill ( - 1 ) ; for ( var i = 0 ; i < 256 ; i ++ ) fi [ i ] = - 1 ; for ( var i = 0 ; i < str . length ; i ++ ) { if ( fi [ str . charCodeAt ( i ) ] == - 1 ) { fi [ str . charCodeAt ( i ) ] = i ; } else { fi [ str . charCodeAt ( i ) ] = - 2 ; } } var res = Infinity ; for ( var i = 0 ; i < 256 ; i ++ ) { if ( fi [ i ] >= 0 ) res = Math . min ( res , fi [ i ] ) ; } if ( res == Infinity ) return - 1 ; else return res ; } var str ; str = " " ; var firstIndex = firstNonRepeating ( str ) ; if ( firstIndex === - 1 ) document . write ( " " ) ; else document . write ( " " + str . charAt ( firstIndex ) ) ;
Remove duplicates from a given string | Function to make the string unique ; loop to traverse the string and check for repeating chars using IndexOf ( ) method in Java ; character at i 'th index of s ; if c is present in str , it returns the index of c , else it returns - 1 ; adding c to str if - 1 is returned ; Input string with repeating chars
function unique ( s ) { let str = " " ; let len = s . length ; for ( let i = 0 ; i < len ; i ++ ) { let c = s [ i ] ; if ( str . indexOf ( c ) < 0 ) { str += c ; } } return str ; } let s = " " ; document . write ( unique ( s ) ) ;
Sum of all subsets whose sum is a Perfect Number from a given array | Function to check is a given number is a perfect number or not ; Stores the sum of its divisors ; Add all divisors of x to sum_div ; If the sum of divisors is equal to the given number , return true ; Otherwise , return false ; Function to find sum of all subsets from an array whose sum is a perfect number ; Prvar the current subset sum if it is a perfect number ; Check if sum is a perfect number or not ; Calculate sum of the subset including arr [ l ] ; Calculate sum of the subset excluding arr [ l ] ; Driver code
function isPerfect ( x ) { var sum_div = 1 ; for ( i = 2 ; i <= x / 2 ; ++ i ) { if ( x % i == 0 ) { sum_div += i ; } } if ( sum_div == x ) { return 1 ; } else return 0 ; } function subsetSum ( arr , l , r , sum ) { if ( l > r ) { if ( isPerfect ( sum ) != 0 ) { document . write ( sum + " " ) ; } return ; } subsetSum ( arr , l + 1 , r , sum + arr [ l ] ) ; subsetSum ( arr , l + 1 , r , sum ) ; } var arr = [ 5 , 4 , 6 ] ; var N = arr . length ; subsetSum ( arr , 0 , N - 1 , 0 ) ;
Print all possible ways to split an array into K subsets | Utility function to find all possible ways to split array into K subsets ; If count of elements in K subsets are greater than or equal to N ; If count of subsets formed is equal to K ; Print K subsets by splitting array into K subsets ; Print current subset ; If current element is the last element of the subset ; Otherwise ; If any subset is occupied , then push the element in that first ; Recursively do the same for remaining elements ; Backtrack ; Otherwise , push it in an empty subset and increase the subset count by 1 ; Break to avoid the case of going in other empty subsets , if available , and forming the same combination ; Function to to find all possible ways to split array into K subsets ; Stores K subset by splitting array into K subsets ; Size of each subset must be less than the number of elements ; Given array ; Given K ; Size of the array ; Prints all possible splits into subsets
function PartitionSub ( arr , i , N , K , nos , v ) { if ( i >= N ) { if ( nos == K ) { for ( let x = 0 ; x < v . length ; x ++ ) { document . write ( " " ) ; for ( let y = 0 ; y < v [ x ] . length ; y ++ ) { document . write ( v [ x ] [ y ] ) ; if ( y == v [ x ] . length - 1 ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } if ( x == v . length - 1 ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } document . write ( " " ) ; } return ; } for ( let j = 0 ; j < K ; j ++ ) { if ( v [ j ] . length > 0 ) { v [ j ] . push ( arr [ i ] ) ; PartitionSub ( arr , i + 1 , N , K , nos , v ) ; v [ j ] . pop ( ) ; } else { v [ j ] . push ( arr [ i ] ) ; PartitionSub ( arr , i + 1 , N , K , nos + 1 , v ) ; v [ j ] . pop ( ) ; break ; } } } function partKSubsets ( arr , N , K ) { let v = [ ] ; for ( let i = 0 ; i < K ; i ++ ) v . push ( [ ] ) ; if ( K == 0 K > N ) { document . write ( " " + " " ) ; } else { document . write ( " " + " " ) ; PartitionSub ( arr , 0 , N , K , 0 , v ) ; } } let arr = [ 1 , 2 , 3 , 4 ] ; let K = 2 ; let N = arr . length ; partKSubsets ( arr , N , K ) ;
Count the number of Prime Cliques in an undirected graph | Javascript implementation to count the number of Prime Cliques in an undirected graph ; Stores the vertices ; Graph ; Degree of the vertices ; To store the count of prime cliques ; Function to create Sieve to check primes ; False here indicates that it is not prime ; Condition if prime [ p ] is not changed , then it is a prime ; Update all multiples of p , set them to non - prime ; Function to check if the given set of vertices in store array is a clique or not ; Run a loop for all set of edges ; If any edge is missing ; Function to find the count of all the cliques having prime size ; Check if any vertices from i + 1 can be inserted ; Add the vertex to store ; If the graph is not a clique of size k then it cannot be a clique by adding another edge ; Increase the count of prime cliques if the size of current clique is prime ; Check if another edge can be added ; Driver code
let MAX = 100 ; let store = new Array ( MAX ) ; store . fill ( 0 ) ; let n ; let graph = new Array ( MAX ) ; for ( let i = 0 ; i < MAX ; i ++ ) { graph [ i ] = new Array ( MAX ) ; } let d = new Array ( MAX ) ; d . fill ( 0 ) ; let ans ; function SieveOfEratosthenes ( prime , p_size ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( let p = 2 ; p * p <= p_size ; p ++ ) { if ( prime [ p ] ) { for ( let i = p * 2 ; i <= p_size ; i += p ) prime [ i ] = false ; } } } function is_clique ( b ) { for ( let i = 1 ; i < b ; i ++ ) { for ( let j = i + 1 ; j < b ; j ++ ) if ( graph [ store [ i ] ] [ store [ j ] ] == 0 ) { return false ; } } return true ; } function primeCliques ( i , l , prime ) { for ( let j = i + 1 ; j <= n ; j ++ ) { store [ l ] = j ; if ( is_clique ( l + 1 ) ) { if ( prime [ l ] ) ans ++ ; else { ans -= 1.3 ; } primeCliques ( j , l + 1 , prime ) ; } } } let edges = [ [ 1 , 2 ] , [ 2 , 3 ] , [ 3 , 1 ] , [ 4 , 3 ] , [ 4 , 5 ] , [ 5 , 3 ] ] ; let size = edges . length ; n = 5 ; let prime = new Array ( n + 1 ) ; for ( let i = 0 ; i < prime . length ; i ++ ) prime [ i ] = true ; SieveOfEratosthenes ( prime , n ) ; for ( let i = 0 ; i < size ; i ++ ) { graph [ edges [ i ] [ 0 ] , edges [ i ] [ 1 ] ] = 1 ; graph [ edges [ i ] [ 1 ] , edges [ i ] [ 0 ] ] = 1 ; d [ edges [ i ] [ 0 ] ] ++ ; d [ edges [ i ] [ 1 ] ] ++ ; } ans = 0 ; primeCliques ( 0 , 1 , prime ) ; document . write ( ans ) ;
Construct a Doubly linked linked list from 2D Matrix | define dimension of matrix ; struct node of doubly linked list with four pointer next , prev , up , down ; function to create a new node ; function to construct the doubly linked list ; Create Node with value contain in matrix at index ( i , j ) ; Assign address of curr into the prev pointer of temp ; Assign address of curr into the up pointer of temp ; Recursive call for next pointer ; Recursive call for down pointer ; Return newly constructed node whose all four node connected at it 's appropriate position ; Function to construct the doubly linked list ; function call for construct the doubly linked list ; function for displaying doubly linked list data ; pointer to move right ; pointer to move down ; loop till node . down is not null ; loop till node . right is not null ; initialise matrix
var dim = 3 ; class Node { constructor ( ) { this . data = 0 ; this . next = null ; this . prev = null ; this . up = null ; this . down = null ; } } function createNode ( data ) { temp = new Node ( ) ; temp . data = data ; temp . next = null ; temp . prev = null ; temp . up = null ; temp . down = null ; return temp ; } function constructDoublyListUtil ( mtrx , i , j , curr ) { if ( i >= dim j >= dim ) { return null ; } var temp = createNode ( mtrx [ i ] [ j ] ) ; temp . prev = curr ; temp . up = curr ; temp . next = constructDoublyListUtil ( mtrx , i , j + 1 , temp ) ; temp . down = constructDoublyListUtil ( mtrx , i + 1 , j , temp ) ; return temp ; } function constructDoublyList ( mtrx ) { return constructDoublyListUtil ( mtrx , 0 , 0 , null ) ; } function display ( head ) { rPtr = null ; dPtr = head ; while ( dPtr != null ) { rPtr = dPtr ; while ( rPtr != null ) { document . write ( rPtr . data + " " ) ; rPtr = rPtr . next ; } document . write ( " " ) ; dPtr = dPtr . down ; } } var mtrx = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] ; var list = constructDoublyList ( mtrx ) ; display ( list ) ;
Count of exponential paths in a Binary Tree | Structure of a Tree node ; Function to create a new node ; Function to find x ; Take log10 of n ; Log ( n ) with base i ; Raising i to the power p ; Function to check whether the given node equals to x ^ y for some y > 0 ; Take logx ( n ) with base x ; Utility function to count the exponent path in a given Binary tree ; Base Condition , when node pointer becomes null or node value is not a number of pow ( x , y ) ; Increment count when encounter leaf node ; Left recursive call save the value of count ; Right recursive call and return value of count ; Function to count exponential paths ; Create Tree ; Retrieve the value of x ; Function call
class Node { constructor ( key ) { this . left = null ; this . right = null ; this . key = key ; } } function newNode ( key ) { let temp = new Node ( key ) ; return ( temp ) ; } function find_x ( n ) { if ( n == 1 ) return 1 ; let num , den , p ; num = Math . log10 ( n ) ; let x = 0 , no = 0 ; for ( let i = 2 ; i <= n ; i ++ ) { den = Math . log10 ( i ) ; p = num / den ; no = parseInt ( Math . pow ( i , parseInt ( p , 10 ) ) , 10 ) ; if ( Math . abs ( no - n ) < 1e-6 ) { x = i ; break ; } } return x ; } function is_key ( n , x ) { let p ; p = Math . log10 ( n ) / Math . log10 ( x ) ; let no = parseInt ( Math . pow ( x , parseInt ( p , 10 ) ) , 10 ) ; if ( n == no ) return true ; return false ; } function evenPaths ( node , count , x ) { if ( node == null || ! is_key ( node . key , x ) ) { return count ; } if ( node . left == null && node . right == null ) { count ++ ; } count = evenPaths ( node . left , count , x ) ; return evenPaths ( node . right , count , x ) ; } function countExpPaths ( node , x ) { return evenPaths ( node , 0 , x ) ; } let root = newNode ( 27 ) ; root . left = newNode ( 9 ) ; root . right = newNode ( 81 ) ; root . left . left = newNode ( 3 ) ; root . left . right = newNode ( 10 ) ; root . right . left = newNode ( 70 ) ; root . right . right = newNode ( 243 ) ; root . right . right . left = newNode ( 81 ) ; root . right . right . right = newNode ( 909 ) ; let x = find_x ( root . key ) ; document . write ( countExpPaths ( root , x ) ) ;
Number of pairs such that path between pairs has the two vertices A and B | Javascript program to find the number of pairs such that the path between every pair contains two given vertices ; Function to perform DFS on the given graph by fixing the a vertex ; To mark a particular vertex as visited ; Variable to store the count of the vertices which can be reached from a ; Performing the DFS by iterating over the visited array ; If the vertex is not visited and removing the vertex b ; Function to return the number of pairs such that path between any two pairs consists of the given two vertices A and B ; Initializing the visited array and assigning it with 0 's ; Initially , the count of vertices is 0 ; Performing DFS by removing the vertex B ; Count the vertices which cannot be reached after removing the vertex B ; Again reinitializing the visited array ; Setting the count of vertices to 0 to perform the DFS again ; Performing the DFS by removing the vertex A ; Count the vertices which cannot be reached after removing the vertex A ; Multiplying both the vertices set ; Driver code ; Loop to store the graph
let N = 1000001 ; let c , n , m , a , b ; function dfs ( a , b , v , vis ) { vis [ a ] = 1 ; c ++ ; for ( let i of v [ a ] ) { if ( vis [ i ] == 0 && i != b ) dfs ( i , b , v , vis ) ; } } function Calculate ( v ) { let vis = new Array ( n + 1 ) ; for ( let i = 0 ; i < n + 1 ; i ++ ) vis [ i ] = 0 ; c = 0 ; dfs ( a , b , v , vis ) ; let ans1 = n - c - 1 ; for ( let i = 0 ; i < n + 1 ; i ++ ) vis [ i ] = 0 ; c = 0 ; dfs ( b , a , v , vis ) ; let ans2 = n - c - 1 ; document . write ( ( ans1 * ans2 ) + " " ) ; } n = 7 ; m = 7 ; a = 3 ; b = 5 ; let edges = [ [ 1 , 2 ] , [ 2 , 3 ] , [ 3 , 4 ] , [ 4 , 5 ] , [ 5 , 6 ] , [ 6 , 7 ] , [ 7 , 5 ] ] ; let v = new Array ( n + 1 ) ; for ( let i = 0 ; i <= n ; i ++ ) { v [ i ] = [ ] ; } for ( let i = 0 ; i < m ; i ++ ) { v [ edges [ i ] [ 0 ] ] . push ( edges [ i ] [ 1 ] ) ; v [ edges [ i ] [ 1 ] ] . push ( edges [ i ] [ 0 ] ) ; } Calculate ( v ) ;