Want help in "Compress the List"

I have surpassed all the test cases easily but when submitted, it shows WA. Suggestions needed

Question link - https://www.codechef.com/problems/CMPRSS

MY CODE LINK - https://www.codechef.com/viewsolution/33615692

Please format your code or give link to your submission

  • use copy- paste/ insert hyperlink to share your solution or

  • use Blockquote

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

Try Tc-
n=4
1 2 3 4

It only returns 3,4

Any suggestions?

Problem is, in your for loop if condition will always run and last it will print last two indices.

I tried solving it and tried to fix it and it passed that tc but still showing WA

new solution code - https://www.codechef.com/viewsolution/33618022

Code
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t,n,i,c=0,temp=0;
    cin>>t;
    while(t--)
    {
        cin>>n;
        int arr[n];
        for(i=0;i<n;i++)
        {
            cin>>arr[i];
        }
        for(i=0;i<n;i++)
        {
            int cnt=0,start=i;
            while(i+1<n&&arr[i+1]==arr[i]+1)
            {
                cnt++;
                i++;
            }
            if(i<n-1)
            {
                if(cnt>=2)
                cout<<arr[start]<<"..."<<arr[i]<<",";
                if(cnt==1)
                cout<<arr[start]<<","<<arr[i]<<",";
                if(cnt==0)
                cout<<arr[start]<<",";
            }
            else
            {
                if(cnt>=2)
                cout<<arr[start]<<"..."<<arr[i];
                if(cnt==1)
                cout<<arr[start]<<","<<arr[i];
                if(cnt==0)
                cout<<arr[start];
            }
        }
        cout<<"\n";
    }
}
1 Like

thanks