Skip to content

Commit 2141c4a

Browse files
committed
solved a couple of string/array problems
1 parent 4441e48 commit 2141c4a

File tree

10 files changed

+625
-6
lines changed

10 files changed

+625
-6
lines changed

Arrays and Strings/README.md

Lines changed: 151 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,159 @@
44

55
> Write an algorithm that accepts an array of `'n'` length , finds the minimum element in it and returns it.
66
7-
#### 2. [Reverse a String (reverseAString.cpp)](reverseAString.cpp)
7+
#### 2. [Reverse a String (reverseAString)](reverseAString.cpp)
88

99
> Write an function that takes in a string, reverses it and returns it.
1010
11-
11+
12+
#### 3. [Reverse an Array (reverseAnArray)](reverseAnArray.cpp)
13+
14+
> Write an function that takes in an array, reverses it and returns it(or its pointer).
15+
16+
#### 3. [Pair of Numbers add up to another in an array (findPairofNumbersToAddUpToAnother)](findPairofNumbersToAddUpToAnother.cpp)
17+
18+
> Check for a pair of numbers in a given number array A[] with the sum 'x'
19+
> Test Case => [2,4,5,7,1,3] - x =12 (5,7 are the pair with sum 12)
20+
21+
22+
#### 4. [Find most occuring number in an Array (mostOccuringNumber)](mostOccuringNumber.cpp)
23+
24+
> Find out the number that appears most number of times
25+
> Test Case => [2,2,2,2,1,4,2,2,2,63,1,1,1,1,15,72,4,3,2,6,7,1,3]
26+
27+
#### 5. [Find a number that occurs odd number of times (arrayNumberOccuringOddTimes)](arrayNumberOccuringOddTimes.cpp)
28+
29+
> Find out the number that appears odd number of times.
30+
> Test Case => [2,2,2,2,2,1,1,1,1,2,3,4,3,4,6,7,6,7,8,9,8,9]
31+
32+
#### 5. [Rotate an array 'd' number of times (rotateAnArray)](rotateAnArray.cpp)
33+
34+
> Given an array, with an argument 'd'. Rotate it by d number of times.
35+
> A = [1,2,3,4,5], d = 3, RotatedArray = [4,5,1,2,3]
36+
> Test Case => [1,2,3,4,5,6,7,8,9,10,11,12,13]
37+
38+
1239
TODO :
1340

