# Check if an encoding represents a unique binary string

Given an encoding of a binary string of length k, the task is to find if the given encoding uniquely identifies a binary string or not. The encoding has counts of contiguous 1s (separated by 0s).

For example, encoding of 11111 is {5}, encoding of 01101010 is {2, 1, 1} and encoding of 111011 is {3, 2}.

Examples :

```Input: encoding[] = {3, 3, 3}
Length, k = 12
Output: No

Explanation: There are more than one possible
binary strings. The strings are 111011101110
and 011101110111. Hence “No”

Input: encoding[] = {3, 3, 2}
Length, k = 10
Output: Yes

Explanation: There is only one possible encoding
that is 1110111011
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

A naive approach is to take an empty string and traverse through the n integers to no of 1s as given in encoding[0] and then add 1 zero to it, then encoding[1] 1s, and at the end check if the string length is equal to k then print “Yes” or print “No”

An efficient approach will be to add all the n integers to sum, and then add (n-1) to sum and check if it is equal to K, as n-1 will be the number of zeros in between every 1’s. Check if sum is equal to k, to get exactly one string or else there are more or none.

## C++

 `// C++ program to check if given encoding ` `// represents a single string. ` `#include ` `using` `namespace` `std; ` ` `  `bool` `isUnique(``int` `a[], ``int` `n, ``int` `k) ` `{ ` `    ``int` `sum = 0; ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``sum += a[i]; ` `     `  `    ``sum += n - 1;  ` `     `  `    ``// Return true if sum becomes k ` `    ``return` `(sum == k);  ` `} ` ` `  `// Driver Code ` `int` `main()  ` `{ ` ` `  `int` `a[] = {3, 3, 3}; ` `int` `n = ``sizeof``(a) / ``sizeof``(a[0]); ` `int` `k = 12; ` `if` `(isUnique(a, n, k)) ` `    ``cout << ``"Yes"``; ` `else` `    ``cout << ``"No"``; ` `return` `0; ` `} `

## Java

 `// Java program to check if given encoding ` `// represents a single string. ` `import` `java.io.*; ` ` `  `class` `GFG ` `{ ` `    ``static` `boolean` `isUnique(``int` `[]a, ``int` `n, ``int` `k) ` `    ``{ ` `        ``int` `sum = ``0``; ` `        ``for` `(``int` `i = ``0``; i < n; i++) ` `            ``sum += a[i]; ` `         `  `        ``sum += n - ``1``;  ` `         `  `        ``// Return true if sum becomes k ` `        ``return` `(sum == k);  ` `    ``} ` `     `  `    ``// Driver Code ` `    ``static` `public` `void` `main (String[] args) ` `    ``{ ` `        ``int` `[]a = {``3``, ``3``, ``3``}; ` `        ``int` `n = a.length; ` `        ``int` `k = ``12``; ` `        ``if` `(isUnique(a, n, k)) ` `            ``System.out.println(``"Yes"``); ` `        ``else` `            ``System.out.println(``"No"``); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m `

## Python3

 `# Python 3 program to check if given  ` `# encoding represents a single string. ` ` `  `def` `isUnique(a, n, k): ` `    ``sum` `=` `0` `    ``for` `i ``in` `range``(``0``, n, ``1``): ` `        ``sum` `+``=` `a[i] ` `     `  `    ``sum` `+``=` `n ``-` `1` `     `  `    ``# Return true if sum becomes k ` `    ``return` `(``sum` `=``=` `k)  ` ` `  `# Driver Code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``a ``=` `[``3``, ``3``, ``3``] ` `    ``n ``=` `len``(a) ` `    ``k ``=` `12` `    ``if` `(isUnique(a, n, k)): ` `        ``print``(``"Yes"``) ` `    ``else``: ` `        ``print``(``"No"``) ` ` `  `# This code is contributed by ` `# Surndra_Gangwar `

## C#

 `// C# program to check if given encoding ` `// represents a single string. ` `using` `System; ` ` `  `class` `GFG ` `{ ` `    ``static` `bool` `isUnique(``int` `[]a, ``int` `n, ``int` `k) ` `    ``{ ` `        ``int` `sum = 0; ` `        ``for` `(``int` `i = 0; i < n; i++) ` `            ``sum += a[i]; ` `         `  `        ``sum += n - 1;  ` `         `  `        ``// Return true if sum becomes k ` `        ``return` `(sum == k);  ` `    ``} ` `     `  `    ``// Driver Code ` `    ``static` `public` `void` `Main () ` `    ``{ ` `        ``int` `[]a = {3, 3, 3}; ` `        ``int` `n = a.Length; ` `        ``int` `k = 12; ` `        ``if` `(isUnique(a, n, k)) ` `            ``Console.WriteLine(``"Yes"``); ` `        ``else` `            ``Console.WriteLine(``"No"``); ` `                 `  `    ``} ` `} ` ` `  `// This code is contributed by vt_m `

## PHP

 ` `

Output :

```No
```

Time complexity : O(n)
Auxiliary Space : O(1)

This article is contributed by Striver. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

My Personal Notes arrow_drop_up

Improved By : vt_m, SURENDRA_GANGWAR

Article Tags :
Practice Tags :

1

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