
- 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++ code to find out number of battery combos
Suppose, we have n batteries that can be used a maximum of 5 times. We have some devices that need three batteries and each usage of the device increases the usage count of the batteries by 1. If we have to use the devices k times, we have to find out how many battery combinations we can make to power the devices. A battery cannot be used in two devices simultaneously and a battery that has been used 5 times cannot be included. The usage count of the batteries is given in the array batt.
So, if the input is like n = 6, k = 2, batt = {2, 4, 4, 2, 1, 3}, then the output will be 1.
There can be only one battery combination made to power devices for k times.
Steps
To solve this, we will follow these steps −
ans := 0 for initialize i := 0, when i < n, update (increase i by 1), do: if batt[i] + k <= 5, then: (increase ans by 1) return ans / 3
Example
Let us see the following implementation to get better understanding
#include <bits/stdc++.h> using namespace std; #define N 100 int solve(int n, int k, int batt[]) { int ans = 0; for(int i = 0; i < n; i++){ if(batt[i] + k <= 5) ans++; } return ans / 3; } int main() { int n = 6, k = 2, batt[] = {2, 4, 4, 2, 1, 3}; cout<< solve(n, k, batt); return 0; }
Input
6, 2, {2, 4, 4, 2, 1, 3}
Output
1
- Related Articles
- C++ code to find out which number can be greater
- How to Find out the source code of a transaction in SAP?
- C++ code to find out the sum of the special matrix elements
- C++ code to find out the total amount of sales we made
- C++ code to find out if a grid is fully accessible
- C++ code to find out if everyone will get ice cream
- C++ code to find out who won an n-round game
- C++ code to find the number of scans needed to find an object
- C++ code to find out if a name is male or female
- C++ code to find total number of digits in special numbers
- C++ code to find the number of refill packs to be bought
- 8085 Program to check for two out of five code
- C++ code to find out if an image is B/W or color
- C++ code to find number to disprove given prime hypothesis
- C++ code to find the number of dial rotations to print a string
