Header Ads Widget

Responsive Advertisement

01:Javasscript DSA question | Sum zero

 Javascript sum zero in js.


javascript sum zero

Today we have a problem called sum zero in js

Q:) We have to find out the first pair which has a sum zero

we have an input array :


 let arr = [-5, -4, -3, -2, 0, 2, 4, 6, 8]

solution :


function sol12(arr) {
    for (let i = 0; i < arr.length; i++) {
        for (let j = i+1; j < arr.length; j++) {
            if (arr[i] + arr[j] == 0) {
                // console.log([arr[i], arr[j]])
                return [arr[i], arr[j]]
            }
        }
    }
}


let result3 = sol12(arr);
console.log(result3);//[-4,4]

in the above solution, we look through step-by-step,

first, we create one loop to iterate all the elements in "arr" and then create a second to check the first pair whose sum is zero 
now first loop at zero index in this point second loop which is "j" is interate all the element in arr
and if arr of zero index plus arr of (i+1 ) index value is zero we got the first pair whose sum is zero 
and this loop will continue to iterate until it got its pair whose sum is zero.

Explaination above code in briefly:

 The function sol12 aims to find a pair of numbers within an array "arr" whose sum equals zero. It operates using two nested loops:

Outer Loop: This loop iterates over each element of the array arr. It starts with the first element at index 0 and proceeds to the last element.

Inner Loop: Nested within the outer loop, this loop starts from the next element after the current outer loop index and continues till the end of the array.

During each iteration of the outer loop, the inner loop checks for pairs of elements whose sum is zero. It does so by adding the current element arr[i] with subsequent elements arr[j] where j ranges from i+1 to the end of the array.

If the sum of arr[i] and arr[j] equals zero, it implies that the pair [arr[i], arr[j]] satisfies the condition, and the function returns this pair.

Let's say the array arr contains elements [-4, 3, 2, -2, 4, -3].

The function proceeds as follows:

It starts with the first element, -4.
The inner loop iterates through the subsequent elements.
It finds that adding -4 with 4 results in zero.
Hence, the function returns the pair [-4, 4].
This pair signifies that within the array, -4 and 4 form a pair whose sum is zero.
Finally, the pair [-4, 4] is printed to the console.

In essence, the function systematically explores all possible pairs within the array to identify the first pair whose sum equals zero and returns it.

Q:) What is bubble sort?

function sort(arr) {
    for (let i = arr.length - 1; i >= 0; i--) {//7
        for (let j = 0; j < arr.length; j++) {//0
            if (arr[j] > arr[j + 1]) { //6>2,6>9,9>3,9>8,9>4
                // console.log(arr[j],"arr[jjjjjj");
                [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]]
            }
        }
    }
    console.log(arr,"22222array22")
}
sort([6, 2, 9, 3, 8, 4, 5, 1])


This above code defines a function named sort that sorts an array in ascending order using the bubble sort algorithm.

Outer Loop: It iterates through each element of the array in reverse order, starting from the last element (index arr.length - 1) and ending at the first element (index 0).

Inner Loop: Within each iteration of the outer loop, another loop iterates through the array from the first element (index 0) to the second-to-last element.

Comparison and Swap: Within the inner loop, adjacent elements are compared. If the current element (arr[j]) is greater than the next element (arr[j + 1]), they are swapped.

Sorting Process: This process repeats for each element in the array until no more swaps are needed. Each pass through the array places the largest unsorted element in its correct position.

Printing Sorted Array: After completing all iterations, the sorted array is printed to the console.

Example:

For the input array [6, 2, 9, 3, 8, 4, 5, 1]:

The function sorts the array in ascending order.
After sorting, the response we got array becomes [1, 2, 3, 4, 5, 6, 8, 9].
The sorted array [1, 2, 3, 4, 5, 6, 8, 9] is printed to the console.

In summary, the code sorts an array using the bubble sort algorithm,
where elements are compared and swapped if they are in the wrong order,
gradually moving the largest elements toward the end of the array with each iteration.


Any query regarding this feel free to message us.

Post a Comment

0 Comments