
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to check given candies can be split with equal weights or not
Suppose we have an array A with n elements. Amal and Bimal received n candies from their parents. Each candy weighs either 1 gram or 2 grams. They want to divide all candies among themselves fairly so that their total candies weight is same. We have to check whether we can do that or not. (We can not divide a candy into halves).
Problem Category
The above-mentioned problem can be solved by applying Greedy problem-solving techniques. The greedy algorithm techniques are types of algorithms where the current best solution is chosen instead of going through all possible solutions. Greedy algorithm techniques are also used to solve optimization problems, like its bigger brother dynamic programming. In dynamic programming, it is necessary to go through all possible subproblems to find out the optimal solution, but there is a drawback of it; that it takes more time and space. So, in various scenarios greedy technique is used to find out an optimal solution to a problem. Though it does not gives an optimal solution in all cases, if designed carefully it can yield a solution faster than a dynamic programming problem. Greedy techniques provide a locally optimal solution to an optimization problem. Examples of this technique include Kruskal's and Prim's Minimal Spanning Tree (MST) algorithm, Huffman Tree coding, Dijkstra's Single Source Shortest Path problem, etc.
https://www.tutorialspoint.com/data_structures_algorithms/greedy_algorithms.htm
https://www.tutorialspoint.com/data_structures_algorithms/dynamic_programming.htm
So, if the input of our problem is like A = [2, 1, 2, 1], then the output will be True, because we can split them into [1, 2] each.
Steps
To solve this, we will follow these steps −
a := 0 b := 0 n := size of A for initialize i := 0, when i < n, update (increase i by 1), do: k := A[i] (if k is same as 1, then (increase a by 1), otherwise (increase b by 1)) if (a mod 2 is 1 OR (b mod 2 is 1 and a < 2)) then return false, otherwise true
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; bool solve(vector<int> A){ int a = 0; int b = 0; int n = A.size(); for (int i = 0; i < n; i++){ int k = A[i]; (k == 1 ? a++ : b++); } return ((a % 2 || (b % 2 && a < 2)) ? false : true); } int main(){ vector<int> A = { 2, 1, 2, 1 }; cout << solve(A) << endl; }
Input
{ 2, 1, 2, 1 }
Output
1
- Related Articles
- Program to check a string can be split into three palindromes or not in Python
- C++ code to check array can be formed from Equal Not-Equal sequence or not
- Program to check two strings can be equal by swapping characters or not in Python
- Program to check whether first player can take more candies than other or not in Python
- C++ program to check we can buy product with given money or not
- Program to check all tasks can be executed using given server cores or not in Python
- C++ Program to check string can be reduced to 2022 or not
- Program to check a string can be broken into given list of words or not in python
- Program to check whether we can split list into consecutive increasing sublists or not in Python
- Program to check whether we can make group of two partitions with equal sum or not in Python?
- Program to check whether one string swap can make strings equal or not using Python
- Golang Program to Check If Two Arrays are Equal or Not
- Golang Program to Check Whether Two Matrices are Equal or Not
- Swift Program to Check Whether Two Matrices Are Equal or Not
- Swift Program to Check if Two Arrays Are Equal or Not
