iamrlm's blog

By iamrlm, history, 9 days ago, In English

Hi so, I was solving this problem 1993B - Parity and Sum, and I solved it using two codes.

Code 1:

import java.io.*;
import java.util.*;
import static java.lang.Math.max;
import static java.lang.Integer.parseInt;
// import static java.lang.Math.min;
// import static java.lang.Math.abs;

public class Problem1993B {
    static BufferedReader br;
    static PrintWriter out;
    static StringTokenizer st;

    public static void main(String[] args) throws IOException {

        br = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(new BufferedOutputStream(System.out));
        int TC = parseInt(nextToken());
        while (TC-- > 0) {
            int n = parseInt(nextToken());
            int a[] = new int[n];
            long od = -1, ev = -1;
            long co = 0, ce = 0;
            for (int i = 0; i < n; i++) {
                a[i] = parseInt(nextToken());
                if ((a[i] & 1) == 0) {
                    ev = max(ev, a[i]);
                    ce += 1;
                } else {
                    od = max(od, a[i]);
                    co += 1;
                }
            }
            if (co == n || ce == n) {
                out.println(0);
                continue;
            }
            long se = 0;
            for (int i = 0; i < n; i++)
                if ((a[i] & 1) == 0 && a[i] != ev) {
                    se += a[i];
                }

            int ans = 0;
            if (ev > od && ((od + se) <= ev) ) {
                ans++;
            }
            
            ans += ce;
            out.println(ans);


        }

        out.flush();
        out.close();
        br.close();
    }

    static String nextToken() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}

Code 2:

import java.io.*;
import java.util.*;

import static java.lang.Integer.parseInt;
// import static java.lang.Math.min;
// import static java.lang.Math.abs;

public class Problem1993B {
    static BufferedReader br;
    static PrintWriter out;
    static StringTokenizer st;

    public static void main(String[] args) throws IOException {

        br = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(new BufferedOutputStream(System.out));
        int TC = parseInt(nextToken());
        while (TC-- > 0) {
            int n = parseInt(nextToken());
            int a[] = new int[n];
            ArrayList<Integer> od = new ArrayList<>(), ev = new ArrayList<>();

            for (int i = 0; i < n; i++) {
                a[i] = parseInt(nextToken());
                if ((a[i] & 1) == 0) {
                    ev.add(a[i]);
                } else {
                    od.add(a[i]);
                }
            }
            if (ev.size() == n || od.size() == 0) {
                out.println(0);
                continue;
            }
            Collections.sort(ev);
            Collections.sort(od);
            int ans = ev.size();
            long s = od.get(od.size() - 1);
            for (int v : ev) {
                if (v < s) {
                    s += v;
                } else {
                    ans += 1;
                    break;
                }
            }

            out.println(ans);


        }

        out.flush();
        out.close();
        br.close();
    }

    static String nextToken() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}

Can anyone help me understand what is the error in Code 1 ? Like why is Code 1 giving the wrong output in testcase 2, whereas Code 2 is getting AC, even though both of them are using the same logic?

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it

By iamrlm, history, 3 months ago, In English

Welcome to the guide on how to install and use the "My Codeforces Journal" Chrome extension! This extension helps Codeforces users easily track and log the problems they have solved by storing the details directly into a Google Spreadsheet. ( If you install this extension do let me know in the comments : ) )

I am not adding this on the chrome store, because Google charges $5 for that (╥﹏╥)

Follow the steps outlined in this video to get started.

Links:

Features:

  • Few-click Storage: Easily save the current Codeforces problem with a single click.

  • Spreadsheet Integration: Link your own Google Spreadsheet to store and organize problems.

  • Problem Details: Stores key information such as problem URL, name, and rating.

Installation Steps:

  1. Check out: My Codeforces Journal

Usage:

  1. Open any Codeforces problem page.
  1. After solving the problem, click the extension icon and then "Add Problem."
  1. Fill in the form and submit to save the details to your spreadsheet.

Common Errors & Solutions:

  • "Error! Something bad happened!": Ensure the Codeforces ID is correct.
  • "APPSCRIPT NOT FOUND!": Verify the AppScript URL and authorization.
  • "Solve the problem first, then submit!": Make sure the problem is recently solved within the last 40 submissions.

For more detailed instructions and troubleshooting, refer to the README file in the repository My Codeforces Journal.

UPD:

  • I have added a new feature to this extension

The new update adds the following feature:

  • Feature to update the problem details of problems already added to your spreadsheet

Requirements for this new update:

  • Use updated AppScript
  • New Version of chrome-extension

If you are already using this extension:

  • First, thank you very much!
  • Second, you can watch the video here V 1.1.0

Full text and comments »

  • Vote: I like it
  • +12
  • Vote: I do not like it