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...

C Program: Final Sum of Max Row and Min Column

C Program: Final Sum of Max Row and Min Column

๐Ÿ”ท C Program: Final Sum of Max Row and Min Column

#include <stdio.h>

int main()
{
    int n, m;
    printf("Enter the number of rows (N): ");
    scanf("%d", &n);
    printf("Enter the number of coloumns (M): ");
    scanf("%d", &m);

    int a[n][m];

    printf("Enter the elements of the array:\n");
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            scanf("%d", &a[i][j]);
        }
    }

    int max_row_sum = 0;
    for (int i = 0; i < n; i++)
    {
        int row_sum = 0;
        for (int j = 0; j < m; j++)
        {
            row_sum += a[i][j];
        }
        if (i == 0 || row_sum > max_row_sum)
        {
            max_row_sum = row_sum;
        }
    }

    int min_col_sum = 0;
    for (int j = 0; j < m; j++)
    {
        int col_sum = 0;
        for (int i = 0; i < n; i++)
        {
            col_sum += a[i][j];
        }
        if (j == 0 || col_sum < min_col_sum)
        {
            min_col_sum = col_sum;
        }
    }

    int final_sum = max_row_sum + min_col_sum;
    printf("\nsum of row with highest sum: %d\n", max_row_sum);
    printf("sum of column with lowest sum: %d\n", min_col_sum);
    printf("Final sum (Largest row sum + smallest column sum): %d\n", final_sum);

    return 0;
}
  

๐Ÿ“˜ Explanation:

This C program works on a 2D array (matrix) and calculates two things:

๐Ÿ”น Maximum Row Sum: The sum of the row which has the highest total.
๐Ÿ”น Minimum Column Sum: The sum of the column which has the smallest total.

๐Ÿ”น After collecting user input into the matrix, it traverses each row to find the maximum row sum.
๐Ÿ”น Then it traverses each column to find the minimum column sum.
๐Ÿ”น It adds both to compute the final_sum and displays all three values: max row sum, min col sum, and their sum.

๐Ÿ” Sample Output:

Enter the number of rows (N): 3
Enter the number of coloumns (M): 3
Enter the elements of the array:
1 2 3
4 5 6
7 8 9

sum of row with highest sum: 24
sum of column with lowest sum: 12
Final sum (Largest row sum + smallest column sum): 36
    

๐Ÿท️ Keywords:

2D array in C, row sum, column sum, max row, min column, matrix manipulation in C, matrix row column sum program

Comments

Popular Posts

๐ŸŒ™