codeparrot/xlcost-text-to-code huggingface dataset:
Check if an array is sorted and rotated using Binary Search | Java implementation of the above approach ; Function to return the index of the pivot ; Base cases ; Check if element at ( mid - 1 ) is pivot Consider the cases like { 4 , 5 , 1 , 2 , 3 } ; Decide whether we need to go to the left half or the right half ; Function to check if a given array is sorted rotated or not ; To check if the elements to the left of the pivot are in descending or not ; To check if the elements to the right of the pivot are in ascending or not ; If any of the above if or else is true Then the array is sorted rotated ; Else the array is not sorted rotated ; Driver code
class GFG { static int findPivot ( int arr [ ] , int low , int high ) { if ( high < low ) return - 1 ; if ( high == low ) return low ; int mid = ( low + high ) / 2 ; if ( mid < high && arr [ mid + 1 ] < arr [ mid ] ) { return mid ; } if ( mid > low && arr [ mid ] < arr [ mid - 1 ] ) { return mid - 1 ; } if ( arr [ low ] > arr [ mid ] ) { return findPivot ( arr , low , mid - 1 ) ; } else { return findPivot ( arr , mid + 1 , high ) ; } } public static boolean isRotated ( int arr [ ] , int n ) { int l = 0 ; int r = n - 1 ; int pivot = - 1 ; if ( arr [ l ] > arr [ r ] ) { pivot = findPivot ( arr , l , r ) ; int temp = pivot ; if ( l < pivot ) { while ( pivot > l ) { if ( arr [ pivot ] < arr [ pivot - 1 ] ) { return false ; } pivot -- ; } } pivot = temp ; else { pivot ++ ; while ( pivot < r ) { if ( arr [ pivot ] > arr [ pivot + 1 ] ) { return false ; } pivot ++ ; } } return true ; } else { return false ; } } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 5 , 1 , 3 , 2 } ; System . out . println ( isRotated ( arr , 5 ) ) ; } }
Find the Deepest Node in a Binary Tree | A Java program to find value of the deepest node in a given binary tree ; A tree node with constructor ; constructor ; Funtion to return the deepest node ; Creating a Queue ; Iterates untill queue become empty ; Driver code
import java . util . * ; public class Node { int data ; Node left , right ; Node ( int key ) { data = key ; left = null ; right = null ; } } ; class Gfg { public static Node deepestNode ( Node root ) { Node tmp = null ; if ( root == null ) return null ; Queue < Node > q = new LinkedList < Node > ( ) ; q . offer ( root ) ; while ( ! q . isEmpty ( ) ) { tmp = q . poll ( ) ; if ( tmp . left != null ) q . offer ( tmp . left ) ; if ( tmp . right != null ) q . offer ( tmp . right ) ; } return tmp ; } public static void main ( String [ ] args ) { Node root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . left = new Node ( 4 ) ; root . right . left = new Node ( 5 ) ; root . right . right = new Node ( 6 ) ; root . right . left . right = new Node ( 7 ) ; root . right . right . right = new Node ( 8 ) ; root . right . left . right . left = new Node ( 9 ) ; Node deepNode = deepestNode ( root ) ; System . out . println ( deepNode . data ) ; } }
Find a distinct pair ( x , y ) in given range such that x divides y | Java implementation of the approach ; Function to return the possible pair ; ans1 , ans2 store value of x and y respectively ; Driver Code
class GFG { static void findpair ( int l , int r ) { int ans1 = l ; int ans2 = 2 * l ; System . out . println ( ans1 + " , ▁ " + ans2 ) ; } public static void main ( String args [ ] ) { int l = 1 , r = 10 ; findpair ( l , r ) ; } }
Jump in rank of a student after updating marks | Java implementation of the approach ; Function to print the name of student who stood first after updation in rank ; Array of students ; Store the name of the student ; Update the marks of the student ; Store the current rank of the student ; Print the name and jump in rank ; Driver code ; Names of the students ; Marks of the students ; Updates that are to be done ; Number of students
import java . util . * ; class GFG { static class Student { String name ; int marks , prev_rank ; Student ( ) { this . marks = 0 ; this . prev_rank = 0 ; } } static void nameRank ( String [ ] names , int [ ] marks , int [ ] updates , int n ) { Student [ ] x = new Student [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { x [ i ] = new Student ( ) ; x [ i ] . name = names [ i ] ; x [ i ] . marks = marks [ i ] + updates [ i ] ; x [ i ] . prev_rank = i + 1 ; } Student highest = x [ 0 ] ; for ( int j = 1 ; j < n ; j ++ ) { if ( x [ j ] . marks >= highest . marks ) highest = x [ j ] ; } System . out . print ( " Name : ▁ " + highest . name + " , ▁ Jump : ▁ " + Math . abs ( highest . prev_rank - 1 ) ) ; } public static void main ( String [ ] args ) { String [ ] names = { " sam " , " ram " , " geek " } ; int [ ] marks = { 80 , 79 , 75 } ; int [ ] updates = { 0 , 5 , - 9 } ; int n = marks . length ; nameRank ( names , marks , updates , n ) ; } }
Integers from the range that are composed of a single distinct digit | java implementation of the approach ; Function to return the count of digits of a number ; Function to return a number that contains only digit ' d ' repeated exactly count times ; Function to return the count of integers that are composed of a single distinct digit only ; Count of digits in L and R ; First digits of L and R ; If L has lesser number of digits than R ; If the number that starts with firstDigitL and has number of digits = countDigitsL is within the range include the number ; Exclude the number ; If the number that starts with firstDigitR and has number of digits = countDigitsR is within the range include the number ; Exclude the number ; If both L and R have equal number of digits ; Include the number greater than L upto the maximum number whose digit = coutDigitsL ; Exclude the numbers which are greater than R ; Return the count ; Driver code
import java . io . * ; class GFG { static int countDigits ( int n ) { int count = 0 ; while ( n > 0 ) { count += 1 ; n /= 10 ; } return count ; } static int getDistinct ( int d , int count ) { int num = 0 ; count = ( int ) Math . pow ( 10 , count - 1 ) ; while ( count > 0 ) { num += ( count * d ) ; count /= 10 ; } return num ; } static int findCount ( int L , int R ) { int count = 0 ; int countDigitsL = countDigits ( L ) ; int countDigitsR = countDigits ( R ) ; int firstDigitL = ( L / ( int ) Math . pow ( 10 , countDigitsL - 1 ) ) ; int firstDigitR = ( R / ( int ) Math . pow ( 10 , countDigitsR - 1 ) ) ; if ( countDigitsL < countDigitsR ) { count += ( 9 * ( countDigitsR - countDigitsL - 1 ) ) ; if ( getDistinct ( firstDigitL , countDigitsL ) >= L ) count += ( 9 - firstDigitL + 1 ) ; else count += ( 9 - firstDigitL ) ; if ( getDistinct ( firstDigitR , countDigitsR ) <= R ) count += firstDigitR ; else count += ( firstDigitR - 1 ) ; } else { if ( getDistinct ( firstDigitL , countDigitsL ) >= L ) count += ( 9 - firstDigitL + 1 ) ; else count += ( 9 - firstDigitL ) ; if ( getDistinct ( firstDigitR , countDigitsR ) <= R ) count -= ( 9 - firstDigitR ) ; else count -= ( 9 - firstDigitR + 1 ) ; } return count ; } public static void main ( String [ ] args ) { int L = 10 , R = 50 ; System . out . println ( findCount ( L , R ) ) ; } }
Check if a pair with given absolute difference exists in a Matrix | Java code to check for pair with given difference exists in the matrix or not ; Function to check if a pair with given difference exists in the matrix ; Store elements in a hash ; Loop to iterate over the elements of the matrix ; Driver Code ; Input matrix ; Given difference
import java . util . * ; class GFG { static final int N = 4 ; static final int M = 4 ; static boolean isPairWithDiff ( int mat [ ] [ ] , int k ) { HashSet < Integer > s = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( mat [ i ] [ j ] > k ) { int m = mat [ i ] [ j ] - k ; if ( s . contains ( m ) ) { return true ; } } else { int m = k - mat [ i ] [ j ] ; if ( s . contains ( m ) ) { return true ; } } s . add ( mat [ i ] [ j ] ) ; } } return false ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 5 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 100 } } ; int k = 85 ; System . out . println ( isPairWithDiff ( mat , k ) == true ? " YES " : " NO " ) ; } }
Next Smaller Element | A Stack based Java program to find next smaller element for all array elements in same order as input . ; prints element and NSE pair for all elements of arr [ ] of size n ; push the first element to stack ; iterate for rest of the elements ; if stack is not empty , then pop an element from stack . If the popped element is greater than next , then a ) print the pair b ) keep popping while elements are greater and stack is not empty ; push next to stack so that we can find next smaller for it ; After iterating over the loop , the remaining elements in stack do not have the next smaller element , so print - 1 for them ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { public static void printNSE ( int arr [ ] , int n ) { Stack < Integer > s = new Stack < Integer > ( ) ; HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; s . push ( arr [ 0 ] ) ; for ( int i = 1 ; i < n ; i ++ ) { if ( s . empty ( ) ) { s . push ( arr [ i ] ) ; continue ; } while ( s . empty ( ) == false && s . peek ( ) > arr [ i ] ) { mp . put ( s . peek ( ) , arr [ i ] ) ; s . pop ( ) ; } s . push ( arr [ i ] ) ; } while ( s . empty ( ) == false ) { mp . put ( s . peek ( ) , - 1 ) ; s . pop ( ) ; } for ( int i = 0 ; i < n ; i ++ ) System . out . println ( arr [ i ] + " ▁ - - - > ▁ " + mp . get ( arr [ i ] ) ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 11 , 13 , 21 , 3 } ; int n = arr . length ; printNSE ( arr , n ) ; } }
Deepest left leaf node in a binary tree | iterative approach | Java program to find deepest left leaf node of binary tree ; tree node ; returns a new tree Node ; return the deepest left leaf node of binary tree ; create a queue for level order traversal ; traverse until the queue is empty ; Since we go level by level , the last stored left leaf node is deepest one , ; Driver Code ; construct a tree
import java . util . * ; class GFG { static class Node { int data ; Node left , right ; } ; static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } static Node getDeepestLeftLeafNode ( Node root ) { if ( root == null ) return null ; Queue < Node > q = new LinkedList < > ( ) ; q . add ( root ) ; Node result = null ; while ( ! q . isEmpty ( ) ) { Node temp = q . peek ( ) ; q . remove ( ) ; if ( temp . left != null ) { q . add ( temp . left ) ; if ( temp . left . left == null && temp . left . right == null ) result = temp . left ; } if ( temp . right != null ) q . add ( temp . right ) ; } return result ; } public static void main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . right . left = newNode ( 5 ) ; root . right . right = newNode ( 6 ) ; root . right . left . right = newNode ( 7 ) ; root . right . right . right = newNode ( 8 ) ; root . right . left . right . left = newNode ( 9 ) ; root . right . right . right . right = newNode ( 10 ) ; Node result = getDeepestLeftLeafNode ( root ) ; if ( result != null ) System . out . println ( " Deepest ▁ Left ▁ Leaf ▁ Node ▁ : : ▁ " + result . data ) ; else System . out . println ( " No ▁ result , ▁ " + " left ▁ leaf ▁ not ▁ found " ) ; } }
Find all possible binary trees with given Inorder Traversal | Java program to find binary tree with given inorder traversal ; Class containing left and right child of current node and key value ; Class to print Level Order Traversal ; A utility function to do preorder traversal of BST ; Function for constructing all possible trees with given inorder traversal stored in an array from arr [ start ] to arr [ end ] . This function returns a vector of trees . ; List to store all possible trees ; if start > end then subtree will be empty so returning NULL in the list ; Iterating through all values from start to end for constructing left and right subtree recursively ; Constructing left subtree ; Constructing right subtree ; Now looping through all left and right subtrees and connecting them to ith root below ; Making arr [ i ] as root ; Connecting left subtree ; Connecting right subtree ; Adding this tree to list ; Driver Program to test above functions
import java . util . Vector ; class Node { int data ; Node left , right ; public Node ( int item ) { data = item ; left = null ; right = null ; } } class BinaryTree { Node root ; void preOrder ( Node node ) { if ( node != null ) { System . out . print ( node . data + " ▁ " ) ; preOrder ( node . left ) ; preOrder ( node . right ) ; } } Vector < Node > getTrees ( int arr [ ] , int start , int end ) { Vector < Node > trees = new Vector < Node > ( ) ; if ( start > end ) { trees . add ( null ) ; return trees ; } for ( int i = start ; i <= end ; i ++ ) { Vector < Node > ltrees = getTrees ( arr , start , i - 1 ) ; Vector < Node > rtrees = getTrees ( arr , i + 1 , end ) ; for ( int j = 0 ; j < ltrees . size ( ) ; j ++ ) { for ( int k = 0 ; k < rtrees . size ( ) ; k ++ ) { Node node = new Node ( arr [ i ] ) ; node . left = ltrees . get ( j ) ; node . right = rtrees . get ( k ) ; trees . add ( node ) ; } } } return trees ; } public static void main ( String args [ ] ) { int in [ ] = { 4 , 5 , 7 } ; int n = in . length ; BinaryTree tree = new BinaryTree ( ) ; Vector < Node > trees = tree . getTrees ( in , 0 , n - 1 ) ; System . out . println ( " Preorder ▁ traversal ▁ of ▁ different ▁ " + " ▁ binary ▁ trees ▁ are : " ) ; for ( int i = 0 ; i < trees . size ( ) ; i ++ ) { tree . preOrder ( trees . get ( i ) ) ; System . out . println ( " " ) ; } } }
Check if a string is suffix of another | Java program to find if a string is suffix of another ; Test case - sensitive implementation of endsWith function
class GFG { public static void main ( String [ ] args ) { String s1 = " geeks " , s2 = " geeksforgeeks " ; boolean result = s2 . endsWith ( s1 ) ; if ( result ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Maximum elements that can be made equal with k updates | java program to find maximum elements that can be made equal with k updates ; Function to calculate the maximum number of equal elements possible with atmost K increment of values . Here we have done sliding window to determine that whether there are x number of elements present which on increment will become equal . The loop here will run in fashion like 0. . . x - 1 , 1. . . x , 2. . . x + 1 , ... . , n - x - 1. . . n - 1 ; It can be explained with the reasoning that if for some x number of elements we can update the values then the increment to the segment ( i to j having length -> x ) so that all will be equal is ( x * maxx [ j ] ) this is the total sum of segment and ( pre [ j ] - pre [ i ] ) is present sum So difference of them should be less than k if yes , then that segment length ( x ) can be possible return true ; sort the array in ascending order ; Initializing the prefix array and maximum array ; Calculating prefix sum of the array ; Calculating max value upto that position in the array ; Binary search applied for computation here ; printing result ; Driver code
import java . util . Arrays ; public class GFG { static boolean ElementsCalculationFunc ( int pre [ ] , int maxx [ ] , int x , int k , int n ) { for ( int i = 0 , j = x ; j <= n ; j ++ , i ++ ) { if ( x * maxx [ j ] - ( pre [ j ] - pre [ i ] ) <= k ) return true ; } return false ; } static void MaxNumberOfElements ( int a [ ] , int n , int k ) { Arrays . sort ( a ) ; for ( int i = 0 ; i <= n ; ++ i ) { pre [ i ] = 0 ; maxx [ i ] = 0 ; } for ( int i = 1 ; i <= n ; i ++ ) { pre [ i ] = pre [ i - 1 ] + a [ i - 1 ] ; maxx [ i ] = Math . max ( maxx [ i - 1 ] , a [ i - 1 ] ) ; } int l = 1 , r = n , ans = 0 ; while ( l < r ) { int mid = ( l + r ) / 2 ; if ( ElementsCalculationFunc ( pre , maxx , mid - 1 , k , n ) ) { ans = mid ; l = mid + 1 ; } else r = mid - 1 ; } System . out . print ( ( int ) ans + "NEW_LINE"); } public static void main ( String args [ ] ) { int arr [ ] = { 2 , 4 , 9 } ; int n = arr . length ; int k = 3 ; MaxNumberOfElements ( arr , n , k ) ; } }
Count palindromic characteristics of a String | Java program which counts different palindromic characteristics of a string . ; function which checks whether a substring str [ i . . j ] of a given string is a palindrome or not . ; P [ i , j ] = true if substring str [ i . . j ] is palindrome , else false ; palindrome of single length ; palindrome of length 2 ; Palindromes of length more then 2. This loop is similar to Matrix Chain Multiplication . We start with a gap of length 2 and fill P table in a way that gap between starting and ending indexes increases one by one by outer loop . ; Pick starting point for current gap ; Set ending point ; If current string is palindrome ; function which recursively counts if a string str [ i . . j ] is a k - palindromic string or not . ; terminating condition for a string which is a k - palindrome . ; terminating condition for a string which is not a k - palindrome . ; increases the counter for the string if it is a k - palindrome . ; mid is middle pointer of the string str [ i ... j ] . ; if length of string which is ( j - i + 1 ) is odd than we have to subtract one from mid else if even then no change . ; if the string is k - palindrome then we check if it is a ( k + 1 ) - palindrome or not by just sending any of one half of the string to the Count_k_Palindrome function . ; Finding all palindromic substrings of given string ; counting k - palindromes for each and every substring of given string . . ; Output the number of K - palindromic substrings of a given string . ; Driver code
import java . io . * ; class GFG { static int MAX_STR_LEN = 1000 ; static boolean P [ ] [ ] = new boolean [ MAX_STR_LEN ] [ MAX_STR_LEN ] ; static int [ ] Kpal = new int [ MAX_STR_LEN ] ; static void checkSubStrPal ( String str , int n ) { for ( int i = 0 ; i < MAX_STR_LEN ; i ++ ) { for ( int j = 0 ; j < MAX_STR_LEN ; j ++ ) P [ i ] [ j ] = false ; Kpal [ i ] = 0 ; } for ( int i = 0 ; i < n ; i ++ ) P [ i ] [ i ] = true ; for ( int i = 0 ; i < n - 1 ; i ++ ) if ( str . charAt ( i ) == str . charAt ( i + 1 ) ) P [ i ] [ i + 1 ] = true ; for ( int gap = 2 ; gap < n ; gap ++ ) { for ( int i = 0 ; i < n - gap ; i ++ ) { int j = gap + i ; if ( str . charAt ( i ) == str . charAt ( j ) && P [ i + 1 ] [ j - 1 ] ) P [ i ] [ j ] = true ; } } } static void countKPalindromes ( int i , int j , int k ) { if ( i == j ) { Kpal [ k ] ++ ; return ; } if ( P [ i ] [ j ] == false ) return ; Kpal [ k ] ++ ; int mid = ( i + j ) / 2 ; if ( ( j - i + 1 ) % 2 == 1 ) mid -- ; countKPalindromes ( i , mid , k + 1 ) ; } static void printKPalindromes ( String s ) { int n = s . length ( ) ; checkSubStrPal ( s , n ) ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n - i ; j ++ ) countKPalindromes ( j , j + i , 1 ) ; for ( int i = 1 ; i <= n ; i ++ ) System . out . print ( Kpal [ i ] + " ▁ " ) ; System . out . println ( ) ; } public static void main ( String args [ ] ) { String s = " abacaba " ; printKPalindromes ( s ) ; } }
Print number in ascending order which contains 1 , 2 and 3 in their digits . | Java program to print all number containing 1 , 2 and 3 in any order . ; prints all the number containing 1 , 2 , 3 in any order ; check if the number contains 1 , 2 & 3 in any order ; sort all the numbers ; convert the number to string and find if it contains 1 , 2 & 3. ; Driver Code
import java . io . FileNotFoundException ; import java . util . ArrayList ; import java . util . Collections ; import java . util . Iterator ; class GFG { private static String printNumbers ( int [ ] numbers ) { ArrayList < Integer > array = new ArrayList < > ( ) ; for ( int number : numbers ) { if ( findContainsOneTwoThree ( number ) ) array . add ( number ) ; } Collections . sort ( array ) ; StringBuffer strbuf = new StringBuffer ( ) ; Iterator it = array . iterator ( ) ; while ( it . hasNext ( ) ) { int value = ( int ) it . next ( ) ; if ( strbuf . length ( ) > 0 ) strbuf . append ( " , ▁ " ) ; strbuf . append ( Integer . toString ( value ) ) ; } return ( strbuf . length ( ) > 0 ) ? strbuf . toString ( ) : " - 1" ; } private static boolean findContainsOneTwoThree ( int number ) { String str = Integer . toString ( number ) ; return ( str . contains ( "1" ) && str . contains ( "2" ) && str . contains ( "3" ) ) ; } public static void main ( String [ ] args ) { int [ ] numbers = { 123 , 1232 , 456 , 234 , 32145 } ; System . out . println ( printNumbers ( numbers ) ) ; } }
Find k | Java implementation to solve k queries for given n ranges ; Structure to store the start and end point ; Comparison function for sorting ; Function to find Kth smallest number in a Vector of merged intervals ; Traverse merged . get ( ) o find Kth smallest element using Linear search . ; To combined both type of ranges , overlapping as well as non - overlapping . ; Sorting intervals according to start time ; Merging all intervals into merged ; To check if starting point of next range is lying between the previous range and ending point of next range is greater than the Ending point of previous range then update ending point of previous range by ending point of next range . ; If starting point of next range is greater than the ending point of previous range then store next range in merged . get ( . ) if ( curr . s > prev . e ) ; Driver code ; Merge all intervals into merged . get ( ) ) ; Processing all queries on merged intervals
import java . util . * ; class GFG { static class Interval { int s ; int e ; Interval ( int a , int b ) { s = a ; e = b ; } } ; static class Sortby implements Comparator < Interval > { public int compare ( Interval a , Interval b ) { return a . s - b . s ; } } static int kthSmallestNum ( Vector < Interval > merged , int k ) { int n = merged . size ( ) ; for ( int j = 0 ; j < n ; j ++ ) { if ( k <= Math . abs ( merged . get ( j ) . e - merged . get ( j ) . s + 1 ) ) return ( merged . get ( j ) . s + k - 1 ) ; k = k - Math . abs ( merged . get ( j ) . e - merged . get ( j ) . s + 1 ) ; } if ( k != 0 ) return - 1 ; return 0 ; } static Vector < Interval > mergeIntervals ( Vector < Interval > merged , Interval arr [ ] , int n ) { Arrays . sort ( arr , new Sortby ( ) ) ; merged . add ( arr [ 0 ] ) ; for ( int i = 1 ; i < n ; i ++ ) { Interval prev = merged . get ( merged . size ( ) - 1 ) ; Interval curr = arr [ i ] ; if ( ( curr . s >= prev . s && curr . s <= prev . e ) && ( curr . e > prev . e ) ) merged . get ( merged . size ( ) - 1 ) . e = curr . e ; else { merged . add ( curr ) ; } } return merged ; } public static void main ( String args [ ] ) { Interval arr [ ] = { new Interval ( 2 , 6 ) , new Interval ( 4 , 7 ) } ; int n = arr . length ; int query [ ] = { 5 , 8 } ; int q = query . length ; Vector < Interval > merged = new Vector < Interval > ( ) ; merged = mergeIntervals ( merged , arr , n ) ; for ( int i = 0 ; i < q ; i ++ ) System . out . println ( kthSmallestNum ( merged , query [ i ] ) ) ; } }
Grouping Countries | Java program to count no of distinct countries from a given group of people ; Answer is valid if adjacent sitting num people give same answer ; someone gives different answer ; check next person ; one valid country group has been found ; driver code
class Country { public static void countCountries ( int ans [ ] , int N ) { int total_countries = 0 , i = 0 ; boolean invalid = false ; while ( i < N ) { int curr_size = ans [ i ] ; int num = ans [ i ] ; while ( num > 0 ) { if ( ans [ i ] != curr_size ) { System . out . print ( "Invalid AnswerNEW_LINE" ); return ; } else num -- ; i ++ ; } total_countries ++ ; } System . out . print ( " There ▁ are ▁ " + total_countries + " distinct companies in the group.NEW_LINE" ); } public static void main ( String [ ] args ) { int ans [ ] = { 1 , 1 , 2 , 2 , 4 , 4 , 4 , 4 } ; int n = 8 ; countCountries ( ans , n ) ; } }