Skip to content

Added LeetCode and Geeks for Geeks Solutions in CPP #849

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions C++/Geeks4Geeks/1's Complement/1s-complement.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// { Driver Code Starts

#include<bits/stdc++.h>
using namespace std;

// } Driver Code Ends

class Solution{
public:
string onesComplement(string S,int N){
//code here
for(int i=0;i<N;i++){
if(S[i] == '0'){
S[i] = '1';
}
else{
S[i] = '0';
}
}
return S;
}
};

// { Driver Code Starts.
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
string s;
cin>>s;
Solution ob;
cout<<ob.onesComplement(s,n)<<"\n";
}
} // } Driver Code Ends
43 changes: 43 additions & 0 deletions C++/Geeks4Geeks/1's Complement/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# 1's Complement
## Basic
<div class="problem-statement">
<p></p><p><span style="font-size:18px">Given an<strong> N </strong>bit binary number, find the 1's complement of the number.&nbsp;The ones'&nbsp;complement&nbsp;of a binary number is&nbsp;defined&nbsp;as the value obtained by inverting all the bits in the binary representation of the number (swapping 0s for 1s and vice versa).</span><br>
&nbsp;</p>

<p><strong><span style="font-size:18px">Example 1:</span></strong></p>

<pre><strong><span style="font-size:18px">Input:</span>
</strong><span style="font-size:18px">N = 3
S = 101
<strong>Output:
</strong>010
<strong>Explanation:
</strong>We get the output by converting 1's in S
to 0 and 0s to 1</span><strong>
</strong></pre>

<p><strong><span style="font-size:18px">Example 2:</span></strong></p>

<pre><span style="font-size:18px"><strong>Input:</strong>
N = 2
S = 10
<strong>Output:</strong>
01
<strong>Explanation:</strong>
We get the output by converting 1's in S
to 0 and 0s to 1</span>
</pre>

<p><br>
<span style="font-size:18px"><strong>Your Task:&nbsp;&nbsp;</strong><br>
You don't need to read input or print anything. Your task is to complete the function&nbsp;<strong>onesComplement()</strong>&nbsp;which takes the binary string S, its size N<strong>&nbsp;</strong>as input parameters&nbsp;and returns 1's complement of S of size N.</span><br>
&nbsp;</p>

<p><span style="font-size:18px"><strong>Expected Time Complexity:</strong> O(N)<br>
<strong>Expected Space Complexity:</strong> O(N)</span><br>
&nbsp;</p>

<p><span style="font-size:18px"><strong>Constraints:</strong><br>
1&lt;=N&lt;=100</span></p>
<p></p>
</div>
39 changes: 39 additions & 0 deletions C++/Geeks4Geeks/Binary Search/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Binary Search
## Basic
<div class="problem-statement">
<p></p><p><span style="font-size:18px">Given a sorted array of size N and an integer K, find the position at which K is present in the array using binary search.</span></p>

<p><br>
<span style="font-size:18px"><strong>Example 1:</strong></span></p>

<pre><span style="font-size:18px"><strong>Input:</strong>
N = 5
arr[] = {1 2 3 4 5}
K = 4
<strong>Output:</strong> 3
<strong>Explanation:</strong> 4 appears at index 3.</span></pre>

<p><br>
<span style="font-size:18px"><strong>Example 2:</strong></span></p>

<pre><span style="font-size:18px"><strong>Input:</strong>
N = 5
arr[] = {11 22 33 44 55}
K = 445
<strong>Output:</strong> -1
<strong>Explanation:</strong> 445 is not present.</span></pre>

<p><br>
<span style="font-size:18px"><strong>Your Task: &nbsp;</strong><br>
You dont need to read input or print anything. Complete the function <strong>binarysearch()</strong> which takes arr[], N and K as input parameters and returns the index of K in the array. If K is not present in the array, return -1.</span></p>

<p><br>
<span style="font-size:18px"><strong>Expected Time Complexity:</strong> O(LogN)<br>
<strong>Expected Auxiliary Space:</strong> O(LogN) if solving recursively and O(1) otherwise.</span></p>

<p><br>
<span style="font-size:18px"><strong>Constraints:</strong><br>
1 &lt;= N &lt;= 10<sup>4</sup><br>
1 &lt;= arr[i] &lt;= 10<sup>4</sup></span></p>
<p></p>
</div>
32 changes: 32 additions & 0 deletions C++/Geeks4Geeks/Binary number to decimal number/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Binary number to decimal number
## Basic
<div class="problem-statement">
<p></p><p><span style="font-size:18px">Given a Binary Number <strong>B</strong>, find&nbsp;its decimal equivalent.</span><br>
&nbsp;</p>

