"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > CodeWars Challenge Decryption: Least Larger, from Basic to Advanced

CodeWars Challenge Decryption: Least Larger, from Basic to Advanced

Posted on 2025-05-03
Browse:578

Sharing Solusi Tantangan CodeWars: Least Larger, dari Dasar hingga aris

Learn from Codewars challenges: Least Larger

Hello friends! This time I want to share about Least Larger challenges from Codewars. This challenge is very exciting because of training logic and systematic ways of thinking, while providing an idea of ​​how this concept can be useful in the real world. Come on, we discuss together!


What is the challenge?

So, we are given a number array and a particular index. Our task is to search for the smallest number that is bigger of the elements in the index, then return the index . If there are no numbers that meet, we must return -1.

Example:

Array: [4, 1, 3, 5, 6]

Index: 0 (reference number is 4)

The result is 3, because the smallest number that is larger than 4 is 5, which is in index 3.
Why can 3?
The array starts from 0
So :
4: Index 0
1: Index 1
3: Index 2
5: Index 3
6: Index 4


The solution I made

To complete this challenge, I made a function called Leastlarger. The following is the code:

function leastLarger(a, i) {
  let smaller = Infinity; // Nilai pembanding awal
  let result = -1;        // Default hasil kalau nggak ada elemen yang memenuhi

  for (let index = 0; index  a[i] && a[index] 





How does it work?

  1. initial initialization

    • I set the smaller to Infinity as the initial value for comparison.
    • Result was set in 1st in case there was no element that meets.
    • I check every element in the array.
    • There are two conditions that must be met:
      1. The element is greater than the reference element (A [i]).
      2. The element is smaller than the previous comparison (smaller).
    • If the two conditions are met, I update the smaller and save the element index in the result.
  2. returns the results

      If there are elements that meet, the function of returning the index.
    • If there isn't, the results are still -1.

Examples of use

Console.log (Leastlarger ([4, 1, 3, 5, 6], 0)); // Output: 3
console.log(leastLarger([4, 1, 3, 5, 6], 0)); // Output: 3

Explanation:

    The reference element is 4 (in index 0).
  • The elements larger than 4 are [5, 6].
  • From the elements, the smallest is 5, and its position is in index 3.

Advance Solution: concise in 1 line

For those of you who like the approach to the more "really javascript," we can write this solution in one line of code using the default method such as filter, math.min, and findindex:


const leastlarger = (a, i) => a.findindex (x => x> a [i] && x === math.min (... a.filter (y => y> a [i]));
console.log(leastLarger([4, 1, 3, 5, 6], 0)); // Output: 3
This code does the same thing, but in a more functional and concise style. Suitable for situations where you want to write a quick solution without too many additional variables.


Case Study in the Real World

Functions like this are actually quite relevant in various real situations. Some examples:

  1. Ticket ordering system

      In the ticket system, we often need to find the lowest price of seats that are higher than a certain price (for example, to fill the next seat).
  2. task scheduling

      When creating a schedule, we can search for the next closest time that is greater than a certain time, for example to allocate a slot meeting or the next task.
  3. Inventory management

      In the warehouse, if you need to find a storage location with the smallest capacity that is still enough to store certain items.

This challenge looks simple, but when I try, I realize that neat logic is very important. What I like about this challenge is how relevant this concept is to be applied in the real world.

Oh yes, if friends have other ways to complete this challenge, don't hesitate to share in the comments column, yes! Who knows, we can learn from each other from different approaches. Hopefully this sharing is useful, and happy coding! ?

Release Statement This article is reproduced at: https://dev.to/rantidhanty/sharing-solusi-tantangan-codewars-least-larger-dari-dasar-hingga-1-baris-4daf?1 If there is any infringement, please contact [email protected] to delete it.
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3