[Solved] What is the shortest and most effecient way to get the result for this program? [closed]


First of all, your code doesn’t compile in most machines. Reason? cout and cin are parts of the Standard namespace std, which means that you cannot access them unless you use the appropriate commands. This can be done in two ways:

  • Write std::cout instead of cout. This tells the compiler that you want to access the object cout, which is a part of the standard namespace
  • Before the main body, write using namespace std;. This will offer you direct access to everything that belongs to the standard namespace, and is found in the headers you included.

Now let’s talk about efficiency:

  • You don’t have to use arrays. You can process the data while you read it from the standard input.
    This will reduce the space complexity from O(N) to O(1). In other words, you will have constant space.
  • If you want your code to execute much faster, and you are familiar with C programming language, you can use C-style input and output. It less readable, but much more efficient and fast, as it is implemented in assembly (machine language).
  • You can reduce the length of your code by using inline if-else statement. Please note that this is not a good practice, as it makes your code very difficult to read.

Putting all together:

#include <cstdio>
int main() {
  int i, n, a, b;
  scanf("%d", &n);
  for(i=0; i<n; ++i) {
    scanf("%d %d", &a, &b);
    printf("Participant %d wins\n", (a > b) ? 1 : 2);
  }
  return 0;
}

solved What is the shortest and most effecient way to get the result for this program? [closed]