findMaxSum

just keep top two large number

#include 
#include 
#define SWAP(x,y) {int t; t = x; x = y; y = t;}

int findMaxSum(int *numbers, int size)
{
    int max1 = 0, max2 = 0;
    for(int i = 0; imax1){
            max1 = numbers[i];
        }
        if(max2<max1){
            SWAP(max1, max2);
        }
    }

    return max1 + max2;
}

#ifndef RunTests
int main()
{
    int v[] = { 5, 9, 7, 11, 1, 2, 3, 4, 5, 20};
    printf("%d", findMaxSum(v, 10));
}
#endif

set bit in specific position

find the bit of that position
int mask = 1 << pos;
value = src & mask;

clear the position of dst then set the bit to value
(dst & ~mask) | (value & mask);

#include 
#include 

int copy_bit(int src, int dst, int pos)
{
    int result = 0;
    int mask = 1 << pos;
    int value = 0;
    value = src & mask;
    return (dst & ~mask) | (value & mask);
}

#ifndef RunTests
int main()
{
    printf("%d", copy_bit(8, 12, 3));
}
#endif

I2C communication

most of the case when we communicate with a I2C driver
we prepare a unsigned char buffer to read/write to the I2C file node, since we cannot know the data read/write is positive or negative, so unsigned is the only way to store the value
and also we cannot know the order of the data(little endian or big endian), so char prevent us from doing miss-order

...
typedef struct {
        unsigned char func;
        unsigned char buffer[8];
} S_SensoryLicense;
...
uint32_t *securityChipComms(uint32_t * challenge)
{
        size_t ret = 0;
        int fd = 0;
        S_SensoryLicense SensoryLicense;
        static uint32_t respond[2];

        if (NULL == challenge) {
                printf("challenge is NULL\n");
                return (void *)-1;
        }

        /*
         * do the hardware communication with security IC
         * send challenge to IC
         * get respond from IC and place in 'respond'
         */
        fd = open(I2C_DEVICE_NAME, O_RDWR);
        if (fd < 0) {
                printf("open %s failed\n", I2C_DEVICE_NAME);
                return (void *)-1;
        }
        SensoryLicense.func = CHIP_WRITE;
        memcpy(&SensoryLicense.buffer, challenge, sizeof(uint32_t) * 2);
        ret = write(fd, &SensoryLicense, sizeof(SensoryLicense));
        if (ret < 0) {
                printf("write %s failed: %s\n", I2C_DEVICE_NAME, strerror(errno));
                return (void *)-1;
        }
        /*
         * userspace prepare a 9 bytes unsigned char array
         * sensory_ic read the 0th bytes for register address
         * then sensory_ic copy respond to 0th of the array
         */
        SensoryLicense.func = CHIP_READ;
        ret = read(fd, &SensoryLicense, sizeof(SensoryLicense));
        memcpy(respond, &SensoryLicense, sizeof(SensoryLicense.buffer));
        if (ret < 0) {
                printf("read %s failed: %s\n", I2C_DEVICE_NAME, strerror(errno));
                return (void *)-1;
        }
        close(fd);
        return respond;
}

two sum(2)

below is a 0ms solution from leetcode
but it actually missed a test case
[50000000,3,2,4,28800,50000000]
100000000
this 0ms submission using a %TABLE_SIZE to shrink the table needed
but collision will happen!!

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* twoSum(int* nums, int numsSize, int target, int* returnSize){
    int i = 0;
    int j = 0;
    int *ret = NULL;
    int *returnSize = 2;
    for(i = 0 ; i<numsSize ; i++){
        for(j = i ; j= 0){
       return (key % TABLE_SIZE);
     }
     else{
       key = TWO_COMPLEMENT + key;
       return (key % TABLE_SIZE);
     }
}


// Our record table definition, an array of M records.

    //record_table = (Record *)malloc(sizeof(*record_table)*TABLE_SIZE);

// Insert function. Inserts record at the index determined by the hash
// function. Uses linear probing if already occupied.

void insert(Record *record_table,int value, int index){
     int idx = hash(value);
     record_table[idx].value = value;
     record_table[idx].index =  index; 
    // printf("insert table[%d] : %d, %d\n", idx, value, index);
}
// Search Function. Returns the record with a matching key. Search starts
// at the index determined by the hash function. Uses linear probing until
// the matching key is found. Returns NULL on search miss.
int search(Record *record_table,int value) {
   int idx = hash(value);
   //printf("search table[%d] : %d,%d\n", idx, record_table[idx].value,record_table[idx].index);
   if (record_table[idx].value == value ) {
        return record_table[idx].index;
   }
   else {
        return 0;
   }
}



/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* twoSum(int* nums, int numsSize, int target, int* returnSize){
    
     
     
    //using hash of value, index
    
    int complement = 0;
   
    //Record record_table[TABLE_SIZE];
    Record *record_table;
    record_table = malloc(sizeof(record_table)*TABLE_SIZE);
    
    
    for(int i = 0; i  for every element in array, find complement with target in the rest of the array
    /*
    int complement = 0;
    int *arr;
    arr = malloc(sizeof(int)*2);
    
    for(int i = 0; i < numsSize; i++){
          arr[0] = i;
          complement = target - nums[i];
        
          for(int j = i+1 ; j < numsSize; j++ ){
              
              if(nums[j] == complement ){
                  arr[1] = j;
                  *returnSize = 2;
                  return arr;
              }   
          }
    }
    
    *returnSize = 0;
     return NULL;
    */

}

two sum

the very basic solution

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* twoSum(int* nums, int numsSize, int target, int* returnSize){
    int i, j;
    int *ret = NULL;
    *returnSize = 2;
    for(i=0 ; i<numsSize ; i++){
        for(j=i+1 ; j<numsSize ; j++){
            if(nums[i]+nums[j] == target){
                ret = (int*)malloc(2*sizeof(int));
                ret[0] = i;
                ret[1] = j;
                return ret;
            }
        }
    }
    return NULL;
}