Sum of subset differences in C++

sudhir sharma
Updated on 11-Mar-2026 22:50:50

402 Views

In this problem, we are given a set S of n number. Our task is to create a program to find the sum of subset difference which is the difference of last and first elements of subset.The formula is, sumSubsetDifference = Σ [last(s) - first(s)] s are subsets of the set S.Let’s take an example to understand the problem, Input −S = {1, 2, 9} n = 3Output −Explanation − All subset are −{1}, last(s) - first(s) = 0 {2}, last(s) - first(s) = 0 {9}, last(s) - first(s) = 0 {1, 2}, last(s) - first(s) = 1 {1, 9}, last(s) ... Read More

Sum of first N natural numbers which are divisible by 2 and 7 in C++

sudhir sharma
Updated on 11-Mar-2026 22:50:50

253 Views

In this problem, we are given a number N. Our task is to find the sum of first N natural numbers which are divisible by 2 and 7.So, here we will be given a number N, the program will find the sum of numbers between 1 to N that is divisible by 2 and 7.Let’s take an example to understand the problem, Input −N = 10Output −37Explanation −sum = 2 + 4 + 6 + 7 + 8 + 10 = 37So, the basic idea to solve the problem is to find all the numbers that are divisible by 2 ... Read More

Build Array Where You Can Find The Maximum Exactly K Comparisons in C++

Arnab Chakraborty
Updated on 11-Mar-2026 22:50:50

280 Views

Suppose we have three integers n, m, and k. If we have following algorithm to find the maximum element of an array of positive integers −max_val := -1 max_ind := -1 search_cost := 0 n := size of arr for initialize i := 0, when i < n, update (increase i by 1), do:    if max_val < arr[i], then:       max_val := arr[i]       max_ind := i       (increase search_cost by 1) return max_indWe have to make the array arr which has the following criteria: arr has exactly n integers. all elements arr[i] are in range 1 to m(including) (0

X of a Kind in a Deck of Cards in C++

Arnab Chakraborty
Updated on 11-Mar-2026 22:50:50

392 Views

Suppose we have a deck of cards, each card has an integer written on it. We have to check whether we can choose X >= 2 such that it is possible to split the entire deck into 1 or more groups of cards, where the following condition satisfies: Each group has exactly X number of cards. All of the cards in each group have the same number.So, if the input is like deck = [1, 2, 3, 4, 4, 3, 2, 1], then the output will be True, as possible partitions are [1, 1], [2, 2], [3, 3], [4, 4].To ... Read More

How to clear screen using Java?

AmitDiwan
Updated on 11-Mar-2026 22:50:50

3K+ Views

Following is the code to clear screen using Java −Examplepublic class Demo{    public static void main(String[] args){       System.out.print("\033[H\033[2J");       System.out.flush();    } }OutputThe screen would be clearedA class named Demo contains the main function. Here, the ANSI escape code is written, that clears the screen. The flush function resets the cursor to the top of the window screen.

Printing Integer between Strings in Java

AmitDiwan
Updated on 11-Mar-2026 22:50:50

1K+ Views

Following is the Java program to print integer between Strings −Examplepublic class Demo{    public static void main(String[] args){       System.out.println("The equals symbol is present between two integer values ");       System.out.println(45+5 + "=" +(56+11));       System.out.println(45+5 + " equals symbol " +(56+11));    } }OutputThe equals symbol is present between two integer values 50=67 50 equals symbol 67A class named Demo contains the main function that prints integer values between two strings.

Flattening arrays in JavaScript

AmitDiwan
Updated on 11-Mar-2026 22:50:50

403 Views

Following is the code for flattening arrays in JavaScript −Example Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result,    .sample {       font-size: 20px;       font-weight: 500;    } Flattening arrays in JavaScript [1,2,3,[4,5],[6,[7,8]]] After flat : CLICK HERE Click on the above button to flat the above array by depth 1    let resEle = document.querySelector(".result");    let sampleEle = document.querySelector(".sample");    let arr = [1,2,3,[4,5],[6,[7,8]]];    document.querySelector(".Btn").addEventListener("click", () => {       let store = arr.flat(1);       store.forEach((item,index) => {          resEle.innerHTML += index + ' : ' + item + '';       });    }); OutputOn clicking the ‘CLICK HERE’ button −

Printing Triangle Pattern in Java

AmitDiwan
Updated on 11-Mar-2026 22:50:50

2K+ Views

Following is the Java program to print Triangle pattern −Exampleimport java.util.*; public class Demo{    public static void main(String[] args){       Scanner my_scan = new Scanner(System.in);       System.out.println("Enter the number of rows which needs to be printed");       int my_row = my_scan.nextInt();       for (int i = 1; i = i; j--){             System.out.print(" ");          }          for (int j = 1; j

Largest Time for Given Digits in C++

Arnab Chakraborty
Updated on 11-Mar-2026 22:50:50

298 Views

Suppose we have an array of 4 digits, we have to find the largest 24-hour time that can be made. We know that the smallest 24-hour time is 00:00, and the largest time is 23:59. Starting from 00:00, a time is larger if more time has elapsed since midnight. We have to return the answer as a string of length 5. If there is no valid time to be returned, then return an empty string.So, if the input is like [1, 2, 3, 4], then the output will be "23:41"To solve this, we will follow these steps −Define a function ... Read More

Maximum sum such that no two elements are adjacent in C++

Ayush Gupta
Updated on 11-Mar-2026 22:50:50

332 Views

In this problem, we are given an array arr[]. Our task is to create a program to find the Maximum sum such that no two elements are adjacent in C++.Problem DescriptionWe need to find the maximum sum of the sequence from the array such that no 2 numbers from the sum sequence are adjacent in the array.Let’s take an example to understand the problem, Inputarr[] = {5, 1, 3, 7, 9, 2, 5}Output22ExplanationTaking sum sequence from index 0 with alternate elements : 5 + 3 + 9 + 5 = 22 Taking sum sequence from index 1 with alternate elements ... Read More

Advertisements 750x300