Found 33676 Articles for Programming

C++ code to find who cannot give sufficient candies

Arnab Chakraborty
Updated on 29-Mar-2022 11:17:25

163 Views

Suppose we have two numbers a and b. There are a and b number of candies in Amal's and Bimal's hand. Amal offered 1 candy to Bimal and Bimal gave two candies to Amal, in the next turn Amal gave 3 candies and Bimal gave 4 and so on. This continued until the moment when one of them couldn’t give the right amount of candy. They don’t consider the candies they have got from the opponent, as their own. We have to find, who is the first can’t give the right amount of candy.So, if the input is like a ... Read More

Java Program to Sort a String

AmitDiwan
Updated on 29-Mar-2022 11:21:25

3K+ Views

In this article, we will understand how to sort a string. String is a datatype that contains one or more characters and is enclosed in double quotes(“ ”). Strings are a sequence of charactersBelow is a demonstration of the same −Suppose our input is −Input string: javaprogramThe desired output would be −String after sorting is: [a, a, a, g, j, m, o, p, r, r, v]AlgorithmStep 1 - START Step 2 - Declare a string value namely input_string, a character array charArray, char value name temp and an int value namely string_size. Step 3 - Define the values. Step 4 ... Read More

C++ code to find minimum number starting from n in a game

Arnab Chakraborty
Updated on 29-Mar-2022 11:12:58

234 Views

Suppose we have a number n. In a game initially the value of n is v and the player is able to do the following operation zero or more times: Select a positive integer x that x < n and x is not a divisor of n, then subtract x from n. The goal of the player is to minimize the value of n in the end.So, if the input is like n = 8, then the output will be 1, because the player can select x = 3 in the first turn, then n becomes 5. We can then ... Read More

C++ code to find money after buying and selling shares

Arnab Chakraborty
Updated on 29-Mar-2022 11:11:01

328 Views

Suppose we have two arrays A of size n, and B of size m, and another number r. There are n opportunities to buy shares. The i-th of them allows to buy as many shares as we want, ith share price is A[i]. And also there are m opportunities to sell shares. The i-th of them allows to sell as many shares as we want, sell price of ith share is B[i]. We can't sell more shares than we have. If we have r amount of money and no existing share, we have to find the maximum number of money ... Read More

Java program to reverse a string using stacks

Aishwarya Naglot
Updated on 05-Jun-2025 11:53:14

2K+ Views

Stack is a linear data structure where we can store elements. It uses the LIFO (last in, first out) principle, which means the item we add last will be the first one to be removed. In this article, we will understand how to reverse a string using stacks. Let's take an example: Input: String input_string = "Java Program"; Output: String reversed_string = "margorP avaJ"; Ways to Reverse a String Using Stacks Below are the different approaches to reverse a string using stacks: Reverse a string using built-in stack methods ... Read More

C++ code to find array from given array with conditions

Arnab Chakraborty
Updated on 29-Mar-2022 11:07:41

146 Views

Suppose we have an array A with n elements. There is another hidden array B of size n. The elements can be negative or positive. For each index i in range 1 to n, following operations will be performed −Set A[i] as 0 initiallyThen add B[i] to A[i], subtract B[i+1], then add B[i+2] and so onWe have to find the array B.So, if the input is like A = [6, -4, 8, -2, 3], then the output will be [2, 4, 6, 1, 3]StepsTo solve this, we will follow these steps −for initialize i := 0, when i < size ... Read More

Java Program to Reverse a String

Shriansh Kumar
Updated on 25-Sep-2024 18:03:35

2K+ Views

Given a String, let's say "str", write a Java program to reverse it. In reverse string operation, we need to display the individual characters of string backwards or from right to left. Here, String is a datatype that contains one or more characters and is enclosed in double quotes(“ ”). Let's understand the problem with an example − Example Scenario: − Input: str = "Java Program"; Output: reversed_string = margorP avaJ For better understanding refer to the below illustration − Reverse String .main { background-color: rgb(151, 231, 89); ... Read More

Java program to replace a character at a specific index

Aishwarya Naglot
Updated on 30-May-2025 18:47:21

4K+ Views

In this article, we'll learn how to replace a character at a specific index in a string. Strings in Java are sequences of characters enclosed in double-quotes (" "). We use strings to represent text in our programs. Problem Statement You are given a string, an index, and a character. Your task is to replace the character at the specified index in the string with the new character. Input string: Java Programming, Index: 6 Output: Java P%ogramming Different Approaches There are multiple ways to achieve this in Java. Below are two common approaches: Using substring() ... Read More

Java Program To Determine If a Given Matrix is a Sparse Matrix

Aishwarya Naglot
Updated on 05-Jun-2025 14:42:35

438 Views

What is a Sparse Matrix? A matrix is said to be a sparse matrix if most of the elements of that matrix are 0. It means that the number of non-zero elements has a lesser count than the number of zero elements.Let's say we have a matrix of size 3*3, which means it has 9 elements. If 5 or more of those elements are 0, then we can say that the matrix is sparse. A = [ 0 2 0 ] [ 0 5 4 ] [ 0 0 0 ] ... Read More

Java Program to display upper triangular matrix

Shriansh Kumar
Updated on 30-Sep-2024 16:07:07

1K+ Views

In this article, we will understand how to display an upper triangular matrix of a given matrix in Java. A matrix has row and column arrangement of its elements. A matrix with m rows and n columns can be called as m × n matrix. And, the term upper triangular matrix refers to a matrix whose all elements below the main diagonal are 0. Example Scenario: Input: matrix = { 2, 1, 4 }, { 1, 2, 3 }, { 3, 6, 2 } Output: upptri_mat = 2 1 4 0 2 3 0 0 2 Using ... Read More

Advertisements