{"id":"85aeb354-c64d-4a6a-bc25-dbd6d5f4aff0","name":"Utf - 8 Encoding","description":"1. You are given an array of integers.\r\n2. You are required to test whether the array represents a valid sequence of UTF-8 characters or \r\n not.\r\n3. A character in UTF-8 can be from 1 to 4 bytes long and follows some rules - \r\n (i) For 1-byte long character, first bit will be 0 and rest represents its unicode code.\r\n (ii) For n-bytes long character, first n-bits will be 1's, the n+1th bit is 0, followed by n-1 bytes \r\n with most significant 2 bits being 10.\r\n\r\nNote -> Only the least significant 8 bits of each element in array is used for data.\r\nNote -> Check out the question video for details.","inputFormat":"A number N\r\narr1\r\narr2..\r\nN numbers","outputFormat":"Check the sample output and question video.","constraints":"1 &lt;= n &lt;= 10^9\r\n0 &lt; a[i] &lt;= 255","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\n void solution(vector<int>& arr) {\n \n }\n\n int main() {\n \n int n;\n cin>>n;\n vector<int> arr(n);\n for (int i = 0; i < n; i++) {\n cin>>arr[i];\n }\n solution(arr);\n }"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static boolean solution(int[] arr) {\r\n //write your code here\r\n \r\n return false;\r\n }\r\n\tpublic static void main(String[] args) {\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n int[] arr = new int[n];\r\n for(int i = 0 ; i < n; i++){\r\n arr[i] = scn.nextInt();\r\n }\r\n System.out.println(solution(arr));\r\n }\r\n \r\n \r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"3\r\n197\r\n130\r\n1","sampleOutput":"true\r\n","questionVideo":"https://www.youtube.com/embed/3zyxpFPKkEU?end=402","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"f3e3dbef-d2b7-4f6d-b357-2ef3738e6c91","name":"Bit Manipulation For Intermediate","slug":"bit-manipulation-for-intermediate-9995","type":0},{"id":"018184a8-0ccd-4266-ab4b-11572595c1cd","name":"Utf - 8 Encoding","slug":"utf-8-encoding","type":1}],"next":{"id":"851b16e1-ccb1-4a17-806f-1b182ae3ef9e","name":"UTF - 8 Encoding MCQ","type":0,"slug":"utf-8-encoding-mcq"},"prev":{"id":"3cf11365-e395-437e-9a23-ce4e73453cf6","name":"Abbreviation 1 - Using Bits","type":3,"slug":"abbreviation-1-using-bits"}}}

Utf - 8 Encoding

1. You are given an array of integers. 2. You are required to test whether the array represents a valid sequence of UTF-8 characters or not. 3. A character in UTF-8 can be from 1 to 4 bytes long and follows some rules - (i) For 1-byte long character, first bit will be 0 and rest represents its unicode code. (ii) For n-bytes long character, first n-bits will be 1's, the n+1th bit is 0, followed by n-1 bytes with most significant 2 bits being 10. Note -> Only the least significant 8 bits of each element in array is used for data. Note -> Check out the question video for details.

{"id":"85aeb354-c64d-4a6a-bc25-dbd6d5f4aff0","name":"Utf - 8 Encoding","description":"1. You are given an array of integers.\r\n2. You are required to test whether the array represents a valid sequence of UTF-8 characters or \r\n not.\r\n3. A character in UTF-8 can be from 1 to 4 bytes long and follows some rules - \r\n (i) For 1-byte long character, first bit will be 0 and rest represents its unicode code.\r\n (ii) For n-bytes long character, first n-bits will be 1's, the n+1th bit is 0, followed by n-1 bytes \r\n with most significant 2 bits being 10.\r\n\r\nNote -> Only the least significant 8 bits of each element in array is used for data.\r\nNote -> Check out the question video for details.","inputFormat":"A number N\r\narr1\r\narr2..\r\nN numbers","outputFormat":"Check the sample output and question video.","constraints":"1 &lt;= n &lt;= 10^9\r\n0 &lt; a[i] &lt;= 255","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\n void solution(vector<int>& arr) {\n \n }\n\n int main() {\n \n int n;\n cin>>n;\n vector<int> arr(n);\n for (int i = 0; i < n; i++) {\n cin>>arr[i];\n }\n solution(arr);\n }"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static boolean solution(int[] arr) {\r\n //write your code here\r\n \r\n return false;\r\n }\r\n\tpublic static void main(String[] args) {\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n int[] arr = new int[n];\r\n for(int i = 0 ; i < n; i++){\r\n arr[i] = scn.nextInt();\r\n }\r\n System.out.println(solution(arr));\r\n }\r\n \r\n \r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"3\r\n197\r\n130\r\n1","sampleOutput":"true\r\n","questionVideo":"https://www.youtube.com/embed/3zyxpFPKkEU?end=402","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"f3e3dbef-d2b7-4f6d-b357-2ef3738e6c91","name":"Bit Manipulation For Intermediate","slug":"bit-manipulation-for-intermediate-9995","type":0},{"id":"018184a8-0ccd-4266-ab4b-11572595c1cd","name":"Utf - 8 Encoding","slug":"utf-8-encoding","type":1}],"next":{"id":"851b16e1-ccb1-4a17-806f-1b182ae3ef9e","name":"UTF - 8 Encoding MCQ","type":0,"slug":"utf-8-encoding-mcq"},"prev":{"id":"3cf11365-e395-437e-9a23-ce4e73453cf6","name":"Abbreviation 1 - Using Bits","type":3,"slug":"abbreviation-1-using-bits"}}}
plane

Editor


Loading...

Utf - 8 Encoding

easy

1. You are given an array of integers. 2. You are required to test whether the array represents a valid sequence of UTF-8 characters or not. 3. A character in UTF-8 can be from 1 to 4 bytes long and follows some rules - (i) For 1-byte long character, first bit will be 0 and rest represents its unicode code. (ii) For n-bytes long character, first n-bits will be 1's, the n+1th bit is 0, followed by n-1 bytes with most significant 2 bits being 10. Note -> Only the least significant 8 bits of each element in array is used for data. Note -> Check out the question video for details.

Constraints

1 <= n <= 10^9 0 < a[i] <= 255

Format

Input

A number N arr1 arr2.. N numbers

Output

Check the sample output and question video.

Example

Sample Input

3 197 130 1

Sample Output

true

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode