< Back to forum

Not sorting zero

This is the problem I am trying to solve:

https://www.codechef.com/problems/ENTEXAM

This is my solution:

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

 

When I compile my solution, I am getting wrong answer. While trying to figure out why, I noticed that when I am sorting the total score in ascending order, it doesn't sort 0. Why is this the case?

 

The code below gives only the output of total score in ascending order

#include<iostream>

int main()
{
 int N, K, E, M, T, i, k, j, s[4], L[10000], temp;
 std::cin >> T;

 for (i = 0; i < T; i++)
 {
  std::cin >> N >> K >> E >> M;
  for (j = 0; j < N; j++)
  {
   if (j != N - 1)
   {
    L[j] = 0;
    for (k = 0; k < E; k++)
    {
     std::cin >> s[k];
    }
    for (k = 0; k < E; k++)
    {
     L[j] = L[j] + s[k];
    }
   }
   else
   {
    L[j] = 0;
    for (k = 0; k < E - 1; k++)
    {
     std::cin >> s[k];
    }
    for (k = 0; k < E - 1; k++)
    {
     L[j] = L[j] + s[k];
    }
   }
  }
  for (j = 0; j < N-2; j++)
  {
   for (k = j + 1; k < N - 2; k++)
   {
    if (L[j] >= L[k])
    {
     temp = L[j];
     L[j] = L[k];
     L[k] = temp;
    }
   }
  }
  for (j = 0; j < N; j++)
   std::cout << L[j] << std::endl;
 
 }
 system("pause");
 std::cin.get();
}

Asked by: Abhinav_Lugun on April 7, 2019, 6:34 p.m. Last updated on April 7, 2019, 6:34 p.m.


Enter your answer details below:


Enter your comment details below:




1 Answer(s)

avatar

try to run last two loops up to n-2 and to optimize your code, use some nlogn sorting algorithm such as merge sort, heap sort.

you can also use stl for sorting. please visit: https://www.geeksforgeeks.org/sort-c-stl/

rishup132 last updated on April 7, 2019, 6:34 p.m. 0    Reply    Upvote   

Instruction to write good question
  1. 1. Write a title that summarizes the specific problem
  2. 2. Pretend you're talking to a busy colleague
  3. 3. Spelling, grammar and punctuation are important!

Bad: C# Math Confusion
Good: Why does using float instead of int give me different results when all of my inputs are integers?
Bad: [php] session doubt
Good: How can I redirect users to different pages based on session data in PHP?
Bad: android if else problems
Good: Why does str == "value" evaluate to false when str is set to "value"?

Refer to Stack Overflow guide on asking a good question.