The Wayback Machine - https://web.archive.org/web/20240823160307/https://www.geeksforgeeks.org/find-missing-point-parallelogram/
Open In App

Find the Missing Point of Parallelogram

Last Updated : 03 Aug, 2022
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Given three coordinate points A, B and C, find the missing point D such that ABCD can be a parallelogram.
Examples : 
 

Input : A = (1, 0)
        B = (1, 1)
        C = (0, 1)
Output : 0, 0
Explanation:
The three input points form a unit
square with the point (0, 0)

Input : A = (5, 0)
        B = (1, 1)
        C = (2, 5)
Output : 6, 4

As shown in below diagram, there can be multiple possible outputs, we need to print any one of them. 
 

Parallelogram

 

A quadrilateral is said to be a parallelogram if its opposite sides are parallel and equal in length. 
 

quadrilateral

As we’re given three points of the parallelogram, we can find the slope of the missing sides as well as their lengths. 
The algorithm can be explained as follows
Let R be the missing point. Now from definition, we have 
 

  • Length of PR = Length of QS = L1 (Opposite sides are equal)
  • Slope of PR = Slope of QS = M1 (Opposite sides are parallel)
  • Length of PQ = Length of RS = L2 (Opposite sides are equal)
  • Slope of PQ= Slope of RS = M2 (Opposite sides are parallel)

Thus we can find the points at a distance L1 from P having slope M1 as mentioned in below article : 
Find points at a given distance on a line of given slope.
Now one of the points will satisfy the above conditions which can easily be checked (using either condition 3 or 4)

Below is the implementation of the above approach: 

C++




// C++ program to find missing point of a
// parallelogram
#include <bits/stdc++.h>
using namespace std;
 
// struct to represent a co-ordinate point
struct Point {
    float x, y;
    Point()
    {
        x = y = 0;
    }
    Point(float a, float b)
    {
        x = a, y = b;
    }
};
 
// given a source point, slope(m) of line
// passing through it this function calculates
// and return two points at a distance l away
// from the source
pair<Point, Point> findPoints(Point source,
                              float m, float l)
{
    Point a, b;
 
    // slope is 0
    if (m == 0) {
        a.x = source.x + l;
        a.y = source.y;
 
        b.x = source.x - l;
        b.y = source.y;
    }
 
    // slope if infinity
    else if (m == std::numeric_limits<float>::max()) {
        a.x = source.x;
        a.y = source.y + l;
 
        b.x = source.x;
        b.y = source.y - l;
    }
 
    // normal case
    else {
        float dx = (l / sqrt(1 + (m * m)));
        float dy = m * dx;
        a.x = source.x + dx, a.y = source.y + dy;
        b.x = source.x - dx, b.y = source.y - dy;
    }
 
    return pair<Point, Point>(a, b);
}
 
// given two points, this function calculates
// the slope of the line/ passing through the
// points
float findSlope(Point p, Point q)
{
    if (p.y == q.y)
        return 0;
    if (p.x == q.x)
        return std::numeric_limits<float>::max();
    return (q.y - p.y) / (q.x - p.x);
}
 
// calculates the distance between two points
float findDistance(Point p, Point q)
{
    return sqrt(pow((q.x - p.x), 2) + pow((q.y - p.y), 2));
}
 
// given three points, it prints a point such
// that a parallelogram is formed
void findMissingPoint(Point a, Point b, Point c)
{
    // calculate points originating from a
    pair<Point, Point> d = findPoints(a, findSlope(b, c),
                                      findDistance(b, c));
 
    // now check which of the two points satisfy
    // the conditions
    if (findDistance(d.first, c) == findDistance(a, b))
        cout << d.first.x << ", " << d.first.y << endl;
    else
        cout << d.second.x << ", " << d.second.y << endl;
}
 
// Driver code
int main()
{
    findMissingPoint(Point(1, 0), Point(1, 1), Point(0, 1));
    findMissingPoint(Point(5, 0), Point(1, 1), Point(2, 5));
    return 0;
}


Java




// Java program to find missing point of a
// parallelogram
import java.util.*;
 
// struct to represent a co-ordinate point
class Point {
  public double x, y;
  public Point()
  {
    x = 0;
    y = 0;
  }
 
  public Point(double a, double b)
  {
    x = a;
    y = b;
  }
};
 
