0

Given an array C of size N-1 and given that there are numbers from 1 to N with one element missing, the missing number is to be found.

Input: The first line of input contains an integer T denoting the number of test cases. For each test case first line contains N(size of array). The subsequent line contains N-1 array elements.

Output: Print the missing number in array.

This problem is to find the missing number in a series of n integers. but, while using the below code I could not get the output as expected.

#include <stdio.h>

int main() 
{
    //code
    int T,run,i;
    scanf("%d", &T);
    long N,res,C,en;
    long arra[1];
    for (run = 0;run <T; run++ )
    { 
        long arra[T];
        scanf("%ld", &N);
        res =0;
        for (i = 0; i <N-1; i++)
        {
            scanf("%ld",&C);
            res = res + C;
        }
        en = ((N*(N+1))/2)- res; // subtracting the overall sum of array elements from N integers 
        arra[run]=en; //saving it to array
    }
    for(run = 0; run < T; run++)
    {
        printf("%ld ",arra[run]);
    }
    return 0;
}

I expected the below input and output:

Input:
2
5
1 2 3 5
10
1 2 3 4 5 6 7 8 10

Output:
4
9

but actual output is

1 -8719623343620674816
0

3 Answers 3

2

You re-declared the variable arra inside the for loop. So when you assign to arra[run], you're assigning to the inner array, not the one in the main() function. So you get garbage when you try to print the contents of the array at the end.

You also declared the first array with only one element, rather than T elements.

Get rid of the second declaration, and change the first one from

long arra[1];

to

long arra[T];
Sign up to request clarification or add additional context in comments.

Comments

0

correct code

declare the arra before the for loop else for every iteration the arra will be re declared deleting the previous values in it

#include <stdio.h>

int main() 
{
    //code
    int T,run,i;
    scanf("%d", &T);
    long N,res,C,en;
    long arra[T];
    for (run = 0;run <T; run++ )
    { 
        scanf("%ld", &N);
        res =0;
        for (i = 0; i <N-1; i++)
        {
            scanf("%ld",&C);
            res = res + C;
        }
        en = ((N*(N+1))/2)- res; // subtracting the overall sum of array elements from N integers 
        arra[run]=en; //saving it to array
    }
    for(run = 0; run < T; run++)
    {
        printf("%ld ",arra[run]);
    }
    return 0;
}

Comments

0

here is a self explainable simple example

  public static void Main()
{
    int[] ary = { 5, 11, 3, 7, 13, 15 }; //{ 3, 5, 7, 11, 13, 15 };
    Array.Sort(ary);
    int freq=0;
    int minval = ary[0];
    int[] correct = new int[ary.Length+1];

    int[,] freqs = new int[(ary.Length), 2];
    freqs[0, 0] = 1;
    freqs[0, 1] = ary[0];

    for (int i = 0; i < ary.Length-1; i++)
    {
        int dif = ary[i + 1] - ary[i];
        int res = Search(freqs, dif);
        if (res < 0)
        {
            freqs[i, 0] = 1;
            freqs[i, 1] = dif;
        }
        else
        {
            freqs[res, 0] = freqs[res, 0] + 1;

        }            
    };
    for (int i = 0; i < freqs.GetLength(0); i++)
    {
      
        freq =freqs[i, 0] >freq? freqs[i, 1] : freq;

    }
    for (int i = 0; i < correct.Length;i++)
    {
        correct[i] = i == 0 ? minval :( correct[i - 1] + freq);
    }

    foreach (int i in correct.Except(ary))
    {
        Console.WriteLine("eksik değer="+i);
    }
    Console.ReadLine();



    int Search(int[,] matrix, int val)
    {
        int hit = -99;
        for (int i = 0; i < matrix.GetLength(0); i++)
        {
            if (val == matrix[i, 1])
                return i;
        }
        return hit;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.