Found 7197 Articles for C++

Boats to Save People in C++

Arnab Chakraborty
Updated on 30-Apr-2020 10:45:23

703 Views

Suppose we have an array called people. Now the i-th person has weight people[i], and each boat can carry a maximum weight of limit. If each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. We have to find the minimum number of boats to carry every given person. So if the input is like [3,2,1,2], and limit is 3, then we need three boats. [(1,2), (2), (3)].To solve this, we will follow these steps −sort the people arrayi := 0, j := size of people array – 1, ret := 0while i

Stone Game in C++

Arnab Chakraborty
Updated on 30-Apr-2020 10:41:58

573 Views

Suppose we have two players Alex and Lee they play a game with piles of stones. There are an even number of piles that are arranged in a row, and each pile has some number of stones piles[i]. The objective of the game is to end with the most stones. When the total number of stones is odd, there are no ties. Alex and Lee take turns, with Alex starting first. In each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This will be continued until there are no ... Read More

Koko Eating Bananas in C++

Arnab Chakraborty
Updated on 30-Apr-2020 10:36:31

444 Views

Suppose we have N piles of bananas, the i-th pile has piles[i] bananas. Here the guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed is K. Each hour, she takes some pile of bananas, and eats K bananas from that pile. If the pile has less than K bananas, then she consumes all of them instead, and won't eat any more bananas during this hour. Consider Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. We have to find the minimum integer ... Read More

Length of Longest Fibonacci Subsequence in C++

Arnab Chakraborty
Updated on 30-Apr-2020 10:33:46

154 Views

Suppose we have a sequence X_1, X_2, ..., X_n is fibonacci-like if −n >= 3X_i + X_{i+1} = X_{i+2} for all i + 2 = 3 otherwise return 0.Let us see the following implementation to get better understanding −Example Live Demo#include using namespace std; class Solution {    public:    int lenLongestFibSubseq(vector & A) {       int ret = 0;       unordered_map m;       int n = A.size();       vector < vector > dp(n, vector (n));       for(int i = 0; i < n; i++){       ... Read More

Reordered Power of 2 in C++

Arnab Chakraborty
Updated on 19-Jul-2020 19:00:29

256 Views

Suppose we have a positive integer N, we reorder the digits in any order (including the original order) such that the leading digit is non-zero. We have to check whether we can do this in a way such that the resulting number is a power of 2. So if the number is like 46, then the answer will be true.To solve this, we will follow these steps −Define a method called count, this will take x as inputret := 0while x is not 0ret := ret + 10 ^ last digit of xx := x / 10return retFrom the main ... Read More

Prime Palindrome in C++

Arnab Chakraborty
Updated on 30-Apr-2020 10:27:11

1K+ Views

Suppose we have to find the smallest prime palindrome that is greater than or equal to N. So if the N is 13, then the smallest palindrome will be 101.To solve this, we will follow these steps −If N is in range 8 to 11, then return 11for i in range 1 to 99999s := i as a stringr := sreverse rnum := concatenate s and substring of r from index 1, then convert to numberif num >= N and num is prime, then return numreturn 0Let us see the following implementation to get better understanding −Example Live Demo#include using ... Read More

Score After Flipping Matrix in C++

Arnab Chakraborty
Updated on 30-Apr-2020 10:22:45

169 Views

Suppose we have a two dimensional matrix A where each value is 0 or 1. Here a move consists of choosing any row or column, and toggling each value in that row or column: changing all 0s to 1s, and all 1s to 0s. Now after making any number of moves, every row of this matrix is interpreted as a binary number, and the score of the matrix is the sum of these numbers. So our task is to find the highest possible score. If the input is like −001110101100The output will be 39 as after toggling, the matrix will ... Read More

map equal_range() in C++ STL

Ayush Gupta
Updated on 06-Apr-2020 14:19:45

239 Views

In this tutorial, we will be discussing a program to understand map equal_range in C++ STL.This function returns a pair of iterators that bounds the range of the container in which the key equivalent to the given parameter resides.Example Live Demo#include using namespace std; int main() {    //initializing container    map mp;    mp.insert({ 4, 30 });    mp.insert({ 1, 40 });    mp.insert({ 6, 60 });    pair       it;    it = mp.equal_range(1);    cout

Maximum of all Subarrays of size k using set in C++ STL

Ayush Gupta
Updated on 06-Apr-2020 14:16:34

232 Views

In this tutorial, we will be discussing a program to get maximum of all subarrays of size k using set in C++ STL.For this we will be provided with a array of size N and integer K. Our task is to get the maximum element in each K elements, add them up and print it out.Example Live Demo#include using namespace std; //returning sum of maximum elements int maxOfSubarrays(int arr[], int n, int k){    set q;    set::reverse_iterator it;    //inserting elements    for (int i = 0; i < k; i++) {       q.insert(pair(arr[i], i));    } ... Read More

Menu Driven C++ Program for a Simple Calculator

Ayush Gupta
Updated on 06-Apr-2020 14:15:19

1K+ Views

In this tutorial, we will be discussing a program to create a menu driven program for a simple calculator.This program will give user the ability to choose among the following mathematical operations − addition, subtraction, multiplication, division, HCF and LCM.Example Live Demo#include using namespace std; //displaying the menu void menu(){    cout

Advertisements