Skip to content

Commit f9d5385

Browse files
committed
solved leaders and maxNonAdjacentSum
1 parent 2141c4a commit f9d5385

File tree

5 files changed

+159
-8
lines changed

5 files changed

+159
-8
lines changed

Arrays and Strings/README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,21 @@
2929
> Find out the number that appears odd number of times.
3030
> Test Case => [2,2,2,2,2,1,1,1,1,2,3,4,3,4,6,7,6,7,8,9,8,9]
3131
32-
#### 5. [Rotate an array 'd' number of times (rotateAnArray)](rotateAnArray.cpp)
32+
#### 6. [Rotate an array 'd' number of times (rotateAnArray)](rotateAnArray.cpp)
3333

3434
> Given an array, with an argument 'd'. Rotate it by d number of times.
3535
> A = [1,2,3,4,5], d = 3, RotatedArray = [4,5,1,2,3]
3636
> Test Case => [1,2,3,4,5,6,7,8,9,10,11,12,13]
3737
38+
#### 7. [Maximum sum in an Array such that no two elements are adjacent(maxNonAdjacentSum)](maxNonAdjacentSum.cpp)
39+
40+
> Find out the number that's maximum sum in an Array such that no two elements are adjacent
41+
> Test Case => [33,2,1,64,23,75,3,7,8,78,3,65];
42+
3843

3944
TODO :
4045

41-
* An algorithm, rotate(A[n], d). Rotate an array A[n] by d times.
46+
4247

4348
* Maximum sum such that no two elements are adjacent
4449
Given an array of positive numbers, find the maximum sum of a subsequence with the constraint that no 2 numbers in the sequence should be adjacent in the array. So 3 2 7 10 should return 13 (sum of 3 and 10) or 3 2 5 10 7 should return 15 (sum of 3, 5 and 7).Answer the question in most efficient way.
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+
}

Arrays and Strings/rotateAnArray.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
/******************************************************************************************************/
1212

1313
/************************************************
14-
/* Find a number that occurs odd number of times
14+
/* Rotate an array 'd' number of times
1515
/************************************************/
1616

1717
// Library imports
@@ -28,11 +28,11 @@ int main(){
2828
int arrayLength = sizeof(arr)/sizeof(arr[0]);
2929

3030
int difference = 3;
31-
// cout<<"How many times do you want to rotate an Array by? has to be less than "<<arrayLength;
32-
// cin>>difference;
33-
if(difference>arrayLength){
34-
// exit program if the input is invalid
35-
return 0;
31+
cout<<"How many times do you want to rotate an Array by? has to be less than "<<arrayLength;
32+
cin>>difference;
33+
if(difference>=arrayLength){
34+
35+
difference = difference % arrayLength;
3636
}
3737
// Call your algorithms here
3838

Arrays and Strings/test

-9 KB
Binary file not shown.

0 commit comments

Comments
 (0)