codeparrot/xlcost-text-to-code huggingface dataset:
Generate an array of size N according to the given rules | Java implementation to generate an array of size N by following the given rules ; Function to search the most recent location of element N If not present in the array it will return - 1 ; Function to generate an array of size N by following the given rules ; Loop to fill the array as per the given rules ; Check for the occurrence of arr [ i - 1 ] ; Driver code
class GFG { static int a [ ] ; static int search ( int a [ ] , int k , int x ) { int j ; for ( j = k - 1 ; j > - 1 ; j -- ) { if ( a [ j ] == x ) return j ; } return - 1 ; } static void genArray ( int [ ] arr , int N ) { for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( search ( arr , i , arr [ i ] ) == - 1 ) arr [ i + 1 ] = 0 ; else arr [ i + 1 ] = ( i - search ( arr , i , arr [ i ] ) ) ; } } public static void main ( String [ ] args ) { int N = 5 ; int size = N + 1 ; int a [ ] = new int [ N ] ; genArray ( a , N ) ; for ( int i = 0 ; i < N ; i ++ ) System . out . print ( a [ i ] + " ▁ " ) ; } }
Count of pairs in an Array whose sum is a Perfect Cube | Java implementation of the approach ; Function to return an ArrayList containing all the perfect cubes upto n ; while current perfect cube is less than or equal to n ; Function to print the sum of maximum two elements from the array ; Function to return the count of numbers that when added with n give a perfect cube ; temp > n is checked so that pairs ( x , y ) and ( y , x ) don 't get counted twice ; Function to count the pairs whose sum is a perfect cube ; Sum of the maximum two elements from the array ; List of perfect cubes upto max ; Contains all the array elements ; Add count of the elements that when added with arr [ i ] give a perfect cube ; Driver code
import java . util . * ; class GFG { static List < Integer > getPerfectcubes ( int n ) { List < Integer > perfectcubes = new ArrayList < Integer > ( ) ; int current = 1 ; int i = 1 ; while ( current <= n ) { perfectcubes . add ( current ) ; i += 1 ; current = ( int ) ( Math . pow ( i , 3 ) ) ; } return perfectcubes ; } static int maxPairSum ( int [ ] arr ) { int n = arr . length ; int max = 0 ; int secondMax = 0 ; if ( arr [ 0 ] > arr [ 1 ] ) { max = arr [ 0 ] ; secondMax = arr [ 1 ] ; } else { max = arr [ 1 ] ; secondMax = arr [ 0 ] ; } for ( int i = 2 ; i < n ; i ++ ) { if ( arr [ i ] > max ) { secondMax = max ; max = arr [ i ] ; } else if ( arr [ i ] > secondMax ) { secondMax = arr [ i ] ; } } return ( max + secondMax ) ; } static int countPairsWith ( int n , List < Integer > perfectcubes , List < Integer > nums ) { int count = 0 ; for ( int i = 0 ; i < perfectcubes . size ( ) ; i ++ ) { int temp = perfectcubes . get ( i ) - n ; if ( temp > n && ( nums . contains ( temp ) ) ) count += 1 ; } return count ; } static int countPairs ( int [ ] arr ) { int n = arr . length ; int max = maxPairSum ( arr ) ; List < Integer > perfectcubes = getPerfectcubes ( max ) ; List < Integer > nums = new ArrayList < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { nums . add ( arr [ i ] ) ; } int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { count += countPairsWith ( arr [ i ] , perfectcubes , nums ) ; } return count ; } public static void main ( String [ ] agrs ) { int [ ] arr = { 2 , 6 , 18 , 9 , 999 , 1 } ; System . out . print ( countPairs ( arr ) ) ; } }
Number of perfect cubes between two given numbers | A Simple Method to count cubes between a and b ; Function to count cubes between two numbers ; Traverse through all numbers ; Check if current number ' i ' is perfect cube ; Driver code
class GFG { static int countCubes ( int a , int b ) { for ( int i = a ; i <= b ; i ++ ) for ( int j = 1 ; j * j * j <= i ; j ++ ) if ( j * j * j == i ) cnt ++ ; return cnt ; } public static void main ( String [ ] args ) { int a = 7 , b = 30 ; System . out . print ( " Count ▁ of ▁ Cubes ▁ is ▁ " + countCubes ( a , b ) ) ; } }
Number of perfect cubes between two given numbers | An Efficient Method to count cubes between a and b ; Function to count cubes between two numbers ; Driver code
class GFG { static int countCubes ( int a , int b ) { return ( int ) ( Math . floor ( Math . cbrt ( b ) ) - Math . ceil ( Math . cbrt ( a ) ) + 1 ) ; } public static void main ( String [ ] args ) { int a = 7 , b = 28 ; System . out . print ( " Count ▁ of ▁ cubes ▁ is ▁ " + countCubes ( a , b ) ) ; } }
XOR and OR of all N | Java program to find the XOR and OR of all Armstrong numbers of N digits ; Function to check if a number is Armstrong or not ; Function to find XOR of all N - digits Armstrong number ; To store the XOR and OR of all Armstrong number ; Starting N - digit Armstrong number ; Ending N - digit Armstrong number ; Iterate over starting and ending number ; To check if i is Armstrong or not ; Print the XOR and OR of all Armstrong number ; Driver Code
class GFG { static boolean isArmstrong ( int x , int n ) { int sum1 = 0 ; int temp = x ; while ( temp > 0 ) { int digit = temp % 10 ; sum1 += Math . pow ( digit , n ) ; temp /= 10 ; } return sum1 == x ; } static void CalculateXORandOR ( int n ) { int CalculateXOR = 0 ; int CalculateOR = 0 ; int start = ( int ) Math . pow ( 10 , n - 1 ) ; int end = ( int ) ( Math . pow ( 10 , n ) ) - 1 ; for ( int i = start ; i < end + 1 ; i ++ ) { if ( isArmstrong ( i , n ) ) { CalculateXOR = CalculateXOR ^ i ; CalculateOR = CalculateOR | i ; } } System . out . println ( " XOR ▁ = ▁ " + CalculateXOR ) ; System . out . println ( " OR ▁ = ▁ " + CalculateOR ) ; } public static void main ( String [ ] args ) { int n = 4 ; CalculateXORandOR ( n ) ; } }
Perfect Cube | Java program to check whether the given number N is the perfect cube or not ; Function to check if a number is a perfect Cube or not ; Iterate from 1 - N ; Find the cube of every number ; Check if cube equals N or not ; Driver code ; Function call
class GFG { static void perfectCube ( int N ) { int cube ; for ( int i = 0 ; i <= N ; i ++ ) { cube = i * i * i ; if ( cube == N ) { System . out . println ( " Yes " ) ; return ; } else if ( cube > N ) { System . out . println ( " NO " ) ; return ; } } } public static void main ( String [ ] args ) { int N = 216 ; perfectCube ( N ) ; } }
Perfect Cube | Java program to check whether the given number N is the perfect cube or not ; Function to check if a number is a perfect Cube using inbuilt function ; If cube of cube_root is equals to N , then print Yes Else print No ; Driver 's code ; Function call to check N is cube or not
public class GFG { static void perfectCube ( int N ) { int cube_root ; cube_root = ( int ) Math . round ( Math . cbrt ( N ) ) ; if ( cube_root * cube_root * cube_root == N ) { System . out . println ( " Yes " ) ; return ; } else { System . out . println ( " NO " ) ; return ; } } public static void main ( String [ ] args ) { int N = 125 ; perfectCube ( N ) ; } }
Check if given array can be made 0 with given operations performed any number of times | Java implementation of the approach ; Function to whether the array can be made zero or not ; Count for even elements ; Count for odd elements ; Traverse the array to count the even and odd ; If arr [ i ] is odd ; If arr [ i ] is even ; Check if count of even is zero or count of odd is zero ; Driver 's Code
import java . util . * ; class GFG { static void check ( int arr [ ] , int N ) { int even = 0 ; int odd = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 1 ) { odd ++ ; } else { even ++ ; } } if ( even == N odd == N ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 3 } ; int N = arr . length ; check ( arr , N ) ; } }
Calculate sum of all integers from 1 to N , excluding perfect power of 2 | Java implementation of the above approach ; Function to find the required summation ; Find the sum of first N integers using the formula ; Find the sum of numbers which are exact power of 2 by using the formula ; Print the final Sum ; Driver Code ; Function to find the sum
import java . lang . Math ; class GFG { public static void findSum ( int N ) { int sum = ( N ) * ( N + 1 ) / 2 ; int r = ( int ) ( Math . log ( N ) / Math . log ( 2 ) ) + 1 ; int expSum = ( int ) ( Math . pow ( 2 , r ) ) - 1 ; System . out . println ( sum - expSum ) ; } public static void main ( String [ ] args ) { int N = 2 ; findSum ( N ) ; } }
Largest Even and Odd N | Java implementation of the above approach ; Function to print the largest N - digit even and odd numbers of base B ; Initialise the Number ; If Base B is even , then B ^ n will give largest Even number of N + 1 digit ; To get even number of N digit subtract 2 from B ^ n ; To get odd number of N digit subtract 1 from B ^ n ; If Base B is odd , then B ^ n will give largest Odd number of N + 1 digit ; To get even number of N digit subtract 1 from B ^ n ; To get odd number of N digit subtract 2 from B ^ n ; Driver 's Code ; Function to find the numbers
import java . util . * ; class GFG { static void findNumbers ( int n , int b ) { double even = 0 , odd = 0 ; if ( b % 2 == 0 ) { even = Math . pow ( b , n ) - 2 ; odd = Math . pow ( b , n ) - 1 ; } else { even = Math . pow ( b , n ) - 1 ; odd = Math . pow ( b , n ) - 2 ; } System . out . println ( " Even ▁ Number ▁ = ▁ " + ( int ) even ) ; System . out . print ( " Odd ▁ Number ▁ = ▁ " + ( int ) odd ) ; } public static void main ( String [ ] args ) { int N = 2 , B = 5 ; findNumbers ( N , B ) ; } }
Minimum divisor of a number to make the number perfect cube | Java program to find minimum number which divide n to make it a perfect cube ; Returns the minimum divisor ; Since 2 is only even prime , compute its power seprately . ; If count is not divisible by 3 , it must be removed by dividing n by prime number power . ; If count is not divisible by 3 , it must be removed by dividing n by prime number power . ; if n is a prime number ; Driver code
class GFG { static int findMinNumber ( int n ) { int count = 0 , ans = 1 ; while ( n % 2 == 0 ) { count ++ ; n /= 2 ; } if ( count % 3 != 0 ) ans *= Math . pow ( 2 , ( count % 3 ) ) ; for ( int i = 3 ; i <= Math . sqrt ( n ) ; i += 2 ) { count = 0 ; while ( n % i == 0 ) { count ++ ; n /= i ; } if ( count % 3 != 0 ) ans *= Math . pow ( i , ( count % 3 ) ) ; } if ( n > 2 ) ans *= n ; return ans ; } public static void main ( String [ ] args ) { int n = 128 ; System . out . print ( findMinNumber ( n ) + "NEW_LINE"); } }
Check whether the number can be made perfect square after adding K | Java program to check whether the number can be made perfect square after adding K ; Function to check whether the number can be made perfect square after adding K ; Computing the square root of the number ; Print Yes if the number is a perfect square ; Driver code
import java . util . * ; class GFG { static void isPerfectSquare ( int x ) { int sr = ( int ) Math . sqrt ( x ) ; if ( sr * sr == x ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } public static void main ( String args [ ] ) { int n = 7 , k = 2 ; isPerfectSquare ( n + k ) ; } }
Number of times the largest Perfect Cube can be subtracted from N | Java implementation of the approach ; Function to return the count of steps ; Variable to store the count of steps ; Iterate while N > 0 ; Get the largest perfect cube and subtract it from N ; Increment steps ; Return the required count ; Driver code
class GFG { static int countSteps ( int n ) { int steps = 0 ; while ( n > 0 ) { int largest = ( int ) Math . cbrt ( n ) ; n -= ( largest * largest * largest ) ; steps ++ ; } return steps ; } public static void main ( String [ ] args ) { int n = 150 ; System . out . print ( countSteps ( n ) ) ; } }
Product of all Subsets of a set formed by first N natural numbers | Java implementation of the approach ; Function to find the product of all elements in all subsets in natural numbers from 1 to N ; Driver Code
class GFG { static int product ( int N ) { int ans = 1 ; int val = ( int ) Math . pow ( 2 , N - 1 ) ; for ( int i = 1 ; i <= N ; i ++ ) { ans *= ( int ) Math . pow ( i , val ) ; } return ans ; } public static void main ( String [ ] args ) { int N = 2 ; System . out . println ( product ( N ) ) ; } }
Number of common tangents between two circles if their centers and radius is given | Java program to find the number of common tangents between the two circles ; Driver code
import java . io . * ; class GFG { static int circle ( int x1 , int y1 , int x2 , int y2 , int r1 , int r2 ) { int distSq = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) ; int radSumSq = ( r1 + r2 ) * ( r1 + r2 ) ; if ( distSq == radSumSq ) return 1 ; else if ( distSq > radSumSq ) return - 1 ; else return 0 ; } public static void main ( String [ ] args ) { int x1 = - 10 , y1 = 8 ; int x2 = 14 , y2 = - 24 ; int r1 = 30 , r2 = 10 ; int t = circle ( x1 , y1 , x2 , y2 , r1 , r2 ) ; if ( t == 1 ) System . out . println ( " There ▁ are ▁ 3 ▁ common ▁ tangents " + " ▁ between ▁ the ▁ circles . " ) ; else if ( t < 0 ) System . out . println ( " There ▁ are ▁ 4 ▁ common ▁ tangents " + " ▁ between ▁ the ▁ circles . " ) ; else System . out . println ( " There ▁ are ▁ 2 ▁ common ▁ tangents " + " ▁ between ▁ the ▁ circles . " ) ; } }
Ratio of the distance between the centers of the circles and the point of intersection of two transverse common tangents to the circles | Java program to find the ratio of the distance between the centres of the circles and the point of intersection of two transverse common tangents to the circles which do not touch each other ; Function to find the ratio ; Driver code
import java . io . * ; class GFG { static int GCD ( int a , int b ) { return ( b != 0 ? GCD ( b , a % b ) : a ) ; } static void ratiotang ( int r1 , int r2 ) { System . out . println ( " The ▁ ratio ▁ is ▁ " + r1 / GCD ( r1 , r2 ) + " : " + r2 / GCD ( r1 , r2 ) ) ; } public static void main ( String [ ] args ) { int r1 = 4 , r2 = 8 ; ratiotang ( r1 , r2 ) ; } }
Program to find the number of region in Planar Graph | Java implementation of the approach ; Function to return the number of regions in a Planar Graph ; Driver code
import java . io . * ; class GFG { static int Regions ( int Vertices , int Edges ) { int R = Edges + 2 - Vertices ; return R ; } public static void main ( String [ ] args ) { int V = 5 , E = 7 ; System . out . println ( Regions ( V , E ) ) ; } }
Ratio of the distance between the centers of the circles and the point of intersection of two direct common tangents to the circles | Java program to find the ratio of the distance between the centers of the circles and the point of intersection of two direct common tangents to the circles which do not touch each other ; Function to find the GCD ; Function to find the ratio ; Driver code
class GFG { static int GCD ( int a , int b ) { return ( b != 0 ? GCD ( b , a % b ) : a ) ; } static void ratiotang ( int r1 , int r2 ) { System . out . println ( " The ▁ ratio ▁ is ▁ " + r1 / GCD ( r1 , r2 ) + " ▁ : ▁ " + r2 / GCD ( r1 , r2 ) ) ; } public static void main ( String args [ ] ) { int r1 = 4 , r2 = 6 ; ratiotang ( r1 , r2 ) ; } }
Length of the transverse common tangent between the two non intersecting circles | Java program to find the length of the transverse common tangent between two circles which do not touch each other ; Function to find the length of the transverse common tangent ; Driver code
class GFG { static void lengthOfTangent ( double r1 , double r2 , double d ) { System . out . println ( " The ▁ length ▁ of ▁ the ▁ transverse " + " ▁ common ▁ tangent ▁ is ▁ " + Math . sqrt ( Math . pow ( d , 2 ) - Math . pow ( ( r1 + r2 ) , 2 ) ) ) ; } public static void main ( String args [ ] ) { double r1 = 4 , r2 = 6 , d = 12 ; lengthOfTangent ( r1 , r2 , d ) ; } }
Area of plot remaining at the end | Java implementation of the approach ; Function to return the area of the remaining plot ; Continue while plot has positive area and there are persons left ; If length > breadth then subtract breadth from length ; Else subtract length from breadth ; Driver code
class GFG { static int remainingArea ( int N , int M , int K ) { while ( K -- > 0 && N > 0 && M > 0 ) { if ( N > M ) N = N - M ; else M = M - N ; } if ( N > 0 && M > 0 ) return N * M ; else return 0 ; } public static void main ( String [ ] args ) { int N = 5 , M = 3 , K = 2 ; System . out . println ( remainingArea ( N , M , K ) ) ; } }
Length of the direct common tangent between two externally touching circles | Java program to find the length of the direct common tangent between two circles which externally touch each other ; Function to find the length of the direct common tangent ; Driver code
class GFG { static void lengtang ( double r1 , double r2 ) { System . out . println ( " The ▁ length ▁ of ▁ the ▁ " + " direct ▁ common ▁ tangent ▁ is ▁ " + ( 2 * Math . sqrt ( r1 * r2 ) ) ) ; } public static void main ( String [ ] args ) { double r1 = 5 , r2 = 9 ; lengtang ( r1 , r2 ) ; } }
Shortest distance between a point and a circle | Java program to find the Shortest distance between a point and a circle ; Function to find the shortest distance ; Driver code
class GFG { static void dist ( double x1 , double y1 , double x2 , double y2 , double r ) { System . out . println ( " The ▁ shortest ▁ distance ▁ " + " between ▁ a ▁ point ▁ and ▁ a ▁ circle ▁ is ▁ " + ( Math . sqrt ( ( Math . pow ( ( x2 - x1 ) , 2 ) ) + ( Math . pow ( ( y2 - y1 ) , 2 ) ) ) - r ) ) ; } public static void main ( String [ ] args ) { double x1 = 4 , y1 = 6 , x2 = 35 , y2 = 42 , r = 5 ; dist ( x1 , y1 , x2 , y2 , r ) ; } }
Distance between two parallel lines | Java program find the distance between two parallel lines ; Function to find the distance between parallel lines ; Driver Code
class GFG { static double dist ( double m , double b1 , double b2 ) { double d = Math . abs ( b2 - b1 ) / ( ( m * m ) - 1 ) ; return d ; } public static void main ( String [ ] args ) { double m = 2 , b1 = 4 , b2 = 3 ; System . out . println ( dist ( m , b1 , b2 ) ) ; } }
Length of the normal from origin on a straight line whose intercepts are given | Java implementation of the approach ; Function to find the normal of the straight line ; Length of the normal ; Driver code
class GFG { static float normal ( float m , float n ) { float N = ( float ) ( ( Math . abs ( m ) * Math . abs ( n ) ) / Math . sqrt ( ( Math . abs ( m ) * Math . abs ( m ) ) + ( Math . abs ( n ) * Math . abs ( n ) ) ) ) ; return N ; } public static void main ( String [ ] args ) { float m = - 5 , n = 3 ; System . out . println ( normal ( m , n ) ) ; } }
Check if it is possible to create a polygon with given n sides | Java implementation of the approach ; Function that returns true if it is possible to form a polygon with the given sides ; Sum stores the sum of all the sides and maxS stores the length of the largest side ; If the length of the largest side is less than the sum of the other remaining sides ; Driver code
class GFG { static boolean isPossible ( int a [ ] , int n ) { int sum = 0 , maxS = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += a [ i ] ; maxS = Math . max ( a [ i ] , maxS ) ; } if ( ( sum - maxS ) > maxS ) return true ; return false ; } public static void main ( String [ ] args ) { int a [ ] = { 2 , 3 , 4 } ; int n = a . length ; if ( isPossible ( a , n ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
Find the area of the shaded region formed by the intersection of four semicircles in a square | Java implementation of the approach ; Function to return the area of the shaded region ; Area of the square ; Area of the semicircle ; There are 4 semicircles shadedArea = Area of 4 semicircles - Area of square ; Driver code
class GFG { static float findAreaShaded ( float a ) { float sqArea = a * a ; float semiCircleArea = ( float ) ( 3.14 * ( a * a ) / 8 ) ; float ShadedArea = 4 * semiCircleArea - sqArea ; return ShadedArea ; } public static void main ( String [ ] args ) { float a = 10 ; System . out . println ( findAreaShaded ( a ) ) ; } }
Number of steps required to reach point ( x , y ) from ( 0 , 0 ) using zig | Java program to find the number of steps required to reach ( x , y ) from ( 0 , 0 ) following a zig - zag path ; Function to return the required position ; Driver Code
class GfG { static int countSteps ( int x , int y ) { if ( x < y ) { return x + y + 2 * ( ( y - x ) / 2 ) ; } else { return x + y + 2 * ( ( ( x - y ) + 1 ) / 2 ) ; } } public static void main ( String [ ] args ) { int x = 4 , y = 3 ; System . out . println ( countSteps ( x , y ) ) ; } }
Find whether only two parallel lines contain all coordinates points or not | Java implementation of the above approach ; Find if slope is good with only two intercept ; if set of lines have only two distinct intercept ; Function to check if required solution exist ; check the result by processing the slope by starting three points ; Driver code
import java . util . * ; class GfG { static boolean isSlopeGood ( double slope , int arr [ ] , int n ) { Set < Double > setOfLines = new HashSet < Double > ( ) ; for ( int i = 0 ; i < n ; i ++ ) setOfLines . add ( arr [ i ] - slope * ( i ) ) ; return setOfLines . size ( ) == 2 ; } static boolean checkForParallel ( int arr [ ] , int n ) { boolean slope1 = isSlopeGood ( arr [ 1 ] - arr [ 0 ] , arr , n ) ; boolean slope2 = isSlopeGood ( arr [ 2 ] - arr [ 1 ] , arr , n ) ; boolean slope3 = isSlopeGood ( ( arr [ 2 ] - arr [ 0 ] ) / 2 , arr , n ) ; return ( slope1 == true slope2 == true slope3 == true ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 6 , 3 , 8 , 5 } ; int n = arr . length ; if ( checkForParallel ( arr , n ) == true ) System . out . println ( "1" ) ; else System . out . println ( "0" ) ; } }
Check whether the point ( x , y ) lies on a given line | Java implementation of the approach ; Function that return true if the given point lies on the given line ; If ( x , y ) satisfies the equation of the line ; Driver code
class GFG { static boolean pointIsOnLine ( int m , int c , int x , int y ) { if ( y == ( ( m * x ) + c ) ) return true ; return false ; } public static void main ( String [ ] args ) { int m = 3 , c = 2 ; int x = 1 , y = 5 ; if ( pointIsOnLine ( m , c , x , y ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
Find the coordinates of the fourth vertex of a rectangle with given 3 vertices | Java implementation of the approach ; Function that return the coordinates of the fourth vertex of the rectangle ; Save the coordinates of the given vertices of the rectangle ; 1 - based indexing ; Driver Code
import java . util . HashMap ; import java . util . Map ; class GfG { static Pair < Integer , Integer > findFourthVertex ( int n , int m , String s [ ] ) { HashMap < Integer , Integer > row = new HashMap < > ( ) ; HashMap < Integer , Integer > col = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( s [ i ] . charAt ( j ) == ' * ' ) { if ( row . containsKey ( i ) ) { row . put ( i , row . get ( i ) + 1 ) ; } else { row . put ( i , 1 ) ; } if ( col . containsKey ( j ) ) { col . put ( j , col . get ( j ) + 1 ) ; } else { col . put ( j , 1 ) ; } } } } int x = 0 , y = 0 ; for ( Map . Entry < Integer , Integer > entry : row . entrySet ( ) ) { if ( entry . getValue ( ) == 1 ) x = entry . getKey ( ) ; } for ( Map . Entry < Integer , Integer > entry : col . entrySet ( ) ) { if ( entry . getValue ( ) == 1 ) y = entry . getKey ( ) ; } Pair < Integer , Integer > ans = new Pair < > ( x + 1 , y + 1 ) ; return ans ; } public static void main ( String [ ] args ) { String s [ ] = { " * . * " , " * . . " , " . . . " } ; int n = s . length ; int m = s [ 0 ] . length ( ) ; Pair < Integer , Integer > rs = findFourthVertex ( n , m , s ) ; System . out . println ( rs . first + " ▁ " + rs . second ) ; } } class Pair < A , B > { A first ; B second ; public Pair ( A first , B second ) { this . first = first ; this . second = second ; } }