0

I am working on an exercise "k largest elements" from geeksforgeeks.org

https://practice.geeksforgeeks.org/problems/k-largest-elements/0

The task:

Given an array of N positive integers, print k largest elements from the array. The output elements should be printed in decreasing order.

Input:

The first line of input contains an integer T denoting the number of test cases. The first line of each test case is N and k, N is the size of array and K is the largest elements to be returned. The second line of each test case contains N input C[i].

Output:

Print the k largest element in descending order.

Constraints:

1 ≤ T ≤ 100

1 ≤ N ≤ 100 (actually 1000 as someone identified)

K ≤ N

1 ≤ C[i] ≤ 1000*

Example:

Input:

2
5 2
12 5 787 1 23
7 3
1 23 12 9 30 2 50

Output:

787 23
50 30 23

Explanation:

Testcase 1: 1st largest element in the array is 787 and second largest is 23.

Testcase 2: 3 Largest element in the array are 50, 30 and 23.

I have constructed a solution, which works with the initial test case, but later throws a segmentation fault. I cannot understand where could I get the segmentation fault:

Runtime Error:
Segmentation Fault (SIGSEGV)

Runtime Error

The problem with these exercises is that I do not know the input data to the test case, which generates the error. Bellow you will find my code, which is rather simple. Perhaps you can help me to identify what could cause the segmentation fault.

#include <stdio.h>

static int Arr[1024], Res[1024];

int main()
{
    int t=0;
    scanf("%d", &t);

    while(t--)
    {
        int n=0,k=0, i=0,j=0,z=0;
        scanf("%d %d", &n, &k);

       // if(n>1000)
       //     printf("Gotya");

        for(i=0; i<n; i++)
        {
            scanf("%d", &Arr[i]);
        }

        i=0;

        int max_l = 0, max_h = 1000, j_max = 0;

        for(i=0; i<k; i++)
        {

            for(j=0; j<n; j++)
            {
                if(Arr[j] >= max_l)
                {
                    max_l = Arr[j];
                    j_max = j;
                }
            }
            Res[i]= max_l;
            max_l = 0;
            Arr[j_max] = 0;
            j_max = 0;
        }

        for(z=0; z<k; z++)
        {
            printf("%d ", Res[z]);
        }

        printf("\n");

    }
    return 0;
}
Dainius
  • 51
  • 8

2 Answers2

2

This site does not seem to be abiding by their constraints.

I made a login and modified the code to print if N>1000. The value of N is 20567

Wrong Answer. !!!Wrong Answer

Possibly your code doesn't work correctly for multiple test-cases (TCs).

The first test case where your code failed:

Input:

20567 18428

9737 16220 4527 21952 22174 12861 29801 8125 13670 9713 5742 14988 31137 21891 25646 18474 18286 30312 6105 19031 18587 15877 14546 29756 18364 24690 12129 16209 378 27774 16552 18302 8238 10483 1752 12929 5551 22299 14756 9871 18279 32386 23286 5182 16557 9726 7137 22434 24399 7661 3544 29878 11885 16318 29725 29438 25486 18099 18811 7275 12781 9700 20024 9087 26644 32648 12870 29873 2988 8560 12217 14099 26516 10964 10622 16434 16432 16210 8777 32574 8708 27444 8143 9067 32385 7410 20022 406 2846 22519 30665 32044 5803 1192 9457 30792 18658 419 30816 3867 64 23108 7056 8849 16915 18030 20332 30257 28883 22408 30029 25926 15541 30405 9255 29500 638 6313 7685 15180 3221 18889 14770 860 27229 30930 5305 30240 20929 8652 8592 18566 16145 16032 26129 20049 18188 8408 32297 16899 20286 6311 14036 17409 8332 3369 21833 21746 2061 27115 24177 20328 31259 1454 29342 9410 27562 26216 4474 24913 21977 1676 23102 15289 6087 32651 7204 13210 10920 15771 19230 880 23576 8375 11464 2706.................

Its Correct output is:

32768 32768 32766 32765 32761 32759 32757 32756 32756 32755 32754 32752 32747 32747 32747 32746 32743 32742 32741 32740 32739 32738 32738 32735 32735 32735 32733 32733 32732 32731 32730 32728 32728 32726 32726 32726 32725 32724 32719 32718 32718 32716 32714 32714 32708 32706 32706 32698 32694 32694 32691 32690 32690 32689 32688 32687 32684 32682 32676 32675 32672 32670 32663 32660 32658 32657 32657 32656 32651 32648 32646 32645 32644 32643 32638 32637 32637 32637 32637 32635 32632 32630 32630 32625 32625 32625 32623 32622 32622 32620 32619 32618 32616 32614 32614 32613 32613 32612 32608 32606 32606 32605 32598 32596 32593 32591 32589 32588 32586 32586 32586 32582 32582 32581 32580 32580 32579 32578 32574 32573 32571 32571 32567 32567 32566 32564 32564 32563 32562 32560 32560 32559 32559 32558 32555 32554 32552 32551 32551 32551 32550 32549 32549 32547 32547 32546 32544 32544 32543 32543 32540 32539 32538 32538 32537 32535 32533 32533 32533 32532 32531 32528 32527 32527 32526 32525 3252.................

And Your Code's output is:

Gotya

However, with the correct length of the array Arr and Res, you get an error of

Expected Time Limit < 1.996sec

Hint : Please optimize your code and submit again.

I leave this up to you.

Hint - Use qsort for a better sorting performance.

Community
  • 1
  • 1
Rishikesh Raje
  • 8,556
  • 2
  • 16
  • 31
  • Well, that is interesting. How did you manage to catch that high N? I tried it as well, but still got the segmentation fault. Could post your full code please? – Dainius Sep 17 '19 at 12:45
  • @DainiusStankevicius You need to increase the size of the arrays to `> 100000` Then you can add `if((k > n)|| (n> 1000)) { printf("Gotya"); exit(1); }` – Rishikesh Raje Sep 17 '19 at 12:50
  • Thank you very much. I was able to catch that high N as well. And thanks for the hint too. I was able to submit a valid solution using qsort. You saved the day! – Dainius Sep 17 '19 at 12:56
  • @RishikeshRaje Cool... thumbs up :-) – Support Ukraine Sep 17 '19 at 16:04
0

A set of points over a straight line is defined as correlative to some K if the absolute difference between any two points is a multiple of K. Given N (2 <= N <= 100000) points and some integer K (1 <= K <= 1000). Your task is to find the largest set which is correlative to K. You can assume that only one largest set exists. N and K will be in the first line of the input. N lines will follow, each one with a single integer, representing the location of one of the points. Print the size of the largest set of points which is correlative to K, in the first line of the input. Remaining lines will contain the points of the set, one per line, in increasing order.

Case 1:

For the input provided as follows:

5 2

1

2

3

4

5

Output of the program will be:

3

1

3

5

Case 2:

For the input provided as follows:

6 4

10

15

12

16

20

32

Output of the program will be:

4

12

16

20

32

#include <bits/stdc++.h>

using namespace std;

// function to find remainder set

int findSet(int arr[], int n, int k, int m) {

vector remainder_set[k];

// calculate remainder set array

// and push element as per their remainder

for (int i = 0; i < n; i++) {

int rem = arr[i] % k;

remainder_set[rem].push_back(arr[i]);

}

// check whether sizeof any remainder set

// is equal or greater than m

for (int i = 0; i < k; i++) {

if (remainder_set[i].size() >= m) {

cout <<m<< "\n";

for (int j = 0; j < m; j++){

cout << remainder_set[i][j] << "\n";

}

return 1;

}

}

return 0;

}

// driver program

int main() {

int n,k;

cin>>n>>k;

int arr[n];

for(int i=0;i<n;i++)

cin>>arr[i];

int z;

int m = sizeof(arr)/sizeof(int);

for(int i=m;i>0;i--)

{

z=findSet(arr, n, k, i);

if(z==1)

break;

}

}

  • Hi and welcome to Stack Overflow! Please take the [tour](https://stackoverflow.com/tour) and check the [help center](https://stackoverflow.com/editing-help) for info on how to format code. – Tyler2P Sep 25 '21 at 14:51
  • Please read this: [Why should I not #include ?](https://stackoverflow.com/q/31816095/10871073) and consider editing your code to use the **standard** header files. – Adrian Mole Sep 25 '21 at 15:05