Thread: Help me to understand this leetcode problem

  1. #1
    Registered User
    Join Date
    Sep 2022
    Posts
    1

    Help me to understand this leetcode problem

    Okay , so a little context.
    I am a fresher who is trying to look for a new job and I have decided to grind leetcode. I am trying to improve my C Basics and Advanced Knowledge as that is the primary language and sadly the only language I use while answering Coding Question.

    Now for the problem , this problem Concatenation of Array

    Given an integer array nums of length n, you want to create an array ans of length 2n where ans[i] == nums[i] and ans[i + n] == nums[i] for 0 <= i < n (0-indexed).

    Specifically, ans is the concatenation of two nums arrays.

    Return the array ans.

    Code:
    /**
     * Note: The returned array must be malloced, assume caller calls free().
     */
    int* getConcatenation(int* nums, int numsSize, int* returnSize){
    
    }
    So for the questions now.
    1. What does the Returned Array must be malloced means?
    2. Why is there a int * returnSize as a parameter? Doesn't ReturnSize means I need to return that value instead of putting it in the parameters?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    1. What does the Returned Array must be malloced means?
    It means you do something like
    int *answer = malloc( n * sizeof(*answer));
    or
    int *answer = malloc( n * sizeof(int));
    I prefer the first style.

    2. Why is there a int * returnSize as a parameter? Doesn't ReturnSize means I need to return that value instead of putting it in the parameters?
    The returnSize tells the caller how big the array you allocated is.
    It's somewhat a moot point in this case, as the caller can easily work out the effective size of the array from the specification (it's just 2*n).
    But in other scenarios, it may be far less obvious, so it's good practice to return the value.
    You just do
    *returnSize = n;

    n in both cases is numsSize*2
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Banned
    Join Date
    Jul 2022
    Posts
    112
    Your lucky day.

    Code:
    /* main.cpp,  Copy_Twice,  Created by Great on 9/10/22. */
    #include <stdio.h>
    #include <stdlib.h>
    
    
    #pragma mark - Interface
    unsigned long array_length(const int *);
    
    
    #pragma mark - Implementation
    unsigned long array_length(const int *array) {
        const int *a = array;
        while (*a) {
            ++a;
        }
        return a - array;
    }
    int main(int argc, const char *argv[]) {
        
        int nums[16] = {1, 2, 3, 0};
        int *ans = 0;
        int i = 0;
        int n = 0;
        int size = 0;
        
        n = (signed int)array_length(nums);
        size = 1 + n + n;
        
        ans = (signed int *)malloc(sizeof(signed int) * size);
        
        if (0 == ans) {
            printf("Error: malloc()\n");
            
        } else {
            
            while ('\0' != nums[i]) {  /* Copy array            */
                ans[i] = nums[i];
                i = 1 + i;
            }
            i = 0;                     /* Reset i to 0          */
            
            while ('\0' != nums[i]) {  /* Copy array again      */
                ans[i + n] = nums[i];  /* {1, 2, 3} + {1, 2, 3} */
                i = 1 + i;
            }
            ans[i + n] = 0;            /* End array with null   */
            
            free(ans);                 /* malloc() -> free()    */
            ans = 0;
        }
        return 0;
    }

  4. #4
    Banned
    Join Date
    Jul 2022
    Posts
    112
    I am a fresher who is trying to look for a new job
    There is money in programming, but not for the programmer,
    certainly not for the employee.

    The worst part, programming is a soul-eating job,
    in that sense, it is much worse than wasting your life..


    "It's easier to fool people than to convince them that they have been fooled."
    Mark Twain.

  5. #5
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by kodax View Post
    Code:
            while ('\0' != nums[i]) {  /* Copy array            */
    This will stop at the first 0 that it finds (element 3). Is that intended?

    Also it's kind of unusual to compare an integer with a character literal '\0'. While it's equivalent to 0, usually you would use '\0' only when comparing characters (e.g., in a string).

  6. #6
    Banned
    Join Date
    Jul 2022
    Posts
    112
    Integer array can end with 0.

    Code:
     #ifndef DEF_H
     #define DEF_H
     
     #ifndef NIL
     #define NIL 0
     #endif
     
     #endif

  7. #7
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by kodax View Post
    Integer array can end with 0.
    Of course it can, if you want to use 0 as a sentinel value. But there's nothing in the original problem that said to have a sentinel-terminated array—zero could be considered a valid value in the array, after all, and it passed around array sizes too.

  8. #8
    Banned
    Join Date
    Jul 2022
    Posts
    112
    The question is written in a terrible way,
    "Given an integer array nums of length n".

    The problem is badly formulated.
    We know that, 2 x n is length of the new array,
    somehow there's is an argument, int *returnSize,
    which means return the size that we already know.
    This is the best example of a contradiction..

    This is correct.
    1. There is an array of integers.
    2. Int n is the "number of integers" in the array.
    3. Allocate a new array that contains two copy of the array.
    4. Eg. the array is 1, 2, 3, the result should be 1, 2, 3, 1, 2, 3.
    5. Use Free and Malloc.

    These are the kind of people who not only run the education system,
    they govern, applying their liberal laws without question,
    what can you expect..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help me to understand the problem in my code
    By alon4963 in forum C Programming
    Replies: 5
    Last Post: 11-18-2013, 07:56 AM
  2. help/understand on a problem
    By 0850079 in forum C++ Programming
    Replies: 2
    Last Post: 04-12-2010, 10:55 AM
  3. i dont understand the problem
    By jorgejags in forum C Programming
    Replies: 4
    Last Post: 10-03-2008, 01:05 PM
  4. Cant understand weired problem...
    By (Slith++) in forum C++ Programming
    Replies: 15
    Last Post: 07-13-2006, 06:08 AM
  5. little problem here... but I don't understand why
    By V1P3R V3N0M in forum Windows Programming
    Replies: 2
    Last Post: 01-30-2003, 04:06 PM

Tags for this Thread