# [Solved] Number Patterns and Finding the Possible Smallest Numeric Value

#### Problem Statement

Question: Given a pattern containing only Ns and Ms. N represent ascending and M represent descending, Each

character (M or N) needs to display a sequence of numbers (2 numbers) explaining the ascending and

descending order (for ex: 21 ->represents the ascending ->M). The second character in the patterns

takes the last digit from the first character the builds the sequence and so on. Please look at the example

section below.

There could be multiple numbers satisfying the patterns. The goal is to find out the lowest numeric

the value following pattern.

Here are some constraints to this problem:

- Input can have a maximum of 8 characters.
- Output can have Digit from 1-9 and Digits can’t repeat.
- In case of no possible output or incorrect input value (like blank/ null/ NON M or N character), kindly return -1.

#### Input and Output Examples

**Example 1:**

Input: M

Output: 21

Explanation: 2->1 shows descending and possible smallest numeric value. Even 65 or 75 can qualify, but

21 being the smallest numeric value is the correct answer.

**Example 2:**

Input: MNM

Output: 2143

Explanation: M represents descending 2->1. N represents ascending 1->4 (1 is coming from the last

character). M represent descending 4->3 (4 is coming from last character sequence) — There would be

many numbers qualifying the pattern like 3142, 8796, 6241, etc. 2143 is the lowest numeric value for this

pattern sequence.

Number patterns and finding the possible smallest numeric value- This competitive coding challenge question was asked in one of the Goldman Sachs coding rounds.

#### Solution (Python Program)

This is really a tricky problem. I would recommend you to solve this problem by yourself first. Solve this competitive coding challenge question in any programming language you are familiar with, like C/C++, Java or Python.

Here is my Python solution to this problem.

#!/bin/python3 def findPossibleSmallestNumberMatchingPattern(pattern): maxTemp=0 valLast=0 lenMsg=len(pattern) if lenMsg>8: return -1 out=[] i=0 while i<lenMsg: numofM=0 if pattern[i]=='N': numofM=pattern[i+1: ].count('M') if i==0: maxTemp=numofM+2 valLast=valLast+1 out.append(str(valLast)) out.append(str(maxTemp)) valLast=maxTemp else: maxTemp=maxTemp+numofM+1 valLast=maxTemp out.append(str(valLast)) for k in range(numofM): valLast=valLast-1 out.append(str(valLast)) i=i+1 else: if i==0: j=i+1 numofM=0 #find the number of next M. while j<lenMsg and pattern[j]=='M': numofM += 1 j += 1 maxTemp=numofM+2 out.append(str(maxTemp)) out.append(str(maxTemp-1)) valLast=maxTemp-1 else: out.append(str(valLast-1)) valLast=valLast-1 i=i+1 return "".join(out) if __name__ == "__main__": print(findPossibleSmallestNumberMatchingPattern("MNM"))

**Output:**

2143

We used various Python list methods like `append()`

and properties like indexing/slicing in this programming solution.

If you are preparing for a job in any product based company, keep solving coding challenge questions. It will even help you to improve your programming skills.