Number of steps required to reach point ( x , y ) from ( 0 , 0 ) using zig | C ++ program to find the number of steps required to reach ( x , y ) from ( 0 , 0 ) following a zig - zag path ; Function to return the required position ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSteps ( int x , int y ) { if ( x < y ) { return x + y + 2 * ( ( y - x ) / 2 ) ; } else { return x + y + 2 * ( ( ( x - y ) + 1 ) / 2 ) ; } } int main ( ) { int x = 4 , y = 3 ; cout << countSteps ( x , y ) ; return 0 ; }