# Decode a given pattern in two ways (Flipkart Interview Question)

A sender sends a binary string to a receiver meanwhile he encrypt the digits. You are given a encrypted form of string. Now, the receiver needs to decode the string, and while decoding there were 2 approaches.

Let the encrypted binary string be P[] and actual string be S[].

```First, receiver starts with first character as 0;
S = 0 // First decoded bit is 1
Remaining bits or S[i]s are decoded using following formulas.
P = S + S
P = S + S + S
P = S + S + S
and so on ...

Second, Receiver starts with first character as 1;
S = 1 // First decoded bit is 1
Remaining bits or S[i]s are decoded using following formulas.
P = S + S
P = S + S + S
P = S + S + S
and so on ... ```

You need to print two string generated using two different after evaluation from both first and second technique. If any string contains other that binary numbers you need to print NONE.

```Input1; 0123210
Output: 0111000, NONE

Explanation for first output
S = 0,
P = S + S, S = 1
P = S + S + S, S = 1
P = S + S + S, S = 1
P = S + S + S, S = 0
P = S + S + S, S = 0
P = S + S + S, S = 0

Explanation for second output
S = 1,
P = S + S, S = 0
P = s + S + S, S = 1
P = S + S + S, S = 2, not a binary character so NONE.```

The idea to solve this problem is simple, we keep track of last two decoded bits. The current bit S[i] can always be calculated by subtracting last two decoded bits from P[i].

Following is the implementation of above idea. We store last two decoded bits in ‘first’ and ‘second’.

## C++

 `#include ` `using` `namespace` `std; ` ` `  `// This function prints decoding of P[] with first decoded ` `// number as 'first'. If the decoded numbers contain anything ` `// other than 0, then "NONE" is printed ` `void` `decodeUtil(``int` `P[], ``int` `n, ``int` `first) ` `{ ` `    ``int` `S[n];  ``// array to store decoded bit pattern ` `    ``S = first;  ``// The first number is always the given number ` ` `  `    ``int` `second = 0;  ``// Initialize second ` ` `  `    ``// Calculate all bits starting from second ` `    ``for` `(``int` `i = 1; i < n; i++) ` `    ``{ ` `        ``S[i] = P[i] -  first - second; ` `        ``if` `(S[i] != 1 && S[i] != 0) ` `        ``{ ` `            ``cout << ``"NONE\n"``; ` `            ``return``; ` `        ``} ` `        ``second = first; ` `        ``first = S[i]; ` `    ``} ` ` `  `    ``// Print the output array ` `    ``for` `(``int` `i = 0; i < n; i++) ` `       ``cout << S[i]; ` `    ``cout << endl; ` `} ` ` `  `// This function decodes P[] using two techniques ` `// 1) Starts with 0 as first number 2) Starts 1 as first number ` `void` `decode(``int` `P[], ``int` `n) ` `{ ` `    ``decodeUtil(P, n, 0); ` `    ``decodeUtil(P, n, 1); ` `} ` ` `  `int` `main() ` `{ ` `    ``int` `P[] = {0, 1, 2, 3, 2, 1, 0}; ` `    ``int` `n = ``sizeof``(P)/``sizeof``(P); ` `    ``decode(P, n); ` `    ``return` `0; ` `} `

## Python3

 `# Python3 program to Decode a given ` `# pattern in two ways ` ` `  `# This function prints decoding of P[] with  ` `# first decoded number as 'first'. If the  ` `# decoded numbers contain anything other ` `# than 0, then "NONE" is printed ` `def` `decodeUtil(P, n, first): ` `    ``S ``=` `[``0` `for` `i ``in` `range``(n)] ` `     `  `    ``# array to store decoded bit pattern ` `    ``S[``0``] ``=` `first ``# The first number is  ` `                 ``# always the given number ` ` `  `    ``second ``=` `0` `     `  `    ``# Initialize second ` ` `  `    ``# Calculate all bits starting from second ` `    ``for` `i ``in` `range``(``1``, n, ``1``): ` `        ``S[i] ``=` `P[i] ``-` `first ``-` `second ` `        ``if` `(S[i] !``=` `1` `and` `S[i] !``=` `0``): ` `            ``print``(``"NONE"``) ` `            ``return` `     `  `        ``second ``=` `first ` `        ``first ``=` `S[i] ` ` `  `    ``# Print the output array ` `    ``for` `i ``in` `range``(``0``, n, ``1``): ` `        ``print``(S[i], end ``=` `"") ` `    ``print``(``"\n"``, end ``=` `"") ` ` `  `# This function decodes P[] using ` `# two techniques ` `# 1) Starts with 0 as first number ` `# 2) Starts 1 as first number ` `def` `decode(P, n): ` `    ``decodeUtil(P, n, ``0``) ` `    ``decodeUtil(P, n, ``1``) ` ` `  `# Driver Code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``P ``=` `[``0``, ``1``, ``2``, ``3``, ``2``, ``1``, ``0``] ` `    ``n ``=` `len``(P) ` `    ``decode(P, n) ` `     `  `# This code is contributed by ` `# Shashank_Sharma `

## PHP

 ` `

Output:

```0111000
NONE```

Time Complexity: O(n)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.