Maximum Set Bit Sum in Array without Considering Adjacent Elements in C++

Ayush Gupta
Updated on 15-Sep-2020 14:11:22

213 Views

In this problem, we are given an array arr[] of integers. Our task is to create a program to calculate the Maximum set bit sum in array without considering adjacent elements in C++.Problem description − Here, we have an array arr[]. We have to find the number of set bits for each number. Then, we will find the maximum set bit sum in adjacent elements of the array. i.e. maximum sum for a[i] + a[i+2] ….Let’s take an example to understand the problem, Inputarr[] = {1, 4, 6, 7}Output4ExplanationArray with the element’s in binary formarr[] = {01, 100, 101, 111} ... Read More

Find Element That Appears Once in Sorted Array in JavaScript

AmitDiwan
Updated on 15-Sep-2020 12:01:35

569 Views

Suppose, we have a sorted array of literals like this −const arr = [2, 2, 3, 3, 3, 5, 5, 6, 7, 8, 9];We are required to write a JavaScript function that takes in one such array and returns the first number that appears only once in the array. If there is no such number in the array, we should return false.For this array, the output should be 6ExampleFollowing is the code −const arr = [2, 2, 3, 3, 3, 5, 5, 6, 7, 8, 9]; const firstNonDuplicate = arr => {    let appeared = false;    for(let i ... Read More

Check Three Consecutive Numbers in JavaScript

AmitDiwan
Updated on 15-Sep-2020 11:59:47

504 Views

We are required to write a JavaScript function that takes in a Number, say n, and we are required to check whether there exist such three consecutive natural numbers (not decimal/floating point) whose sum equals to n.If there exist such numbers, our function should return them, otherwise it should return false. Following is the code −Exampleconst sum = 54; const threeConsecutiveSum = sum => {    if(sum < 6 || sum % 3 !== 0){       return false;    }    // three numbers will be of the form:    // x + x + 1 + x ... Read More

Sorting String in Reverse Order in JavaScript

AmitDiwan
Updated on 15-Sep-2020 11:58:17

368 Views

We are required to write a JavaScript function that takes in a lowercase string and sorts it in the reverse order i.e., b should come before a, c before b and so on.For example: If the input string is −const str = "hello";Then the output should be −const output = "ollhe";ExampleFollowing is the code −const string = 'hello'; const sorter = (a, b) => {    const legend = [-1, 0, 1];    return legend[+(a < b)]; } const reverseSort = str => {    const strArr = str.split("");    return strArr    .sort(sorter)    .join(""); }; console.log(reverseSort(string));OutputFollowing is the output in the console −ollhe

Corner Digit Number Difference in JavaScript

AmitDiwan
Updated on 15-Sep-2020 11:56:41

142 Views

We are required to write a JavaScript function that takes in a number, constructs a new number from the first and last digit of that number and returns the difference between the original number and the number thus formed.For example: If the input is 34567Then the corner digits number will be −37And the output will be −34530ExampleFollowing is the code −const num = 34567; const cornerDifference = num => {    let temp = Math.abs(num);    let corner = temp % 10;    if(temp < 100){       corner = temp;    }else{       while(temp >= 10){ ... Read More

Difference Between Consecutive Elements in JavaScript Array

AmitDiwan
Updated on 15-Sep-2020 11:54:44

166 Views

Suppose, we have an array of numbers like this −const arr = [3, 5, 5, 23, 3, 5, 6, 43, 23, 7];We are required to write a function that takes in one such array and constructs another array whose elements are the difference between consecutive elements of the input array.For this array, the output will be −const output = [-2, 0, -18, 20, -2, -1, -37, 20, 16];ExampleFollowing is the code −const arr = [3, 5, 5, 23, 3, 5, 6, 43, 23, 7]; const consecutiveDifference = arr => {    const res = [];    for(let i = 0; ... Read More

Diagonal Product of a Matrix in JavaScript

AmitDiwan
Updated on 15-Sep-2020 11:52:25

2K+ Views

Suppose, we have a 2-D array representing a square matrix like this −const arr = [    [1, 3, 4, 2],    [4, 5, 3, 5],    [5, 2, 6, 4],    [8, 2, 9, 3] ];We are required to write a function that takes in this array and returns the product of the element present at the principal Diagonal of the matrix.For this array the elements present at the principal diagonal are −1, 5, 6, 3Hence the output should be −90ExampleFollowing is the code −const arr = [    [1, 3, 4, 2],    [4, 5, 3, 5],   ... Read More

Twice Repetitive Word Count in a String using JavaScript

AmitDiwan
Updated on 15-Sep-2020 11:50:23

1K+ Views

We are required to write a JavaScript function that takes in a string that contains some words that are repeated twice, we need to count such words.For example −If the input string is −const str = "car bus jeep car jeep bus motorbike truck";Then the output should be −3ExampleFollowing is the code −const str = "car bus jeep car jeep bus motorbike truck"; const countRepetitive = str => {    const strArr = str.split(" ");    let count = 0;    for(let i = 0; i < strArr.length; i++){       if(i === strArr.lastIndexOf(strArr[i])){          continue; ... Read More

Change String Based on a Condition in JavaScript

AmitDiwan
Updated on 15-Sep-2020 11:47:31

782 Views

We are required to write a JavaScript function that takes in a string. The task of our function is to change the string according to the following condition −If the first letter in the string is a capital letter then we should change the full string to capital letters.Otherwise, we should change the full string to small letters.ExampleFollowing is the code −const str1 = "This is a normal string"; const str2 = "thisIsACamelCasedString"; const changeStringCase = str => {    let newStr = '';    const isUpperCase = str[0].charCodeAt(0) >= 65 && str[0].charCodeAt(0)

Check If Array Is Sorted Regardless of Sorting Order in JavaScript

AmitDiwan
Updated on 15-Sep-2020 11:45:15

519 Views

We are required to write a JavaScript function that takes in an array of literals and checks if the array is sorted or not (irrespective of the order of sorting.)Our function should return true if the array is sorted, false otherwise. Following is the code −Exampleconst arr = [1, 3, 56, 87, 99, 102, 144, 255, 456, 788, 999]; const isSorted = arr => {    const { length: l } = arr;    if(l 0 && arr[i-1] < 0;       const con2 = arr[i] < 0 && arr[i-1] > 0;       if(con1 || con2){          return false;       };    };    return true; }; console.log(isSorted(arr)); OutputFollowing is the output in the console −true

Advertisements