The Wayback Machine - https://web.archive.org/web/20240823160309/https://www.geeksforgeeks.org/find-a-point-that-lies-inside-exactly-k-given-squares/
Open In App

Find a point that lies inside exactly K given squares

Last Updated : 09 Sep, 2022
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow
Solve Problem
Basic
28.66%
11.2K

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) 

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 <bits/stdc++.h>
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[0]);
 
    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




<?php
// PHP implementation of the approach
 
function PointInKSquares($n, $a, $k)
{
    sort($a);
    return $a[$n - $k];
}
 
// Driver Code
$k = 2;
$a = array(1, 2, 3, 4);
$n = sizeof($a);
 
$x = PointInKSquares($n, $a, $k);
echo "(" . $x . ", " . $x . ")";
 
// This code is contributed
// by Akanksha Rai
?>


Javascript




<script>
 
// Javascript implementation of the approach
 
 
function PointInKSquares(n, a, k)
{
    a.sort();
    return a[n - k];
}
 
// Driver Program to test above function
  
    let k = 2;
    let a = [ 1, 2, 3, 4 ];
    let n = a.length;
 
    let x = PointInKSquares(n, a, k);
    document.write("(" + x + ", " + x + ")");
 
// This code is contributed by Mayank Tyagi
 
</script>


Output

(3, 3)

Complexity Analysis:

  • Time Complexity: O(nlog(n)), since to sort the elements of the array, the best algorithm takes (n * log n) time.
  • Auxiliary Space: O(1), since no extra space has been taken.


Similar Reads

