Find the slope of the given number
Find the slope of the given number num. Slope of a number is the count of the minima and maxima digits in it. A digit is called a minima if the digit is lesser than the digit before and after it. Similarly a digit is called a maxima if the digit is greater than the digit before and after it.
Examples:
Input : 1213321 Output : 2 1213321- The highlighted digit '2' is a maxima and highlighted digit '1' is a minima. Input : 273299302236131 Output : 6
Source: Barclays Interview Experience (On Campus).
Approach: Traverse the digits of the given number from the 2nd digit up to the 2nd last digit. For each digit check whether the digit is greater or smaller than digits before and after it. Get the count of such digits.
C++
// C++ implementation to find slope of a number #include <bits/stdc++.h> using namespace std; // function to find slope of a number int slopeOfNum(string num, int n) { // to store slope of the given // number 'num' int slope = 0; // loop from the 2nd digit up to the 2nd last digit // of the given number 'num' for (int i = 1; i < n - 1; i++) { // if the digit is a maxima if (num[i] > num[i - 1] && num[i] > num[i + 1]) slope++; // if the digit is a minima else if (num[i] < num[i - 1] && num[i] < num[i + 1]) slope++; } // required slope return slope; } // Driver program to test above int main() { string num = "1213321"; int n = num.size(); cout << "Slpoe = " << slopeOfNum(num, n); return 0; } |
Java
// Java implementation to // find slope of a number import java.io.*; class GFG { // function to find // slope of a number static int slopeOfNum(String num, int n) { // to store slope of the // given number 'num' int slope = 0; // loop from the 2nd digit // up to the 2nd last digit // of the given number 'num' for (int i = 1; i < n - 1; i++) { // if the digit is a maxima if (num.charAt(i) > num.charAt(i - 1) && num.charAt(i) > num.charAt(i + 1)) slope++; // if the digit is a minima else if (num.charAt(i) < num.charAt(i - 1) && num.charAt(i) < num.charAt(i + 1)) slope++; } // required slope return slope; } // Driver code public static void main (String[] args) { String num = "1213321"; int n = num.length(); System.out.println("Slope = " + slopeOfNum(num, n)); } } // This code is contributed by Mahadev99 |
Python 3
# Python 3 implementation to find # slope of a number # function to find slope of a number def slopeOfNum(num, n): # to store slope of the given # number 'num' slope = 0 # loop from the 2nd digit up # to the 2nd last digit # of the given number 'num' for i in range(1, n - 1) : # if the digit is a maxima if (num[i] > num[i - 1] and num[i] > num[i + 1]): slope += 1 # if the digit is a minima elif (num[i] < num[i - 1] and num[i] < num[i + 1]): slope += 1 # required slope return slope # Driver Code if __name__ == "__main__": num = "1213321" n = len(num) print("Slpoe =", slopeOfNum(num, n)) # This code is contributed by ita_c |
C#
// C# implementation to // find slope of a number class GFG { // function to find slope of a number static int slopeOfNum(string num, int n) { // to store slope of the // given number 'num' int slope = 0; // loop from the 2nd digit // up to the 2nd last digit // of the given number 'num' for (int i = 1; i < n - 1; i++) { // if the digit is a maxima if (num[i] > num[i - 1] && num[i] > num[i + 1]) slope++; // if the digit is a minima else if (num[i] < num[i - 1] && num[i] < num[i + 1]) slope++; } // required slope return slope; } // Driver code public static void Main() { string num = "1213321"; int n = num.Length; System.Console.WriteLine("Slope = " + slopeOfNum(num, n)); } } // This code is contributed by mits |
PHP
<?php // PHP implementation to find // slope of a number // function to find slope of a number function slopeOfNum($num, $n) { // to store slope of the given // number 'num' $slope = 0; // loop from the 2nd digit up // to the 2nd last digit of // the given number 'num' for ($i = 1; $i < $n - 1; $i++) { // if the digit is a maxima if ($num[$i] > $num[$i - 1] && $num[$i] > $num[$i + 1]) $slope++; // if the digit is a minima else if ($num[$i] < $num[$i - 1] && $num[$i] < $num[$i + 1]) $slope++; } // required slope return $slope; } // Driver Code $num = "1213321"; $n = strlen($num); echo "Slpoe = " . slopeOfNum($num, $n); // This code is contributed // by Akanksha Rai ?> |
Output:
Slope = 2
Time complexity: O(n).
Recommended Posts:
- Find points at a given distance on a line of given slope
- Slope of the line parallel to the line with the given slope
- Find minimum number to be divided to make a number a perfect square
- Given number of matches played, find number of teams in tournament
- Find the number of positive integers less than or equal to N that have an odd number of digits
- Find the maximum number of composite summands of a number
- Find row number of a binary matrix having maximum number of 1s
- Find count of digits in a number that divide the number
- Find the number of ways to divide number into four parts such that a = c and b = d
- Find the total number of composite factor for a given number
- Find the smallest number whose digits multiply to a given number n
- Find the number of integers x in range (1,N) for which x and x+1 have same number of divisors
- Find the largest good number in the divisors of given number N
- Find maximum number that can be formed using digits of a given number
- Find the number of jumps to reach X in the number line from zero
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



