Saddle Point in Matrix in C
  • Twitter
  • Facebook
  • Snapchat
  • Instagram

Find Saddle Point in a Matrix in C: Program to Find Saddle Point

📑 On this page:
  • Introduction
  • C Program to Find Saddle Point
  • Sample Output
  • Program Explanation
  • Algorithm
  • Practice Exercise
  • Frequently Asked Questions
📚 In this tutorial, you will learn:
  • What is a saddle point in a matrix
  • How to find saddle point using row minima and column maxima
  • How to handle multiple saddle points
  • Step-by-step explanation of the program
  • Practice exercises to test your understanding

Introduction

In this tutorial, we will learn how to write a C program to find the saddle point in a matrix.

A saddle point in a matrix is an element that is the minimum in its row and the maximum in its column (or vice versa). It is called a saddle point because the value "sits" like a saddle between the row and column extremes.

💡 Key Point: A saddle point has two conditions: it is the smallest in its row and the largest in its column. A matrix can have zero, one, or multiple saddle points.

Finding saddle points is used in many real-world applications, such as:

  • Game Theory: Finding optimal strategies in games
  • Optimization: Identifying critical points in functions
  • Data Analysis: Finding extreme values in datasets
  • Image Processing: Edge detection algorithms

Visual Example

Matrix:

[1  2  3]
[4  5  6]
[7  8  9]

Analysis:

  • Row 1 minima: 1 (at position 0,0)
  • Column 0 maxima: 7 (at position 2,0)
  • Row 2 minima: 7 (at position 2,0)
  • Column 0 maxima: 7 (at position 2,0)

✅ Saddle Point Found: 7 at (2, 0)

C Program to Find Saddle Point in a Matrix

#include <stdio.h>

int main() {
    int rows, cols, i, j, k;
    int saddle_found = 0;
    
    // Ask user for matrix dimensions
    printf("Enter the number of rows: ");
    scanf("%d", &rows);
    printf("Enter the number of columns: ");
    scanf("%d", &cols);
    
    // Declare matrix
    int matrix[rows][cols];
    
    // Read elements into the matrix
    printf("\nEnter %d elements:\n", rows * cols);
    for(i = 0; i < rows; i++) {
        for(j = 0; j < cols; j++) {
            printf("matrix[%d][%d] = ", i, j);
            scanf("%d", &matrix[i][j]);
        }
    }
    
    // Display the matrix
    printf("\n=== The Matrix ===\n");
    for(i = 0; i < rows; i++) {
        for(j = 0; j < cols; j++) {
            printf("%d\t", matrix[i][j]);
        }
        printf("\n");
    }
    
    // Find saddle point(s)
    printf("\n=== Saddle Points ===\n");
    for(i = 0; i < rows; i++) {
        // Find the minimum element in the current row
        int min_row = matrix[i][0];
        int col_index = 0;
        
        for(j = 1; j < cols; j++) {
            if(matrix[i][j] < min_row) {
                min_row = matrix[i][j];
                col_index = j;
            }
        }
        
        // Check if this minimum is the maximum in its column
        int is_saddle = 1;
        for(k = 0; k < rows; k++) {
            if(matrix[k][col_index] > min_row) {
                is_saddle = 0;
                break;
            }
        }
        
        if(is_saddle) {
            printf("✅ Saddle point found at matrix[%d][%d] = %d\n", 
                   i, col_index, min_row);
            saddle_found = 1;
        }
    }
    
    if(saddle_found == 0) {
        printf("❌ No saddle point found in the matrix.\n");
    }
    
    return 0;
}

Sample Output

Enter the number of rows: 3
Enter the number of columns: 3

Enter 9 elements:
matrix[0][0] = 1
matrix[0][1] = 2
matrix[0][2] = 3
matrix[1][0] = 4
matrix[1][1] = 5
matrix[1][2] = 6
matrix[2][0] = 7
matrix[2][1] = 8
matrix[2][2] = 9

=== The Matrix ===
1	2	3
4	5	6
7	8	9

=== Saddle Points ===
✅ Saddle point found at matrix[2][0] = 7

Example with Multiple Saddle Points:

Enter the number of rows: 3
Enter the number of columns: 3

Enter 9 elements:
matrix[0][0] = 1
matrix[0][1] = 2
matrix[0][2] = 3
matrix[1][0] = 4
matrix[1][1] = 5
matrix[1][2] = 6
matrix[2][0] = 7
matrix[2][1] = 8
matrix[2][2] = 9