Check if the given point lies inside given N points of a Convex Polygon
Given coordinates of the N points of a Convex Polygon. The task is to check if the given point (X, Y) lies inside the polygon. Examples: Input: N = 7, Points: {(1, 1), (2, 1), (3, 1), (4, 1), (4, 2), (4, 3), (4, 4)}, Query: X = 3, Y = 2 Below is the image of plotting of the given points: Output: YES Input: N = 7, Points: {(1, 1), (2, 1), (3, 1), (4
15 min read
Find if a point lies inside a Circle
Given a circle (coordinates of centre and radius) and a point (coordinate), find if the point lies inside or on the circle, or not. Examples : Input: x = 4, y = 4 // Given Point circle_x = 1, circle_y = 1, rad = 6; // Circle Output: Inside Input: x = 3, y = 3 // Given Point circle_x = 0, circle_y = 1, rad = 2; // Circle Output: Outside We strongly
5 min read
Find if a point lies inside, outside or on the circumcircle of three points A, B, C
Given four non-collinear points A, B, C, and P. The task is to find whether point P lies inside, outside or on the circumcircle formed by points A, B, and C.Examples Input: A = {2, 8}, B = {2, 1}, C = {4, 5}, P = {2, 4}; Output: Point (2, 4) is inside the circumcircle. Input: A = {2, 8}, B = {2, 1}, C = {4, 5}, P = {3, 0}; Output: Point (3, 0) lies
15+ min read
Check whether a given point lies on or inside the rectangle | Set 3
Given two numbers a and b where b &lt; a form a rectangle with points (0, b), (b, 0), (a-b, b), (b, a-b). Given a point (x, y), the task is to check whether this point lies inside or on the rectangle or not. Examples: Input: a = 7, b = 2, x = 5, y = 2; Output: Given point does not lie on the rectangle Input: a = 7, b = 2, x = 4, y = 5; Output: Give
6 min read
Check whether a given point lies inside a triangle or not
Given three corner points of a triangle, and one more point P. Write a function to check whether P lies within the triangle or not. Example: Input: A = (0, 0), B = (10, 30), C = (20, 0), P(10, 15)Output: InsideExplanation: B(10,30) / \ / \ / \ / P \ P' / \ A(0,0) ----------- C(20,0) Input: A = (0, 0), B = (10, 30), C = (20, 0), P(30, 15)Output: Out
15+ min read
How to check if a given point lies inside or outside a polygon?
Given a polygon and a point 'p', find if 'p' lies inside the polygon or not. The points lying on the border are considered inside. Examples: Recommended ProblemPlease solve it on PRACTICE first, before moving on to the solution Solve ProblemApproach: The idea to solve this problem is based on How to check if two given line segments intersect, and t
9 min read
Check whether a given point lies inside a rectangle or not
Given four points of a rectangle, and one more point P. Write a function to check whether P lies within the given rectangle or not.Examples:   Input : R = [(10, 10), (10, -10), (-10, -10), (-10, 10)] P = (0, 0)Output : yesIllustration : Input : R = [(10, 10), (10, -10), (-10, -10), (-10, 10)], P = (20, 20)Output : noIllustration : Prerequisite: Che
14 min read
Check whether a point lies inside a sphere or not
Given co-ordinates of the center of a sphere (cx, cy, cz) and its radius r. Our task is to check whether a point (x, y, z) lies inside, outside or on this sphere.Examples: Input : Centre(0, 0, 0) Radius 3 Point(1, 1, 1) Output :Point is inside the sphere Input :Centre(0, 0, 0) Radius 3 Point(2, 1, 2) Output :Point lies on the sphere Input :Centre(0
7 min read
Check if a point lies inside a rectangle | Set-2
Given coordinates of bottom-left and top-right corners of a rectangle. Check if a point (x, y) lies inside this rectangle or not. Examples: Input: bottom-left: (0, 0), top-right: (10, 8), point: (1, 5) Output: Yes Input: bottom-left: (-1, 4), top-right:(2, 3), point:(0, 4) Output: No This problem is already discussed in a previous post. In this pos
6 min read
Check if given point lies in range of any of the given towers
Given a 2D array arr[][] consisting of N rows of the form {Xi, Yi, Ri} such that (Xi, Yi) represents the position of a tower and Ri represents the network range of that tower. Given two integers X and Y, the task is to check if the point (X, Y) lies in the network range of the towers or not. Examples: Input: arr[][] = { {1, 1, 3}, {10, 10, 5}, {15,
7 min read
Check if a given circle lies completely inside the ring formed by two concentric circles
Given two circles of radius r and R, both have their centre at the origin. Now, given another circle of radius r1 and centre at (x1, y1). Check, if the third circle(circle of radius r1) lies completely inside the ring formed by two circles of radius r and R.Examples : Input : r = 8 R = 4 r1 = 2 x1 = 6 y1 = 0 Output : yes Input : r = 8 R = 4 r1 = 2
6 min read
Check if given point lies on the Square Spiral Path
Given a 2D Graph, present initially at (0, 0). Print "YES" if the given point (X, Y) lies on the spiral path, otherwise "NO". The spiral path is to be followed ln a sequential manner as given below: Move one unit right side from the current position.Move two units upwards from the current position.Move three units left side from the current positio
6 min read
Check whether the point (x, y) lies on a given line
Given the values of m and c for the equation of a line y = (m * x) + c, the task is to find whether the point (x, y) lies on the given line. Examples: Input: m = 3, c = 2, x = 1, y = 5 Output: Yes m * x + c = 3 * 1 + 2 = 3 + 2 = 5 which is equal to y Hence, the given point satisfies the line's equation Input: m = 5, c = 2, x = 2, y = 5 Output: No A
4 min read
Count points from an array that lies inside a semi-circle
Given two pairs (X, Y), (P, Q) and R the coordinate of the center of semi-circle, coordinate of the intersection of semicircle and diameter of the semicircle and, the radius of the semicircle, and an array arr[] of dimension N*2 consisting of the coordinates of few points, the task is to find the number of points from the array that lies inside or
7 min read
Count of integral coordinates that lies inside a Square
Given lower left and upper right coordinates (x1, y1) and (x2, y2) of a square, the task is to count the number of integral coordinates that lies strictly inside the square.Examples: Input: x1 = 1, y1 = 1, x2 = 5, x3 = 5 Output: 9 Explanation: Below is the square for the given coordinates: Input: x1 = 1, y1 = 1, x2 = 4, x3 = 4 Output: 4 Approach: T
4 min read
Check if a circle lies inside another circle or not
Given two circles with radii and centres given. The task is to check whether the smaller circle lies inside the bigger circle or not.  Examples:   Input: x1 = 10, y1 = 8, x2 = 1, y2 = 2, r1 = 30, r2 = 10 Output: The smaller circle lies completely inside the bigger circle without touching each other at a point of circumference. Input :x1 = 7, y1 = 8
6 min read
Find the number of squares inside the given square grid
Given a grid of side N * N, the task is to find the total number of squares that exist inside it. All squares selected can be of any length.Examples: Input: N = 1 Output: 1 Input: N = 2 Output: 5 Input: N = 4 Output: 30 Approach 1: Taking a few examples, it can be observed that for a grid on size N * N, the number of squares inside it will be 12 +
4 min read
Find time required to reach point N from point 0 according to given rules
Given two positive integers X and Y and N number of points arranged in a line, the task is to find the time required to reach point N from point 0 according to the following rules: Every point has one barrier that closes after every Y minutes and remains closed for the next Y minutes.On reaching a point if the barrier is closed, then the person nee
7 min read
Find minimum radius such that atleast k point lie inside the circle
Given a positive integer K, a circle center at (0, 0) and coordinates of some points. The task is to find minimum radius of the circle so that at-least k points lie inside the circle. Output the square of the minimum radius. Examples: Input : (1, 1), (-1, -1), (1, -1), k = 3Output : 2We need a circle of radius at least 2to include 3 points.Input :
12 min read
Find intersection point of lines inside a section
Given N lines in two-dimensional space in y = mx + b form and a vertical section. We need to find out whether there is an intersection point inside the given section or not. Examples: In below diagram four lines are there, L1 : y = x + 2 L2 : y = -x + 7 L3 : y = -3 L4 : y = 2x – 7 and vertical section is given from x = 2 to x = 4 We can see that in
9 min read
Maximum number of 2x2 squares that can be fit inside a right isosceles triangle
What is the maximum number of squares of size 2x2 units that can be fit in a right-angled isosceles triangle of a given base (in units). A side of the square must be parallel to the base of the triangle. Examples: Input : 8 Output : 6 Please refer below diagram for explanation. Input : 7 Output : 3Recommended PracticeTriangle and SquareTry It! Sinc
4 min read
Find the GCD that lies in given range
Given two positive integer a and b and a range [low, high]. The task is to find the greatest common divisor of a and b which lie in the given range. If no divisor exist in the range, print -1.Examples: Input : a = 9, b = 27, low = 1, high = 5 Output : 3 3 is the highest number that lies in range [1, 5] and is common divisor of 9 and 27. Input : a =
7 min read
Find two distinct numbers such that their LCM lies in given range
Given two numbers L and R, the task is to find two distinct minimum positive integers X and Y such that whose LCM lies in the range [L, R]. If there doesn't exist any value of X and Y then print "-1". Examples: Input: L = 3, R = 8 Output: x = 3, y=6Explanation:LCM of 3 and 6 is 6 which is in range 3, 8 Input: L = 88, R = 90Output: -1Explanation:Min
4 min read
Find the most honest person from given statements of truths and lies
Given a binary matrix of size N * N, mat[][]. Value at cell (i, j) indicates what ith person says about jth person. A person x is considered honest if he always tells the truth. If person x speaks lie sometimes and truth sometimes, then he is a liar. If mat[i][j] = 0, then ith person says jth person is a liar.If mat[i][j] = 1, then ith person says
8 min read
Rearrange two given arrays such that sum of same indexed elements lies within given range
Given two arrays arr1[] and arr2[] consisting of N positive integers and an even integer K, the task is to check if the sum of the same indexed elements in the two arrays lie in the range [K/2, K] after rearranging the given arrays or not. If it is possible to obtain such an arrangement, then print "Yes". Otherwise, print "No". Examples: Input: arr
6 min read
Count pairs from a given array whose product lies in a given range
Given an array arr[] of size N, and integers L and R, the task is to count the number of pairs [arri , arrj] such that i &lt; j and the product of arr[i] * arr[j] lies in the given range [L, R], i.e. L ? arr[i] * arr[j] ? R. Examples: Input: arr[ ] = {4, 1, 2, 5}, L = 4, R = 9Output: 3Explanation: Valid pairs are {4, 1}, {1, 5} and {4, 2}. Input: a
13 min read
Check if a point is inside, outside or on a Hyperbola
Given a hyperbola centered at (h, k), with semi-major axis a, semi-minor axis b, both aligned with the Cartesian plane, the task is to determine if the point (x, y) lies within the area bounded by the hyperbola or not. Examples: Input: h = 0, k = 0, x = 2, y = 1, a = 4, b = 5 Output: Inside Input: h = 1, k = 2, x = 200, y = 100, a = 6, b = 5Output:
5 min read
Make N pairs from Array as (X, Y) coordinate point that are enclosed inside a minimum area rectangle
Given a number N, and an array A[] of size 2N, the task is to make N pairs of these array elements and place them on an X-Y coordinate plane, such that they are enclosed inside a minimum area rectangle( with sides parallel to the X-axis and Y-axis) and print the area of the rectangle. Examples: Input: N = 4, A = {1, 4, 2, 5, 3, 6, 7, 8}Output: 9Exp
7 min read
Check if a point is inside, outside or on the ellipse
Given an ellipse centered at (h, k), with semi-major axis a, and semi-minor axis b, both aligned with the Cartesian plane. The task is to determine if the point (x, y) is within the area bounded by the ellipse. Examples:  Input: h = 0, k = 0, x = 2, y = 1, a = 4, b = 5 Output: InsideInput: h = 1, k = 2, x = 200, y = 100, a = 6, b = 5Output: Outside
10 min read
Triangle with no point inside
Given N points in 2-dimensional space, we need to find three points such that triangle made by choosing these points should not contain any other points inside. All given points will not lie on the same line so solution will always exist. Examples: In above diagram possible triangle with no point inside can be formed by choosing these triplets,[(0,
15+ min read