Skip to content

Commit f2fcad5

Browse files
committed
added C++ folder
1 parent f9d5385 commit f2fcad5

12 files changed

+660
-13
lines changed
File renamed without changes.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
2+
/*******************************************************************************************************
3+
/* ___ _ _ _ _
4+
/* / _ \ | | (_)| | | |
5+
/* / /_\ \__ __ ___ | | __ _ ___ _ __ _ | |_ | |__ _ __ ___ ___
6+
/* | _ |\ \ /\ / / / _ \| | / _` | / _ \ | '__|| || __|| '_ \ | '_ ` _ \ / __|
7+
/* | | | | \ V V / | __/| || (_| || (_) || | | || |_ | | | || | | | | |\__ \
8+
/* \_| |_/ \_/\_/ \___||_| \__, | \___/ |_| |_| \__||_| |_||_| |_| |_||___/
9+
/* __/ |
10+
/* |___/
11+
/******************************************************************************************************/
12+
13+
/***********************************
14+
/* Pair of Numbers add up to another in an array
15+
/***********************************/
16+
17+
// Library imports
18+
#include <iostream>
19+
using namespace std;
20+
21+
//Prototypes for the algorithms
22+
void findPairofNumbersToAddUpToAnother(int* ,int, int);
23+
24+
int main(){
25+
// Lines to handle user input go here, be descriptive...
26+
// use cin, cout
27+
int arr[] = {2,4,5,7,1,3};
28+
int additionNumber = 12;
29+
int arrayLength = sizeof(arr)/sizeof(arr[0]);
30+
// Call your algorithms here
31+
findPairofNumbersToAddUpToAnother(arr, arrayLength, additionNumber);
32+
33+
34+
35+
return 0;
36+
}
37+
38+
/**********************************************************
39+
/* <findPairofNumbersToAddUpToAnother>
40+
/*
41+
/* @description : description here
42+
/* @param : Integer<variable> (Datatype<variable_name>)
43+
/* @return : datatype (int,float,double etc)
44+
/* @complexity : Big O notation. (O(n),O(n^2) etc)
45+
/* @explanation : Detailed description about what the algorithm does
46+
/* @author : <Your name> @<GitHub handle> (John Doe @johndoe)
47+
/**********************************************************/
48+
void findPairofNumbersToAddUpToAnother(int* arr,int len, int targetAdditionSum){
49+
// Algorithm content goes here.
50+
51+
for(int i=0;i<len-1;i++){
52+
for(int j=i+1;j<len;j++){
53+
//cout << arr[i] << " " << arr[j]<<endl;
54+
if(arr[i] + arr[j] == targetAdditionSum){
55+
cout<< arr[i] << " "<< arr[j]<<endl;
56+
return ;
57+
}
58+
}
59+
}
60+
61+
62+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
2+
/*******************************************************************************************************
3+
/* ___ _ _ _ _
4+
/* / _ \ | | (_)| | | |
5+
/* / /_\ \__ __ ___ | | __ _ ___ _ __ _ | |_ | |__ _ __ ___ ___
6+
/* | _ |\ \ /\ / / / _ \| | / _` | / _ \ | '__|| || __|| '_ \ | '_ ` _ \ / __|
7+
/* | | | | \ V V / | __/| || (_| || (_) || | | || |_ | | | || | | | | |\__ \
8+
/* \_| |_/ \_/\_/ \___||_| \__, | \___/ |_| |_| \__||_| |_||_| |_| |_||___/
9+
/* __/ |
10+
/* |___/
11+
/******************************************************************************************************/
12+
13+
/************************************************
14+
/* Print all the LEADERS in the array
15+
/************************************************/
16+
17+
// Library imports
18+
#include <iostream>
19+
using namespace std;
20+
21+
//Prototypes for the algorithms
22+
int leadersInAnArray(int* arr,int len);
23+
24+
int main(){
25+
// Lines to handle user input go here, be descriptive...
26+
// use cin, cout
27+
int arr[] = {16, 17, 4, 3, 5, 2};
28+
int arrayLength = sizeof(arr)/sizeof(arr[0]);
29+
// Call your algorithms here
30+
leadersInAnArray(arr, arrayLength);
31+
32+
33+
34+
return 0;
35+
}
36+
37+
/**********************************************************
38+
/* <leadersInAnArray>
39+
/*
40+
/* @description : description here
41+
/* @param : Integer<variable> (Datatype<variable_name>)
42+
/* @return : datatype (int,float,double etc)
43+
/* @complexity : Big O notation. (O(n),O(n^2) etc)
44+
/* @explanation : Detailed description about what the algorithm does
45+
/* @author : <Your name> @<GitHub handle> (John Doe @johndoe)
46+
/**********************************************************/
47+
int leadersInAnArray(int* arr,int len){
48+
// Algorithm content goes here.
49+
if(len ==0){
50+
return 0;
51+
}
52+
53+
int great = 0;
54+
55+
for(int i=0; i<len-1; i++){
56+
for(int j=i+1; j<len; j++){
57+
if(arr[i] < arr[j]){
58+
great = 0;
59+
break;
60+
}
61+
great = 1;
62+
}
63+
64+
if(great){
65+
cout<<arr[i]<< " ";
66+
great = 0;
67+
}
68+
69+
}
70+
cout<< arr[len-1];
71+
return 1;
72+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
2+
/*******************************************************************************************************
3+
/* ___ _ _ _ _
4+
/* / _ \ | | (_)| | | |
5+
/* / /_\ \__ __ ___ | | __ _ ___ _ __ _ | |_ | |__ _ __ ___ ___
6+
/* | _ |\ \ /\ / / / _ \| | / _` | / _ \ | '__|| || __|| '_ \ | '_ ` _ \ / __|
7+
/* | | | | \ V V / | __/| || (_| || (_) || | | || |_ | | | || | | | | |\__ \
8+
/* \_| |_/ \_/\_/ \___||_| \__, | \___/ |_| |_| \__||_| |_||_| |_| |_||___/
9+
/* __/ |
10+
/* |___/
11+
/******************************************************************************************************/
12+
13+
/************************************************
14+
/* Maximum sum in an Array such that no two elements are adjacent
15+
/************************************************/
16+
17+
// Library imports
18+
#include <iostream>
19+
using namespace std;
20+
21+
//Prototypes for the algorithms
22+
int maxNonAdjacentSum(int* arr,int len);
23+
24+
int main(){
25+
// Lines to handle user input go here, be descriptive...
26+
// use cin, cout
27+
int arr[] = {93,2,1,64,23,65,3,7,8,78,3,85};
28+
int arrayLength = sizeof(arr)/sizeof(arr[0]);
29+
// Call your algorithms here
30+
maxNonAdjacentSum(arr, arrayLength);
31+
32+
33+
34+
return 0;
35+
}
36+
37+
/**********************************************************
38+
/* <maxNonAdjacentSum>
39+
/*
40+
/* @description : description here
41+
/* @param : Integer<variable> (Datatype<variable_name>)
42+
/* @return : datatype (int,float,double etc)
43+
/* @complexity : Big O notation. (O(n),O(n^2) etc)
44+
/* @explanation : Detailed description about what the algorithm does
45+
/* @author : <Your name> @<GitHub handle> (John Doe @johndoe)
46+
/**********************************************************/
47+
int maxNonAdjacentSum(int* arr,int len){
48+
// Algorithm content goes here.
49+
int maxSum=0;
50+
int numA=0;
51+
int numB=0;
52+
53+
for(int i=0;i<len-2;i++){
54+
for(int j=i+2;j<len;j++){
55+
56+
if(arr[i]+arr[j]>=maxSum){
57+
58+
59+
numA = arr[i];
60+
numB = arr[j];
61+
maxSum = numA+numB;
62+
63+
// Max sum and numbers that contribute to it.
64+
cout<<maxSum<<" "<<numA<<" "<<numB<< endl;
65+
}
66+
67+
}
68+
}
69+
70+
cout<<maxSum;
71+
72+
return maxSum;
73+
74+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2+
/*******************************************************************************************************
3+
/* ___ _ _ _ _
4+
/* / _ \ | | (_)| | | |
5+
/* / /_\ \__ __ ___ | | __ _ ___ _ __ _ | |_ | |__ _ __ ___ ___
6+
/* | _ |\ \ /\ / / / _ \| | / _` | / _ \ | '__|| || __|| '_ \ | '_ ` _ \ / __|
7+
/* | | | | \ V V / | __/| || (_| || (_) || | | || |_ | | | || | | | | |\__ \
8+
/* \_| |_/ \_/\_/ \___||_| \__, | \___/ |_| |_| \__||_| |_||_| |_| |_||___/
9+
/* __/ |
10+
/* |___/
11+
/******************************************************************************************************/
12+
13+
/***********************************
14+
/* Get minimum element in an array
15+
/***********************************/
16+
17+
#include<iostream>
18+
using namespace std;
19+
20+
// Function Prototype
21+
int GetMinimumElementInArray(int*, int);
22+
23+
int main(){
24+
int testArr[] = {10,9,5,6,7,8,2,1,3,4};
25+
26+
// Calculating array size to pass to the argument of the algorithm function,
27+
// since you can't really find out the length of an array since you pass the pointer
28+
// of the array as an argument and the size of the whole array is lost
29+
// to avoid this, we calculate the length before hand and pass it as an argument.
30+
int arrSize = sizeof(testArr)/sizeof(testArr[0]);
31+
32+
cout<<GetMinimumElementInArray(testArr,arrSize);
33+
34+
return 1;
35+
}
36+
37+
38+
/**********************************************************
39+
/* <printPattern_02>
40+
/*
41+
/* @description : Loops through the array and returns the minimum element
42+
/* @param : Integer<input>, 'input' represents the n value in nxn grid.
43+
/* @return : None, void
44+
/* @complexity : 0(n)
45+
/* @explanation : An algorithm that iterates over the array and tracks the least element it's
46+
encountered, reassigns the new, lower element if its lower than the current lower
47+
value. The lowest value is found upon completion of iteration in the end.
48+
/* @author : Pranay Kothapalli @kotAPI
49+
/**********************************************************/
50+
int GetMinimumElementInArray(int* array, int size){
51+
int minElement=array[0]; //Initialize the minimum element it to first element of the array
52+
53+
//Loop through the array
54+
for(int i=0;i<size;i++){
55+
// If an element is lesser than the present minElement, reassign minElement to curr element.
56+
if(minElement > array[i]){
57+
minElement = array[i];
58+
}
59+
}
60+
61+
// Return minElement on completion
62+
return minElement;
63+
}
64+
65+
66+
67+
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
2+
/*******************************************************************************************************
3+
/* ___ _ _ _ _
4+
/* / _ \ | | (_)| | | |
5+
/* / /_\ \__ __ ___ | | __ _ ___ _ __ _ | |_ | |__ _ __ ___ ___
6+
/* | _ |\ \ /\ / / / _ \| | / _` | / _ \ | '__|| || __|| '_ \ | '_ ` _ \ / __|
7+
/* | | | | \ V V / | __/| || (_| || (_) || | | || |_ | | | || | | | | |\__ \
8+
/* \_| |_/ \_/\_/ \___||_| \__, | \___/ |_| |_| \__||_| |_||_| |_| |_||___/
9+
/* __/ |
10+
/* |___/
11+
/******************************************************************************************************/
12+
13+
/***********************************
14+
/* Find most occuring number in an Array
15+
/***********************************/
16+
17+
// Library imports
18+
#include <iostream>
19+
using namespace std;
20+
21+
//Prototypes for the algorithms
22+
int mostOccuringNumber(int* arr,int len);
23+
24+
int main(){
25+
// Lines to handle user input go here, be descriptive...
26+
// use cin, cout
27+
int arr[] = {2,2,2,2,1,4,2,2,2,63,1,1,1,1,15,72,4,3,2,6,7,1,3};
28+
int arrayLength = sizeof(arr)/sizeof(arr[0]);
29+
// Call your algorithms here
30+
cout<<mostOccuringNumber(arr, arrayLength);
31+
32+
33+
34+
return 0;
35+
}
36+
37+
/**********************************************************
38+
/* <mostOccuringNumber>
39+
/*
40+
/* @description : description here
41+
/* @param : Integer<variable> (Datatype<variable_name>)
42+
/* @return : datatype (int,float,double etc)
43+
/* @complexity : Big O notation. (O(n),O(n^2) etc)
44+
/* @explanation : Detailed description about what the algorithm does
45+
/* @author : <Your name> @<GitHub handle> (John Doe @johndoe)
46+
/**********************************************************/
47+
int mostOccuringNumber(int* arr,int len){
48+
// Algorithm content goes here.
49+
50+
int occurences = 0;
51+
int currentOccurences =0;
52+
int highestOccuringNumber=0;
53+
54+
for(int i=0;i<len-1;i++){
55+
for(int j=i+1;j<len;j++){
56+
if(arr[i]==arr[j]){
57+
currentOccurences++;
58+
}
59+
}
60+
if(currentOccurences>occurences){
61+
highestOccuringNumber = arr[i];
62+
occurences = currentOccurences;
63+
}
64+
currentOccurences =0;
65+
}
66+
67+
return highestOccuringNumber;
68+
}

0 commit comments

Comments
 (0)