<p><span style="font-size:18px"><strong>Example 1:</strong></span></p>

<pre><span style="font-size:18px"><strong>Input: </strong>B = 10001000
<strong>Output: </strong>136</span>
</pre>

<p><span style="font-size:18px"><strong>Example 2:</strong></span></p>

<pre><span style="font-size:18px"><strong>Input: </strong>B = 101100
<strong>Output: </strong>44</span>
</pre>

<p>&nbsp;</p>

<p><span style="font-size:18px"><strong>Your Task:</strong><br>
You don't need to read or print anything. Your task is to complete the function&nbsp;<strong>binary_to_decimal()</strong>&nbsp;which takes the binary number as string input parameter and returns its decimal equivalent.</span><br>
&nbsp;</p>

<p><span style="font-size:18px"><strong>Expected Time Complexity:&nbsp;</strong>O(K * Log(K)) where K is number of bits&nbsp;in binary number.<br>
<strong>Expected Space Complexity:&nbsp;</strong>O(1)</span><br>
&nbsp;</p>

<p><span style="font-size:18px"><strong>Constraints:</strong><br>
1 &lt;= number of bits in binary number&nbsp;&nbsp;&lt;= 16</span></p>
<p></p>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// { Driver Code Starts
#include<bits/stdc++.h>
using namespace std;

// } Driver Code Ends
class Solution
{
public:
int binary_to_decimal(string str)
{
// Code here.
int decimal=0;
int power = 1;
for(int i=str.length()-1;i>=0;i--){
int a = str[i]-'0';
decimal += a*power;
power *= 2;
}
return decimal;
}
};

// { Driver Code Starts.
int main(){
int T;
cin >> T;
while(T--)
{
string str;
cin >> str;
Solution ob;
int ans = ob.binary_to_decimal(str);
cout << ans <<"\n";
}
return 0;
} // } Driver Code Ends
35 changes: 35 additions & 0 deletions C++/Geeks4Geeks/Bitonic Point/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Bitonic Point
## Easy
<div class="problem-statement">
<p></p><p><span style="font-size:18px">Given an array <strong>arr</strong> of <strong>n</strong> elements which is first increasing and then may be decreasing,&nbsp;find the maximum element in the array.<br>
<strong>Note: </strong>If the array is increasing then just print then <strong>last element</strong> will be the maximum value.</span></p>

<p><span style="font-size:18px"><strong>Example 1:</strong></span></p>

<pre><span style="font-size:18px"><strong>Input:</strong>
n = 9
arr[] = {1,15,25,45,42,21,17,12,11}
<strong>Output:</strong> 45
<strong>Explanation:</strong> Maximum element is 45.</span></pre>

<p><span style="font-size:18px"><strong>Example 2:</strong></span></p>

<pre><span style="font-size:18px"><strong>Input:</strong>
n = 5
arr[] = {1, 45, 47, 50, 5}
<strong>Output:</strong> 50
<strong>Explanation:</strong> Maximum element is 50.</span></pre>

<p><span style="font-size:18px"><strong>Your Task:&nbsp;&nbsp;</strong><br>
You don't need to read input or print anything. Your task is to complete the function&nbsp;<strong>findMaximum()</strong>&nbsp;which takes the array&nbsp;<strong>arr[], </strong>and<strong> n</strong><strong>&nbsp;</strong>as parameters and returns an integer&nbsp;denoting&nbsp;the answer.<br>
<br>
<strong>Expected Time Complexity:</strong>&nbsp;O(logn)<br>
<strong>Expected Auxiliary Space:</strong>&nbsp;O(1)</span></p>

<p><span style="font-size:18px"><strong>Constraints:</strong><br>
3 ≤ n ≤ 10<sup>6</sup><br>
1 ≤ arr<sub>i</sub> ≤ 10<sup>6</sup></span></p>

<p>&nbsp;</p>
<p></p>
</div>
37 changes: 37 additions & 0 deletions C++/Geeks4Geeks/Bubble Sort/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Bubble Sort
## Easy
<div class="problem-statement">
<p></p><p><span style="font-size:18px">Given an Integer <strong>N</strong> and a list <strong>arr</strong>. Sort the array using bubble sort algorithm.</span><br>
<span style="font-size:18px"><strong>Example 1:</strong></span></p>

