

- 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
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 Questions & Answers
- Find the Number of Quadrilaterals Possible from the Given Points using C++
- How to find the number of digits in a given number using Python?
- Java program to find the factorial of a given number using recursion
- C++ Program to Find the Number of occurrences of a given Number using Binary Search approach
- Find the number of solutions to the given equation in C++
- Find the number of zeroes using C++
- 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++
- How to find the 95% confidence interval for the slope of regression line in R?
- Find the largest good number in the divisors of given number N in C++
- Find the Number of Siblings of a Given Node in N-ary Tree using C++
- Golang Program to find the parity of a given number.
- Write a Golang program to find the factorial of a given number (Using Recursion)
- Program to find number of subsequences that satisfy the given sum condition using Python