class GFG {
  // given a source point, slope(m) of line
  // passing through it this function calculates
  // and return two points at a distance l away
  // from the source
  static Point[] findPoints(Point source, double m,
                            double l)
  {
    Point a = new Point();
    Point b = new Point();
 
    // slope is 0
    if (m == 0) {
      a.x = source.x + l;
      a.y = source.y;
 
      b.x = source.x - l;
      b.y = source.y;
    }
 
    // slope if infinity
    else if (m == Double.MAX_VALUE) {
      a.x = source.x;
      a.y = source.y + l;
 
      b.x = source.x;
      b.y = source.y - l;
    }
 
    // normal case
    else {
      double dx = (l / Math.sqrt(1 + (m * m)));
      double dy = m * dx;
      a.x = source.x + dx;
      a.y = source.y + dy;
      b.x = source.x - dx;
      b.y = source.y - dy;
    }
 
    Point[] res = { a, b };
    return res;
  }
 
  // given two points, this function calculates
  // the slope of the line/ passing through the
  // points
  static double findSlope(Point p, Point q)
  {
    if (p.y == q.y)
      return 0;
    if (p.x == q.x)
      return Double.MAX_VALUE;
    return (q.y - p.y) / (q.x - p.x);
  }
 
  // calculates the distance between two points
  static double findDistance(Point p, Point q)
  {
    return Math.sqrt(Math.pow((q.x - p.x), 2)
                     + Math.pow((q.y - p.y), 2));
  }
 
  // given three points, it prints a point such
  // that a parallelogram is formed
  static void findMissingPoint(Point a, Point b, Point c)
  {
    // calculate points originating from a
    Point[] d = findPoints(a, findSlope(b, c),
                           findDistance(b, c));
 
    // now check which of the two points satisfy
    // the conditions
    if (findDistance(d[0], c) == findDistance(a, b))
      System.out.println(d[0].x + ", " + d[0].y);
    else
      System.out.println(d[1].x + ", " + d[1].y);
  }
 
  // Driver code
  public static void main(String[] args)
  {
    findMissingPoint(new Point(1, 0), new Point(1, 1),
                     new Point(0, 1));
    findMissingPoint(new Point(5, 0), new Point(1, 1),
                     new Point(2, 5));
  }
}
 
// This code is contributed by phasing17


Python3




# Python program to find missing point of a
# parallelogram
 
import math as Math
FLOAT_MAX = 3.40282e+38
 
# given a source point, slope(m) of line
# passing through it this function calculates
# and return two points at a distance l away
# from the source
 
 
def findPoints(source, m, l):
    a = [0] * (2)
    b = [0] * (2)
 
    # slope is 0
    if (m == 0):
        a[0] = source[0] + l
        a[1] = source[1]
 
        b[0] = source[0] - l
        b[1] = source[1]
    # slope if infinity
    elif (m == FLOAT_MAX):
        a[0] = source[0]
        a[1] = source[1] + l
 
        b[0] = source[0]
        b[1] = source[1] - l
    # normal case
    else:
        dx = (l / ((1 + (m * m)) ** 0.5))
        dy = m * dx
        a[0] = source[0] + dx
        a[1] = source[1] + dy
        b[0] = source[0] - dx
        b[1] = source[1] - dy
 
    return [a, b]
 
 
# given two points, this function calculates
# the slope of the line/ passing through the
# points
def findSlope(p, q):
    if (p[1] == q[1]):
        return 0
    if (p[0] == q[0]):
        return FLOAT_MAX
    return (q[1] - p[1]) / (q[0] - p[0])
 
 
# calculates the distance between two points
def findDistance(p, q):
    return Math.sqrt(Math.pow((q[0] - p[0]), 2) + Math.pow((q[1] - p[1]), 2))
 
 
# given three points, it prints a point such
# that a parallelogram is formed
def findMissingPoint(a, b, c):
    # calculate points originating from a
    d = findPoints(a, findSlope(b, c), findDistance(b, c))
 
    # now check which of the two points satisfy
    # the conditions
    if (findDistance(d[0], c) == findDistance(a, b)):
        print(f"{(int)(d[0][0])}, {(int)(d[0][1])}")
 
    else:
        print(f"{(int)(d[1][0])}, {(int)(d[1][1])}")
 
 
# Driver code
Point1 = [1, 0]
Point2 = [1, 1]
Point3 = [0, 1]
findMissingPoint(Point1, Point2, Point3)
 
Point1 = [5, 0]
Point2 = [1, 1]
Point3 = [2, 5]
findMissingPoint(Point1, Point2, Point3)
 
# The code is contributed by Saurabh Jaiswal


C#




// C# program to find missing point of a
// parallelogram
using System;
using System.Collections.Generic;
 
// struct to represent a co-ordinate point
class Point {
  public double x, y;
  public Point()
  {
    x = 0;
    y = 0;
  }
 
  public Point(double a, double b)
  {
    x = a;
    y = b;
  }
};
 
class GFG
{
 
  // given a source point, slope(m) of line
  // passing through it this function calculates
  // and return two points at a distance l away
  // from the source
  static Point[] findPoints(Point source, double m,
                            double l)
  {
    Point a = new Point();
    Point b = new Point();
 
    // slope is 0
    if (m == 0) {
      a.x = source.x + l;
      a.y = source.y;
 
      b.x = source.x - l;
      b.y = source.y;
    }
 
    // slope if infinity
    else if (m == Double.MaxValue) {
      a.x = source.x;
      a.y = source.y + l;
 
      b.x = source.x;
      b.y = source.y - l;
    }
 
    // normal case
    else {
      double dx = (l / Math.Sqrt(1 + (m * m)));
      double dy = m * dx;
      a.x = source.x + dx;
      a.y = source.y + dy;
      b.x = source.x - dx;
      b.y = source.y - dy;
    }
 
    Point[] res = { a, b };
    return res;
  }
 
  // given two points, this function calculates
  // the slope of the line/ passing through the
  // points
  static double findSlope(Point p, Point q)
  {
    if (p.y == q.y)
      return 0;
    if (p.x == q.x)
      return Double.MaxValue;
    return (q.y - p.y) / (q.x - p.x);
  }
 
  // calculates the distance between two points
  static double findDistance(Point p, Point q)
  {
    return Math.Sqrt(Math.Pow((q.x - p.x), 2)
                     + Math.Pow((q.y - p.y), 2));
  }
 
  // given three points, it prints a point such
  // that a parallelogram is formed
  static void findMissingPoint(Point a, Point b, Point c)
  {
    // calculate points originating from a
    Point[] d = findPoints(a, findSlope(b, c),
                           findDistance(b, c));
 
    // now check which of the two points satisfy
    // the conditions
    if (findDistance(d[0], c) == findDistance(a, b))
      Console.WriteLine(d[0].x + ", " + d[0].y);
    else
      Console.WriteLine(d[1].x + ", " + d[1].y);
  }
 
  // Driver code
  public static void Main(string[] args)
  {
    findMissingPoint(new Point(1, 0), new Point(1, 1),
                     new Point(0, 1));
    findMissingPoint(new Point(5, 0), new Point(1, 1),
                     new Point(2, 5));
  }
}
 
// This code is contributed by phasing17


Javascript




// JavaScript program to find missing point of a
// parallelogram
 
const FLOAT_MAX = 3.40282e+38;
 
// given a source point, slope(m) of line
// passing through it this function calculates
// and return two points at a distance l away
// from the source
function findPoints(source, m, l)
{
    let a = new Array(2);
    let b = new Array(2);
 
    // slope is 0
    if (m == 0) {
        a[0] = source[0] + l;
        a[1] = source[1];
 
        b[0] = source[0] - l;
        b[1] = source[1];
    }
    // slope if infinity
    else if (m == FLOAT_MAX) {
        a[0] = source[0];
        a[1] = source[1] + l;
 
        b[0] = source[0];
        b[1] = source[1] - l;
    }
    // normal case
    else {
        let dx = (l / Math.sqrt(1 + (m * m)));
        let dy = m * dx;
        a[0] = source[0] + dx, a[1] = source[1] + dy;
        b[0] = source[0] - dx, b[1] = source[1] - dy;
    }
 
    return [a, b];
}
 
// given two points, this function calculates
// the slope of the line/ passing through the
// points
function findSlope(p, q)
{
    if (p[1] == q[1]){
        return 0;
    }
    if (p[0] == q[0]){
        return FLOAT_MAX;
    }
    return (q[1] - p[1]) / (q[0] - p[0]);
}
 