<pre><span style="font-size:18px"><strong>Input</strong>:
N = 5
arr[] = {4, 1, 3, 9, 7}
<strong>Output</strong>:
1 3 4 7 9</span>
</pre>

<p><span style="font-size:18px"><strong>Example 2:</strong></span></p>

<pre><span style="font-size:18px"><strong>Input</strong>:
N = 10
arr[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}
<strong>Output</strong>:
1 2 3 4 5 6 7 8 9 10</span>
</pre>

<div><br>
<strong><span style="font-size:18px">Your Task:&nbsp;</span></strong></div>

<div><span style="font-size:18px">You don't have to read input or print anything. Your task is to complete the function <strong>bubblesort()</strong> which takes the array and it's size as input and sorts the array using bubble sort algorithm.</span></div>

<div><br>
<span style="font-size:18px"><strong>Expected Time Complexity:</strong>&nbsp;O(N^2).<br>
<strong>Expected Auxiliary Space:</strong>&nbsp;O(1).</span></div>

<p><br>
<span style="font-size:18px"><strong>Constraints:</strong><br>
1 &lt;= N &lt;= 10<sup>3</sup><br>
1 &lt;= arr[i] &lt;= 10<sup>3</sup></span></p>
<p></p>
</div>
70 changes: 70 additions & 0 deletions C++/Geeks4Geeks/Bubble Sort/bubble-sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// { Driver Code Starts
//Initial Template for C++

// C program for implementation of Bubble sort
#include <stdio.h>

// swapping the elements
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}


// } Driver Code Ends
//User function Template for C++

class Solution
{
public:
//Function to sort the array using bubble sort algorithm.
void bubbleSort(int arr[], int n)
{
// Your code here
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
if(arr[i]>arr[j]){
int a = arr[j];
arr[j] = arr[i];
arr[i] = a;
}
}
}
}
};


// { Driver Code Starts.

/* Function to print an array */
void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}

// Driver program to test above functions
int main()
{
int arr[1000],n,T,i;

scanf("%d",&T);

while(T--){

scanf("%d",&n);

for(i=0;i<n;i++)
scanf("%d",&arr[i]);

Solution ob;

ob.bubbleSort(arr, n);
printArray(arr, n);
}
return 0;;
} // } Driver Code Ends
42 changes: 42 additions & 0 deletions C++/Geeks4Geeks/C++ Array (print an element) _ Set 2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# C++ Array (print an element) | Set 2
## School
<div class="problem-statement">
<p></p><p><span style="font-size:18px">Given an array <strong>A[]</strong> of <strong>N</strong> integers and an index <strong>Key</strong>. Your task is to print the element present at index key in the array.</span></p>

<p>&nbsp;</p>

<p><span style="font-size:18px"><strong>Example 1:</strong></span></p>

<pre><span style="font-size:18px"><strong>Input:</strong></span><span style="font-size:18px">
5 2</span>
<span style="font-size:18px">10 20 30 40 50
<strong>Output:</strong>
30</span></pre>

<p>&nbsp;</p>

<p><span style="font-size:18px"><strong>Example 2:</strong></span></p>

<pre><span style="font-size:18px"><strong>Input:</strong></span>
<span style="font-size:18px">7 4</span>
<span style="font-size:18px">10 20 30 40 50 60 70</span>
<span style="font-size:18px"><strong>Output:</strong></span>
<span style="font-size:18px">50</span></pre>

<p>&nbsp;</p>

<p><span style="font-size:18px"><strong>Your Task:&nbsp;&nbsp;</strong><br>
You don't need to read input or print anything. Your task is to complete the function&nbsp;<strong>findElementAtIndex()</strong>&nbsp;which takes the array <strong>A[]</strong>, its size <strong>N </strong>and an integer <strong>Key </strong>as inputs and returns the element present at index Key.</span></p>

<p><br>
<span style="font-size:18px"><strong>Expected Time Complexity:</strong> O(1)<br>
<strong>Expected Auxiliary Space:</strong> O(1)</span></p>

<p>&nbsp;</p>

<p><span style="font-size:18px"><strong>Constraints:</strong><br>
1 ≤ N ≤ 100<br>
0 ≤ Key ≤ N - 1</span><br>
<span style="font-size:18px">1 ≤ A[i] ≤ 100</span></p>
<p></p>
</div>
Loading