

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ program to count number of problems can be solved from left or right end of list
Suppose we have an array A with n elements and another number k is there. Consider there are n problems on a contest. Amal's problem solving skill is k. Amal always solves problem from any of the ends of a list. And He cannot solve problem whose difficulty is greater than k. He stops when the left and right problems' difficulty is greater than k. We have to count how many problems he can solve. A[i] represents the difficulty of ith problem.
So, if the input is like A = [4, 2, 3, 1, 5, 1, 6, 4]; k = 4, then the output will be 5, because initially solve left most problem with 4, then right most problem with 4, then cannot solve right most problem anymore, then from left, solve problems with difficulty 2, 3 and 1. In total 5 problems can be solved.
Steps
To solve this, we will follow these steps −
n := size of A l := 0 r := n - 1 for initialize i := 0, when i < n, update (increase i by 1), do: if A[i] <= k and l is same as i, then: (increase l by 1) while A[r] <= k, do: (decrease r by 1) if l is same as n, then: return n Otherwise return n - 1 - r + l
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; int solve(vector<int> A, int k) { int n = A.size(); int l = 0, r = n - 1; for (int i = 0; i < n; ++i) { if (A[i] <= k && l == i) ++l; } while (A[r] <= k) --r; if (l == n) return n; else return n - 1 - r + l; } int main() { vector<int> A = { 4, 2, 3, 1, 5, 1, 6, 4 }; int k = 4; cout << solve(A, k) << endl; }
Input
{ 4, 2, 3, 1, 5, 1, 6, 4 }, 4
Output
5
- Related Questions & Answers
- C++ Program to count minimum problems to be solved to make teams
- Path Loss - Solved Numerical Problems from Wireless Communications
- Program to sqeeze list elements from left or right to make it single element in Python
- Program to find number of ways we can reach from top left point to bottom right point in Python
- Program to count number of horizontal brick pattern can be made from set of bricks in Python
- Program to count number of walls required to partition top-left and bottom-right cells in Python
- Wireless Channel Noise: Solved Problems on Noise Power
- C++ Program to count number of teams can be formed for coding challenge
- Program to count number of palindromes of size k can be formed from the given string characters in Python
- Program to count number of paths with cost k from start to end point in Python
- C++ program to find number of groups can be formed from set of programmers
- Program to check we can reach end of list by starting from k in Python
- Program to count maximum number of strings we can generate from list of words and letter counts in python
- C++ code to count number of times stones can be given
- Count all possible paths from top left to bottom right of a mXn matrix in C++