How to convert integer to string in C++?

There are 2 approaches to convert integer variables to string. Both the approaches with a sample code are mentioned below. Approach-1 #include<iostream> #include<string> using namespace std; void main() { int n= 1; string s= to_string(n); cout << s; } Approach-2 #include<iostream> #include <sstream> #include <string> using namespace std; int main() { int n = 17; … Read more

Find maximum difference between two elements such that larger element appears after the smaller number in Java

Given array of integers, find Maximum difference between two elements such that larger element appears after the smaller number. public static int calculateMaxDifferenceBetweenTwoElements(int[] arr) { int minElementTillNow=arr[0]; int maxDifference=Integer.MIN_VALUE; for (int i = 0; i < arr.length; i++) { int difference=0; if(arr[i] >minElementTillNow) { difference=arr[i]-minElementTillNow; if(difference > maxDifference) { maxDifference=difference; } } else { minElementTillNow=arr[i]; … Read more

Stock Buy Sell to Maximize Profit in Java

Given an array of integers representing stock price on a single day, find max profit that can be earned by 1 transaction.So you need to find a pair (buyDay,sellDay) where buyDay < = sellDay and it should maximize the profit. For example: SOLUTION public static int calculateMaxProfit(int[] arr) { int lowestPriceTillThatDay = arr[0]; int maxProfit = Integer.MIN_VALUE; … Read more

Java Program to check if a vowel is present in the string?

We can use regular expression to check if the string contains vowels or not. public class StringContainsVowels { public static void main(String[] args) { System.out.println(stringContainsVowels(“Hello”)); // true System.out.println(stringContainsVowels(“TV”)); // false } public static boolean stringContainsVowels(String input) { return input.toLowerCase().matches(“.*[aeiou].*”); } }

Find the missing number in the array in python

You are given an array of positive numbers from 1 to n, such that all numbers from 1 to n are present except one number x. You have to find x. The input array is not sorted. Look at the below array and give it a try before checking the solution. Runtime Complexity: Linear, O(n)O(n) Memory Complexity: Constant, O(1)O(1)