Find the slope of the given number
Last Updated :
24 Mar, 2023
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++
#include <bits/stdc++.h>
using namespace std;
int slopeOfNum(string num, int n)
{
int slope = 0;
for ( int i = 1; i < n - 1; i++) {
if (num[i] > num[i - 1] && num[i] > num[i + 1])
slope++;
else if (num[i] < num[i - 1] && num[i] < num[i + 1])
slope++;
}
return slope;
}
int main()
{
string num = "1213321" ;
int n = num.size();
cout << "Slope = "
<< slopeOfNum(num, n);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int slopeOfNum(String num, int n)
{
int slope = 0 ;
for ( int i = 1 ; i < n - 1 ; i++)
{
if (num.charAt(i) > num.charAt(i - 1 ) &&
num.charAt(i) > num.charAt(i + 1 ))
slope++;
else if (num.charAt(i) < num.charAt(i - 1 ) &&
num.charAt(i) < num.charAt(i + 1 ))
slope++;
}
return slope;
}
public static void main (String[] args)
{
String num = "1213321" ;
int n = num.length();
System.out.println( "Slope = " +
slopeOfNum(num, n));
}
}
|
Python 3
def slopeOfNum(num, n):
slope = 0
for i in range ( 1 , n - 1 ) :
if (num[i] > num[i - 1 ] and
num[i] > num[i + 1 ]):
slope + = 1
elif (num[i] < num[i - 1 ] and
num[i] < num[i + 1 ]):
slope + = 1
return slope
if __name__ = = "__main__" :
num = "1213321"
n = len (num)
print ( "Slope =" , slopeOfNum(num, n))
|
C#
class GFG
{
static int slopeOfNum( string num, int n)
{
int slope = 0;
for ( int i = 1; i < n - 1; i++)
{
if (num[i] > num[i - 1] &&
num[i] > num[i + 1])
slope++;
else if (num[i] < num[i - 1] &&
num[i] < num[i + 1])
slope++;
}
return slope;
}
public static void Main()
{
string num = "1213321" ;
int n = num.Length;
System.Console.WriteLine( "Slope = " +
slopeOfNum(num, n));
}
}
|
PHP
<?php
function slopeOfNum( $num , $n )
{
$slope = 0;
for ( $i = 1; $i < $n - 1; $i ++)
{
if ( $num [ $i ] > $num [ $i - 1] &&
$num [ $i ] > $num [ $i + 1])
$slope ++;
else if ( $num [ $i ] < $num [ $i - 1] &&
$num [ $i ] < $num [ $i + 1])
$slope ++;
}
return $slope ;
}
$num = "1213321" ;
$n = strlen ( $num );
echo "Slope = " . slopeOfNum( $num , $n );
?>
|
Javascript
<script>
function slopeOfNum(num,n)
{
let slope = 0;
for (let i = 1; i < n - 1; i++)
{
if (num[i] > num[i-1] &&
num[i] > num[i+1])
slope++;
else if (num[i] < num[i-1] &&
num[i] < num[i+1])
slope++;
}
return slope;
}
let num = "1213321" ;
let n = num.length;
document.write( "Slope = " +
slopeOfNum(num, n));
</script>
|
Output:
Slope = 2
Time complexity: O(n).
Auxiliary Space : O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...