PROBLEM CODE SIPM

code runs absolutely fine in my ide.perfect output …codechef says its wrong …maybe constraints help…
#include

using namespace std;

int main ()

{

int t,N;

cin>>t;

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

{

   int p=0; int n=0;

   cin>>N;

   int arr[N];

   cin>>arr[0];

    if(arr[0]<0)

         n=arr[0];

         else

         {

             p=arr[0];

         }

   for (int j=1;j<N;j++)

   { 

       cin>>arr[j];

    

        int k=0;

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

     {

         if(arr[j]==arr[m])

        

          {k++;}

     }

    

     if(k==0)

     {

         if(arr[j]<0)

         n=n+arr[j];

         else p=p+arr[j];

     }

   }

            

    cout<<p<<n;

}

return 0;

}

while asking any problem, format your code by ``` three backticks before and after code , and give the link of question.

sry here’s the link https://www.codechef.com/problems/SIPM

https://www.codechef.com/viewsolution/39488575 @ssrivastava990

cout<<s1<<" "<<s2<<endl;

and remove the first cout<<endl;

https://www.codechef.com/viewsolution/39489054

Learn language first , learn set in c++.

thnx it worked… i know all the simple basics of c++(class 11 nd 12) …i wanted to challenge myself so did the code using just that knowledge…so before doing ques on c++ what all topics should do? could u provide me with a list of some kind?

set , array , stack , queue , priority_queue , map , pair , vector in c++

1 Like

Bcz Test cases were weak , see what u did is taking an element and check whether it is present previously that means for each i u iterate a loop till i

so if u are at index n , u again iterate from 0 to n-1 , so overall complexity is O(N2) and here N is 106 => N2 = 1012 which is not possible to run in given Time Limit.

So better to take two set one for neg , one for pos and give the sum.

ohk…i couldnt understand some of the terms u used but got the gist of what u wanted to say…
so u are saying that first i should check that the particular term entered is positive or negative then check if that term has been entered before (sep for both cases) which will somehow decrease time taken?? (sry for using such crude terms…i wiill read about complexity nd time limits surely)…

Learn " set in c++ " then u will understand .

sure … u have been of gr8 help …despite being a 4 star u have helped nd answered my doubts nd trivial ques…thnx for the time and effort…have a nice day

1 Like