Decode a given pattern in two ways (Flipkart Interview Question)
Last Updated :
31 Jan, 2023
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] = 0 // First decoded bit is 1
Remaining bits or S[i]s are decoded using following formulas.
P[1] = S[1] + S[0]
P[2] = S[2] + S[1] + S[0]
P[3] = S[3] + S[2] + S[1]
and so on ...
Second, Receiver starts with first character as 1;
S[0] = 1 // First decoded bit is 1
Remaining bits or S[i]s are decoded using following formulas.
P[1] = S[1] + S[0]
P[2] = S[2] + S[1] + S[0]
P[3] = S[3] + S[2] + S[1]
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] = 0,
P[1] = S[1] + S[0], S[1] = 1
P[2] = S[2] + S[1] + S[0], S[2] = 1
P[3] = S[3] + S[2] + S[1], S[3] = 1
P[4] = S[4] + S[3] + S[2], S[4] = 0
P[5] = S[5] + S[4] + S[3], S[5] = 0
P[6] = S[6] + S[5] + S[4], S[6] = 0
Explanation for second output
S[0] = 1,
P[1] = S[1] + S[0], S[1] = 0
P[2] = s[2] + S[1] + S[0], S[2] = 1
P[3] = S[3] + S[2] + S[1], S[3] = 2, not a binary character so NONE.
Source: Flipkart Interview | Set 9 (On-Campus)
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<iostream>
using namespace std;
void decodeUtil( int P[], int n, int first)
{
int S[n];
S[0] = first;
int second = 0;
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];
}
for ( int i = 0; i < n; i++)
cout << S[i];
cout << endl;
}
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[0]);
decode(P, n);
return 0;
}
|
Java
class GFG{
public static void decodeUtil( int P[], int n,
int first)
{
int S[] = new int [n];
S[ 0 ] = first;
int second = 0 ;
for ( int i = 1 ; i < n; i++)
{
S[i] = P[i] - first-second;
if (S[i] != 1 && S[i] != 0 )
{
System.out.println( "NONE" );
return ;
}
second = first;
first = S[i];
}
for ( int i = 0 ; i < n; i++)
{
System.out.print(S[i]);
}
System.out.println();
}
public static void main(String []args)
{
int P[] = { 0 , 1 , 2 , 3 , 2 , 1 , 0 };
int n = P.length;
decodeUtil(P, n, 0 );
decodeUtil(P, n, 1 );
}
}
|
Python3
def decodeUtil(P, n, first):
S = [ 0 for i in range (n)]
S[ 0 ] = first
second = 0
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]
for i in range ( 0 , n, 1 ):
print (S[i], end = "")
print ( "\n" , end = "")
def decode(P, n):
decodeUtil(P, n, 0 )
decodeUtil(P, n, 1 )
if __name__ = = '__main__' :
P = [ 0 , 1 , 2 , 3 , 2 , 1 , 0 ]
n = len (P)
decode(P, n)
|
C#
using System;
class GFG{
static void decodeUtil( int [] P, int n, int first)
{
int [] S = new int [n];
S[0] = first;
int second = 0;
for ( int i = 1; i < n; i++)
{
S[i] = P[i] - first - second;
if (S[i] != 1 && S[i] != 0)
{
Console.WriteLine( "NONE" );
return ;
}
second = first;
first = S[i];
}
for ( int i = 0; i < n; i++)
{
Console.Write(S[i]);
}
Console.WriteLine();
}
static public void Main()
{
int [] P = { 0, 1, 2, 3, 2, 1, 0 };
int n = P.Length;
decodeUtil(P, n, 0);
decodeUtil(P, n, 1);
}
}
|
PHP
<?php
function decodeUtil( $P , $n , $first )
{
$S [0] = $first ;
$second = 0;
for ( $i = 1; $i < $n ; $i ++)
{
$S [ $i ] = $P [ $i ] - $first - $second ;
if ( $S [ $i ] != 1 && $S [ $i ] != 0)
{
echo "NONE\n" ;
return ;
}
$second = $first ;
$first = $S [ $i ];
}
for ( $i = 0; $i < $n ; $i ++)
echo $S [ $i ];
echo "\n" ;
}
function decode( $P , $n )
{
decodeUtil( $P , $n , 0);
decodeUtil( $P , $n , 1);
}
$P = array (0, 1, 2, 3, 2, 1, 0);
$n = sizeof( $P );
decode( $P , $n );
?>
|
Javascript
<script>
function decodeUtil(P, n, first)
{
let S = new Array(n);
S[0] = first;
let second = 0;
for (let i = 1; i < n; i++)
{
S[i] = P[i] - first - second;
if (S[i] != 1 && S[i] != 0)
{
document.write( "NONE" + "</br>" );
return ;
}
second = first;
first = S[i];
}
for (let i = 0; i < n; i++)
{
document.write(S[i]);
}
document.write( "</br>" );
}
let P = [ 0, 1, 2, 3, 2, 1, 0 ];
let n = P.length;
decodeUtil(P, n, 0);
decodeUtil(P, n, 1);
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Share your thoughts in the comments
Please Login to comment...