- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find the slope of the given number using C++
In this problem, we are given a number N. Our task is to find the slope of the given number.
Slope of a number is the total number of maxima and minima digits in the number.
Maxima digit is the digit whose both neighbours (previous and next) are smaller.
Maxima digit is the digit whose both neighbours (previous and next) are greater.
Let’s take an example to understand the problem,
Input
N = 9594459
Output
2
Solution Approach
A simple solution to the problem is by traversing the number digit by digit from excluding the first and last one (the don’t count form maxima or minima). Now, for each digit, we will check whether the digits are greater or smaller than digits before and after it. At last, we will return the maxima and minima count.
Example
Program to illustrate the working of our solution
#include <iostream> using namespace std; int findNumberSlope(string N, int len){ int slope = 0; for (int i = 1; i < len - 1; i++) { if (N[i] > N[i - 1] && N[i] > N[i + 1]) slope++; else if (N[i] < N[i - 1] && N[i] < N[i + 1]) slope++; } return slope; } int main(){ string N = "574473434329"; int len = N.size(); cout<<" The slope of the given number is "<<findNumberSlope(N, len); return 0; }
Output
The slope of the given number is 7
- Related Articles
- Find the Number of Quadrilaterals Possible from the Given Points using C++
- C++ Program to Find the Number of occurrences of a given Number using Binary Search approach
- How to find the power of any given number by backtracking using C#?
- Find the Number of Prefix Sum Prime in Given Range Query using C++
- Find the Number Of Subarrays Having Sum in a Given Range using C++
- Find the number of zeroes using C++
- Find the Number of Siblings of a Given Node in N-ary Tree using C++
- Program to find slope of a line in C++
- How to find the number of digits in a given number using Python?
- Find the number of solutions to the given equation in C++
- Find the Number Whose Sum of XOR with Given Array Range is Maximum using C++
- Which physical quantity is given by the slope of distance-time graph?
- Find the largest good number in the divisors of given number N in C++
- Find the Number of Stopping Stations using C++
- Find the number of stair steps using C++
