ONO-Editorial

PROBLEM LINK:

Practice

Contest: Hey Newbees, Here is Some Honey (Round-3)

Idea inspired from-CF-1291A

Author: Samia Rahman

Tester: Arefin Labib,Sanjida Akter,Redowan Ibrahim

Editorialist: Samia Rahman
Please read this blog if you are a Python user and I apologize to you

DIFFICULTY:

SIMPLE.

PREREQUISITES:

Greedy

PROBLEM:

You will be given an integer number.You have to find out the number of minimum number of moves means minimum digits you need to remove from the integer to make it a ONO (You can shuffle the digits as many times as you want which will not count as a move)

ONO means Odd Not Odd more specifically it’s a number which is odd but the summation of it’s digits are even.

EXPLANATION:

We just need to calculate how many odd digits we have in a number.

If there is no odd digit means the number is even.Ans is -1.

If there are even numbers of odd digits in a number(E.g-33, 3322, 3333222),sum of the number’s digits is already even and the number is either odd(e.g-33) or through free shuffling we can make it odd{E.g-2233,2323,3223 etc for the number (3322)} .So we don’t need any move.and is 0.

If there are odd numbers of odd (E.g-333 , 33322) we need to remove a digit to make the sum odd.ans is 1.

Corner case-If there is only one odd (E.g- 3 , 3222 ) we can’t remove any digit to make it a ONO.Because if we remove the odd digit the number will be an even.If we remove even digits sum will still be odd.So if there is only one odd ans is -1.

TIME COMPLEXITY:

O(N)

SOLUTIONS:

Setter's Solution
#include<bits/stdc++.h>
#define ll long long
#define endl "\n"
using namespace std;
int main()
{
    ll t,j;
    cin>>t;
    for(j=0;j<t;j++)
    {
        ll odd=0,i;
        string s;
        cin>>s;
        for(i=0;i<s.size();i++)
        {
            if((s[i]-'0')%2==1)
                odd++;
        }
        if(odd%2==0)
        {
            if(odd==0)
                cout<<"-1"<<endl;
            else
                cout<<"0"<<endl;
        }
        else
        {
           if(odd==1)
                cout<<"-1"<<endl;
            else
                cout<<"1"<<endl;
        }
    }
    return 0;

}

One of the Participants named akamoha’s solve in Python
Click here

1 Like

Can anyone tell me where is this code going wrong? I know I have used excessive if -else loops but I strongly belive that my code’s logic is right. If anyone could point out some mistake or a test case it could fail, would be great.

#include <iostream>
using namespace std;

int main() {
    int t;cin>>t;
    while(t--){
        long long n;cin>>n;
        long long even=0,odd=0;
        while(n>0){
            if((n%10)%2==0){
                even++;
            }else{
                odd++;
            }
            n=n/10;
        }
        if(odd==0){
            
            cout<<-1<<endl;
        }else if(odd % 2 == 0){
            cout<<0<<endl;
        }else{
            if(odd==1){
                cout<<-1<<endl;
            }else{
                if(odd%2!=0){
                    cout<<1<<endl;
                }
            }
        }
    }
	return 0;
}

Please recheck the constrains.
Highest limit of a number’s digit is 10^100.
You’ve taken an long long int data type which can’t afford 10^100 in the worst case
Your logic is absolutely alright.You just need to take string as input instead of long long int.

2 Likes

I didn’t realize this, thanks for pointing out the mistake !

1 Like

You’re welcome

Can anyone please tell my why this JAVA Code gives NZEC error? I cant seem to find any mistakes. Thanks in advance!

import java.io.;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.util.
;
import java.util.concurrent.LinkedBlockingDeque;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
class scratch_25 {

static class Reader {
    static BufferedReader reader;
    static StringTokenizer tokenizer;

    /**
     * call this method to initialize reader for InputStream
     */
    static void init(InputStream input) {
        reader = new BufferedReader(
                new InputStreamReader(input));
        tokenizer = new StringTokenizer("");
    }

    /**
     * get next word
     */
    static String next() throws IOException {
        while (!tokenizer.hasMoreTokens()) {
            //TODO add check for eof if necessary
            tokenizer = new StringTokenizer(
                    reader.readLine());
        }
        return tokenizer.nextToken();
    }

    static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    static long nextLong() throws IOException {
        return Long.parseLong(next());
    }
}
public static void main(String[] args)  throws IOException {
    Reader.init(System.in);
    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
    int t= Reader.nextInt();
    for (int tt = 0; tt <t ; tt++) {
        String s= Reader.next();
        long sum=0;
        long odd=0;
        for (int i = 0; i <s.length() ; i++) {
            char c= s.charAt(i);
            int x= Integer.parseInt(c+"");
            if(x%2!=0){
                odd++;
            }
            sum+=x;
        }
        if(sum%2==0 && odd>0){
            out.append(0+"\n");
        }
        else if(sum%2==0 && odd==0){
            out.append(-1+"\n");
        }
        else if(sum%2!=0 && odd>1){
            out.append(1+"\n");
        }
        else if(sum%2!=0 && odd<=1){
            out.append(-1+"\n");
        }
    }
    out.flush();
    out.close();
}}

Check here Blog.We have rejudged all the submission.
You’ve got Ac now.
Congratulations