{"group":{"group":{"id":70,"name":"Combinatorics II","lockable":false,"created_at":"2018-08-29T19:03:13.000Z","updated_at":"2025-12-14T01:33:56.000Z","description":"Hmm...did we already try that route? No, I'm not lost. No, I don't need directions.","is_default":false,"created_by":26769,"badge_id":62,"featured":false,"trending":false,"solution_count_in_trending_period":12,"trending_last_calculated":"2025-12-14T00:00:00.000Z","image_id":477,"published":true,"community_created":false,"status_id":2,"is_default_group_for_player":false,"deleted_by":null,"deleted_at":null,"restored_by":null,"restored_at":null,"description_opc":"{\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eHmm...did we already try that route? No, I'm not lost. No, I don't need directions.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\",\"relationship\":null}],\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"target\":\"/matlab/document.xml\",\"relationshipId\":\"rId1\"}]}","description_html":"\u003cdiv style = \"text-align: start; line-height: normal; min-height: 0px; white-space: normal; color: rgb(0, 0, 0); font-family: Menlo, Monaco, Consolas, monospace; font-style: normal; font-size: 14px; font-weight: 400; text-decoration: none solid rgb(0, 0, 0); white-space: normal; \"\u003e\u003cdiv style=\"display: block; min-width: 0px; padding-top: 0px; perspective-origin: 289.5px 10.5px; transform-origin: 289.5px 10.5px; \"\u003e\u003cdiv style=\"font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-bottom: 9px; margin-left: 4px; margin-right: 10px; margin-top: 2px; text-align: left; white-space: pre-wrap; perspective-origin: 266.5px 10.5px; transform-origin: 266.5px 10.5px; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 0px 0px; transform-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eHmm...did we already try that route? No, I'm not lost. No, I don't need directions.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e","published_at":"2019-08-26T19:01:18.000Z"},"current_player":null},"problems":[{"id":1049,"title":"Path of least resistance","description":"Find the length of the shortest path through the matrix from the top left to bottom right corner. You may move right, down, or diagonally right-down one element at a time. The length of the path is the sum of the elements you pass through.\r\nE.g.\r\n M = [*8      6      10      10     4     7     7      7\r\n       9     *1      10      5      9     0     8      2\r\n       1      3     *2       8      8     8     7      7\r\n       9      5      10     *1     *10   *9    *4     *0 ]; \r\n\r\n\u003e\u003e shortest_path(M)\r\n\r\nans =\r\n\r\n      35\r\nThe shortest path through this matrix has length 35. Each element along the path is marked with a *","description_html":"\u003cdiv style = \"text-align: start; line-height: 20.4333px; min-height: 0px; white-space: normal; color: rgb(0, 0, 0); font-family: Menlo, Monaco, Consolas, monospace; font-style: normal; font-size: 14px; font-weight: 400; text-decoration: rgb(0, 0, 0); white-space: normal; \"\u003e\u003cdiv style=\"block-size: 317.333px; display: block; min-width: 0px; padding-block-start: 0px; padding-top: 0px; perspective-origin: 407px 158.667px; transform-origin: 407px 158.667px; vertical-align: baseline; \"\u003e\u003cdiv style=\"block-size: 42px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 21px; text-align: left; transform-origin: 384px 21px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 381.5px 8px; transform-origin: 381.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eFind the length of the shortest path through the matrix from the top left to bottom right corner. You may move right, down, or diagonally right-down one element at a time. The length of the path is the sum of the elements you pass through.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 12.5px 8px; transform-origin: 12.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eE.g.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgb(247, 247, 247); block-size: 204.333px; border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; border-end-end-radius: 4px; border-end-start-radius: 4px; border-start-end-radius: 4px; border-start-start-radius: 4px; border-top-left-radius: 4px; border-top-right-radius: 4px; margin-block-end: 10px; margin-block-start: 10px; margin-bottom: 10px; margin-inline-end: 3px; margin-inline-start: 3px; margin-left: 3px; margin-right: 3px; margin-top: 10px; perspective-origin: 404px 102.167px; transform-origin: 404px 102.167px; margin-left: 3px; margin-top: 10px; margin-bottom: 10px; margin-right: 3px; \"\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4333px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2167px; transform-origin: 404px 10.2167px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 224px 8.5px; tab-size: 4; transform-origin: 224px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e M = [*8      6      10      10     4     7     7      7\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4333px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2167px; transform-origin: 404px 10.2167px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 224px 8.5px; tab-size: 4; transform-origin: 224px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e       9     *1      10      5      9     0     8      2\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4333px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2167px; transform-origin: 404px 10.2167px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 224px 8.5px; tab-size: 4; transform-origin: 224px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e       1      3     *2       8      8     8     7      7\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4333px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2167px; transform-origin: 404px 10.2167px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 240px 8.5px; tab-size: 4; transform-origin: 240px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e       9      5      10     *1     *10   *9    *4     *0 ]; \u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4333px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2167px; transform-origin: 404px 10.2167px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 0px 8.5px; tab-size: 4; transform-origin: 0px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4333px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2167px; transform-origin: 404px 10.2167px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 76px 8.5px; tab-size: 4; transform-origin: 76px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e\u0026gt;\u0026gt; shortest_path(M)\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4333px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2167px; transform-origin: 404px 10.2167px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 0px 8.5px; tab-size: 4; transform-origin: 0px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4333px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2167px; transform-origin: 404px 10.2167px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 20px 8.5px; tab-size: 4; transform-origin: 20px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003eans =\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4333px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2167px; transform-origin: 404px 10.2167px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 0px 8.5px; tab-size: 4; transform-origin: 0px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4333px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2167px; transform-origin: 404px 10.2167px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 32px 8.5px; tab-size: 4; transform-origin: 32px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e      35\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 10px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 10px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 10px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 308px 8px; transform-origin: 308px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eThe shortest path through this matrix has length 35. Each element along the path is marked with a\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 4.5px 8px; transform-origin: 4.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e *\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e","function_template":"function p = shortest_path(M)\r\n  p = sum(M(1:end,1)) + sum(M(end,2:end));\r\nend","test_suite":"%%\r\nM = [8     6    10    10     4     7     7     7\r\n     9     1    10     5     9     0     8     2\r\n     1     3     2     8     8     8     7     7\r\n     9     5    10     1    10     9     4     0 ];\r\ny_correct = 35;\r\nassert(isequal(shortest_path(M),y_correct))\r\n\r\n%%\r\nM =  [6     8     5     5     3\r\n      5     4     8     0     5\r\n      9     6     9     3     2\r\n      3     1     1     2     6\r\n      8     1     6     8     3 ];\r\ny_correct = 22;\r\nassert(isequal(shortest_path(M),y_correct))\r\n\r\n%% \r\nM = hadamard(8);\r\ny_correct = -5;\r\nassert(isequal(shortest_path(M),y_correct))\r\n\r\n%% \r\nassert(isequal(arrayfun(@(x) shortest_path(eye(x)), 2:5),[2 2 2 2]))\r\n","published":true,"deleted":false,"likes_count":9,"comments_count":1,"created_by":450,"edited_by":223089,"edited_at":"2022-05-09T17:30:33.000Z","deleted_by":null,"deleted_at":null,"solvers_count":80,"test_suite_updated_at":"2022-05-09T17:30:33.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2012-11-22T20:58:51.000Z","updated_at":"2026-01-06T08:40:37.000Z","published_at":"2012-11-22T21:17:01.000Z","restored_at":null,"restored_by":null,"spam":null,"simulink":false,"admin_reviewed":false,"description_opc":"{\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eFind the length of the shortest path through the matrix from the top left to bottom right corner. You may move right, down, or diagonally right-down one element at a time. The length of the path is the sum of the elements you pass through.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eE.g.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ M = [*8      6      10      10     4     7     7      7\\n       9     *1      10      5      9     0     8      2\\n       1      3     *2       8      8     8     7      7\\n       9      5      10     *1     *10   *9    *4     *0 ]; \\n\\n\u003e\u003e shortest_path(M)\\n\\nans =\\n\\n      35]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe shortest path through this matrix has length 35. Each element along the path is marked with a\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e *\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\",\"relationship\":null}],\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"target\":\"/matlab/document.xml\",\"relationshipId\":\"rId1\"}]}"},{"id":2534,"title":"Path of least resistance - Move all direction","description":"Extension of the wonderful Problem 1049 (Path of Least Resistance).\r\nA matrix is given as input. You have to take a tour starting from the top left corner to the bottom right corner of the matrix. You can move to any one of the adjacent 8 elements. Return only the sum of the elements in your path.\r\nExample :\r\nInput = [1 9 1 1\r\n         9 1 9 1\r\n         1 3 9 1\r\n         9 9 8 1];\r\n\r\nOutput = 7;\r\nHere, the least-sum path though the matrix is shown below with asterisk(*):\r\n          [1* 9  1* 1*\r\n           9  1* 9  1*\r\n           1  3  9  1*\r\n           9  9  8  1*];\r\nReturn only the sum of the elements along the path. I will add robust test cases time to time WITHOUT re-scoring existing solutions.","description_html":"\u003cdiv style = \"text-align: start; line-height: 20.4333px; min-height: 0px; white-space: normal; color: rgb(0, 0, 0); font-family: Menlo, Monaco, Consolas, monospace; font-style: normal; font-size: 14px; font-weight: 400; text-decoration: rgb(0, 0, 0); white-space: normal; \"\u003e\u003cdiv style=\"block-size: 409.333px; display: block; min-width: 0px; padding-block-start: 0px; padding-top: 0px; perspective-origin: 407px 204.667px; transform-origin: 407px 204.667px; vertical-align: baseline; \"\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 83px 8px; transform-origin: 83px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eExtension of the wonderful\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 2px 8px; transform-origin: 2px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"https://www.mathworks.com/matlabcentral/cody/problems/1049-path-of-least-resistance\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"\"\u003eProblem 1049\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 88px 8px; transform-origin: 88px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e (Path of Least Resistance).\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 42px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 21px; text-align: left; transform-origin: 384px 21px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 381.5px 8px; transform-origin: 381.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eA matrix is given as input. You have to take a tour starting from the top left corner to the bottom right corner of the matrix. You can move to any one of the adjacent 8 elements. Return only the sum of the elements in your path.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 30.5px 8px; transform-origin: 30.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eExample :\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgb(247, 247, 247); block-size: 122.6px; border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; border-end-end-radius: 4px; border-end-start-radius: 4px; border-start-end-radius: 4px; border-start-start-radius: 4px; border-top-left-radius: 4px; border-top-right-radius: 4px; margin-block-end: 10px; margin-block-start: 10px; margin-bottom: 10px; margin-inline-end: 3px; margin-inline-start: 3px; margin-left: 3px; margin-right: 3px; margin-top: 10px; perspective-origin: 404px 61.3px; transform-origin: 404px 61.3px; margin-left: 3px; margin-top: 10px; margin-bottom: 10px; margin-right: 3px; \"\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4333px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2167px; transform-origin: 404px 10.2167px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 64px 8.5px; tab-size: 4; transform-origin: 64px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003eInput = [1 9 1 1\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4333px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2167px; transform-origin: 404px 10.2167px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 64px 8.5px; tab-size: 4; transform-origin: 64px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e         9 1 9 1\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4333px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2167px; transform-origin: 404px 10.2167px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 64px 8.5px; tab-size: 4; transform-origin: 64px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e         1 3 9 1\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4333px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2167px; transform-origin: 404px 10.2167px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 72px 8.5px; tab-size: 4; transform-origin: 72px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e         9 9 8 1];\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4333px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2167px; transform-origin: 404px 10.2167px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 0px 8.5px; tab-size: 4; transform-origin: 0px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4333px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2167px; transform-origin: 404px 10.2167px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 44px 8.5px; tab-size: 4; transform-origin: 44px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003eOutput = 7;\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 10px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 10px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 10px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 223.5px 8px; transform-origin: 223.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eHere, the least-sum path though the matrix is shown below with asterisk\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 5px 8px; transform-origin: 5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e(*\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 4.5px 8px; transform-origin: 4.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e):\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgb(247, 247, 247); block-size: 81.7333px; border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; border-end-end-radius: 4px; border-end-start-radius: 4px; border-start-end-radius: 4px; border-start-start-radius: 4px; border-top-left-radius: 4px; border-top-right-radius: 4px; margin-block-end: 10px; margin-block-start: 10px; margin-bottom: 10px; margin-inline-end: 3px; margin-inline-start: 3px; margin-left: 3px; margin-right: 3px; margin-top: 10px; perspective-origin: 404px 40.8667px; transform-origin: 404px 40.8667px; margin-left: 3px; margin-top: 10px; margin-bottom: 10px; margin-right: 3px; \"\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4333px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2167px; transform-origin: 404px 10.2167px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 88px 8.5px; tab-size: 4; transform-origin: 88px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e          [1* 9  1* 1*\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4333px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2167px; transform-origin: 404px 10.2167px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 88px 8.5px; tab-size: 4; transform-origin: 88px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e           9  1* 9  1*\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4333px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2167px; transform-origin: 404px 10.2167px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 88px 8.5px; tab-size: 4; transform-origin: 88px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e           1  3  9  1*\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4333px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2167px; transform-origin: 404px 10.2167px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 96px 8.5px; tab-size: 4; transform-origin: 96px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e           9  9  8  1*];\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 42px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 10px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 10px; perspective-origin: 384px 21px; text-align: left; transform-origin: 384px 21px; white-space: pre-wrap; margin-left: 4px; margin-top: 10px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 381px 8px; transform-origin: 381px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eReturn only the sum of the elements along the path. I will add robust test cases time to time WITHOUT re-scoring existing solutions.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e","function_template":"function y = lPath(x)\r\n\r\nend","test_suite":"%%\r\nfiletext = fileread('lPath.m');\r\nillegal = contains(filetext, 'regexp') || contains(filetext, 'assert') || ...\r\n          contains(filetext, 'switch'); \r\nassert(~illegal)\r\n\r\n%%\r\nx = [1 9 1 1\r\n     9 1 9 1\r\n     1 3 9 1\r\n     9 9 8 1];\r\ny_correct = 6;\r\nassert(isequal(lPath(x),y_correct))\r\n\r\n\r\n%%\r\nx = [1 9 9 9 9 9 9\r\n     1 9 9 9 3 1 9\r\n     1 1 1 1 9 9 1];\r\ny_correct = 10;\r\nassert(isequal(lPath(x),y_correct))\r\n\r\n%%\r\nx = eye(randi(100));\r\ny_correct = 2;\r\nassert(isequal(lPath(x),y_correct))\r\n\r\n%%\r\nx = zeros(randi(100));\r\ny_correct = 0;\r\nassert(isequal(lPath(x),y_correct))\r\n\r\n%%\r\nx = ones(randi(10));\r\ny_correct = trace(x);\r\nassert(isequal(lPath(x),y_correct))","published":true,"deleted":false,"likes_count":2,"comments_count":7,"created_by":17203,"edited_by":223089,"edited_at":"2022-12-26T07:35:56.000Z","deleted_by":null,"deleted_at":null,"solvers_count":26,"test_suite_updated_at":"2022-12-26T07:35:56.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2014-08-27T09:50:17.000Z","updated_at":"2025-12-16T03:13:42.000Z","published_at":"2014-08-27T09:50:17.000Z","restored_at":null,"restored_by":null,"spam":null,"simulink":false,"admin_reviewed":false,"description_opc":"{\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eExtension of the wonderful\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://www.mathworks.com/matlabcentral/cody/problems/1049-path-of-least-resistance\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eProblem 1049\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e (Path of Least Resistance).\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eA matrix is given as input. You have to take a tour starting from the top left corner to the bottom right corner of the matrix. You can move to any one of the adjacent 8 elements. Return only the sum of the elements in your path.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eExample :\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[Input = [1 9 1 1\\n         9 1 9 1\\n         1 3 9 1\\n         9 9 8 1];\\n\\nOutput = 7;]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eHere, the least-sum path though the matrix is shown below with asterisk\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e(*\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e):\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[          [1* 9  1* 1*\\n           9  1* 9  1*\\n           1  3  9  1*\\n           9  9  8  1*];]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eReturn only the sum of the elements along the path. I will add robust test cases time to time WITHOUT re-scoring existing solutions.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\",\"relationship\":null}],\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"target\":\"/matlab/document.xml\",\"relationshipId\":\"rId1\"}]}"},{"id":1483,"title":"Number of paths on a grid","description":"\r\nConsider a grid formed by n vertices vertically down, and m vertices horizontally right. Your starting point is at the top left vertex. \r\nYour destination is the bottom right vertex. You are permitted at each vertex to choose to move down or right, that is in the direction towards the destination. You are not to move on what constitutes a back step like moving left or up. If you hit the bottom boundary, or right boundary take it to be given there is only 1 way to the destination, that is following along the boundary. \r\n\r\nEx: in a 2x2 grid there are two ways. One way: First down, then right. The other way: First right, then down.   \r\n\r\n4x3 has 10 ways\r\n\r\n6x5 has 126 ways\r\n\r\nThis problem can be solved using dynamic programming but there are other methods too. \r\n\r\nProblem 7)\r\nPrev: \u003chttp://www.mathworks.com/matlabcentral/cody/problems/1482 1482\u003e\r\nNext: \u003chttp://www.mathworks.com/matlabcentral/cody/problems/1484 1484\u003e","description_html":"\u003cdiv style = \"text-align: start; line-height: 20px; min-height: 0px; white-space: normal; color: rgb(0, 0, 0); font-family: Menlo, Monaco, Consolas, monospace; font-style: normal; font-size: 14px; font-weight: normal; text-decoration: none; white-space: normal; \"\u003e\u003cdiv style=\"display: block; min-width: 0px; padding-top: 0px; transform-origin: 332px 148.5px; vertical-align: baseline; perspective-origin: 332px 148.5px; \"\u003e\u003cdiv style=\"font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-bottom: 9px; margin-left: 4px; margin-right: 10px; margin-top: 2px; text-align: left; transform-origin: 309px 63px; white-space: pre-wrap; perspective-origin: 309px 63px; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eConsider a grid formed by n vertices vertically down, and m vertices horizontally right. Your starting point is at the top left vertex. Your destination is the bottom right vertex. You are permitted at each vertex to choose to move down or right, that is in the direction towards the destination. You are not to move on what constitutes a back step like moving left or up. If you hit the bottom boundary, or right boundary take it to be given there is only 1 way to the destination, that is following along the boundary.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-bottom: 9px; margin-left: 4px; margin-right: 10px; margin-top: 2px; text-align: left; transform-origin: 309px 21px; white-space: pre-wrap; perspective-origin: 309px 21px; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eEx: in a 2x2 grid there are two ways. One way: First down, then right. The other way: First right, then down.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-bottom: 9px; margin-left: 4px; margin-right: 10px; margin-top: 2px; text-align: left; transform-origin: 309px 10.5px; white-space: pre-wrap; perspective-origin: 309px 10.5px; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e4x3 has 10 ways\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-bottom: 9px; margin-left: 4px; margin-right: 10px; margin-top: 2px; text-align: left; transform-origin: 309px 10.5px; white-space: pre-wrap; perspective-origin: 309px 10.5px; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e6x5 has 126 ways\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-bottom: 9px; margin-left: 4px; margin-right: 10px; margin-top: 2px; text-align: left; transform-origin: 309px 10.5px; white-space: pre-wrap; perspective-origin: 309px 10.5px; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eThis problem can be solved using dynamic programming but there are other methods too.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-bottom: 9px; margin-left: 4px; margin-right: 10px; margin-top: 2px; text-align: left; transform-origin: 309px 10.5px; white-space: pre-wrap; perspective-origin: 309px 10.5px; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003eProblem 7) Prev:\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"http://www.mathworks.com/matlabcentral/cody/problems/1482\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"\"\u003e1482\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e Next:\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"display: inline; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; transform-origin: 0px 0px; perspective-origin: 0px 0px; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"http://www.mathworks.com/matlabcentral/cody/problems/1484\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"\"\u003e1484\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e","function_template":"function y = paths2dest_ongrid(n,m)\r\n  y = n+m;\r\nend","test_suite":"%%\r\nm = 1; n = 1 ;\r\ny_correct = 1;\r\nassert(isequal(paths2dest_ongrid(m,n),y_correct))\r\n\r\n%%\r\nm = 2; n = 2 ;\r\ny_correct = 2;\r\nassert(isequal(paths2dest_ongrid(m,n),y_correct))\r\n\r\n%%\r\nm = 4; n = 3 ;\r\ny_correct = 10;\r\nassert(isequal(paths2dest_ongrid(m,n),y_correct))\r\n\r\n%%\r\nm = 6; n = 5 ;\r\ny_correct = 126;\r\nassert(isequal(paths2dest_ongrid(m,n),y_correct))\r\n\r\n%%\r\nm = 5; n = 5 ;\r\ny_correct = 70;\r\nassert(isequal(paths2dest_ongrid(m,n),y_correct))\r\n\r\n%%\r\nm = 1; n = 100 ;\r\ny_correct = 1;\r\nassert(isequal(paths2dest_ongrid(m,n),y_correct))\r\n\r\n%%\r\nm = 100; n = 1 ;\r\ny_correct = 1;\r\nassert(isequal(paths2dest_ongrid(m,n),y_correct))\r\n\r\n%%\r\nm = 2; n = 100 ;\r\ny_correct = 100;\r\nassert(isequal(paths2dest_ongrid(m,n),y_correct))\r\n\r\n%%\r\nm = 100; n = 2 ;\r\ny_correct = 100;\r\nassert(isequal(paths2dest_ongrid(m,n),y_correct))\r\n\r\n%%\r\nm = 15; n = 20 ;\r\ny_correct = 818809200;\r\nassert(isequal(paths2dest_ongrid(m,n),y_correct))\r\n","published":true,"deleted":false,"likes_count":2,"comments_count":5,"created_by":11275,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":81,"test_suite_updated_at":"2020-09-28T20:02:06.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2013-05-01T14:58:23.000Z","updated_at":"2026-03-19T08:10:31.000Z","published_at":"2013-05-01T14:58:23.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eConsider a grid formed by n vertices vertically down, and m vertices horizontally right. Your starting point is at the top left vertex. Your destination is the bottom right vertex. You are permitted at each vertex to choose to move down or right, that is in the direction towards the destination. You are not to move on what constitutes a back step like moving left or up. If you hit the bottom boundary, or right boundary take it to be given there is only 1 way to the destination, that is following along the boundary.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eEx: in a 2x2 grid there are two ways. One way: First down, then right. The other way: First right, then down.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e4x3 has 10 ways\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e6x5 has 126 ways\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThis problem can be solved using dynamic programming but there are other methods too.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eProblem 7) Prev:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"http://www.mathworks.com/matlabcentral/cody/problems/1482\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e1482\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e Next:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"http://www.mathworks.com/matlabcentral/cody/problems/1484\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e1484\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\",\"relationship\":null}],\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"target\":\"/matlab/document.xml\",\"relationshipId\":\"rId1\"}]}"},{"id":44066,"title":"Number of paths on a 3d grid","description":"This problem is inspired by \u003chttps://www.mathworks.com/matlabcentral/cody/problems/1483-number-of-paths-on-a-grid\u003e, which you might want to solve first.\r\n\r\nConsider a 3d grid formed by n vertices vertically down, m vertices horizontally right, l vertices horizontally front. Your starting point is at the top left front vertex. Your destination is the bottom right back vertex. (From one corner to the furthest corner) You are permitted at each vertex to choose to move down,  right or back, that is in the direction towards the destination. You are not to move on what constitutes a back step like moving left, up, or front. If you hit the bottom boundary, right boundary, or back boundary, take it to be given that you move along the 2d boundary.\r\n\r\nEx: in a 2x2X2 grid there are 6 ways. (down, right, back), (d,b,r), (r,d,b), (r,b,d), (b,r,d), (b,d,r)\r\n\r\n4x3x2 has 60 ways\r\n\r\n6x5x4 has 27720 ways\r\n\r\nThis problem can be solved using dynamic programming but there are other methods too.\r\n\r\n","description_html":"\u003cp\u003eThis problem is inspired by \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/1483-number-of-paths-on-a-grid\"\u003ehttps://www.mathworks.com/matlabcentral/cody/problems/1483-number-of-paths-on-a-grid\u003c/a\u003e, which you might want to solve first.\u003c/p\u003e\u003cp\u003eConsider a 3d grid formed by n vertices vertically down, m vertices horizontally right, l vertices horizontally front. Your starting point is at the top left front vertex. Your destination is the bottom right back vertex. (From one corner to the furthest corner) You are permitted at each vertex to choose to move down,  right or back, that is in the direction towards the destination. You are not to move on what constitutes a back step like moving left, up, or front. If you hit the bottom boundary, right boundary, or back boundary, take it to be given that you move along the 2d boundary.\u003c/p\u003e\u003cp\u003eEx: in a 2x2X2 grid there are 6 ways. (down, right, back), (d,b,r), (r,d,b), (r,b,d), (b,r,d), (b,d,r)\u003c/p\u003e\u003cp\u003e4x3x2 has 60 ways\u003c/p\u003e\u003cp\u003e6x5x4 has 27720 ways\u003c/p\u003e\u003cp\u003eThis problem can be solved using dynamic programming but there are other methods too.\u003c/p\u003e","function_template":"function y = count3dPath(x)\r\n  y = x;\r\nend","test_suite":"%%\r\nm = 2; n = 2 ; l = 5;\r\ny_correct = 30;\r\nassert(isequal(count3dPath(m,n,l),y_correct))\r\n \t\t\r\n%%\r\nm = 8; n = 5 ; l = 2;\r\ny_correct = 3960;\r\nassert(isequal(count3dPath(m,n,l),y_correct))\r\n\t\r\n%%\r\nm = 5; n = 5 ; l = 10;\r\ny_correct = 1701700;\r\nassert(isequal(count3dPath(m,n,l),y_correct))\r\n\t\r\n%%\r\nm = 8; n = 4 ; l=2;\r\ny_correct = 1320;\r\nassert(isequal(count3dPath(m,n,l),y_correct))\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":1,"created_by":115733,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":53,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2017-02-14T00:01:08.000Z","updated_at":"2026-03-19T08:12:10.000Z","published_at":"2017-02-14T00:01:08.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThis problem is inspired by\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://www.mathworks.com/matlabcentral/cody/problems/1483-number-of-paths-on-a-grid\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e\u0026lt;https://www.mathworks.com/matlabcentral/cody/problems/1483-number-of-paths-on-a-grid\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e\u0026gt;, which you might want to solve first.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eConsider a 3d grid formed by n vertices vertically down, m vertices horizontally right, l vertices horizontally front. Your starting point is at the top left front vertex. Your destination is the bottom right back vertex. (From one corner to the furthest corner) You are permitted at each vertex to choose to move down, right or back, that is in the direction towards the destination. You are not to move on what constitutes a back step like moving left, up, or front. If you hit the bottom boundary, right boundary, or back boundary, take it to be given that you move along the 2d boundary.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eEx: in a 2x2X2 grid there are 6 ways. (down, right, back), (d,b,r), (r,d,b), (r,b,d), (b,r,d), (b,d,r)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e4x3x2 has 60 ways\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e6x5x4 has 27720 ways\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThis problem can be solved using dynamic programming but there are other methods too.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":44072,"title":"Number of paths on a n-dimensional grid","description":"This problem is inspired by \u003chttps://www.mathworks.com/matlabcentral/cody/problems/1483-number-of-paths-on-a-grid\u003e and  \u003chttps://www.mathworks.com/matlabcentral/cody/problems/44066-number-of-paths-on-a-3d-grid\u003e, which you might want to solve first.\r\n \r\nConsider n-dimensional grid, and you are moving from one corner to the farthest corner in a minimal number of moves. Each move corresponds to moving to a neighbouring hypercube (among possible up to 2*n neighbours). How many ways are there?\r\n\r\nInput format is a row array of size \"d\" (for d dimension) with number of grid points on each direction.\r\n\r\nOptional: can you solve it without loops?","description_html":"\u003cp\u003eThis problem is inspired by \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/1483-number-of-paths-on-a-grid\"\u003ehttps://www.mathworks.com/matlabcentral/cody/problems/1483-number-of-paths-on-a-grid\u003c/a\u003e and  \u003ca href = \"https://www.mathworks.com/matlabcentral/cody/problems/44066-number-of-paths-on-a-3d-grid\"\u003ehttps://www.mathworks.com/matlabcentral/cody/problems/44066-number-of-paths-on-a-3d-grid\u003c/a\u003e, which you might want to solve first.\u003c/p\u003e\u003cp\u003eConsider n-dimensional grid, and you are moving from one corner to the farthest corner in a minimal number of moves. Each move corresponds to moving to a neighbouring hypercube (among possible up to 2*n neighbours). How many ways are there?\u003c/p\u003e\u003cp\u003eInput format is a row array of size \"d\" (for d dimension) with number of grid points on each direction.\u003c/p\u003e\u003cp\u003eOptional: can you solve it without loops?\u003c/p\u003e","function_template":"function y = countNdPath(NdRowVector)\r\n  y = sum(NdRowVector);\r\nend","test_suite":"%%\r\nNdRowVector = [3,3,3,3,3];\r\ny_correct = 113400;\r\nassert(isequal(countNdPath(NdRowVector),y_correct))\r\n\r\n%%\r\nNdRowVector = [1,3,3,3,3,1,1,1,3,3];\r\ny_correct = 7484400;\r\nassert(isequal(countNdPath(NdRowVector),y_correct))\r\n\r\n%%\r\nNdRowVector = [2,2,2,2,2,2,2,1,1,2,2,2,2];\r\ny_correct = 39916800;\r\nassert(isequal(countNdPath(NdRowVector),y_correct))","published":true,"deleted":false,"likes_count":1,"comments_count":0,"created_by":115733,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":50,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2017-02-14T01:28:35.000Z","updated_at":"2025-12-16T03:16:34.000Z","published_at":"2017-02-14T01:28:35.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThis problem is inspired by\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://www.mathworks.com/matlabcentral/cody/problems/1483-number-of-paths-on-a-grid\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e\u0026lt;https://www.mathworks.com/matlabcentral/cody/problems/1483-number-of-paths-on-a-grid\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e\u0026gt; and \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://www.mathworks.com/matlabcentral/cody/problems/44066-number-of-paths-on-a-3d-grid\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e\u0026lt;https://www.mathworks.com/matlabcentral/cody/problems/44066-number-of-paths-on-a-3d-grid\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e\u0026gt;, which you might want to solve first.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eConsider n-dimensional grid, and you are moving from one corner to the farthest corner in a minimal number of moves. Each move corresponds to moving to a neighbouring hypercube (among possible up to 2*n neighbours). How many ways are there?\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eInput format is a row array of size \\\"d\\\" (for d dimension) with number of grid points on each direction.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eOptional: can you solve it without loops?\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":2176,"title":"Which way to go?","description":"\r\nGiven an m*n grid, \r\n\r\nHow many ways are there to go from upper left corner to the lower right one?\r\n\r\nYou can only move right and down, not up or left\r\n\r\n","description_html":"\u003cp\u003eGiven an m*n grid,\u003c/p\u003e\u003cp\u003eHow many ways are there to go from upper left corner to the lower right one?\u003c/p\u003e\u003cp\u003eYou can only move right and down, not up or left\u003c/p\u003e","function_template":"function y = wayz_in_maze(m,n)\r\n  y = x\r\nend","test_suite":"%%\r\nm=2\r\nn=2\r\ny_correct =6;\r\nassert(isequal(wayz_in_maze(m,n),y_correct))\r\n\r\n\r\n%%\r\nm=3\r\nn=4\r\ny_correct =35;\r\nassert(isequal(wayz_in_maze(m,n),y_correct))\r\n\r\n%%\r\nm=5\r\nn=6\r\ny_correct =462;\r\nassert(isequal(wayz_in_maze(m,n),y_correct))","published":true,"deleted":false,"likes_count":1,"comments_count":3,"created_by":17228,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":75,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2014-02-11T19:41:44.000Z","updated_at":"2025-12-16T03:17:50.000Z","published_at":"2014-02-11T19:41:44.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eGiven an m*n grid,\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eHow many ways are there to go from upper left corner to the lower right one?\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eYou can only move right and down, not up or left\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":2173,"title":"How many rectangles in a grid ?","description":"How many rectangles are there in an m × n grid ?\r\n\r\nFor example, if m=1 \u0026 n=2, we have 3 rectangles.\r\n\r\n","description_html":"\u003cp\u003eHow many rectangles are there in an m × n grid ?\u003c/p\u003e\u003cp\u003eFor example, if m=1 \u0026 n=2, we have 3 rectangles.\u003c/p\u003e","function_template":"function y = recingrid(m,n)\r\n  y = [m,n]\r\nend","test_suite":"%%\r\nm=1;\r\nn=1;\r\ny_correct = 1;\r\nassert(isequal(recingrid(m,n),y_correct))\r\n\r\n%%\r\nm=3;\r\nn=4;\r\ny_correct = 60;\r\nassert(isequal(recingrid(m,n),y_correct))\r\n\r\n%%\r\nm=7;\r\nn=3;\r\ny_correct = 168;\r\nassert(isequal(recingrid(m,n),y_correct))\r\n\r\n%%\r\nm=5;\r\nn=5;\r\ny_correct = 225;\r\nassert(isequal(recingrid(m,n),y_correct))","published":true,"deleted":false,"likes_count":3,"comments_count":1,"created_by":17228,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":78,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2014-02-11T12:15:07.000Z","updated_at":"2025-12-16T03:18:25.000Z","published_at":"2014-02-11T12:15:07.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eHow many rectangles are there in an m × n grid ?\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eFor example, if m=1 \u0026amp; n=2, we have 3 rectangles.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":2405,"title":"The Number of Ways","description":"Find all the possible ways to reach the end of a matrix. So given\r\n\r\n  [a b\r\n   c d] \r\n\r\nthe possible ways [a c],[a d],[b c],[b d] as inputs *p* the number of rows and *n* the number of column. You will produce a matrix by using these two input inputs. The matrix starts with 1 and increases left to right. The last number in the matrix will be n*p. After that you find the possible ways to reach end of the matrix \r\n\r\nExample 1\r\n\r\n  p = 2; the number of rows \r\n  n = 2; the number of columns\r\n\r\n  will produce this output matrix \r\n\r\n  [1 2 \r\n   3 4]\r\n\r\nPossible ways are [1 3],[1 4],[2 3],[2 4]\r\n  \r\n output =\r\n\r\n     1     3\r\n     1     4\r\n     2     3\r\n     2     4\r\n\r\nExample 2 \r\n\r\n p = 4;\r\n n = 2;\r\n\r\nThese produce the matrix\r\n\r\n [1 2\r\n  3 4\r\n  5 6\r\n  7 8]\r\n\r\nPossible ways are [1 3 5 7], [1 3 5 8],...,[2 4 6 8]\r\n\r\n output =\r\n     1     3     5     7\r\n     1     3     5     8\r\n     1     3     6     7\r\n     1     3     6     8\r\n     1     4     5     7\r\n     1     4     5     8\r\n     1     4     6     7\r\n     1     4     6     8\r\n     2     3     5     7\r\n     2     3     5     8\r\n     2     3     6     7\r\n     2     3     6     8\r\n     2     4     5     7\r\n     2     4     5     8\r\n     2     4     6     7\r\n     2     4     6     8\r\n","description_html":"\u003cp\u003eFind all the possible ways to reach the end of a matrix. So given\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003e[a b\r\n c d] \r\n\u003c/pre\u003e\u003cp\u003ethe possible ways [a c],[a d],[b c],[b d] as inputs \u003cb\u003ep\u003c/b\u003e the number of rows and \u003cb\u003en\u003c/b\u003e the number of column. You will produce a matrix by using these two input inputs. The matrix starts with 1 and increases left to right. The last number in the matrix will be n*p. After that you find the possible ways to reach end of the matrix\u003c/p\u003e\u003cp\u003eExample 1\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003ep = 2; the number of rows \r\nn = 2; the number of columns\r\n\u003c/pre\u003e\u003cpre class=\"language-matlab\"\u003ewill produce this output matrix \r\n\u003c/pre\u003e\u003cpre class=\"language-matlab\"\u003e[1 2 \r\n 3 4]\r\n\u003c/pre\u003e\u003cp\u003ePossible ways are [1 3],[1 4],[2 3],[2 4]\u003c/p\u003e\u003cpre\u003e output =\u003c/pre\u003e\u003cpre\u003e     1     3\r\n     1     4\r\n     2     3\r\n     2     4\u003c/pre\u003e\u003cp\u003eExample 2\u003c/p\u003e\u003cpre\u003e p = 4;\r\n n = 2;\u003c/pre\u003e\u003cp\u003eThese produce the matrix\u003c/p\u003e\u003cpre\u003e [1 2\r\n  3 4\r\n  5 6\r\n  7 8]\u003c/pre\u003e\u003cp\u003ePossible ways are [1 3 5 7], [1 3 5 8],...,[2 4 6 8]\u003c/p\u003e\u003cpre\u003e output =\r\n     1     3     5     7\r\n     1     3     5     8\r\n     1     3     6     7\r\n     1     3     6     8\r\n     1     4     5     7\r\n     1     4     5     8\r\n     1     4     6     7\r\n     1     4     6     8\r\n     2     3     5     7\r\n     2     3     5     8\r\n     2     3     6     7\r\n     2     3     6     8\r\n     2     4     5     7\r\n     2     4     5     8\r\n     2     4     6     7\r\n     2     4     6     8\u003c/pre\u003e","function_template":"function y = Ways(n,p)\r\n  \r\nend","test_suite":"%%\r\nn=2;\r\np=2;\r\n y_correct=[    1     3\r\n     1     4\r\n     2     3\r\n     2     4]\r\n\r\nassert(isequal(Ways(n,p),y_correct))\r\n%%\r\n\r\nn=3;\r\np=5;\r\n\r\n\r\n    y_correct=[ 1     4     7    10    13\r\n     1     4     7    10    14\r\n     1     4     7    10    15\r\n     1     4     7    11    13\r\n     1     4     7    11    14\r\n     1     4     7    11    15\r\n     1     4     7    12    13\r\n     1     4     7    12    14\r\n     1     4     7    12    15\r\n     1     4     8    10    13\r\n     1     4     8    10    14\r\n     1     4     8    10    15\r\n     1     4     8    11    13\r\n     1     4     8    11    14\r\n     1     4     8    11    15\r\n     1     4     8    12    13\r\n     1     4     8    12    14\r\n     1     4     8    12    15\r\n     1     4     9    10    13\r\n     1     4     9    10    14\r\n     1     4     9    10    15\r\n     1     4     9    11    13\r\n     1     4     9    11    14\r\n     1     4     9    11    15\r\n     1     4     9    12    13\r\n     1     4     9    12    14\r\n     1     4     9    12    15\r\n     1     5     7    10    13\r\n     1     5     7    10    14\r\n     1     5     7    10    15\r\n     1     5     7    11    13\r\n     1     5     7    11    14\r\n     1     5     7    11    15\r\n     1     5     7    12    13\r\n     1     5     7    12    14\r\n     1     5     7    12    15\r\n     1     5     8    10    13\r\n     1     5     8    10    14\r\n     1     5     8    10    15\r\n     1     5     8    11    13\r\n     1     5     8    11    14\r\n     1     5     8    11    15\r\n     1     5     8    12    13\r\n     1     5     8    12    14\r\n     1     5     8    12    15\r\n     1     5     9    10    13\r\n     1     5     9    10    14\r\n     1     5     9    10    15\r\n     1     5     9    11    13\r\n     1     5     9    11    14\r\n     1     5     9    11    15\r\n     1     5     9    12    13\r\n     1     5     9    12    14\r\n     1     5     9    12    15\r\n     1     6     7    10    13\r\n     1     6     7    10    14\r\n     1     6     7    10    15\r\n     1     6     7    11    13\r\n     1     6     7    11    14\r\n     1     6     7    11    15\r\n     1     6     7    12    13\r\n     1     6     7    12    14\r\n     1     6     7    12    15\r\n     1     6     8    10    13\r\n     1     6     8    10    14\r\n     1     6     8    10    15\r\n     1     6     8    11    13\r\n     1     6     8    11    14\r\n     1     6     8    11    15\r\n     1     6     8    12    13\r\n     1     6     8    12    14\r\n     1     6     8    12    15\r\n     1     6     9    10    13\r\n     1     6     9    10    14\r\n     1     6     9    10    15\r\n     1     6     9    11    13\r\n     1     6     9    11    14\r\n     1     6     9    11    15\r\n     1     6     9    12    13\r\n     1     6     9    12    14\r\n     1     6     9    12    15\r\n     2     4     7    10    13\r\n     2     4     7    10    14\r\n     2     4     7    10    15\r\n     2     4     7    11    13\r\n     2     4     7    11    14\r\n     2     4     7    11    15\r\n     2     4     7    12    13\r\n     2     4     7    12    14\r\n     2     4     7    12    15\r\n     2     4     8    10    13\r\n     2     4     8    10    14\r\n     2     4     8    10    15\r\n     2     4     8    11    13\r\n     2     4     8    11    14\r\n     2     4     8    11    15\r\n     2     4     8    12    13\r\n     2     4     8    12    14\r\n     2     4     8    12    15\r\n     2     4     9    10    13\r\n     2     4     9    10    14\r\n     2     4     9    10    15\r\n     2     4     9    11    13\r\n     2     4     9    11    14\r\n     2     4     9    11    15\r\n     2     4     9    12    13\r\n     2     4     9    12    14\r\n     2     4     9    12    15\r\n     2     5     7    10    13\r\n     2     5     7    10    14\r\n     2     5     7    10    15\r\n     2     5     7    11    13\r\n     2     5     7    11    14\r\n     2     5     7    11    15\r\n     2     5     7    12    13\r\n     2     5     7    12    14\r\n     2     5     7    12    15\r\n     2     5     8    10    13\r\n     2     5     8    10    14\r\n     2     5     8    10    15\r\n     2     5     8    11    13\r\n     2     5     8    11    14\r\n     2     5     8    11    15\r\n     2     5     8    12    13\r\n     2     5     8    12    14\r\n     2     5     8    12    15\r\n     2     5     9    10    13\r\n     2     5     9    10    14\r\n     2     5     9    10    15\r\n     2     5     9    11    13\r\n     2     5     9    11    14\r\n     2     5     9    11    15\r\n     2     5     9    12    13\r\n     2     5     9    12    14\r\n     2     5     9    12    15\r\n     2     6     7    10    13\r\n     2     6     7    10    14\r\n     2     6     7    10    15\r\n     2     6     7    11    13\r\n     2     6     7    11    14\r\n     2     6     7    11    15\r\n     2     6     7    12    13\r\n     2     6     7    12    14\r\n     2     6     7    12    15\r\n     2     6     8    10    13\r\n     2     6     8    10    14\r\n     2     6     8    10    15\r\n     2     6     8    11    13\r\n     2     6     8    11    14\r\n     2     6     8    11    15\r\n     2     6     8    12    13\r\n     2     6     8    12    14\r\n     2     6     8    12    15\r\n     2     6     9    10    13\r\n     2     6     9    10    14\r\n     2     6     9    10    15\r\n     2     6     9    11    13\r\n     2     6     9    11    14\r\n     2     6     9    11    15\r\n     2     6     9    12    13\r\n     2     6     9    12    14\r\n     2     6     9    12    15\r\n     3     4     7    10    13\r\n     3     4     7    10    14\r\n     3     4     7    10    15\r\n     3     4     7    11    13\r\n     3     4     7    11    14\r\n     3     4     7    11    15\r\n     3     4     7    12    13\r\n     3     4     7    12    14\r\n     3     4     7    12    15\r\n     3     4     8    10    13\r\n     3     4     8    10    14\r\n     3     4     8    10    15\r\n     3     4     8    11    13\r\n     3     4     8    11    14\r\n     3     4     8    11    15\r\n     3     4     8    12    13\r\n     3     4     8    12    14\r\n     3     4     8    12    15\r\n     3     4     9    10    13\r\n     3     4     9    10    14\r\n     3     4     9    10    15\r\n     3     4     9    11    13\r\n     3     4     9    11    14\r\n     3     4     9    11    15\r\n     3     4     9    12    13\r\n     3     4     9    12    14\r\n     3     4     9    12    15\r\n     3     5     7    10    13\r\n     3     5     7    10    14\r\n     3     5     7    10    15\r\n     3     5     7    11    13\r\n     3     5     7    11    14\r\n     3     5     7    11    15\r\n     3     5     7    12    13\r\n     3     5     7    12    14\r\n     3     5     7    12    15\r\n     3     5     8    10    13\r\n     3     5     8    10    14\r\n     3     5     8    10    15\r\n     3     5     8    11    13\r\n     3     5     8    11    14\r\n     3     5     8    11    15\r\n     3     5     8    12    13\r\n     3     5     8    12    14\r\n     3     5     8    12    15\r\n     3     5     9    10    13\r\n     3     5     9    10    14\r\n     3     5     9    10    15\r\n     3     5     9    11    13\r\n     3     5     9    11    14\r\n     3     5     9    11    15\r\n     3     5     9    12    13\r\n     3     5     9    12    14\r\n     3     5     9    12    15\r\n     3     6     7    10    13\r\n     3     6     7    10    14\r\n     3     6     7    10    15\r\n     3     6     7    11    13\r\n     3     6     7    11    14\r\n     3     6     7    11    15\r\n     3     6     7    12    13\r\n     3     6     7    12    14\r\n     3     6     7    12    15\r\n     3     6     8    10    13\r\n     3     6     8    10    14\r\n     3     6     8    10    15\r\n     3     6     8    11    13\r\n     3     6     8    11    14\r\n     3     6     8    11    15\r\n     3     6     8    12    13\r\n     3     6     8    12    14\r\n     3     6     8    12    15\r\n     3     6     9    10    13\r\n     3     6     9    10    14\r\n     3     6     9    10    15\r\n     3     6     9    11    13\r\n     3     6     9    11    14\r\n     3     6     9    11    15\r\n     3     6     9    12    13\r\n     3     6     9    12    14\r\n     3     6     9    12    15]\r\nassert(isequal(Ways(n,p),y_correct))\r\n%%\r\n\r\nn=3;\r\np=3;\r\ny_correct =[1     4     7\r\n     1     4     8\r\n     1     4     9\r\n     1     5     7\r\n     1     5     8\r\n     1     5     9\r\n     1     6     7\r\n     1     6     8\r\n     1     6     9\r\n     2     4     7\r\n     2     4     8\r\n     2     4     9\r\n     2     5     7\r\n     2     5     8\r\n     2     5     9\r\n     2     6     7\r\n     2     6     8\r\n     2     6     9\r\n     3     4     7\r\n     3     4     8\r\n     3     4     9\r\n     3     5     7\r\n     3     5     8\r\n     3     5     9\r\n     3     6     7\r\n     3     6     8\r\n     3     6     9]\r\nassert(isequal(Ways(n,p),y_correct))\r\n%%\r\np=1;\r\nn=2;\r\nG=[1;2]\r\nassert(isequal(Ways(n,p),G))\r\n%%\r\np=4 \r\nn=2\r\nG=[1     3     5     7\r\n     1     3     5     8\r\n     1     3     6     7\r\n     1     3     6     8\r\n     1     4     5     7\r\n     1     4     5     8\r\n     1     4     6     7\r\n     1     4     6     8\r\n     2     3     5     7\r\n     2     3     5     8\r\n     2     3     6     7\r\n     2     3     6     8\r\n     2     4     5     7\r\n     2     4     5     8\r\n     2     4     6     7\r\n     2     4     6     8]\r\nassert(isequal(Ways(n,p),G))\r\n%%\r\n\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":0,"created_by":22216,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":41,"test_suite_updated_at":"2014-07-07T18:31:30.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2014-07-07T18:07:58.000Z","updated_at":"2025-12-16T03:19:49.000Z","published_at":"2014-07-07T18:31:30.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eFind all the possible ways to reach the end of a matrix. So given\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[[a b\\n c d]]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ethe possible ways [a c],[a d],[b c],[b d] as inputs\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ep\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e the number of rows and\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003en\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e the number of column. You will produce a matrix by using these two input inputs. The matrix starts with 1 and increases left to right. The last number in the matrix will be n*p. After that you find the possible ways to reach end of the matrix\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eExample 1\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[p = 2; the number of rows \\nn = 2; the number of columns\\n\\nwill produce this output matrix \\n\\n[1 2 \\n 3 4]]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ePossible ways are [1 3],[1 4],[2 3],[2 4]\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ output =\\n\\n     1     3\\n     1     4\\n     2     3\\n     2     4]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eExample 2\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ p = 4;\\n n = 2;]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThese produce the matrix\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ [1 2\\n  3 4\\n  5 6\\n  7 8]]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ePossible ways are [1 3 5 7], [1 3 5 8],...,[2 4 6 8]\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ output =\\n     1     3     5     7\\n     1     3     5     8\\n     1     3     6     7\\n     1     3     6     8\\n     1     4     5     7\\n     1     4     5     8\\n     1     4     6     7\\n     1     4     6     8\\n     2     3     5     7\\n     2     3     5     8\\n     2     3     6     7\\n     2     3     6     8\\n     2     4     5     7\\n     2     4     5     8\\n     2     4     6     7\\n     2     4     6     8]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":2406,"title":"possible ways through matrix","description":"This problem is inspired from problem 2405 \u003chttps://www.mathworks.com/matlabcentral/cody/problems/2405-the-number-of-ways\u003e.\r\nCheck that problem description first for better understanding. Input p is the number of rows and n the number of columns of matrix A. Matrix A is filled with numbers from 1 to n*p in a row-wise fashion. Example:\r\nn=2; p=2\r\nA = [1 2\r\n     3 4];\r\nThe objective is to move through the matrix starting from the top row (at any position) and end at bottom row (at any position). The difference with problem 2405 is that here the movement is limited. You can not jump from, say, 3rd column of 1st row to 1st column of 2nd row. If you are at j-th column of i-th row, the only valid next movements are (i+1,j-1), (i+1,j) and (i+1,j+1). That means, you can only move vertically or diagonally (in both direction). No wrap around the edges is assumed (will be in next problem). Just like the previous problem, return a matrix where each row is a possible move.\r\nFor the example given above, the possible moves are (1,3), (1,4), (2,3) and (2,4). Thus the output matrix is\r\n[1 3\r\n 1 4\r\n 2 3\r\n 2 4]","description_html":"\u003cdiv style = \"text-align: start; line-height: 20.4333px; min-height: 0px; white-space: normal; color: rgb(0, 0, 0); font-family: Menlo, Monaco, Consolas, monospace; font-style: normal; font-size: 14px; font-weight: 400; text-decoration: rgb(0, 0, 0); white-space: normal; \"\u003e\u003cdiv style=\"block-size: 401.033px; display: block; min-width: 0px; padding-block-start: 0px; padding-top: 0px; perspective-origin: 407px 200.517px; transform-origin: 407px 200.517px; vertical-align: baseline; \"\u003e\u003cdiv style=\"block-size: 42px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 21px; text-align: left; transform-origin: 384px 21px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 136.5px 8px; transform-origin: 136.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eThis problem is inspired from problem 2405\u003c/span\u003e\u003c/span\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 2px 8px; transform-origin: 2px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e \u003c/span\u003e\u003c/span\u003e\u003ca target='_blank' href = \"https://www.mathworks.com/matlabcentral/cody/problems/2405-the-number-of-ways\"\u003e\u003cspan style=\"\"\u003e\u003cspan style=\"\"\u003e\u0026lt;https://www.mathworks.com/matlabcentral/cody/problems/2405-the-number-of-ways\u003c/span\u003e\u003c/span\u003e\u003c/a\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 6px 8px; transform-origin: 6px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003e\u0026gt;.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 42px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 21px; text-align: left; transform-origin: 384px 21px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 381px 8px; transform-origin: 381px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eCheck that problem description first for better understanding. Input p is the number of rows and n the number of columns of matrix A. Matrix A is filled with numbers from 1 to n*p in a row-wise fashion. Example:\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgb(247, 247, 247); block-size: 61.3px; border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; border-end-end-radius: 4px; border-end-start-radius: 4px; border-start-end-radius: 4px; border-start-start-radius: 4px; border-top-left-radius: 4px; border-top-right-radius: 4px; margin-block-end: 10px; margin-block-start: 10px; margin-bottom: 10px; margin-inline-end: 3px; margin-inline-start: 3px; margin-left: 3px; margin-right: 3px; margin-top: 10px; perspective-origin: 404px 30.65px; transform-origin: 404px 30.65px; margin-left: 3px; margin-top: 10px; margin-bottom: 10px; margin-right: 3px; \"\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4333px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2167px; transform-origin: 404px 10.2167px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 32px 8.5px; tab-size: 4; transform-origin: 32px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003en=2; p=2\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4333px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2167px; transform-origin: 404px 10.2167px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 32px 8.5px; tab-size: 4; transform-origin: 32px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003eA = [1 2\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4333px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2167px; transform-origin: 404px 10.2167px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 40px 8.5px; tab-size: 4; transform-origin: 40px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e     3 4];\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 105px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 10px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 10px; perspective-origin: 384px 52.5px; text-align: left; transform-origin: 384px 52.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 10px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 365.5px 8px; transform-origin: 365.5px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eThe objective is to move through the matrix starting from the top row (at any position) and end at bottom row (at any position). The difference with problem 2405 is that here the movement is limited. You can not jump from, say, 3rd column of 1st row to 1st column of 2nd row. If you are at j-th column of i-th row, the only valid next movements are (i+1,j-1), (i+1,j) and (i+1,j+1). That means, you can only move vertically or diagonally (in both direction). No wrap around the edges is assumed (will be in next problem). Just like the previous problem, return a matrix where each row is a possible move.\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"block-size: 21px; font-family: Helvetica, Arial, sans-serif; line-height: 21px; margin-block-end: 9px; margin-block-start: 2px; margin-bottom: 9px; margin-inline-end: 10px; margin-inline-start: 4px; margin-left: 4px; margin-right: 10px; margin-top: 2px; perspective-origin: 384px 10.5px; text-align: left; transform-origin: 384px 10.5px; white-space: pre-wrap; margin-left: 4px; margin-top: 2px; margin-bottom: 9px; margin-right: 10px; \"\u003e\u003cspan style=\"block-size: auto; display: inline; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; perspective-origin: 336px 8px; transform-origin: 336px 8px; unicode-bidi: normal; \"\u003e\u003cspan style=\"\"\u003eFor the example given above, the possible moves are (1,3), (1,4), (2,3) and (2,4). Thus the output matrix is\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgb(247, 247, 247); block-size: 81.7333px; border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; border-end-end-radius: 4px; border-end-start-radius: 4px; border-start-end-radius: 4px; border-start-start-radius: 4px; border-top-left-radius: 4px; border-top-right-radius: 4px; margin-block-end: 10px; margin-block-start: 10px; margin-bottom: 10px; margin-inline-end: 3px; margin-inline-start: 3px; margin-left: 3px; margin-right: 3px; margin-top: 10px; perspective-origin: 404px 40.8667px; transform-origin: 404px 40.8667px; margin-left: 3px; margin-top: 10px; margin-bottom: 10px; margin-right: 3px; \"\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4333px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2167px; transform-origin: 404px 10.2167px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 16px 8.5px; tab-size: 4; transform-origin: 16px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e[1 3\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4333px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2167px; transform-origin: 404px 10.2167px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 16px 8.5px; tab-size: 4; transform-origin: 16px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e 1 4\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4333px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2167px; transform-origin: 404px 10.2167px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 16px 8.5px; tab-size: 4; transform-origin: 16px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e 2 3\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003cdiv style=\"background-color: rgba(0, 0, 0, 0); block-size: 20.4333px; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-end-end-radius: 0px; border-end-start-radius: 0px; border-inline-end-color: rgb(233, 233, 233); border-inline-end-style: solid; border-inline-end-width: 1px; border-inline-start-color: rgb(233, 233, 233); border-inline-start-style: solid; border-inline-start-width: 1px; border-left-color: rgb(233, 233, 233); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(233, 233, 233); border-right-style: solid; border-right-width: 1px; border-start-end-radius: 0px; border-start-start-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; margin-block-end: 0px; margin-block-start: 0px; margin-bottom: 0px; margin-inline-end: 0px; margin-inline-start: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; min-block-size: 18px; min-height: 18px; padding-inline-start: 4px; padding-left: 4px; perspective-origin: 404px 10.2167px; transform-origin: 404px 10.2167px; white-space: nowrap; \"\u003e\u003cspan style=\"block-size: auto; border-inline-end-color: rgb(0, 0, 0); border-inline-end-style: none; border-inline-end-width: 0px; border-inline-start-color: rgb(0, 0, 0); border-inline-start-style: none; border-inline-start-width: 0px; border-left-color: rgb(0, 0, 0); border-left-style: none; border-left-width: 0px; border-right-color: rgb(0, 0, 0); border-right-style: none; border-right-width: 0px; display: inline; margin-inline-end: 45px; margin-right: 45px; min-block-size: 0px; min-height: 0px; padding-inline-start: 0px; padding-left: 0px; perspective-origin: 20px 8.5px; tab-size: 4; transform-origin: 20px 8.5px; unicode-bidi: normal; white-space: pre; margin-right: 45px; \"\u003e\u003cspan style=\"margin-inline-end: 0px; margin-right: 0px; \"\u003e 2 4]\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e","function_template":"function y = your_fcn_name(x)\r\n  y = x;\r\nend","test_suite":"%%\r\nn=2;\r\np=2;\r\ny_correct=[1   3\r\n     1     4\r\n     2     3\r\n     2     4];\r\nassert(isequal(your_fcn_name(n,p),y_correct))\r\n\r\n%%\r\nn=3;\r\np=2;\r\ny_correct=[1   4;\r\n    1   5\r\n    2   4\r\n    2   5\r\n    2   6\r\n    3   5\r\n    3   6];\r\nassert(isequal(your_fcn_name(n,p),y_correct))\r\n\r\n%%\r\nn=1;\r\np=2;\r\ny_correct=[1 2];\r\nassert(isequal(your_fcn_name(n,p),y_correct))\r\n\r\n%%\r\nn=2;\r\np=1;\r\ny_correct=[1;2];\r\nassert(isequal(your_fcn_name(n,p),y_correct))\r\n\r\n%%\r\nn=3;\r\np=5;\r\ny_correct=[1     4     7    10    13\r\n     1     4     7    10    14\r\n     1     4     7    11    13\r\n     1     4     7    11    14\r\n     1     4     7    11    15\r\n     1     4     8    10    13\r\n     1     4     8    10    14\r\n     1     4     8    11    13\r\n     1     4     8    11    14\r\n     1     4     8    11    15\r\n     1     4     8    12    14\r\n     1     4     8    12    15\r\n     1     5     7    10    13\r\n     1     5     7    10    14\r\n     1     5     7    11    13\r\n     1     5     7    11    14\r\n     1     5     7    11    15\r\n     1     5     8    10    13\r\n     1     5     8    10    14\r\n     1     5     8    11    13\r\n     1     5     8    11    14\r\n     1     5     8    11    15\r\n     1     5     8    12    14\r\n     1     5     8    12    15\r\n     1     5     9    11    13\r\n     1     5     9    11    14\r\n     1     5     9    11    15\r\n     1     5     9    12    14\r\n     1     5     9    12    15\r\n     2     4     7    10    13\r\n     2     4     7    10    14\r\n     2     4     7    11    13\r\n     2     4     7    11    14\r\n     2     4     7    11    15\r\n     2     4     8    10    13\r\n     2     4     8    10    14\r\n     2     4     8    11    13\r\n     2     4     8    11    14\r\n     2     4     8    11    15\r\n     2     4     8    12    14\r\n     2     4     8    12    15\r\n     2     5     7    10    13\r\n     2     5     7    10    14\r\n     2     5     7    11    13\r\n     2     5     7    11    14\r\n     2     5     7    11    15\r\n     2     5     8    10    13\r\n     2     5     8    10    14\r\n     2     5     8    11    13\r\n     2     5     8    11    14\r\n     2     5     8    11    15\r\n     2     5     8    12    14\r\n     2     5     8    12    15\r\n     2     5     9    11    13\r\n     2     5     9    11    14\r\n     2     5     9    11    15\r\n     2     5     9    12    14\r\n     2     5     9    12    15\r\n     2     6     8    10    13\r\n     2     6     8    10    14\r\n     2     6     8    11    13\r\n     2     6     8    11    14\r\n     2     6     8    11    15\r\n     2     6     8    12    14\r\n     2     6     8    12    15\r\n     2     6     9    11    13\r\n     2     6     9    11    14\r\n     2     6     9    11    15\r\n     2     6     9    12    14\r\n     2     6     9    12    15\r\n     3     5     7    10    13\r\n     3     5     7    10    14\r\n     3     5     7    11    13\r\n     3     5     7    11    14\r\n     3     5     7    11    15\r\n     3     5     8    10    13\r\n     3     5     8    10    14\r\n     3     5     8    11    13\r\n     3     5     8    11    14\r\n     3     5     8    11    15\r\n     3     5     8    12    14\r\n     3     5     8    12    15\r\n     3     5     9    11    13\r\n     3     5     9    11    14\r\n     3     5     9    11    15\r\n     3     5     9    12    14\r\n     3     5     9    12    15\r\n     3     6     8    10    13\r\n     3     6     8    10    14\r\n     3     6     8    11    13\r\n     3     6     8    11    14\r\n     3     6     8    11    15\r\n     3     6     8    12    14\r\n     3     6     8    12    15\r\n     3     6     9    11    13\r\n     3     6     9    11    14\r\n     3     6     9    11    15\r\n     3     6     9    12    14\r\n     3     6     9    12    15];\r\nassert(isequal(your_fcn_name(n,p),y_correct))\r\n\r\n%%\r\nn=3;\r\np=3;\r\ny_correct =[1     4     7\r\n     1     4     8\r\n     1     5     7\r\n     1     5     8\r\n     1     5     9\r\n     2     4     7\r\n     2     4     8\r\n     2     5     7\r\n     2     5     8\r\n     2     5     9\r\n     2     6     8\r\n     2     6     9\r\n     3     5     7\r\n     3     5     8\r\n     3     5     9\r\n     3     6     8\r\n     3     6     9];\r\nassert(isequal(your_fcn_name(n,p),y_correct))","published":true,"deleted":false,"likes_count":0,"comments_count":2,"created_by":17203,"edited_by":223089,"edited_at":"2022-10-06T05:21:24.000Z","deleted_by":null,"deleted_at":null,"solvers_count":36,"test_suite_updated_at":"2022-10-06T05:21:24.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2014-07-08T15:01:54.000Z","updated_at":"2025-12-16T03:21:05.000Z","published_at":"2014-07-08T15:18:54.000Z","restored_at":null,"restored_by":null,"spam":null,"simulink":false,"admin_reviewed":false,"description_opc":"{\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThis problem is inspired from problem 2405\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://www.mathworks.com/matlabcentral/cody/problems/2405-the-number-of-ways\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e\u0026lt;https://www.mathworks.com/matlabcentral/cody/problems/2405-the-number-of-ways\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e\u0026gt;.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eCheck that problem description first for better understanding. Input p is the number of rows and n the number of columns of matrix A. Matrix A is filled with numbers from 1 to n*p in a row-wise fashion. Example:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[n=2; p=2\\nA = [1 2\\n     3 4];]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe objective is to move through the matrix starting from the top row (at any position) and end at bottom row (at any position). The difference with problem 2405 is that here the movement is limited. You can not jump from, say, 3rd column of 1st row to 1st column of 2nd row. If you are at j-th column of i-th row, the only valid next movements are (i+1,j-1), (i+1,j) and (i+1,j+1). That means, you can only move vertically or diagonally (in both direction). No wrap around the edges is assumed (will be in next problem). Just like the previous problem, return a matrix where each row is a possible move.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eFor the example given above, the possible moves are (1,3), (1,4), (2,3) and (2,4). Thus the output matrix is\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[[1 3\\n 1 4\\n 2 3\\n 2 4]]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\",\"relationship\":null}],\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"target\":\"/matlab/document.xml\",\"relationshipId\":\"rId1\"}]}"},{"id":2617,"title":"Yet Another Path Finder","description":"Assume there is a rectangular grid of points. These points are indicated by linear indices in a MATLAB-fashion. Some of the grid points are connected by vertical or horizontal lines. Your task is to find a path through the points which are not connected or touched by any line starting from the top left to the bottom right corner. One additional difficulty is that you can not move diagonally. That means the valid paths should only contain horizontal and vertical moves. There exists only one unique path. You cannot go through a particular path more than once.\r\n\r\nA matrix M of size N-by-2 will be given containing the grid information. Each row of M indicates two grid points which are connected by a line. Return a vector containing the linear indices of points in the grid that forms a valid path. The second (r) and third (c) input indicates the row and column size of the grid.\r\n\r\nExample:\r\n\r\n  M =\r\n   \r\n  [2 3\r\n   3 6\r\n   7 10\r\n   10 11]\r\n  r = 3\r\n  c = 4\r\n\r\nGrid points 2-3, 3-6, 7-10 and 10-11 are connected by lines. Thus the only path though which you can navigate is that formed by grid points 1,4,5,8,9 and 12. Thus return [1 4 5 8 9 12]\r\n\r\n\r\n","description_html":"\u003cp\u003eAssume there is a rectangular grid of points. These points are indicated by linear indices in a MATLAB-fashion. Some of the grid points are connected by vertical or horizontal lines. Your task is to find a path through the points which are not connected or touched by any line starting from the top left to the bottom right corner. One additional difficulty is that you can not move diagonally. That means the valid paths should only contain horizontal and vertical moves. There exists only one unique path. You cannot go through a particular path more than once.\u003c/p\u003e\u003cp\u003eA matrix M of size N-by-2 will be given containing the grid information. Each row of M indicates two grid points which are connected by a line. Return a vector containing the linear indices of points in the grid that forms a valid path. The second (r) and third (c) input indicates the row and column size of the grid.\u003c/p\u003e\u003cp\u003eExample:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003eM =\r\n\u003c/pre\u003e\u003cpre class=\"language-matlab\"\u003e[2 3\r\n 3 6\r\n 7 10\r\n 10 11]\r\nr = 3\r\nc = 4\r\n\u003c/pre\u003e\u003cp\u003eGrid points 2-3, 3-6, 7-10 and 10-11 are connected by lines. Thus the only path though which you can navigate is that formed by grid points 1,4,5,8,9 and 12. Thus return [1 4 5 8 9 12]\u003c/p\u003e","function_template":"function y = pathFinder(x)\r\n\r\nend","test_suite":"%%\r\nM = [2 3\r\n     3 6\r\n     7 10\r\n     10 11];\r\nr = 3;\r\nc = 4;\r\ny_correct = [1 4 5 8 9 12];\r\nassert(isequal(pathFinder(M,r,c),y_correct))\r\n\r\n\r\n\r\n%%\r\nM = [4 5];\r\nr = 3;\r\nc = 3;\r\ny_correct = [1 2 3 6 9];\r\nassert(isequal(pathFinder(M,r,c),y_correct))\r\n\r\n\r\n%%\r\nM = [2 5];\r\nr = 3;\r\nc = 3;\r\ny_correct = [1 4 7 8 9];\r\nassert(isequal(pathFinder(M,r,c),y_correct))\r\n\r\n\r\n%%\r\nM = [2 3\r\n     5 10\r\n     12 13\r\n     13 18\r\n     18 19];\r\nr = 5;\r\nc = 4;\r\ny_correct = [1 6 7 8 9 14 15 20];\r\nassert(isequal(pathFinder(M,r,c),y_correct))\r\n\r\n\r\n%%\r\nM = [ ];\r\nr = 1;\r\nc = 1000;\r\ny_correct = 1:1000;\r\nassert(isequal(pathFinder(M,r,c),y_correct))\r\n\r\n%%\r\nM = [7 8\r\n     9 10\r\n     10 11];\r\nr = 6;\r\nc = 2;\r\ny_correct = [1:6 12];\r\nassert(isequal(pathFinder(M,r,c),y_correct))\r\n\r\n%%\r\nM = [2 3\r\n     4 5\r\n     5 10\r\n     10 15\r\n     15 14\r\n     13 14\r\n     11 16\r\n     16 21\r\n     23 24];\r\nr = 5;\r\nc = 5;\r\ny_correct = [1 6 7 12 17 18 19 20 25];\r\nassert(isequal(pathFinder(M,r,c),y_correct))\r\n\r\n\r\n\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":3,"created_by":17203,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":32,"test_suite_updated_at":"2014-10-06T07:21:26.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2014-10-05T05:18:09.000Z","updated_at":"2025-11-24T15:30:37.000Z","published_at":"2014-10-05T05:18:09.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eAssume there is a rectangular grid of points. These points are indicated by linear indices in a MATLAB-fashion. Some of the grid points are connected by vertical or horizontal lines. Your task is to find a path through the points which are not connected or touched by any line starting from the top left to the bottom right corner. One additional difficulty is that you can not move diagonally. That means the valid paths should only contain horizontal and vertical moves. There exists only one unique path. You cannot go through a particular path more than once.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eA matrix M of size N-by-2 will be given containing the grid information. Each row of M indicates two grid points which are connected by a line. Return a vector containing the linear indices of points in the grid that forms a valid path. The second (r) and third (c) input indicates the row and column size of the grid.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eExample:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[M =\\n\\n[2 3\\n 3 6\\n 7 10\\n 10 11]\\nr = 3\\nc = 4]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eGrid points 2-3, 3-6, 7-10 and 10-11 are connected by lines. Thus the only path though which you can navigate is that formed by grid points 1,4,5,8,9 and 12. Thus return [1 4 5 8 9 12]\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":42797,"title":"Find the path through the cell","description":"A list of cells is given.\r\n\r\nReturn true if the elements of the list increase monotonically (each element is strictly larger than the previous). Return false otherwise.\r\n\r\nFor example :\r\nIf list={1 2 3 4 5} =\u003e the answer is YES (1-\u003e2-\u003e3-\u003e4-\u003e5)\r\n\r\nIf list={[1 2] 4 [4 5] 6} =\u003e the answer is YES (1(or 2)-\u003e4-\u003e5-\u003e6) \r\n\r\nBut if list={[1 2] 0 [4 5] 6} =\u003e the answer is NO (1 or 2 are \u003e0)\r\n\r\nEasy ?\r\n\r\nAnd a list like ... {[10 1] [2 3] [4 40] [1 1 2 2 2 5] 6 40 [1:41] [1:42]} ?\r\n\r\n\r\nGood luck !\r\n","description_html":"\u003cp\u003eA list of cells is given.\u003c/p\u003e\u003cp\u003eReturn true if the elements of the list increase monotonically (each element is strictly larger than the previous). Return false otherwise.\u003c/p\u003e\u003cp\u003eFor example :\r\nIf list={1 2 3 4 5} =\u0026gt; the answer is YES (1-\u0026gt;2-\u0026gt;3-\u0026gt;4-\u0026gt;5)\u003c/p\u003e\u003cp\u003eIf list={[1 2] 4 [4 5] 6} =\u0026gt; the answer is YES (1(or 2)-\u0026gt;4-\u0026gt;5-\u0026gt;6)\u003c/p\u003e\u003cp\u003eBut if list={[1 2] 0 [4 5] 6} =\u0026gt; the answer is NO (1 or 2 are \u0026gt;0)\u003c/p\u003e\u003cp\u003eEasy ?\u003c/p\u003e\u003cp\u003eAnd a list like ... {[10 1] [2 3] [4 40] [1 1 2 2 2 5] 6 40 [1:41] [1:42]} ?\u003c/p\u003e\u003cp\u003eGood luck !\u003c/p\u003e","function_template":"function y = through_list(x)\r\n  y = x;\r\nend","test_suite":"%%\r\nx = {1 2 3 4 5};\r\ny_correct=1;\r\nassert(isequal(through_list(x),y_correct))\r\n%%\r\nx = {1 2 3 4 5 6 7 8 9 10 9};\r\nassert(isequal(through_list(x),0))\r\n%%\r\nx = {0 1 2 3 4 5 [5 5 5 6]};\r\nassert(isequal(through_list(x),1))\r\n%%\r\nx = {[1 2] 4 [4 5] 6};\r\nassert(isequal(through_list(x),1))\r\n%%\r\nassert(isequal(through_list({[1 2] 0 [4 5] 6} ),0))\r\n%%\r\nx = {[-2 -2 0 1] 2 3 4 5};\r\nassert(isequal(through_list(x),1))\r\n%%\r\n{[10 1] [2 3] [4 40] [1 1 2 2 2 5] 6 40 [1:41] [1:42]};\r\nassert(isequal(through_list(ans),1))\r\n%%\r\n{[10 1] [2 3] [4 40] [1 1 2 2 2 5] 6 40 [1:41] [1:12]};\r\nassert(isequal(through_list(ans),0))\r\n%%\r\n{[-10:1:10] [2 3] [4 40] [1 1 2 2 2 5] 6 40 [1:41] [1:12]};\r\nassert(isequal(through_list(ans),0))\r\n%%\r\nassert(isequal(through_list({[-10:1:10] [2 3] [4 40] [1 1 2 2 2 5 100 1000 -100] 6 [5 40 200 210] [1:41] [10:52]}),1))\r\n%%\r\nassert(isequal(through_list({[-10:1:10] [2 3] [4 40] [1 1 2 2 2 5 100 1000 -100] 6 [5 40 200 210] [1:41] [10:52] 53 [1001 54] [1 2 3 4 5 55]}),1))\r\n","published":true,"deleted":false,"likes_count":3,"comments_count":2,"created_by":5390,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":45,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2016-04-07T19:19:21.000Z","updated_at":"2025-12-14T23:23:23.000Z","published_at":"2016-04-07T19:39:46.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eA list of cells is given.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eReturn true if the elements of the list increase monotonically (each element is strictly larger than the previous). Return false otherwise.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eFor example : If list={1 2 3 4 5} =\u0026gt; the answer is YES (1-\u0026gt;2-\u0026gt;3-\u0026gt;4-\u0026gt;5)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eIf list={[1 2] 4 [4 5] 6} =\u0026gt; the answer is YES (1(or 2)-\u0026gt;4-\u0026gt;5-\u0026gt;6)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eBut if list={[1 2] 0 [4 5] 6} =\u0026gt; the answer is NO (1 or 2 are \u0026gt;0)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eEasy ?\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eAnd a list like ... {[10 1] [2 3] [4 40] [1 1 2 2 2 5] 6 40 [1:41] [1:42]} ?\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003cw:jc w:val=\\\"left\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eGood luck !\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":42938,"title":"Project Euler: Problem 18, Maximum path sum I","description":"By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23.\r\n\r\n       3*\r\n     7*  4\r\n   2   4*  6\r\n 8   5   9*  3\r\n \r\n3 + 7 + 4 + 9 = 23\r\n\r\nFind the maximum total from top to bottom of a given triangle.\r\n","description_html":"\u003cp\u003eBy starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23.\u003c/p\u003e\u003cpre\u003e       3*\r\n     7*  4\r\n   2   4*  6\r\n 8   5   9*  3\u003c/pre\u003e\u003cp\u003e3 + 7 + 4 + 9 = 23\u003c/p\u003e\u003cp\u003eFind the maximum total from top to bottom of a given triangle.\u003c/p\u003e","function_template":"function s = maxPathSum(tr)\r\n    % tr: lower triangular matrix\r\n  s = inf;\r\nend","test_suite":"%%\r\nx = [3 0 0 0\r\n  7 4 0 0\r\n  2 4 6 0\r\n  8 5 9 3];\r\nassert(isequal(maxPathSum(x),23))\r\n\r\n%%\r\nassert(isequal(maxPathSum(tril(magic(10))),891))\r\n\r\n%%\r\nassert(isequal(maxPathSum(tril(magic(100)^2)),251716502500))\r\n\r\n%%\r\nassert(isequal(maxPathSum(tril(mod(magic(1000),997))),741570))\r\n\r\n%%\r\nassert(isequal(maxPathSum(tril(gallery('integerdata',1e4,5e3,4))),37455089))","published":true,"deleted":false,"likes_count":4,"comments_count":2,"created_by":85274,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":144,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2016-08-30T10:52:35.000Z","updated_at":"2026-01-05T00:26:41.000Z","published_at":"2016-08-30T10:53:42.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eBy starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[       3*\\n     7*  4\\n   2   4*  6\\n 8   5   9*  3]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e3 + 7 + 4 + 9 = 23\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eFind the maximum total from top to bottom of a given triangle.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":1873,"title":"Break it up!  Break it up!","description":"You have N pennies.  Write a Matlab script that will reveal how many different ways you can break up those pennies.  For example, with 5 pennies, there are seven different ways you can divide them:\r\n\r\n* (1,1,1,1,1)\r\n* (2,1,1,1)\r\n* (2,2,1)\r\n* (3,2)\r\n* (3,1,1)\r\n* (4,1)\r\n* (5)\r\n\r\nThe order of the coins does not matter, so (2,2,1) is considered the same combination as (2,1,2).  You can assume that N will always be a positive integer.","description_html":"\u003cp\u003eYou have N pennies.  Write a Matlab script that will reveal how many different ways you can break up those pennies.  For example, with 5 pennies, there are seven different ways you can divide them:\u003c/p\u003e\u003cul\u003e\u003cli\u003e(1,1,1,1,1)\u003c/li\u003e\u003cli\u003e(2,1,1,1)\u003c/li\u003e\u003cli\u003e(2,2,1)\u003c/li\u003e\u003cli\u003e(3,2)\u003c/li\u003e\u003cli\u003e(3,1,1)\u003c/li\u003e\u003cli\u003e(4,1)\u003c/li\u003e\u003cli\u003e(5)\u003c/li\u003e\u003c/ul\u003e\u003cp\u003eThe order of the coins does not matter, so (2,2,1) is considered the same combination as (2,1,2).  You can assume that N will always be a positive integer.\u003c/p\u003e","function_template":"function y = partitions(x)\r\n  y = x;\r\nend","test_suite":"%%\r\nx = 1;y_correct = 1;assert(isequal(partitions(x),y_correct))\r\n%%\r\nx = 5;y_correct = 7;assert(isequal(partitions(x),y_correct))\r\n%%\r\nx = 50;y_correct = 204226;assert(isequal(partitions(x),y_correct))\r\n%%\r\nassert(isequal(partitions(partitions(partitions(partitions(5)))),476715857290));\r\n%%\r\nassert(isequal(partitions(partitions(partitions(6))),526823));\r\n%%\r\nassert(isequal(partitions(partitions(12)),10619863));\r\n%%\r\nassert(isequal(partitions(199),3646072432125))\r\n%%\r\nP=arrayfun(@(x) partitions(x),10:10:100);\r\ny_correct=[42 627 5604 37338 204226 966467 4087968 15796476 56634173 190569292];\r\nassert(all(isequal(P,y_correct)))","published":true,"deleted":false,"likes_count":4,"comments_count":4,"created_by":1615,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":81,"test_suite_updated_at":"2016-08-19T11:36:33.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2013-09-13T18:34:19.000Z","updated_at":"2026-04-03T20:00:35.000Z","published_at":"2013-09-13T18:34:19.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eYou have N pennies. Write a Matlab script that will reveal how many different ways you can break up those pennies. For example, with 5 pennies, there are seven different ways you can divide them:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e(1,1,1,1,1)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e(2,1,1,1)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e(2,2,1)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e(3,2)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e(3,1,1)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e(4,1)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e(5)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe order of the coins does not matter, so (2,2,1) is considered the same combination as (2,1,2). You can assume that N will always be a positive integer.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":1482,"title":"Number of cyles and fixed points in a permutation","description":"A permutation can be constructed from one or more sets of elements in cyclic order. \r\nA permutation in a single row vector form indicates where elements indexed sequentially will be repositioned to. Ex. [ 2 4 1 3] means the first element goes to position 2, 2nd to 4, 3rd to 1 and 4rth to 3 and this happens to be one cycle 1 to 2 to 4 to 3 back to 1. \r\n\r\nGiven a permutation, determine the number of cycles. \r\nConsider fixed points as cycles of length 1.\r\n\r\n\u003chttp://en.wikipedia.org/wiki/Cyclic_permutation\u003e\r\n\r\n\u003chttp://en.wikipedia.org/wiki/Cycles_and_fixed_points\u003e \r\n\r\nEx. \r\n[2 3 1 5 6 4] has two cycles [2 3 1][5 6 4]\r\n\r\nProblem 6) Prev: \u003chttp://www.mathworks.com/matlabcentral/cody/problems/1481 1481\u003e \r\nNext: \u003chttp://www.mathworks.com/matlabcentral/cody/problems/1483 1483\u003e\r\n","description_html":"\u003cp\u003eA permutation can be constructed from one or more sets of elements in cyclic order. \r\nA permutation in a single row vector form indicates where elements indexed sequentially will be repositioned to. Ex. [ 2 4 1 3] means the first element goes to position 2, 2nd to 4, 3rd to 1 and 4rth to 3 and this happens to be one cycle 1 to 2 to 4 to 3 back to 1.\u003c/p\u003e\u003cp\u003eGiven a permutation, determine the number of cycles. \r\nConsider fixed points as cycles of length 1.\u003c/p\u003e\u003cp\u003e\u003ca href = \"http://en.wikipedia.org/wiki/Cyclic_permutation\"\u003ehttp://en.wikipedia.org/wiki/Cyclic_permutation\u003c/a\u003e\u003c/p\u003e\u003cp\u003e\u003ca href = \"http://en.wikipedia.org/wiki/Cycles_and_fixed_points\"\u003ehttp://en.wikipedia.org/wiki/Cycles_and_fixed_points\u003c/a\u003e\u003c/p\u003e\u003cp\u003eEx. \r\n[2 3 1 5 6 4] has two cycles [2 3 1][5 6 4]\u003c/p\u003e\u003cp\u003eProblem 6) Prev: \u003ca href = \"http://www.mathworks.com/matlabcentral/cody/problems/1481\"\u003e1481\u003c/a\u003e \r\nNext: \u003ca href = \"http://www.mathworks.com/matlabcentral/cody/problems/1483\"\u003e1483\u003c/a\u003e\u003c/p\u003e","function_template":"function n = number_of_cycles(perm)\r\n  n=1;\r\nend","test_suite":"%%\r\nx = [3 4 5 7 6 1 8 2];\r\ny_correct = 2;\r\nassert(isequal(number_of_cycles(x),y_correct))\r\n\r\n%%\r\nx = [2 3 4 1 5 6];\r\ny_correct = 3;\r\nassert(isequal(number_of_cycles(x),y_correct))\r\n\r\n%%\r\nx = [1];\r\ny_correct = 1;\r\nassert(isequal(number_of_cycles(x),y_correct))\r\n\r\n%%\r\nx = [1 2];\r\ny_correct = 2;\r\nassert(isequal(number_of_cycles(x),y_correct))\r\n\r\n%%\r\nx = [2 1];\r\ny_correct = 1;\r\nassert(isequal(number_of_cycles(x),y_correct))\r\n\r\n%%\r\nx = [4 5 7 6 8 2 1 3];\r\ny_correct = 1;\r\nassert(isequal(number_of_cycles(x),y_correct))\r\n\r\n%%\r\nx = [4 2 7 6 5 8 1 3];\r\ny_correct = 3;\r\nassert(isequal(number_of_cycles(x),y_correct))\r\n\r\n%%\r\nx = [3 4 1 2 6 5];\r\ny_correct = 3;\r\nassert(isequal(number_of_cycles(x),y_correct))\r\n\r\n%%\r\nx = [3 4 1 2 6 5 8 9 7];\r\ny_correct = 4;\r\nassert(isequal(number_of_cycles(x),y_correct))\r\n\r\n%%\r\nx = [3 4 1 2 6 5 8 9 7 10];\r\ny_correct = 5;\r\nassert(isequal(number_of_cycles(x),y_correct))\r\n\r\n%%\r\nx = [3 4 2 5 1 8 9 7 10 6];\r\ny_correct = 2;\r\nassert(isequal(number_of_cycles(x),y_correct))\r\n\r\n%%\r\nx = [3 4 2 5 1 8 9 7 10 6 11];\r\ny_correct = 3;\r\nassert(isequal(number_of_cycles(x),y_correct))\r\n","published":true,"deleted":false,"likes_count":3,"comments_count":0,"created_by":11275,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":38,"test_suite_updated_at":"2013-05-01T11:59:08.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2013-05-01T01:00:49.000Z","updated_at":"2025-11-23T23:33:12.000Z","published_at":"2013-05-01T01:32:36.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eA permutation can be constructed from one or more sets of elements in cyclic order. A permutation in a single row vector form indicates where elements indexed sequentially will be repositioned to. Ex. [ 2 4 1 3] means the first element goes to position 2, 2nd to 4, 3rd to 1 and 4rth to 3 and this happens to be one cycle 1 to 2 to 4 to 3 back to 1.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eGiven a permutation, determine the number of cycles. Consider fixed points as cycles of length 1.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:hyperlink w:docLocation=\\\"http://en.wikipedia.org/wiki/Cyclic_permutation\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ehttp://en.wikipedia.org/wiki/Cyclic_permutation\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:hyperlink w:docLocation=\\\"http://en.wikipedia.org/wiki/Cycles_and_fixed_points\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ehttp://en.wikipedia.org/wiki/Cycles_and_fixed_points\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eEx. [2 3 1 5 6 4] has two cycles [2 3 1][5 6 4]\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eProblem 6) Prev:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"http://www.mathworks.com/matlabcentral/cody/problems/1481\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e1481\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e Next:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"http://www.mathworks.com/matlabcentral/cody/problems/1483\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e1483\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":1488,"title":"Generate binary combinations for a given number of bit(s)","description":"Generate the binary combination as in the example below.\r\n \r\nExample: If you are given: \r\n\r\n bin_comb(2)\r\n\r\nThe answer will be:  \r\n\r\n 0 0\r\n 0 1\r\n 1 0\r\n 1 1\r\n\r\nThe answer will appear in double class.\r\n","description_html":"\u003cp\u003eGenerate the binary combination as in the example below.\u003c/p\u003e\u003cp\u003eExample: If you are given:\u003c/p\u003e\u003cpre\u003e bin_comb(2)\u003c/pre\u003e\u003cp\u003eThe answer will be:\u003c/p\u003e\u003cpre\u003e 0 0\r\n 0 1\r\n 1 0\r\n 1 1\u003c/pre\u003e\u003cp\u003eThe answer will appear in double class.\u003c/p\u003e","function_template":"function b = bin_comb(bits)\r\n  b= expression(bits);   \r\n  % write an expression to generate binary combination for a    \r\n  % given no. of bits\r\nend","test_suite":"%%\r\nbits = 1;\r\ny_correct = [0; 1];\r\nassert(isequal(bin_comb(bits),y_correct))\r\n%%\r\nbits = 2;\r\ny_correct = [0 0;0 1; 1 0; 1 1]\r\nassert(isequal(bin_comb(bits),y_correct))\r\n%%\r\nbits = 3;\r\ny_correct = [0 0 0;0 0 1;0 1 0;0 1 1;1 0 0; 1 0 1; 1 1 0; 1 1 1]\r\nassert(isequal(bin_comb(bits),y_correct))","published":true,"deleted":false,"likes_count":2,"comments_count":5,"created_by":13514,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":95,"test_suite_updated_at":"2013-05-06T20:51:27.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2013-05-04T07:13:19.000Z","updated_at":"2025-11-23T23:24:36.000Z","published_at":"2013-05-04T07:13:19.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eGenerate the binary combination as in the example below.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eExample: If you are given:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ bin_comb(2)]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe answer will be:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ 0 0\\n 0 1\\n 1 0\\n 1 1]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe answer will appear in double class.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"}],"no_progress_badge":{"id":53,"name":"Unknown","symbol":"unknown","description":"Partially completed groups","description_html":null,"image_location":"/images/responsive/supporting/matlabcentral/cody/badges/problem_groups_unknown_2.png","bonus":null,"players_count":0,"active":false,"created_by":null,"updated_by":null,"deleted_by":null,"deleted_at":null,"restored_by":null,"restored_at":null,"created_at":"2018-01-10T23:20:29.000Z","updated_at":"2018-01-10T23:20:29.000Z","community_badge_id":null,"award_multiples":false}}