// calculates the distance between two points
function findDistance(p, q)
{
    return Math.sqrt(Math.pow((q[0] - p[0]), 2) + Math.pow((q[1] - p[1]), 2));
}
 
// given three points, it prints a point such
// that a parallelogram is formed
function findMissingPoint(a, b, c)
{
    // calculate points originating from a
    let d = findPoints(a, findSlope(b, c), findDistance(b, c));
 
    // now check which of the two points satisfy
    // the conditions
    if (findDistance(d[0], c) === findDistance(a, b)){
        console.log(d[0][0], ",", d[0][1]);
    }
    else{
        console.log(d[1][0], ",", d[1][1]);
    }    
}
 
// Driver code
{
    let Point1 = [1, 0];
    let Point2 = [1, 1];
    let Point3 = [0, 1];
    findMissingPoint(Point1, Point2, Point3);
     
    Point1 = [5, 0];
    Point2 = [1, 1];
    Point3 = [2, 5];
    findMissingPoint(Point1, Point2, Point3);
}
 
// The code is contributed by Gautam goel (gautamgoel962)


Output : 
 

0, 0
6, 4

Time Complexity: O(log(log n)) since using inbuilt sqrt and log functions 
Auxiliary Space: O(1)

Alternative Approach: 
 

parallelogram

Since the opposite sides are equal, AD = BC and AB = CD, we can calculate the co-ordinates of the missing point (D) as: 
 

AD = BC
(Dx - Ax, Dy - Ay) = (Cx - Bx, Cy - By)
Dx = Ax + Cx - Bx 
Dy = Ay + Cy - By

References: https://math.stackexchange.com/questions/887095/find-the-4th-vertex-of-the-parallelogram
Below is the implementation of above approach:
 

C++




// C++ program to find missing point
// of a parallelogram
#include <bits/stdc++.h>
using namespace std;
 
// main method
int main()
{
   int ax = 5, ay = 0; //coordinates of A
   int bx = 1, by = 1; //coordinates of B
   int cx = 2, cy = 5; //coordinates of C
    cout << ax + cx - bx << ", "
         << ay + cy - by;
    return 0;
}


Java




// Java program to
// find missing point
// of a parallelogram
import java.io.*;
 
class GFG
{
public static void main (String[] args)
{
 
    int ax = 5, ay = 0; //coordinates of A
    int bx = 1, by = 1; //coordinates of B
    int cx = 2, cy = 5; //coordinates of C
    System.out.println(ax + (cx - bx) + ", " +
                       ay + (cy - by));
}
}
 
// This code is contributed by m_kit


Python 3




# Python 3 program to find missing point
# of a parallelogram
 
# Main method
if __name__ == "__main__":
     
    # coordinates of A
     
    ax, ay = 5, 0
     
    # coordinates of B
     
    bx ,by = 1, 1
     
    # coordinates of C
     
    cx ,cy = 2, 5
     
    print(ax + cx - bx , ",", ay + cy - by)
 
# This code is contributed by Smitha


C#




// C# program to
// find missing point
// of a parallelogram
using System;
 
class GFG
{
    static public void Main ()
    {
        int ax = 5, ay = 0; //coordinates of A
        int bx = 1, by = 1; //coordinates of B
        int cx = 2, cy = 5; //coordinates of C
        Console.WriteLine(ax + (cx - bx) + ", " +
                             ay + (cy - by));
    }
}
 
// This code is contributed by ajit


PHP




<?php
// PHP program to find missing
// point of a parallelogram
 
// Driver Code
$ax = 5;
$ay = 0; //coordinates of A
$bx = 1;
$by = 1; //coordinates of B
$cx = 2;
$cy = 5; //coordinates of C
echo $ax + $cx - $bx , ", ",
     $ay + $cy - $by;
 
// This code is contributed by aj_36
?>


Javascript




<script>
    // Javascript program to find missing point of a parallelogram
     
    let ax = 5, ay = 0; //coordinates of A
    let bx = 1, by = 1; //coordinates of B
    let cx = 2, cy = 5; //coordinates of C
    document.write((ax + (cx - bx)) + ", " + (ay + (cy - by)));
     
</script>


Output: 
 

6, 4

Time Complexity: O(1) 
Auxiliary Space: O(1)

 



Previous Article
Next Article

Similar Reads

Find area of parallelogram if vectors of two adjacent sides are given
Given two vectors in form of (xi+yj+zk) of two adjacent sides of a parallelogram. The task is to find out the area of a parallelogram.Example: Input: x1 = 3, y1 = 1, z1 = -2 x2 = 1, y2 = -3, z2 = 4Output: Area = 17.3205081 Input: x1 = 1, y1 = 3, z1 = 2 x2 = 1, y2 = -3, z2 = 4Output: Area = 19.078784028338912 Approach: Suppose we have two vectors a(
5 min read
Program to find the Area of a Parallelogram
Given the sides of a Parallelogram, task is calculate the area of a Parallelogram. Examples: Input: base = 30, height = 40 Output: 1200.000000 As Area of parallelogram = base * height, Therefore, Area = 30 * 40 = 1200.00 Approach: Area of parallelogram = base * height Below is the implementation of the above approach: C/C++ Code #include &lt;iostre
2 min read
Find all possible coordinates of parallelogram
Find all the possible coordinates from the three coordinates to make a parallelogram of a non-zero area.Let's call A, B, and C are the three given points. We can have only the three possible situations:  (1) AB and AC are sides, and BC a diagonal(2) AB and BC are sides, and AC a diagonal (3) BC and AC are sides, and AB a diagonal Hence, we can say
5 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
Program for Circumference of a Parallelogram
Given the sides of Parallelogram then calculate the circumference.Examples : Input: a = 10, b = 8 Output: 36.00 Input: a = 25.12, b = 20.4 Output: 91.04 The opposite sides of a parallelogram are of equal length and parallel. The angles are pairwise equal but not necessarily 90 degree. The circumference of a Parallelogram can be computed as sum of t
3 min read
Program to print the hollow numerical parallelogram
Program to print the hollow numerical parallelogram.Examples : Input : 7 Output : 7777777777777 666666 666666 55555 55555 4444 4444 333 333 22 22 1 1 22 22 333 333 4444 4444 55555 55555 666666 666666 7777777777777 C/C++ Code // C++ program to print the given pattern #include &lt;bits/stdc++.h&gt; using namespace std; // Function for displaying the
9 min read
Area of a triangle inside a parallelogram
Given the base and height of the parallelogram ABCD are b and h respectively. The task is to calculate the area of the triangle â–ēABM (M can be any point on upper side) constructed on the base AB of the parallelogram as shown below: Examples: Input: b = 30, h = 40 Output: 600.000000 Approach: Area of a triangle constructed on the base of parallelogr
3 min read
Perimeter and Area of Varignon's Parallelogram
Given a and b are the lengths of the diagonals AC and BD of a quadrilateral ABCD with the area of quadrilateral as s. The task is to find the perimeter and area of the Varignon's parallelogram PQRS. Note: When we join the mid-points of the sides of any quadrilateral, the new quadrilateral formed inside will always be a parallelogram and this parall
4 min read
Program to calculate area of a parallelogram
Given integers A and B denoting the length of sides of a parallelogram and Y that is the angle between the sides and length of diagonals D1 and D2 of the parallelogram and an angle 0 at the intersection of the diagonal, the task is to find the area of the parallelogram from the information provided. A parallelogram is a type of quadrilateral that h
7 min read
Length of a Diagonal of a Parallelogram using the length of Sides and the other Diagonal
Given two integers A and B, denoting the length of a parallelogram and an integer D, denoting the length of a diagonal, the task is to find the length of another diagonal of the parallelogram. Examples: Input: A = 10, B = 30, D = 20Output: 40.0 Input: A = 6, B = 8, D = 10Output: 10.0 Approach: The relation between sides and diagonals of a parallelo
3 min read
Check whether four points make a parallelogram
Given four points in a 2-dimensional space we need to find out whether they make a parallelogram or not.   A parallelogram has four sides. Two opposite sides are parallel and are of same lengths.   Examples: Points = [(0, 0), (4, 0), (1, 3), (5, 3)]Above points make a parallelogram.Points = [(0, 0), (2, 0), (4, 0), (2, 2)]Above points does not make
15+ min read
Length of diagonal of a parallelogram using adjacent sides and angle between them
Given two integers a and b where a and b represents the length of adjacent sides of a parallelogram and an angle 0 between them, the task is to find the length of diagonal of the parallelogram. Examples: Input: a = 6, b = 10, 0=30Output: 6.14  Input: a = 3, b = 5, 0=45Output: 3.58 Approach: Consider a parallelogram ABCD with sides a and b, now appl
4 min read
Reflection of a point at 180 degree rotation of another point
Given two points coordinates (x1, y1) and (x2, y2)on 2D plane. The task is to find the reflection of (x1, y1) at 180 degree rotation of (x2, y2).Examples: Input : x1 = 0, y1 = 0, x2 = 1, y2 = 1 Output : (2, 2) Input : x1 = 1, y1 = 1, x2 = 2, y2 = 2 Output : (3, 3) Let the reflection point of point (x1, y1) about (x2, y2) be (x', y'). For (x', y') b
4 min read
Rotation of a point about another point
We have already discussed the rotation of a point P about the origin in the Set 1 and Set 2. The rotation of point P about origin with an angle ? in the anti-clockwise direction is given as under: Rotation of P about origin: P * polar(1.0, ?)Rotation of P about point Q Now, we have to rotate the point P not about origin but about a general point Q.
8 min read
Find the smallest missing number
Given a sorted array of n distinct integers where each integer is in the range from 0 to m-1 and m &gt; n. Find the smallest number that is missing from the array. Examples: Input: {0, 1, 2, 6, 9}, n = 5, m = 10 Output: 3 Input: {4, 5, 10, 11}, n = 4, m = 12 Output: 0 Input: {0, 1, 2, 3}, n = 4, m = 5 Output: 4 Input: {0, 1, 2, 3, 4, 5, 6, 7, 10},
15 min read
Find the one missing number in range
Given an array of size n. It is also given that range of numbers is from smallestNumber to smallestNumber + n where smallestNumber is the smallest number in array. The array contains number in this range but one number is missing so the task is to find this missing number. Examples: Input: arr[] = {13, 12, 11, 15}Output: 14 Input: arr[] = {33, 36,
9 min read
Find the missing integer in an array if mean is given
Given an array of size N-1 and the mean of N elements (one element is not given). We need to find the missing value X in the array. Examples: Input : a[] = {2, 4, 20}, Mean = 9Output : Missing Element = 10 Explanation : Mean of (2, 4, 20, 10) is (2 + 4 + 20 + 10)/4 = 9 Let x be the missing element Mean = (a1 + a2 + a3.. + X + ..an)/N. So (a1 + a2 +
4 min read
Find n-variables from n sum equations with one missing
You are given an array a[] of n values as a[1], a[2]...a[n] which are part of n-equations in n-variables where equations are as: Eqn1 =&gt; X2 + X3 +.....+Xn = a1 Eqn2 =&gt; X1 + X3 +.....+Xn = a2 Eqni =&gt; X1 + X2 +...+Xi-1 + Xi+1..+Xn = ai Eqnn =&gt; X1 + X2 +.....+Xn-1 = anAs you have n-equations in n-variables find the value of all variables(X
6 min read
Find the missing digit x from the given expression
Given an alphanumeric string, consisting of a single alphabet X, which represents an expression of the form: A operator B = C where A, B and C denotes integers and the operator can be either of +, -, * or / The task is to evaluate the missing digit X present in any of the integers A, B and C such that the given expression holds to be valid. Example
9 min read
Find the missing value from the given equation a + b = c
Given an equation of the form: a + b = c Out of which any one of the terms [Tex]a [/Tex], [Tex]b [/Tex]or [Tex]c [/Tex]is missing. The task is to find the missing term. Examples: Input: 2 + 6 = ? Output: 8 Input: ? + 3 =6 Output: 3 Approach: Missing numbers can be found simply using the equation [Tex]a + b = c [/Tex]. First, we will find two known
7 min read
Find the missing elements from 1 to M in given N ranges
Given N segments as ranges [L, R] where ranges are non-intersecting and non-overlapping. The task is to find all number between 1 to M that doesn't belong to any of the given ranges. Examples: Input : N = 2, M = 6 Ranges: [1, 2] [4, 5] Output : 3, 6 Explanation: Only 3 and 6 are missing from the above ranges. Input : N = 1, M = 5 Ranges: [2, 4] Out
8 min read
Find the missing elements from 1 to M in given N ranges | Set-2
Given an integer m and n ranges (e.g. [a, b]) which are intersecting and overlapping. The task is to find all the number within the range that doesn’t belong to any of the given ranges. Examples: Input: m = 6, ranges = {{1, 2}, {4, 5}} Output: 3 6 As only 3 and 6 are missing from the given ranges. Input: m = 5, ranges = {{2, 4}} Output: 1 5 Approac
14 min read
Find missing element in a sorted array of consecutive numbers
Given an array arr[] of n distinct integers. Elements are placed sequentially in ascending order with one element missing. The task is to find the missing element.Examples: Input: arr[] = {1, 2, 4, 5, 6, 7, 8, 9} Output: 3Input: arr[] = {-4, -3, -1, 0, 1, 2} Output: -2Input: arr[] = {1, 2, 3, 4} Output: -1 No element is missing. Principles: Look fo
7 min read
Find the smallest positive number missing from an unsorted array : Hashing Implementation
Given an unsorted array with both positive and negative elements including 0. The task is to find the smallest positive number missing from the array in O(N) time. Examples: Input: arr[] = {-5, 2, 0, -1, -10, 15} Output: 1 Input: arr[] = {0, 1, 2, 3, 4, 5} Output: 6 Input: arr[] = {1, 1, 1, 0, -1, -2} Output: 2 We can use hashing to solve this prob
5 min read
Find the smallest positive number missing from an unsorted array | Set 3
Given an unsorted array with both positive and negative elements. The task is to find the smallest positive number missing from the array. Examples: Input: arr[] = {2, 3, 7, 6, 8, -1, -10, 15} Output: 1 Input: arr[] = { 2, 3, -7, 6, 8, 1, -10, 15 } Output: 4 Input: arr[] = {1, 1, 0, -1, -2} Output: 2 Approach : We have already discussed some of the
6 min read
Find missing elements from an Array with duplicates
Given an array arr[] of size N having integers in the range [1, N] with some of the elements missing. The task is to find the missing elements. Note: There can be duplicates in the array. Examples: Input: arr[] = {1, 3, 3, 3, 5}, N = 5Output: 2 4Explanation: The numbers missing from the list are 2 and 4All other elements in the range [1, 5] are pre
12 min read
Find temperature of missing days using given sum and average
Given integers x and y which denotes the average temperature of the week except for days Day1 and Day2 respectively, and the sum of the temperature of Day1 and Day2 as S, the task is to find the temperature of the days Day1 and Day2.Examples: Input: x = 15, y = 10, S = 50 Output: Day1 = 10, Day2 = 40 Explanation: The average of week excluding Day1
5 min read
Find all missing numbers from a given sorted array
Given a sorted array arr[] of N integers, The task is to find the multiple missing elements in the array between the ranges [arr[0], arr[N-1]]. Examples: Input: arr[] = {6, 7, 10, 11, 13}Output: 8 9 12 Explanation: The elements of the array are present in the range of the maximum and minimum array element [6, 13]. Therefore, the total values will b
13 min read
Python3 Program to Find the smallest missing number
Given a sorted array of n distinct integers where each integer is in the range from 0 to m-1 and m &gt; n. Find the smallest number that is missing from the array. Examples Input: {0, 1, 2, 6, 9}, n = 5, m = 10 Output: 3 Input: {4, 5, 10, 11}, n = 4, m = 12 Output: 0 Input: {0, 1, 2, 3}, n = 4, m = 5 Output: 4 Input: {0, 1, 2, 3, 4, 5, 6, 7, 10}, n
4 min read
Javascript Program for Find the smallest missing number
Given a sorted array of n distinct integers where each integer is in the range from 0 to m-1 and m &gt; n. Find the smallest number that is missing from the array. Examples Input: {0, 1, 2, 6, 9}, n = 5, m = 10 Output: 3 Input: {4, 5, 10, 11}, n = 4, m = 12 Output: 0 Input: {0, 1, 2, 3}, n = 4, m = 5 Output: 4 Input: {0, 1, 2, 3, 4, 5, 6, 7, 10}, n
4 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg