C++ & C Programming Solutions: Algorithms & Patterns
Classified in Computers
Written on in English with a size of 5.13 KB
1. Anagram Detection: C++ String Comparison
This C++ program determines if two input strings are anagrams of each other. It achieves this by converting both strings to lowercase, sorting their characters alphabetically, and then comparing the sorted strings. If they are identical, the original strings are considered anagrams.
#include<bits/stdc++.h>
using namespace std;
int main(){
string s2,s1;
cin>>s1>>s2;
transform(s1.begin(),s1.end(),s1.begin(),::tolower);
transform(s2.begin(),s2.end(),s2.begin(),::tolower);
sort(s1.begin(),s1.end());
sort(s2.begin(),s2.end());
cout<< (s2==s1);
}
2. Array Subarray: C++ Sliding Window Minimum
This C++ program attempts to find the maximum of minimums within sliding windows of a given size x
in an array. The sorting
function recursively finds the minimum element in a specified range, while the func
function aims to optimize this by leveraging a previously calculated minimum. Note that the array indexing and window handling in sorting
and func
might require careful review for standard sliding window implementations, especially with the circular array-like logic.
#include <bits/stdc++.h>
using namespace std;
vector < int > arr;
int prevmin=-1;
int flag=0;
int x,n,q;
int sorting(int start,int end)
{
if(start+1==n) {start=0;end=end-n;}
if(start==end) return arr[start];
return min(arr[start],sorting(start+1,end));
}
int func(int start,int end)
{
if(flag==0) {flag++;return prevmin=sorting(start,end);}
if(arr[start-1]==prevmin) return prevmin;
return prevmin=(arr[end] <= prevmin)?prevmin:sorting(start,end);
}
int main() {
cin >> x >> n;
int ans=0;
for(int i=0;i < n;i++) {cin >> q;arr.push_back(q);}
for(int i=0;i < n;i++)
{
ans=max(ans,func(i,i+x-1));
}
cout << ans;
}
3. C Language Pattern Printing: Trapezium Shape
This C program generates a unique character pattern on the console, resembling a trapezium or diamond shape. It uses nested loops to print a combination of asterisks (*
) and periods (.
) based on the input integer n
, creating a symmetrical design.
#include<stdio.h>
int main(){
int i,j,n;
scanf("%d",&n);
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(j<n-i-1)
printf("*");
else
printf(".");
}
for(j=0;j<n-1;j++){
if(j<i)
printf(".");
else
printf("*");
}
printf("\n");
}
for(i=2;i<=n;i++){
for(j=0;j<n;j++){
if(j<i-1)
printf("*");
else
printf(".");
}
for(j=0;j<n-1;j++){
if(j<n-i)
printf(".");
else
printf("*");
}
printf("\n");
}
return 0;
}
4. C++ Large Product Formatting: Trailing Zeros
This C++ program calculates the product of all integers within a given range [a, b]
. It then formats the result by separating the non-zero digits from the trailing zeros, expressing the product in the form X * 10^Y
, where X
is the significant part and Y
is the count of trailing zeros. This is particularly useful for handling large products that might exceed standard integer limits if not represented efficiently.
#include <bits/stdc++.h>
using namespace std;
void formatProducts(int a , int b){
int res =1 ;
for(int i=a; i<=b; i++){
res = res * i;
}
int temp = res;
int power = 0;
while ((res % 10) == 0) {
power = power + 1 ;
res = res / 10;
}
cout << res << " * 10^" << power ;
}
int main(){
int a , b;
cin>> a >> b;
formatProducts(a,b);
}
5. C++ Password Creation: String Interleaving
This C++ program demonstrates a method for creating a new "password" by interleaving characters from two input strings. It takes characters alternately from each string until one string is exhausted, then appends any remaining characters from the longer string. This technique can be used for simple string combination or obfuscation.
#include <iostream>
#include <string>
std::string newPassword(const std::string &a, const std::string &b) {
std::string result;
int i = 0, j = 0;
// Interleave characters from both strings
while (i < a.size() && j < b.size()) {
result += a[i++];
result += b[j++];
}
// Append any remaining characters from 'a' or 'b'
if (i < a.size()) result += a.substr(i);
if (j < b.size()) result += b.substr(j);
return result;
}
int main() {
std::string a = "abc";
std::string b = "def";
std::cout << "New Password: " << newPassword(a, b) << std::endl;
return 0;
}