{"id":6112,"date":"2021-03-01T06:00:50","date_gmt":"2021-03-01T04:00:50","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=6112"},"modified":"2021-03-08T08:58:53","modified_gmt":"2021-03-08T06:58:53","slug":"indice-del-menor-elemento-a-eliminar-para-que-la-suma-sea-divisible-por-k","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/indice-del-menor-elemento-a-eliminar-para-que-la-suma-sea-divisible-por-k\/","title":{"rendered":"\u00cdndice del menor elemento a eliminar para que la suma sea divisible por K"},"content":{"rendered":"<p>Definir la funci\u00f3n<\/p>\n<pre lang=\"text\">\n   indice :: [Int] -> Int -> Maybe Int\n<\/pre>\n<p>tal que (indice xs k) es el \u00edndice del menor elemento a eliminar de la lista de enteros positivos xs para que la suma de los restantes sea divisible por k o Nothing, si no existe dicho elemento. Por ejemplo,<\/p>\n<pre lang=\"text\">\n   indice [1,8,4,1] 2         ==  Just 2\n   indice [4,6,7,5,7] 11      ==  Just 2\n   indice [4,6,7,5,7] 12      ==  Just 3\n   indice [4,6,7,5,7] 13      ==  Nothing\n   indice [1..10^7] 7         ==  Just 5\n   indice [10^7,10^7-1..1] 7  ==  Just 9999994\n<\/pre>\n<h4>Soluciones<\/h4>\n<pre lang=\"haskell\">\nimport Test.QuickCheck\nimport Data.List (sort)\nimport Data.Maybe (listToMaybe)\n\n-- 1\u00aa soluci\u00f3n\n-- ===========\n\nindice :: [Int] -> Int -> Maybe Int\nindice xs k\n  | null ys   = Nothing\n  | otherwise = Just (head ys)\n  where ys = [i | (_,i,zs) <- sort (eliminaciones xs),\n                  sum zs `mod` k == 0]\n\n-- (eliminaciones xs) es la lista de ternas (x,i,zs) tales que x es un\n-- elemento de xs, i es la posici\u00f3n de x en xs y zs es la lista de los\n-- restantes elementos de xs. Por ejemplo,\n--    \u03bb> eliminaciones [5,7,6,5]\n--    [(5,0,[7,6,5]),(7,1,[5,6,5]),(6,2,[5,7,5]),(5,3,[5,7,6])]\neliminaciones :: [a] -> [(a,Int,[a])]\neliminaciones xs = [(z,i,zs) | ((z,zs),i) <- zip (aux xs) [0..]]\n  where aux []       = []\n        aux [x]      = [(x,[])]\n        aux (x:y:zs) = (x,y:zs) : [(v,x:vs) | (v,vs) <- aux (y:zs)]\n\n-- 2\u00aa soluci\u00f3n\n-- ===========\n\nindice2 :: [Int] -> Int -> Maybe Int\nindice2 xs k\n  | null ys   = Nothing\n  | otherwise = Just (head ys)\n  where d = sum xs `mod` k\n        ys = [i | (x,i) <- sort (zip xs [0..]),\n                  x `mod` k == d]\n\n-- 3\u00aa soluci\u00f3n\n-- ===========\n\nindice3 :: [Int] -> Int -> Maybe Int\nindice3 xs k = listToMaybe ys\n  where d = sum xs `mod` k\n        ys = [i | (x,i) <- sort (zip xs [0..]),\n                  x `mod` k == d]\n\n-- Comprobaci\u00f3n de la equivalencia\n-- ===============================\n\n-- La propiedad es\nprop_equivalencia :: [Int] -> Int -> Bool\nprop_equivalencia xs k =\n  indice  xs' k' == indice2 xs' k' &&\n  indice2 xs' k' == indice3 xs' k'\n  where xs' = map ((+1) . abs) xs\n        k'  = 1 + abs k\n\n-- La comprobaci\u00f3n es\n--    \u03bb> quickCheck prop_equivalencia\n--    +++ OK, passed 100 tests.\n\n-- Comparaci\u00f3n de eficiencia\n-- =========================\n\n-- La comparaci\u00f3n es\n--    \u03bb> indice [1..5000] 7\n--    Just 2\n--    (2.82 secs, 2,458,555,104 bytes)\n--    \u03bb> indice2 [1..5000] 7\n--    Just 2\n--    (0.01 secs, 1,991,232 bytes)\n--    \u03bb> indice3 [1..5000] 7\n--    Just 2\n--    (0.01 secs, 1,991,072 bytes)\n<\/pre>\n<h4>Nuevas soluciones<\/h4>\n<ul>\n<li>En los comentarios se pueden escribir nuevas soluciones.\n<li>El c\u00f3digo se debe escribir entre una l\u00ednea con &#60;pre lang=&quot;haskell&quot;&#62; y otra con &#60;\/pre&#62;\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Definir la funci\u00f3n indice :: [Int] -> Int -> Maybe Int tal que (indice xs k) es el \u00edndice del menor elemento a eliminar de la lista de enteros positivos xs para que la suma de los restantes sea divisible por k o Nothing, si no existe dicho elemento. Por ejemplo, indice [1,8,4,1] 2 ==&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"footnotes":"","_jetpack_memberships_contains_paid_content":false},"categories":[4],"tags":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/6112"}],"collection":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/comments?post=6112"}],"version-history":[{"count":2,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/6112\/revisions"}],"predecessor-version":[{"id":6163,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/6112\/revisions\/6163"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=6112"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=6112"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=6112"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}