Skip to main content

Featured

C Program to Solve Two Sum Using Brute Force (With Algorithm & Output)

 Introduction The Two Sum problem is a popular coding interview question where we must find two indices of an array whose values add up to a given target. This program demonstrates a simple brute-force solution in C using nested loops and dynamic memory allocation. Problem Statement Given an integer array and a target value, return the indices of the two numbers such that they add up to the target. Each input has exactly one solution, and the same element cannot be used twice. The result should return the indices, not the values. If no solution exists, return NULL.  Algorithm / Logic Explanation Start the program. Traverse the array using a loop from index 0 to numsSize - 1 . Inside this loop, use another loop starting from i + 1 to numsSize - 1 . For every pair (i, j) , check if nums[i] + nums[j] == target . If condition becomes true: Allocate memory for 2 integers using malloc() . Store indices i and j . Set returnSize = 2 . Return the result poi...

Find Minimum and Maximum in 2D Array - C Program

Find Minimum and Maximum in 2D Array - C Program

✅ Find Minimum and Maximum in 2D Array - C Program

#include <stdio.h>

int main() {
    int row, col;

    // Input number of rows and columns
    printf("Enter number of rows and columns:\n");
    scanf("%d %d", &row, &col);

    int arr[row][col];

    // Input 2D array elements
    printf("Enter %d elements:\n", row * col);
    for(int i = 0; i < row; i++) {
        for(int j = 0; j < col; j++) {
            scanf("%d", &arr[i][j]);
        }
    }

    // Initialize min and max with the first element
    int min = arr[0][0];
    int max = arr[0][0];

    // Traverse the array to find min and max
    for(int i = 0; i < row; i++) {
        for(int j = 0; j < col; j++) {
            if(arr[i][j] < min) {
                min = arr[i][j];
            }
            if(arr[i][j] > max) {
                max = arr[i][j];
            }
        }
    }

    // Print results
    printf("Minimum element = %d\n", min);
    printf("Maximum element = %d\n", max);

    return 0;
}
  

๐Ÿ“˜ Explanation:

This C program reads a 2D array (matrix) from the user, then traverses all its elements using nested loops to determine the minimum and maximum values. The min and max variables are initialized with the first element, and the program compares each subsequent value to update these accordingly. This logic is useful for matrix-based problems in C, including search, statistics, and data processing.

๐Ÿงพ Sample Output:

Enter number of rows and columns:
2 3
Enter 6 elements:
5 7 1 9 4 2
Minimum element = 1
Maximum element = 9
  

๐Ÿ”‘ Keywords:

2D Array in C, Matrix traversal, Find min in matrix, Find max in matrix, C program array problem, Nested loop logic in C

๐Ÿ“Œ Hashtags:

#CProgramming #2DArray #MatrixMinMax #BeginnerC #ArrayTraversal #CodingPractice

Comments

Popular Posts

๐ŸŒ™