Click here to Skip to main content
15,888,142 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Code:

/*
Question Link
https://codeforces.com/contest/1669/problem/B
*/


#include<bits/stdc++.h>
using namespace std;
int func(int arr[], int n)
{
    int i;
    sort(arr,arr+n);
    for(i=0;i<n-2;i++)
    {
        if(arr[i]==arr[i+1]&&arr[i]==arr[i+2])
        {
            return arr[i];
        }
    }
    return -1;
}

int  main()
{
    int t,p, a[10000],i;
    cin>>t;
    p=t;
    while(t--)
    {
        int n, arr[20000];
        cin>>n;
        for(i=0;i<n;i++)
        {
            cin>>arr[i];
        }
        a[p-t-1]=func(arr,n);
    }

    for(i=0;i<t;i++)
    {
        cout<<a[i]<<endl;
    }
    return 0;
}


The link to the question is given under comments.

What I have tried:

I tried to write the code, but I was not getting the output.
Please don't assume me as rude or lazy because I share the link. Coping the whole code is not possible.
Posted
Updated 10-May-22 20:32pm
v3

first:
p=t;


then:
a[p-t-1]=a[-1]=func(arr,n);


ooops......????WTF

And:
while(t--)

at last :t = 0
so:
for(i=0;i<t;i++)
{
    cout<<a[i]<<endl;
}

DON'T WORK AS YOUR WISH.
Boom!
 
Share this answer
 
A couple of things:
1) Don't be rude and lazy: post your code to the site you are asking for help from, rather than expecting others to go to a random site and look at your code. Think about it: if you help people to help you they are more likely to want to.

2) Syntax errors are simple to fix if you just look at them closely: you shouldn't even be thinking of entering contests if you can't fix simple problems. Particularly when the error message explicitly tells you what the problem is, and where ...
main.cpp:44:24: error: expected ‘;’ before ‘}’ token
   44 |             a[p-t-1]=-1
      |                        ^
      |                        ;
   45 |     }
      |     ~
if it's complaining that a semicolon is missing, and showing you the line it is missing from, isn't it easier to add the semicolon than find someone else who can tell you "add the missing semicolon to this line:
a[p-t-1]=-1
So it looks like this:"
a[p-t-1]=-1;

This may help you next time you get a compilation error: How to Write Code to Solve a Problem, A Beginner's Guide Part 2: Syntax Errors[^]
 
Share this answer
 
You should reconsider your code logic (note the input array is already sorted).
Note you don't need an array either, you might find the consecutive items on-the-fly, while reading the input: just use two variables: cur_value and cur_repetitions.
 
Share this answer
 
v2
C++
for(i=0;i<t;i++) // The problem is that when you reach this line, t is 0
{
    cout<<a[i]<<endl;
}

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900