LeetCode //C - 1200. Minimum Absolute Difference

1200. Minimum Absolute Difference

Given an array of distinct integers arr, find all pairs of elements with the minimum absolute difference of any two elements.

Return a list of pairs in ascending order(with respect to pairs), each pair a, b follows

  • a, b are from arr
  • a < b
  • b - a equals to the minimum absolute difference of any two elements in arr
Example 1:

Input: arr = 4,2,1,3

Output: \[1,2,2,3,3,4]

Explanation: The minimum absolute difference is 1. List all pairs with difference equal to 1 in ascending order.

Example 2:

Input: arr = 1,3,6,10,15

Output: \[1,3]

Example 3:

Input: arr = 3,8,-10,23,19,-4,-14,27

Output: \[-14,-10,19,23,23,27]

Constraints:
  • 2 < = a r r . l e n g t h < = 10 5 2 <= arr.length <= 10^5 2<=arr.length<=105
  • − 10 6 < = a r r i < = 10 6 -10^6 <= arri <= 10^6 −106<=arri<=106

From: LeetCode

Link: 1200. Minimum Absolute Difference


Solution:

Ideas:

Sort first, then only compare neighboring numbers.

Code:
c 复制代码
#include <stdlib.h>

int cmpInt(const void* a, const void* b) {
    int x = *(int*)a;
    int y = *(int*)b;
    return (x > y) - (x < y);
}

/**
 * Return an array of arrays of size *returnSize.
 * The sizes of the arrays are returned as *returnColumnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 */
int** minimumAbsDifference(int* arr, int arrSize, int* returnSize, int** returnColumnSizes) {
    qsort(arr, arrSize, sizeof(int), cmpInt);

    int minDiff = arr[1] - arr[0];
    for (int i = 1; i < arrSize - 1; i++) {
        int diff = arr[i + 1] - arr[i];
        if (diff < minDiff) {
            minDiff = diff;
        }
    }

    int count = 0;
    for (int i = 0; i < arrSize - 1; i++) {
        if (arr[i + 1] - arr[i] == minDiff) {
            count++;
        }
    }

    int** result = (int**)malloc(sizeof(int*) * count);
    *returnColumnSizes = (int*)malloc(sizeof(int) * count);
    *returnSize = count;

    int idx = 0;
    for (int i = 0; i < arrSize - 1; i++) {
        if (arr[i + 1] - arr[i] == minDiff) {
            result[idx] = (int*)malloc(sizeof(int) * 2);
            result[idx][0] = arr[i];
            result[idx][1] = arr[i + 1];
            (*returnColumnSizes)[idx] = 2;
            idx++;
        }
    }

    return result;
}
相关推荐
是隼人29 分钟前
buuctf-pwn [HarekazeCTF2019]baby_rop2(64位ret2libc)题解(学习过程持续更新)
c语言·学习·安全·pwn入门·ctf入门
青 春 记 忆44 分钟前
LeetCode 350. 两个数组的交集 II|Python 解法详解
python·算法·leetcode
阳明山水1 小时前
销量预测2025—2026:从模型竞赛到系统融合的范式转型
人工智能·深度学习·算法·机器学习·架构
桐盛科技2 小时前
拒绝“漂绿”风险:基于边缘计算的碳排放数据清洗算法与实时校验逻辑
人工智能·算法·边缘计算
wuyk5552 小时前
107.FreeRTOS 链表深度解析:从原理到面试满分答案
c语言·开发语言·数据结构·stm32·单片机·链表·面试
Tongzhi20262 小时前
通芝科技考勤软件三大发展趋势:AI大模型、无感识别与数据底座
大数据·科技·算法·爬山算法
eBest数字化转型方案2 小时前
Route Optimization for FMCG:多目标排线算法的工程实现
大数据·人工智能·算法
residual_fan3 小时前
持续对比强化学习(Continual Contrastive Reinforcement Learning)论文分享
人工智能·算法·数据挖掘·数据分析
s_w.h3 小时前
【 计网 】序列化与反序列化
linux·服务器·网络·算法·bash
信奥卷王4 小时前
2025年09月GESPC++五级真题解析(含视频)
算法