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

typedef Examples in C

typedef Examples in C

✅ Typedef Examples in C

1. Typedef with Pointers

#include <stdio.h>

typedef int* IntPtr;

int main() {
    int a = 100;
    IntPtr p = &a;  // same as int *p = &a;
    printf("Value at pointer: %d\n", *p);
    return 0;
}
  

2. Function Pointer with Typedef

#include <stdio.h>

typedef void (*FuncPtr)(int);

void greet(int x) {
    printf("Hello %d times!\n", x);
}

int main() {
    FuncPtr fp = greet;
    fp(3);
    return 0;
}
  

3. Typedef with Enum

#include <stdio.h>

typedef enum {
    RED, GREEN, BLUE
} Color;

int main() {
    Color c = GREEN;
    printf("Color value: %d\n", c);
    return 0;
}
  

4. Enum without Typedef

enum Color { RED, GREEN, BLUE };
enum Color c = RED;
  

5. Typedef with Structure

#include <stdio.h>

typedef struct {
    int x, y;
} Point;

int main() {
    Point p1 = {10, 20};
    printf("x = %d, y = %d\n", p1.x, p1.y);
    return 0;
}
  

6. Structure Without Typedef

struct Point {
    int x, y;
};

int main() {
    struct Point p = {10, 20};
}
  

7. Another Typedef Struct Example

#include <stdio.h>

typedef struct {
    int x, y;
} point;

int main() {
    point p;
    p.x = 3;
    p.y = 5;
    printf("%d and %d", p.x, p.y);
}
  

8. Typedef with Unsigned Int

#include <stdio.h>

typedef unsigned int unit;

int main() {
    unit a = 0;
    printf("The value of a is: %u", a);
}
  

Comments

Popular Posts

πŸŒ™