DAY 5. DSA challange for 45 days
March 20,2024
Leetcode and geeksforgeeks questions and solutions
begin with simple and straightforward problems to test my concepts.
problem 1 : https://leetcode.com/problems/rotate-array/
solution:
var rotate = function(nums, k) {k=k%nums.lengthswap(nums,0,nums.length-1)swap(nums,0,k-1)swap(nums,k,nums.length-1)return nums};function swap(arr,start,end){while(start<end){[arr[start],arr[end]]=[arr[end],arr[start]]start++end--}}
solution:
class Solution
{
//Function to sort the array using bubble sort algorithm.
bubbleSort(arr,n){
//code here
for(let i=0;i<n;i++){
for(let j=0;j<n;j++){
if(arr[j]>arr[j+1]){
[arr[j],arr[j+1]]=[arr[j+1],arr[j]]
}
}
}
return arr
}
}

Comments
Post a Comment