14-
* Algorithm to reverse a string.
15-
* Algorithm to reverse an array.
16-
*
41+
* An algorithm, rotate(A[n], d). Rotate an array A[n] by d times.
42+
43+
* Maximum sum such that no two elements are adjacent
44+
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.
45+
46+
* Write a program to print all the LEADERS in the array. An element is leader if it is greater than all the elements to its right side. And the rightmost element is always a leader. For example int the array {16, 17, 4, 3, 5, 2}, leaders are 17, 5 and 2.
47+
48+
* You are given an array of 0s and 1s in random order. Segregate 0s on left side and 1s on right side of the array. Traverse array only once.
49+
50+
* Write a program to print all the LEADERS in the array. An element is leader if it is greater than all the elements to its right side. And the rightmost element is always a leader. For example int the array {16, 17, 4, 3, 5, 2}, leaders are 17, 5 and 2.
51+
52+
* Given an array arr[] of integers, find out the difference between any two elements such that larger element appears after the smaller number in arr[].
53+
54+
* A Product Array Puzzle
55+
Given an array arr[] of n integers, construct a Product Array prod[] (of same size) such that prod[i] is equal to the product of all the elements of arr[] except arr[i]. Solve it without division operator and in O(n).
56+
57+
Example:
58+
arr[] = {10, 3, 5, 6, 2}
59+
prod[] = {180, 600, 360, 300, 900}
60+
61+
* Given two sorted arrays, find their union and intersection.
62+
63+
For example, if the input arrays are:
64+
arr1[] = {1, 3, 4, 5, 7}
65+
arr2[] = {2, 3, 5, 6}
66+
Then your program should print Union as {1, 2, 3, 4, 5, 6, 7} and Intersection as {3, 5}.
67+
68+
* Given an array A[], write a function that segregates even and odd numbers. The functions should put all even numbers first, and then odd numbers.
69+
70+
Example
71+
72+
Input = {12, 34, 45, 9, 8, 90, 3}
73+
Output = {12, 34, 8, 90, 45, 9, 3}
74+
75+
76+
* Find the two repeating elements in a given array
77+
You are given an array of n+2 elements. All elements of the array are in range 1 to n. And all elements occur once except two numbers which occur twice. Find the two repeating numbers.
78+
79+
For example, array = {4, 2, 4, 5, 2, 3, 1} and n = 5
80+
81+
The above array has n + 2 = 7 elements with all elements occurring once except 2 and 4 which occur twice. So the output should be 4 2.
82+
83+
* Equilibrium index of an array
84+
Equilibrium index of an array is an index such that the sum of elements at lower indexes is equal to the sum of elements at higher indexes. For example, in an arrya A:
85+
86+
A[0] = -7, A[1] = 1, A[2] = 5, A[3] = 2, A[4] = -4, A[5] = 3, A[6]=0
87+
88+
3 is an equilibrium index, because:
89+
A[0] + A[1] + A[2] = A[4] + A[5] + A[6]
90+
91+
* Check if array elements are consecutive | Added Method 3
92+
Given an unsorted array of numbers, write a function that returns true if array consists of consecutive numbers.
93+
94+
Examples:
95+
a) If array is {5, 2, 3, 1, 4}, then the function should return true because the array has consecutive numbers from 1 to 5.
96+
97+
b) If array is {83, 78, 80, 81, 79, 82}, then the function should return true because the array has consecutive numbers from 78 to 83.
98+
99+
c) If the array is {34, 23, 52, 12, 3 }, then the function should return false because the elements are not consecutive.
100+
101+
102+
* Find the smallest missing number
103+
Given a sorted array of n integers where each integer is in the range from 0 to m-1 and m > n. Find the smallest number that is missing from the array.
104+
105+
Examples
106+
Input: {0, 1, 2, 6, 9}, n = 5, m = 10
107+
Output: 3
108+
109+
Input: {4, 5, 10, 11}, n = 4, m = 12
110+
Output: 0
111+
112+
Input: {0, 1, 2, 3}, n = 4, m = 5
113+
Output: 4
114+
115+
Input: {0, 1, 2, 3, 4, 5, 6, 7, 10}, n = 9, m = 11
116+
Output: 8
117+
118+
* Find the minimum distance between two numbers
119+
Given an unsorted array arr[] and two numbers x and y, find the minimum distance between x and y in arr[]. The array might also contain duplicates. You may assume that both x and y are different and present in arr[].
120+
121+
Examples:
122+
Input: arr[] = {1, 2}, x = 1, y = 2
123+
Output: Minimum distance between 1 and 2 is 1.
124+
125+
Input: arr[] = {3, 4, 5}, x = 3, y = 5
126+
Output: Minimum distance between 3 and 5 is 2.
127+
128+
Input: arr[] = {3, 5, 4, 2, 6, 5, 6, 6, 5, 4, 8, 3}, x = 3, y = 6
129+
Output: Minimum distance between 3 and 6 is 4.
130+
131+
Input: arr[] = {2, 5, 3, 5, 4, 4, 2, 3}, x = 3, y = 2
132+
Output: Minimum distance between 3 and 2 is 1.
133+
134+
Count smaller elements on right side
135+
136+
* Write a function to count number of smaller elements on right of each element in an array. Given an unsorted array arr[] of distinct integers, construct another array countSmaller[] such that countSmaller[i] contains count of smaller elements on right side of each element arr[i] in array.
137+
138+
Examples:
139+
140+
Input: arr[] = {12, 1, 2, 3, 0, 11, 4}
141+
Output: countSmaller[] = {6, 1, 1, 1, 0, 1, 0}
142+
143+
(Corner Cases)
144+
Input: arr[] = {5, 4, 3, 2, 1}
145+
Output: countSmaller[] = {4, 3, 2, 1, 0}
146+
147+
Input: arr[] = {1, 2, 3, 4, 5}
148+
Output: countSmaller[] = {0, 0, 0, 0, 0}
149+
150+
* Find subarray with given sum | Set 1 (Nonnegative Numbers)
151+
Given an unsorted array of nonnegative integers, find a continous subarray which adds to a given number.
152+
153+
Examples:
154+
155+
Input: arr[] = {1, 4, 20, 3, 10, 5}, sum = 33
156+
Ouptut: Sum found between indexes 2 and 4
157+
158+
Input: arr[] = {1, 4, 0, 0, 3, 10, 5}, sum = 7
159+
Ouptut: Sum found between indexes 1 and 4
160+
161+
Input: arr[] = {1, 4}, sum = 0
162+
Output: No subarray found
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2+
/*******************************************************************************************************
3+
/* ___ _ _ _ _
4+
/* / _ \ | | (_)| | | |
5+
/* / /_\ \__ __ ___ | | __ _ ___ _ __ _ | |_ | |__ _ __ ___ ___
6+
/* | _ |\ \ /\ / / / _ \| | / _` | / _ \ | '__|| || __|| '_ \ | '_ ` _ \ / __|
7+
/* | | | | \ V V / | __/| || (_| || (_) || | | || |_ | | | || | | | | |\__ \
8+
/* \_| |_/ \_/\_/ \___||_| \__, | \___/ |_| |_| \__||_| |_||_| |_| |_||___/
9+
/* __/ |
10+
/* |___/
11+
/******************************************************************************************************/
12+
13+
/************************************************
14+
/* Find a number that occurs odd number of times
15+
/************************************************/
16+
17+
// Library imports
18+
#include <iostream>
19+
using namespace std;
20+
21+
//Prototypes for the algorithms
22+
int arrayNumberOccuringOddTimes(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,2,2,1,1,1,1,2,3,4,3,4,6,7,6,7,8,9,8,9};
28+
int arrayLength = sizeof(arr)/sizeof(arr[0]);
29+
// Call your algorithms here
30+
cout<<arrayNumberOccuringOddTimes(arr, arrayLength)<<endl;
31+
32+
33+
34+
return 0;
35+
}
36+
37+
/**********************************************************
38+
/* <arrayNumberOccuringOddTimes>
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 arrayNumberOccuringOddTimes(int* arr,int len){
48+
// Algorithm content goes here.
49+
int occurences = 0;
50+
51+
for(int i=0;i<len;i++){
52+
for(int j=0;j<len;j++){
53+
if(arr[i]==arr[j]){
54+
occurences++;
55+
}
56+
}
57+
if(occurences%2!=0){
58+
return arr[i];
59+
}
60+
else{
61+
occurences=0;
62+
}
63+
}
64+
}
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: 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+
}

Arrays and Strings/reverseAString.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
// Library imports
1818
#include <iostream>
19-
#include <stdio.h>
2019
#include <cstring> // Import cstring for string operations
2120
using namespace std;
2221

0 commit comments

Comments
 (0)