DEV Community

Anjali Gurjar
Anjali Gurjar

Posted on

Solution

// function User(name){
// this.name=name

// };

// User.prototype.showName=function(){
// console.log(this.name)
// };
// function Student(branch,name){
// this.branch=branch
// this.proto= new User(name)

// };

// const student1=new Student("it","Anjali")

// student1.showName()
// const arr=[8, 4, 2, 1]
// const pairs=[]
// for(i=0;i // for(j=i+1;j // if(arr[i]>arr[j]){
// pairs.push([arr[i],arr[j]])
// }

// }
// }

// console.log("Inversion Pairs:", pairs);
// console.log("Number of Inversions:", pairs.length);

// const deeplyNested = [1, [2, [3, [4, [5]]]]];
// console.log(deeplyNested.flat(Infinity)); // [1, 2, 3, 4, 5]

// const arr= [1, 2, 3, 4, 5], k = 2

// const y=arr.slice(3,5).reverse()// start an d end with index number
// const
// console.log(y)

// const findMajorityElement = (arr) => {
// const majorityCount = Math.floor(arr.length / 2);

// for (let num of arr) {
// const count = arr.filter(x => x === num).length;
// if (count > majorityCount) {
// return num;
// }
// }

// return null; // No majority element
// };

// const arr = [3, 3, 4, 2, 4, 4, 2, 4, 4];
// console.log(findMajorityElement(arr)); // Output: 4

// const arr = [3, 4, 5, 1, 2];

// const isSortedAndRotated = (arr) => {
// let count = 0;
// const n = arr.length;

// for (let i = 0; i < n; i++) {
// // Check if current element is greater than the next (mod n for circular array)
// if (arr[i] > arr[(i + 1) % n]) {
// count++;
// }
// // If more than one inversion, return false
// if (count > 1) {
// return false;
// }
// }

// return count === 1; // Exactly one rotation
// };

// console.log(isSortedAndRotated(arr)); // Output: true

// non neagtive arr
// const arr = [1, 2, 3, 3, -3, -4, 7, -8];

// const removeNegatives = (arr) => {
// return arr.filter(num => num >= 0);
// };

// console.log(removeNegatives(arr)); // Output: [1, 2, 3, 3, 7]
sum of two number

var twoSum = function(nums, target) {
let sum=[]
for(let i=0;i<nums.length;i++){
for(let j=i+1;j<nums.length;j++){
if(nums[i]+nums[j]===target){
return [nums[i],nums[j]]

    }}
}
Enter fullscreen mode Exit fullscreen mode

}

    console.log(twoSum([1,23,4,5,52,7,2],9))
Enter fullscreen mode Exit fullscreen mode

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (2)

Collapse
 
anjali_gurjar_dff81513867 profile image
Anjali Gurjar

function brackFormat(s){
const stack=[]
const string={
"}": "{",
"]": "[",
")": "("
}

 for(let char of s ){
     if (char === "{" || char === "[" || char === "(") {
            stack.push(char)
     }
     else if (char==="}"|| char==="]"|| char===")"){
         if(stack.pop()!==string[char]){
             return false

         }

      }


 }
     return stack.length === 0; 
Enter fullscreen mode Exit fullscreen mode

}

console.log(brackFormat("{[()]}"))

Collapse
 
anjali_gurjar_dff81513867 profile image
Anjali Gurjar

const s ={"{[()]}" solve this question

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay