# Find a point that lies inside exactly K given squares

Given an integer K and an array arr each of whose element x represents a square with two of its vertices as (0, 0) and (x, x). The task is to find a point which lies in exactly K squares.

Examples:

Input: arr[] = {1, 2, 3, 4}, K = 2
Output: (3, 3)
The point (3, 3) lies inside 3rd and 4th square only.

Input: arr[] = {8, 1, 55, 90}, K = 3
Output: (8, 8)

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Since all squares have a common corner point (0, 0), any point which lies in any square would also lie in any larger square. Hence, we can simply print the other corner of the Kth largest square.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `int` `PointInKSquares(``int` `n, ``int` `a[], ``int` `k) ` `{ ` `    ``sort(a, a + n); ` `    ``return` `a[n - k]; ` `} ` ` `  `// Driver Program to test above function ` `int` `main() ` `{ ` `    ``int` `k = 2; ` `    ``int` `a[] = { 1, 2, 3, 4 }; ` `    ``int` `n = ``sizeof``(a) / ``sizeof``(a); ` ` `  `    ``int` `x = PointInKSquares(n, a, k); ` `    ``cout << ``"("` `<< x << ``", "` `<< x << ``")"``; ` `} `

## Java

 `// Java implementation of the approach ` ` `  `import` `java.io.*; ` `import` `java.util.*; ` `class` `GFG { ` ` `  ` `  `static` `int` `PointInKSquares(``int` `n, ``int` `a[], ``int` `k) ` `{ ` `    ``Arrays.sort(a); ` `    ``return` `a[n - k]; ` `} ` ` `  `// Driver Program to test above function ` ` `  `    ``public` `static` `void` `main (String[] args) { ` `            ``int` `k = ``2``; ` `    ``int` `[]a = { ``1``, ``2``, ``3``, ``4` `}; ` `    ``int` `n = a.length; ` ` `  `    ``int` `x = PointInKSquares(n, a, k); ` `    ``System.out.println( ``"("` `+ x + ``", "` `+ x +``")"``); ` ` `  `     `  `    ``} ` `} ` `// This code is contributed by anuj_67.. `

## Python3

 `# Python 3 implementation of the ` `# above approach ` `def` `PointInKSquares(n, a, k) : ` `     `  `    ``a.sort() ` `    ``return` `a[n ``-` `k] ` ` `  `# Driver Code ` `if` `__name__ ``=``=` `"__main__"` `: ` `     `  `    ``k ``=` `2` `    ``a ``=` `[``1``, ``2``, ``3``, ``4``] ` `    ``n ``=` `len``(a) ` `     `  `    ``x ``=` `PointInKSquares(n, a, k) ` `    ``print``(``"("``, x, ``","``, x, ``")"``) ` ` `  `# This code is contributed by Ryuga `

## C#

 `// C# implementation of the approach  ` `using` `System;  ` `class` `GFG  ` `{  ` ` `  `static` `int` `PointInKSquares(``int` `n,  ` `                           ``int` `[]a, ``int` `k)  ` `{  ` `    ``Array.Sort(a);  ` `    ``return` `a[n - k];  ` `}  ` ` `  `// Driver Code ` `public` `static` `void` `Main (String[] args)  ` `{  ` `    ``int` `k = 2;  ` `    ``int` `[]a = { 1, 2, 3, 4 };  ` `    ``int` `n = a.Length;  ` `     `  `    ``int` `x = PointInKSquares(n, a, k);  ` `    ``Console.WriteLine(``"("` `+ x + ``", "` `+ x +``")"``);  ` `}  ` `}  ` ` `  `// This code is contributed  ` `// by Arnab Kundu `

## PHP

 ` `

Output:

```(3, 3)
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.