Showing posts with label C++. Show all posts
Showing posts with label C++. Show all posts
Floyds triangle pattern
Generate the Floyd triangle.
Example:
Input: 3
Output: 1
2 3
4 5 6
Approach
Java
import java.util.ArrayList;import java.util.List;public class FloydsTrianglePattern {public static void main(String[] args) {int n = 3;List<List<Integer>> floyed = floyedTriangle(n);for (int i = 0; i < floyed.size(); i++) {for (int j = 0; j < floyed.get(i).size(); j++)System.out.print(floyed.get(i).get(j) + " ");System.out.println();}}//function to generate the floyed trianglestatic List<List<Integer>> floyedTriangle(int n) {List<List<Integer>> floyed = new ArrayList<List<Integer>>();int num = 1;for (int i = 1; i <= n; i++) {List<Integer> x = new ArrayList<Integer>();for (int j = 1; j <= i; j++)x.add(num++);floyed.add(x);}return floyed;}}
C++
#include <bits/stdc++.h>using namespace std;//function to generate the floyed//trianglevector<vector<int>> floyedTriangle(int n){vector<vector<int>> floyed;int num=1;for(int i=1;i<=n;i++){vector<int> x;for(int j=1;j<=i;j++)x.push_back(num++);floyed.push_back(x);}return floyed;}int main(){int n=3;vector<vector<int>> floyed=floyedTriangle(n);for(int i=0;i<floyed.size();i++){for(int j=0;j<floyed[i].size();j++)cout<<floyed[i][j]<<" ";cout<<"\n";}return 0;}
Find maximum product subarray
Find the maximum product of subarray of the array
Example
Input: arr[]={2,3,-2,4} Output: 6
Approach:
Java
public class FindMaxProductOFSubarray {
public static void main(String[] args) {
int arr[] = { 2, 3, -2, 4 };
int n = arr.length;
int maximum = maxProduct(arr, n);
System.out.println("Maximum product is " + maximum);
}
// method to find the maximum
// product sub-array
private static int maxProduct(int[] arr, int n) {
// set varible max1 ,min1 and ans
int max1 = 1, min1 = 1, ans = Integer.MIN_VALUE;
// Iterate till end of array
for (int i = 0; i < arr.length; i++) {
// if current element is
// negative then change swap min1 and max1
if (arr[i] < 0) {
int f = min1;
min1 = max1;
max1 = f;
}
// update max1
max1 = Math.max(arr[i], arr[i] * max1);
// update min1
min1 = Math.min(arr[i], min1 * arr[i]);
// update the answer
ans = Math.max(ans, max1);
}
return ans;
}
}
C++
#include <bits/stdc++.h>
using namespace std;
//function to find the maximum
//product subarray
int maxProduct(int arr[],int n)
{
//set varible max1 ,min1 and ans
int max1=1,min1=1,ans=INT_MIN;
//iterate till the end of
//array
for(int i=0;i<n;i++)
{
//if current element is
//negative then change swap min1 and max1
if(arr[i]<0)
{
int f=min1;
min1=max1;
max1=f;
}
//update max1
max1=max(arr[i],arr[i]*max1);
//update min1
min1=min(arr[i],min1*arr[i]);
//update the answer
ans=max(ans,max1);
}
//return the final answer
return ans;
}
int main()
{
int arr[]={2,3,-2,4};
int n=sizeof(arr)/sizeof(arr[0]);
int maximum=maxProduct(arr,n);
cout<<"Maximum product is\n";
cout<<maximum<<"\n";
return 0;
}
//Time Complexity: O(n)
Reverse string using stack
Reverse the given string using stack.
Implementation: Iterate the string till the end of the length and put each character into the stack and in last iterate the stack and add each character into a new string.
Example:
Input: str="Hello Word"
Output: droW olleH
Approach
Java
public class ReverseStringUsingStack {public static void main(String[] args) {String str = "Hello Word";Stack<Character> rev = revStrUsingStack(str);System.out.println("Reverse String is ");while (!rev.isEmpty()) {System.out.print(rev.pop());}}// Method used for reverse the given stringprivate static Stack<Character> revStrUsingStack(String str) {// declare stack to hold characterStack<Character> rev = new Stack<Character>();// Iterate till end of stringfor (int i = 0; i < str.length(); i++) {// push to stackrev.add(str.charAt(i));}return rev;}}
C++
#include <bits/stdc++.h>using namespace std;//function to reverse a stringstring reverseString(string str){//character stack to hold the//the current current characterssstack<char> st;for(int i=0;i<str.size();i++)st.push(str[i]);string reverse="";//iterate till the stack is//not emptywhile(!st.empty()){reverse+=st.top();st.pop();}return reverse;}int main(){string str = "Hello Word";string reverse=reverseString(str);cout<<reverse;return 0;}
Check whether two strings are anagram of each other
Write a program to check whether two strings are an anagram of each other.
Anagram Strings: Two strings are said to be anagram if the characters' frequency is the same in both the strings.
Example 1:
Input: str1="abc" , str2="cab"
Output: Strings are anagram
Example 2:
Input: str1="aacc" , str2="ccac"
Output: String are not anagram
Approach
Java
import java.util.HashMap;public class StringAnagram {public static void main(String[] args) {String str1 = "aacc";String str2 = "ccac";if (checkStrAnagram(str1, str2)) {System.out.println("Strings are anagram");} else {System.out.println("Strings are not anagram");}}// Method used for check string anagarmprivate static boolean checkStrAnagram(String str1, String str2) {// hold char frequencyHashMap<Character, Integer> map = new HashMap<>();// Base case // null checkif (str1.length() == 0 && str2.length() == 0)return true;if (str1 == null || str1.length() == 0 ||str2 == null || str2.length() == 0) {return false;}// calculate frequency of str1for (int i = 0; i < str1.length(); i++) {map.put(str1.charAt(i),map.getOrDefault(str1.charAt(i), 0) + 1);}// check frequency of str1 is equals of str2for (int i = 0; i < str2.length(); i++) {// if element not contain in str1if (!map.containsKey(str2.charAt(i)))return false;// if frequency not equalif (map.get(str2.charAt(i)) == -1)return false;map.put(str2.charAt(i),map.getOrDefault(str2.charAt(i), 0) - 1);}// worst case : check remaining frequencyfor (Character ch : map.keySet()) {if (map.get(ch) > 0) {return false;}}return true;}}
C++
#include <bits/stdc++.h>using namespace std;//function to check the//given strings are anagram or notbool checkAnagram(string str1,string str2){int len1=str1.size();int len2=str2.size();if(len1!=len2)return false;unordered_map<char,int> ump;for(int i=0;i<len1;i++){ump[str1[i]]++;}for(int i=0;i<len2;i++){if(ump[str2[i]]==0)return false;elseump[str2[i]]--;}for(auto it=ump.begin();it!=ump.end();it++){if(it->second>0)return false;}return true;}int main(){string str1="aacc";string str2="ccac";if(checkAnagram(str1,str2))cout<<"Srings are anagram\n";elsecout<<"String are not anagram\n";return 0;}
Subscribe to:
Posts (Atom)