1323A - Even Subset Sum Problem - CodeForces Solution


brute force dp greedy implementation *800

Please click on ads to support us..

Python Code:

import sys
input = lambda : sys.stdin.readline().strip()
'''
╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬
╬╬        ▓▓     ▓▓      ╬╬
╬╬        ▓▓     ▓▓      ╬╬
╬╬        ▓▓█████▓▓      ╬╬
╬╬        ▓▓     ▓▓      ╬╬
╬╬        ▓▓     ▓▓      ╬╬
╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬
'''
for i in range(int(input())):
    l = int(input())
    arr = [int(i)%2 for i in input().split()]
    if arr.count(0)>0:
        print(1)
        print(arr.index(0)+1)
    elif l == 1:
        print(-1)
    else:
        print(2)
        print(l-1,l)



C++ Code:

///  بِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيمِ
///  اللهم صّلِ وسَلّمْ عَلي نَبِيْنَا مُحَمد



#define _CRT_SECURE_NO_WARNINGS
#define ThinkTwiceCodeOnce ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define Num_of_Digits(n) ((int)log10(n) + 1)
#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1e9 + 7;
using namespace std;


vector <int> v;
bool ans = 0;

void rec(int idx, ll sum, int sz, bool Add, queue <int> q)
{

    if (ans)
    {
        return;
    }

    if (Add)
    {
        q.push(idx);
    }

    if (idx >= v.size())
    {
        if (sum % 2 == 0 && sum != 0)
        {
            ans = 1;
            cout << sz << endl;
            while (q.size())
            {
                cout << q.front() << " ";
                q.pop();
            }

            cout << endl;
        
        }

        return;
    
    }

    else if (sum % 2 == 0 && sum != 0)
    {
        ans = 1;
        cout << sz << endl;
        while (q.size())
        {
            cout << q.front() << " ";
            q.pop();
        }

        cout << endl;

        return;
    }

    rec(idx + 1, sum + v[idx], sz + 1, true, q);
    rec(idx + 1, sum, sz, false, q);

}


int main()
{

    ThinkTwiceCodeOnce;

    //     Think Twice Code once!!!!!!     \\
    

    int tc;
    cin >> tc;

    while (tc--)
    {
        v.clear();
        int n;
        cin >> n;        
        
        for (int i = 0; i < n; i++)
        {
            int x;
            cin >> x;
            v.push_back(x);
        }

        ans = 0;

        queue <int> q;
        
        while (q.size())
        {
            q.pop();
        }

        rec(0, 0, 0, 0, q);
        
        if (ans == 0)
        {
            cout << -1 << endl;
        }

    }

    


}


Comments

Submit
0 Comments
More Questions

Divisible
Three primes
Coprimes
Cost of balloons
One String No Trouble
Help Jarvis!
Lift queries
Goki and his breakup
Ali and Helping innocent people
Book of Potion making
Duration
Birthday Party
e-maze-in
Bricks Game
Char Sum
Two Strings
Anagrams
Prime Number
Lexical Sorting Reloaded
1514A - Perfectly Imperfect Array
580A- Kefa and First Steps
1472B- Fair Division
996A - Hit the Lottery
MSNSADM1 Football
MATCHES Playing with Matches
HRDSEQ Hard Sequence
DRCHEF Doctor Chef
559. Maximum Depth of N-ary Tree
821. Shortest Distance to a Character
1441. Build an Array With Stack Operations