0

I was solving this problem https://practice.geeksforgeeks.org/problems/frequency-of-array-elements-1587115620/1 .
I am unable to figure why it is giving runtime error .

Problem Statement: Given an array A[] of N positive integers which can contain integers from 1 to P where elements can be repeated or can be absent from the array. Your task is to count the frequency of all elements from 1 to N.
Note: The elements greater than N in the array can be ignored for counting.

Constraints:
1 ≤ N ≤ 105
1 ≤ P ≤ 4*104
1 <= A[i] <= P

My Approach:
Since A[i] >= 1 , I stored the counts in negative and at the end just took their absolute value .

using namespace std; 

 // } Driver Code Ends
class Solution{
    public:
    //Function to count the frequency of all elements from 1 to N in the array.
    void frequencyCount(vector<int>& arr,int N, int P)
    { 
        // code here
        for(int i = 0 ;i < N; ++i){
            int ele = arr[i];
            if(ele > N ){
                arr[i] = 0 ;
            }
            else if(ele < 1){
                continue;
            }
            else{
                arr[i] = 0 ;
                int next;
                while(arr[ele - 1] > 0){
                    next = arr[ele - 1];
                    arr[ele - 1] = -1 ;
                    ele = next ;
                }
                arr[ele - 1] = arr[ele - 1] - 1;
            }
        }
        for(int &i : arr ){
            i = abs(i);
        }
    }
};


// { Driver Code Starts.

int main() 
{ 
    long long t;
    
    //testcases
    cin >> t;
    
    while(t--){
        
        int N, P;
        //size of array
        cin >> N; 
        
        vector<int> arr(N);
        
        //adding elements to the vector
        for(int i = 0; i < N ; i++){
            cin >> arr[i]; 
        }
        cin >> P;
        Solution ob;
        //calling frequncycount() function
        ob.frequencyCount(arr, N, P); 
        
        //printing array elements
        for (int i = 0; i < N ; i++) 
            cout << arr[i] << " ";
        cout << endl;
    }   
    return 0; 
}



  // } Driver Code Ends

Error:
On submitting the solution it shows this

Test Cases Passed: 
2 /  100020

Input: 
5
1 1 1 1 1
1

Your Output:
Abort signal from abort(3) (SIGABRT)

Its Correct output is : 
5 0 0 0 0

The problem is my code gives correct output for the above test case.

  • In the loop `while(arr[ele - 1] > 0){`, will `ele - 1` always be a valid index into `arr`? Will `ele - 1` be a valid index after that loop? – Some programmer dude Jan 24 '22 at 09:17
  • You have the actual input, and the expected output as well as the behavior of your code. That should make it *very* easy to create a [mre] to test and ***debug*** locally. – Some programmer dude Jan 24 '22 at 09:27
  • @Someprogrammerdude Thanks, `ele - 1` actually may exceed array size. But any reason why it gives error for that case even when for that case it doesn't exceed array size ? – Rahul Mallick Jan 24 '22 at 09:29
  • Are you sure that site you use gives input for `t` (in the `main` function)? If it doesn't then your code will fail to handle input-failure correctly. – Some programmer dude Jan 24 '22 at 14:29
  • 1
    On another note, and a pet peeve of mine, don't use so called "competition" or "online judge" sites (like Geek for geeks) to learn programming or programming languages. Most of the sites aren't supposed to be used for learning or teaching, and for sites that claim to be then they often teach bad habits, leading to bad code. And sometimes even *invalid* code. If you really want to learn C++, then invest in [some good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282). – Some programmer dude Jan 24 '22 at 14:32

0 Answers0