Origin Passing

Last Updated : 8 Jul, 2026

Given two points (x1, y1) and (x2, y2), where x1 and x2 are integers and y1 and y2 are Excel-style column labels, determine whether the line passing through the two points also passes through the origin (0, 0).

Excel-style column labels are interpreted as follows: A = 1, B = 2, ..., Z = 26, AA = 27, AB = 28, and so on. Return true if the line passes through the origin, otherwise return false.

Examples: 

Input: x1 = 1, y1 = "A", x2 = 2, y2 = "B"
Output: true
Explanation: The points are (1, A) and (2, B), i.e., (1, 1) and (2, 2). These points lie on the same straight line as the origin (0, 0).

Input: x1 = 1, y1 = "AA", x2 = 2, y2 = "AB"
Output: false
Explanation: The points are (1, AA) and (2, AB), i.e., (1, 27) and (2, 28). The origin (0, 0) does not lie on the line passing through these points.

Cross Product Property - O(|y1| + |y2|) Time and O(1) Space

The idea is to convert the Excel column labels into integers and directly use the cross-product property. If x1 × y2 == x2 × y1, then the line passing through the two points also passes through the origin.

Working of Approach:

  • Convert both Excel-style column labels into their corresponding numeric values using base-26 conversion (A = 1, Z = 26, AA = 27, etc.).
  • Treat the converted values as the y-coordinates of the two given points.
  • Check whether the origin (0, 0) and the two points are collinear using the condition x1 × y2 = x2 × y1.
  • If the equality holds, the line passes through the origin, so return true; otherwise, return false.
  • Cross multiplication avoids floating-point division and performs the check in constant time after the conversions.

Let us understand with an example:
Input: x1 = 1, y1 = "A", x2 = 2, y2 = "B"

  • Convert the Excel labels: "A" -> 1 and "B" -> 2, so the given points become (1, 1) and (2, 2).
  • Compute the left-hand side: x1 × y2 = 1 × 2 = 2.
  • Compute the right-hand side: x2 × y1 = 2 × 1 = 2.
  • Since both values are equal, the origin (0, 0) and the two given points are collinear.
  • Therefore, the function returns true.
C++
#include <iostream>
#include <string>
using namespace std;

// Convert Excel column label into its numeric value.
int excelValue(string &col)
{
    int val = 0;

    // Convert Excel column label to its numeric value.
    for (char ch : col)
    {
        val = val * 26 + (ch - 'A' + 1);
    }
    return val;
}

bool passOrigin(int x1, string y1, int x2, string y2)
{
    int y1Val = excelValue(y1);
    int y2Val = excelValue(y2);

    // Check if origin and both points are collinear.
    return (x1 * y2Val == x2 * y1Val);
}

int main()
{

    int x1 = 1, x2 = 2;
    string y1 = "A", y2 = "B";

    if (passOrigin(x1, y1, x2, y2))
        cout << "true";
    else
        cout << "false";

    return 0;
}
Java
public class GFG {
    
    // Convert Excel column label into its numeric value.
    static int excelValue(String col)
    {
        int val = 0;
        for (char ch : col.toCharArray()) {
            val = val * 26 + (ch - 'A' + 1);
        }
        return val;
    }

    static boolean passOrigin(int x1, String y1, int x2,
                              String y2)
    {
        int y1Val = excelValue(y1);
        int y2Val = excelValue(y2);
        return (x1 * y2Val == x2 * y1Val);
    }

    public static void main(String[] args)
    {
        int x1 = 1, x2 = 2;
        String y1 = "A", y2 = "B";
        if (passOrigin(x1, y1, x2, y2))
            System.out.print("true");
        else
            System.out.print("false");
    }
}
Python
def excelValue(col):
    val = 0

    # Convert Excel column label to its numeric value.
    for ch in col:
        val = val * 26 + (ord(ch) - ord('A') + 1)
    return val


def passOrigin(x1, y1, x2, y2):
    y1Val = excelValue(y1)
    y2Val = excelValue(y2)

    # Check if origin and both points are collinear.
    return (x1 * y2Val == x2 * y1Val)


if __name__ == "__main__":
    x1 = 1
    x2 = 2
    y1 = "A"
    y2 = "B"

    if passOrigin(x1, y1, x2, y2):
        print("true")
    else:
        print("false")
C#
using System;

public class GFG {
    
    // Convert Excel column label into its numeric value.
    static int excelValue(string col)
    {
        int val = 0;
        foreach(char ch in col)
        {
            val = val * 26 + (ch - 'A' + 1);
        }
        return val;
    }

    static bool passOrigin(int x1, string y1, int x2,
                           string y2)
    {
        int y1Val = excelValue(y1);
        int y2Val = excelValue(y2);
        return (x1 * y2Val == x2 * y1Val);
    }

    public static void Main()
    {
        int x1 = 1, x2 = 2;
        string y1 = "A", y2 = "B";
        if (passOrigin(x1, y1, x2, y2))
            Console.Write("true");
        else
            Console.Write("false");
    }
}
JavaScript
function excelValue(col)
{
    let val = 0;

    // Convert Excel column label to its numeric value.
    for (let ch of col) {
        val = val * 26
              + (ch.charCodeAt(0) - "A".charCodeAt(0) + 1);
    }
    return val;
}

function passOrigin(x1, y1, x2, y2)
{
    const y1Val = excelValue(y1);
    const y2Val = excelValue(y2);

    // Check if origin and both points are collinear.
    return (x1 * y2Val === x2 * y1Val);
}

// Driver Code
const x1 = 1, x2 = 2;
const y1 = "A", y2 = "B";

if (passOrigin(x1, y1, x2, y2)) {
    console.log("true");
}
else {
    console.log("false");
}

Output
true
Comment