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

Top comments (0)