=== The Matrix ===
1	2	3
4	5	6
7	8	9

=== Saddle Points ===
✅ Saddle point found at matrix[2][0] = 7

Example with No Saddle Point:

Enter the number of rows: 3
Enter the number of columns: 3

Enter 9 elements:
matrix[0][0] = 5
matrix[0][1] = 6
matrix[0][2] = 7
matrix[1][0] = 8
matrix[1][1] = 9
matrix[1][2] = 1
matrix[2][0] = 2
matrix[2][1] = 3
matrix[2][2] = 4

=== The Matrix ===
5	6	7
8	9	1
2	3	4

=== Saddle Points ===
❌ No saddle point found in the matrix.

Program Explanation

Let's break down the code step by step:

  1. Include Header File: #include <stdio.h> includes the standard input/output library.
  2. Declare Variables:
    • int rows, cols; — dimensions of the matrix
    • int i, j, k; — loop counters
    • int saddle_found = 0; — flag to track if a saddle point was found
  3. Read Matrix: Uses nested loops to read elements into the matrix.
  4. Display Matrix: Prints the matrix in a readable format.
  5. Find Saddle Points:
    • Step 1: For each row, find the minimum element and its column index.
    • Step 2: Check if this minimum is the maximum in its column.
    • Step 3: If both conditions are met, it's a saddle point.
  6. Display Results: Prints all saddle points found or a message if none exist.
  7. Return: return 0; indicates successful program execution.

📝 Note: For a matrix to have a saddle point, the element must be the smallest in its row and the largest in its column. Some definitions also accept the opposite (largest in row, smallest in column).

Algorithm to Find Saddle Point

Step-by-step algorithm:

  1. Start
  2. Read the matrix dimensions and elements
  3. For i = 0 to rows-1:
    • Find the minimum element in row i
    • Record its value and column index
    • Check if this value is the maximum in its column
    • If yes, print the saddle point
  4. If no saddle point found, print "No saddle point found"
  5. End

Visual Walkthrough

Finding saddle point in the matrix [[1, 2, 3], [4, 5, 6], [7, 8, 9]]:

Step 1: Row 0 [1, 2, 3] → Minimum = 1 (col 0)

Check column 0: [1, 4, 7] → Maximum = 7

1 ≠ 7 → Not a saddle point


Step 2: Row 1 [4, 5, 6] → Minimum = 4 (col 0)

Check column 0: [1, 4, 7] → Maximum = 7

4 ≠ 7 → Not a saddle point


Step 3: Row 2 [7, 8, 9] → Minimum = 7 (col 0)

Check column 0: [1, 4, 7] → Maximum = 7

✅ 7 == 7 → Saddle point found at (2, 0)

Time and Space Complexity

Operation Time Complexity Space Complexity
Reading Matrix O(r × c) O(1)
Finding Saddle Points O(r × c) O(1)
Overall O(r × c) O(1)

💻 Practice Exercise

Challenge 1: Modify the program to find saddle points where the element is the maximum in its row and the minimum in its column.

Challenge 2: Write a program that finds all saddle points in a matrix and displays their positions and values.

🔍 Click to Show Solution for Challenge 2
#include <stdio.h>

int main() {
    int rows, cols, i, j, k;
    int saddle_found = 0;
    int saddle_count = 0;
    
    printf("Enter the number of rows: ");
    scanf("%d", &rows);
    printf("Enter the number of columns: ");
    scanf("%d", &cols);
    
    int matrix[rows][cols];
    
    printf("\nEnter %d elements:\n", rows * cols);
    for(i = 0; i < rows; i++) {
        for(j = 0; j < cols; j++) {
            printf("matrix[%d][%d] = ", i, j);
            scanf("%d", &matrix[i][j]);
        }
    }
    
    printf("\n=== The Matrix ===\n");
    for(i = 0; i < rows; i++) {
        for(j = 0; j < cols; j++) {
            printf("%d\t", matrix[i][j]);
        }
        printf("\n");
    }
    
    printf("\n=== Saddle Points ===\n");
    for(i = 0; i < rows; i++) {
        for(j = 0; j < cols; j++) {
            // Check if matrix[i][j] is min in row
            int is_min_row = 1;
            for(k = 0; k < cols; k++) {
                if(matrix[i][k] < matrix[i][j]) {
                    is_min_row = 0;
                    break;
                }
            }
            
            // Check if matrix[i][j] is max in column
            int is_max_col = 1;
            for(k = 0; k < rows; k++) {
                if(matrix[k][j] > matrix[i][j]) {
                    is_max_col = 0;
                    break;
                }
            }
            
            if(is_min_row && is_max_col) {
                printf("✅ Saddle point at matrix[%d][%d] = %d\n", i, j, matrix[i][j]);
                saddle_count++;
                saddle_found = 1;
            }
        }
    }
    
    if(saddle_found == 0) {
        printf("❌ No saddle point found.\n");
    } else {
        printf("\nTotal saddle points found: %d\n", saddle_count);
    }
    
    return 0;
}

Frequently Asked Questions

1. What is a saddle point in a matrix?

A saddle point is an element that is the minimum in its row and the maximum in its column. It is called a saddle point because it "sits" between the row and column extremes.

2. Can a matrix have multiple saddle points?

Yes, a matrix can have zero, one, or multiple saddle points. For example, if all elements in a row and column are equal, there can be multiple saddle points.

3. What is the time complexity of finding a saddle point?

The time complexity is O(r × c), where r is the number of rows and c is the number of columns. We need to check each element once.

4. Can the saddle point condition be reversed?

Yes, some definitions consider a saddle point as the maximum in its row and the minimum in its column. Both definitions are valid depending on the context.

5. What is the significance of saddle points in real life?

Saddle points are used in game theory to find optimal strategies, in optimization to identify critical points, and in image processing for edge detection.

💡 Tip: When checking for saddle points, always verify both conditions: min in row AND max in column. Missing either condition means it's not a saddle point.

📖 Related Tutorials

  • Matrix Multiplication in C
  • Transpose a Matrix in C
  • Matrix Addition in C
  • More Array Assignments

Previous Topic: -->> Matrix Multiplication in C   ||   Next topic: -->> Matrix Addition in C


📚 Explore More Topics

🗄️ SQL Interview Questions & Answers

SQL SELECT Statement FAQ SQL Restricting & Sorting Data FAQ SQL Group Functions & Aggregated Data FAQ SQL Multiple Tables (JOINs) FAQ SQL Subqueries FAQ SQL DML Statements (Managing Tables) FAQ SQL Indexes, Synonyms & Sequences FAQ SQL DDL (Tables & Relationships) FAQ SQL Views FAQ SQL Indexing Best Practices FAQ SQL Window & Analytic Functions FAQ

🐍 Python Interview Questions & Answers

Python Interview Questions Python Syntax & Variables FAQ Python Data Types FAQ Python If-Else FAQ Python Loops FAQ Python Functions Interview Q Python String Manipulation FAQ Python Lists & Dictionaries FAQ Python Tuples & Sets FAQ Python Exception Handling FAQ Python OOP Interview Questions

☕ Java Interview Questions & Answers

Java Introduction Interview Q Java Development Environment FAQ Java Data Types FAQ Java Control Flow & Operators FAQ Java Basic Input/Output FAQ Java Arrays FAQ Java Strings FAQ Java Methods FAQ Java Basic OOP Concepts FAQ Java Advanced OOP Concepts FAQ Java OOP Best Practices FAQ Java Exception Handling FAQ Java Synchronization FAQ Java Threads & Concurrency FAQ Java Collection Framework FAQ Java File I/O & Serialization FAQ Java Serialization & Deserialization FAQ Java Features FAQ Java Inner & Anonymous Classes FAQ Java Memory Management FAQ Java Packages FAQ Java Wrapper Classes FAQ Java Streams & Lambda FAQ

C Language

  • Home
  • Why C Language
  • History of C Language
  • Applications of C Language
  • Introduction To C
    • What is Program?
    • Structure of C Program
    • Working Of C Program
    • CHARACTER SET
    • VARIABLES AND IDENTIFIERS
    • BUILT-IN DATA TYPES
    • OPERATORS AND EXPRESSIONS
    • CONSTANTS AND LITERALS
    • SIMPLE ASSIGNMENT STATEMENT
    • BASIC INPUT/OUTPUT STATEMENT
    • SIMPLE 'C' PROGRAMS
    • Assignments
  • Operators in C Programming
    • Arithmetic Operators
    • Assignment Operators
    • Increment and Decrement Operators
    • Relational Operators
    • Logical Operators
    • Bitwise Operators
    • Other Operators
    • Assignments
  • Conditional Statements
    • DECISION MAKING WITHIN A PROGRAM
    • CONDITIONS
    • IF STATEMENT
    • IF-ELSE STATEMENT
    • IF-ELSE LADDER
    • NESTED IF-ELSE
    • SWITCH CASE
    • Assignments
  • Loops Statements
    • Introduction to Loops
    • GO TO Statement
    • Do while Loop
    • While Loop
    • Nested While Loop
    • Difference Between While and Do while
    • Difference Between Goto and loop
    • while loop assignments
    • C FOR Loop
    • C For loop examples
    • Nested for loop
    • Nested for loop examples
    • Infinite while Loops
    • Infinite for Loops
    • Continue in Loops
    • break in Loops
    • difference while do..while & for
    • Assignments
  • Arrays
    • One Dimensional Array
    • Declaring 1D Arrays
    • Initilization of 1D arrays
    • Accessing element of one 1D Array
    • Read and Display 1D Arrays
    • Two Dimensional Arrays
    • Declare 2D Arrays
    • Read and Display 2D Arrays
    • Assignments/Examples
  • Functions
    • Introduction
    • Need For User-Defined Function
    • Multiple Function Program
    • Modular Programming
    • Elements Of User Defined Function
    • Function Definition
    • Function Declaration
    • Types of functions
    • Nesting of Function
    • Recursion
    • Passing Array To Functions
    • Scope,Visibility and Lifetime of Variables
    • Assignments
  • Structure
    • Introduction
    • Array vs Structure
    • Defining Structure
    • Declaring Structure Variables
    • Type Defined Structure
    • Accessing Structure Members
    • Structure Initilization
    • Copying & Comparing Structure Variables
    • Array of Structure
    • Arrays Within Structure
    • Structures Within Structures
    • Structures and Functions
    • Structure Examples/Assignments
  • Union
    • Define Union
    • Create and use Union
    • Difference Between Structure and Union
    • Union Examples
    • Union FAQ
  • Pointers
    • What Are Pointers In C?
    • How Do We Use Pointers In C?
    • Declaration Of A Pointer
    • The Initialization Of A Pointer
    • Syntax Of Pointer Initialization
    • Use Of Pointers In C
    • The Pointer To An Array
    • The Pointer To A Function
    • The Pointer To A Structure
    • Types Of Pointers
    • The Null Pointer
    • The Void Pointer
    • The Wild Pointer
    • The Near Pointer
    • The Huge Pointer
    • The far Pointer
    • dangling pointer
    • Accessing Pointers- Indirectly And Directly
    • Pros Of Using Pointers In C
    • Cons Of Pointers In C
    • Applications Of Pointers In C
    • The & Address Of Operator In C
    • How To Read The Complex Pointers In C?
    • Practice Problems On Pointers
  • File Processing
    • File Handling In C
    • Types Of Files In C
    • Operations Done In File Handling
    • File Examples
    • Binary Files
    • count words,lines in a file
    • Copy files
    • Update File
    • count vowels in a file
  • Preprocessor
    • Macro substitution division
    • File Inclusion
    • Conditional Compilation
    • Other directives
    • Examples
  • Dynamic Memory Allocation
    • malloc
    • calloc
    • free
    • realloc
    • Examples
  • Storage Classes
  • Graphics
  • Frequently Asked Interview Questions (FAQ)
    • Introduction To C FAQ
    • Operators FAQ
    • Conditional Statements FAQ
    • Loops FAQ
    • Arrays FAQ
    • Function FAQ
    • Structure FAQ
    • Pointers FAQ
    • Files FAQ
    • Storage classes FAQ
    • Dynamic Memory FAQ
  • Programs/Assignments
    • Introduction To C
    • Operators
    • Conditional Statements
    • Loops
    • Arrays
    • Function
    • Structure
    • Pointers
    • Files
    • Storage classes
    • Dynamic Memory
  • Case Studies
  • Multiple Choice Questions
    • Introduction To C MCQ
    • Operators MCQ
    • Conditional Statements MCQ
    • Loops MCQ
    • Arrays MCQ
    • Function MCQ
    • Structure MCQ
    • Pointers MCQ
    • Files MCQ
    • Storage classes MCQ
    • Dynamic Memory MCQ
    • More MCQ

Get in touch

  • tech2dsm@gmail.com

© Sankalan Data Tech. All rights reserved.