Logo

dev-resources.site

for different kinds of informations.

Recursion it is : LeetCode 494

Published at
12/26/2024
Categories
cpp
programming
datastructures
computerscience
Author
ankit_rattan
Author
12 person written this
ankit_rattan
open
Recursion it is : LeetCode 494

Today is 26 December, and I found a perfect problem that explains recursion to the point. LeetCode problem 494 is the POTD (Problem of the day). It is named Target Sum, but wait, not the normal generalized target sum that you are thinking right now. It is better to read the problem first and then proceed further here.

So, in this... better to follow your first intuition, that is handling addition (+) and subtraction (-), separately. And when it comes to handle cases individually and combining it in every step we have our BOSS ===> Recursion!

Everytime for each index you are adding and subtracting for each. And at the end when traversing the whole array then, just check if it is equal to the target value or not.

Well, here's the code below. But better you try it first.

*BTW, there is one more approach which I found later, just giving hint : an additional check of even totaling and playing with subsets and maximum value.... okay dp it is! 🫀

    void solve(int& ans, int ind, vector<int>& nums, int sum, int t) {
        if (ind == nums.size()) {
            if (sum == t) {
                ans++;
            }
            return;
        }
        solve(ans, ind + 1, nums, sum + nums[ind], t);
        solve(ans, ind + 1, nums, sum - nums[ind], t);
    }
    int findTargetSumWays(vector<int>& nums, int target) {
        int ans = 0;
        sort(nums.begin(), nums.end());
        int sum = 0;
        int ind = 0;
        solve(ans, ind, nums, sum, target);
        return ans;
    }
Enter fullscreen mode Exit fullscreen mode
computerscience Article's
30 articles in total
Favicon
Binary Made Easy β€” Understand the Basics
Favicon
Understanding Lists in Python
Favicon
Truth Tables: Foundations and Applications in Logic and Neural Networks
Favicon
LinearBoost: Faster Than XGBoost and LightGBM, Outperforming Them on F1 Score on Seven Famous Benchmark Datasets
Favicon
External Merge Problem - Complete Guide for Gophers
Favicon
From 0 to O(n): A Beginner's Guide to Big O Notation
Favicon
Here’s why Julia is THE language in scientific programming
Favicon
Securing C++ iostream: Key Vulnerabilities and Mitigation Strategies
Favicon
Why Your Brain Ghosts Most of Your Memories!?
Favicon
I am currently reducing at least 22 proofs by at least 99312 steps total.
Favicon
Guide to TCP/IP Ports and UDP Ports
Favicon
Important Port Numbers in Networking and Open-Source Projects
Favicon
A Practical Approach to Problem-Solving in JavaScript
Favicon
CS50 - Week 6
Favicon
Quintum Computing And History
Favicon
Relational Database Design: DBMS
Favicon
πŸš€ Say Hello to PaperLens! πŸ”Ž
Favicon
Database Scaling NLogN πŸ“ˆ
Favicon
LeetCode Meditations: Sum of Two Integers
Favicon
Is there an ethics issue in computer science? (EPQ)
Favicon
Confusion about coding field after introduced chatgpt and other AI models AI can make itself code and also make websites and apps etc. We have a carrier confusion because I am a BTech 1st year computer science student. Please help.
Favicon
Kubernetes Gateway API
Favicon
Recursion it is : LeetCode 494
Favicon
Simplifying the Cloud: Fundamentals of Cloud Computing with a Focus on AWS
Favicon
Trees in SQL
Favicon
Trees in SQL (part 2)
Favicon
Behavioural Analysis models for a project
Favicon
Choosing the Right Excel Consultant: Boost Efficiency and Productivity
Favicon
Pointers in C++: Memory Management, Arrays, and Smart Pointers
Favicon
Designing for Durability: How Precision Engineering Creates Tools That Last

Featured ones: