diff --git a/C++/Geeks4Geeks/1's Complement/1s-complement.cpp b/C++/Geeks4Geeks/1's Complement/1s-complement.cpp new file mode 100644 index 00000000..06cfad94 --- /dev/null +++ b/C++/Geeks4Geeks/1's Complement/1s-complement.cpp @@ -0,0 +1,38 @@ +// { Driver Code Starts + +#include +using namespace std; + + // } Driver Code Ends + +class Solution{ +public: + string onesComplement(string S,int N){ + //code here + for(int i=0;i>t; + while(t--) + { + int n; + cin>>n; + string s; + cin>>s; + Solution ob; + cout< +

Given an N bit binary number, find the 1's complement of the number. The ones' complement of a binary number is defined as the value obtained by inverting all the bits in the binary representation of the number (swapping 0s for 1s and vice versa).

+ +

Example 1:

+ +
Input:
+N = 3
+S = 101
+Output:
+010
+Explanation:
+We get the output by converting 1's in S
+to 0 and 0s to 1
+
+ +

Example 2:

+ +
Input:
+N = 2
+S = 10
+Output:
+01
+Explanation:
+We get the output by converting 1's in S
+to 0 and 0s to 1
+
+ +


+Your Task:  
+You don't need to read input or print anything. Your task is to complete the function onesComplement() which takes the binary string S, its size N as input parameters and returns 1's complement of S of size N.

+ +

Expected Time Complexity: O(N)
+Expected Space Complexity: O(N)

+ +

Constraints:
+1<=N<=100

+

+ \ No newline at end of file diff --git a/C++/Geeks4Geeks/Binary Search/README.md b/C++/Geeks4Geeks/Binary Search/README.md new file mode 100644 index 00000000..4511abad --- /dev/null +++ b/C++/Geeks4Geeks/Binary Search/README.md @@ -0,0 +1,39 @@ +# Binary Search +## Basic +
+

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.

+ +


+Example 1:

+ +
Input:
+N = 5
+arr[] = {1 2 3 4 5} 
+K = 4
+Output: 3
+Explanation: 4 appears at index 3.
+ +


+Example 2:

+ +
Input:
+N = 5
+arr[] = {11 22 33 44 55} 
+K = 445
+Output: -1
+Explanation: 445 is not present.
+ +


+Your Task:  
+You dont need to read input or print anything. Complete the function binarysearch() 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.

+ +


+Expected Time Complexity: O(LogN)
+Expected Auxiliary Space: O(LogN) if solving recursively and O(1) otherwise.

+ +


+Constraints:
+1 <= N <= 104
+1 <= arr[i] <= 104

+

+
\ No newline at end of file diff --git a/C++/Geeks4Geeks/Binary number to decimal number/README.md b/C++/Geeks4Geeks/Binary number to decimal number/README.md new file mode 100644 index 00000000..0a20849c --- /dev/null +++ b/C++/Geeks4Geeks/Binary number to decimal number/README.md @@ -0,0 +1,32 @@ +# Binary number to decimal number +## Basic +
+

Given a Binary Number B, find its decimal equivalent.

+ +

Example 1:

+ +
Input: B = 10001000
+Output: 136
+
+ +

Example 2:

+ +
Input: B = 101100
+Output: 44
+
+ +

 

+ +

Your Task:
+You don't need to read or print anything. Your task is to complete the function binary_to_decimal() which takes the binary number as string input parameter and returns its decimal equivalent.

+ +

Expected Time Complexity: O(K * Log(K)) where K is number of bits in binary number.
+Expected Space Complexity: O(1)

+ +

Constraints:
+1 <= number of bits in binary number  <= 16

+

+
\ No newline at end of file diff --git a/C++/Geeks4Geeks/Binary number to decimal number/binary-number-to-decimal-number.cpp b/C++/Geeks4Geeks/Binary number to decimal number/binary-number-to-decimal-number.cpp new file mode 100644 index 00000000..5a84409c --- /dev/null +++ b/C++/Geeks4Geeks/Binary number to decimal number/binary-number-to-decimal-number.cpp @@ -0,0 +1,36 @@ +// { Driver Code Starts +#include +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 \ No newline at end of file diff --git a/C++/Geeks4Geeks/Bitonic Point/README.md b/C++/Geeks4Geeks/Bitonic Point/README.md new file mode 100644 index 00000000..2b9ff98a --- /dev/null +++ b/C++/Geeks4Geeks/Bitonic Point/README.md @@ -0,0 +1,35 @@ +# Bitonic Point +## Easy +
+

Given an array arr of n elements which is first increasing and then may be decreasing, find the maximum element in the array.
+Note: If the array is increasing then just print then last element will be the maximum value.

+ +

Example 1:

+ +
Input: 
+n = 9
+arr[] = {1,15,25,45,42,21,17,12,11}
+Output: 45
+Explanation: Maximum element is 45.
+ +

Example 2:

+ +
Input: 
+n = 5
+arr[] = {1, 45, 47, 50, 5}
+Output: 50
+Explanation: Maximum element is 50.
+ +

Your Task:  
+You don't need to read input or print anything. Your task is to complete the function findMaximum() which takes the array arr[], and n as parameters and returns an integer denoting the answer.
+
+Expected Time Complexity: O(logn)
+Expected Auxiliary Space: O(1)

+ +

Constraints:
+3 ≤ n ≤ 106
+1 ≤ arri ≤ 106

+ +

 

+

+
\ No newline at end of file diff --git a/C++/Geeks4Geeks/Bubble Sort/README.md b/C++/Geeks4Geeks/Bubble Sort/README.md new file mode 100644 index 00000000..1fd5fed1 --- /dev/null +++ b/C++/Geeks4Geeks/Bubble Sort/README.md @@ -0,0 +1,37 @@ +# Bubble Sort +## Easy +
+

Given an Integer N and a list arr. Sort the array using bubble sort algorithm.
+Example 1:

+ +
Input: 
+N = 5
+arr[] = {4, 1, 3, 9, 7}
+Output: 
+1 3 4 7 9
+
+ +

Example 2:

+ +
Input:
+N = 10 
+arr[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}
+Output: 
+1 2 3 4 5 6 7 8 9 10
+
+ +

+Your Task: 
+ +
You don't have to read input or print anything. Your task is to complete the function bubblesort() which takes the array and it's size as input and sorts the array using bubble sort algorithm.
+ +

+Expected Time Complexity: O(N^2).
+Expected Auxiliary Space: O(1).
+ +


+Constraints:
+1 <= N <= 103
+1 <= arr[i] <= 103

+

+
\ No newline at end of file diff --git a/C++/Geeks4Geeks/Bubble Sort/bubble-sort.cpp b/C++/Geeks4Geeks/Bubble Sort/bubble-sort.cpp new file mode 100644 index 00000000..36917d4e --- /dev/null +++ b/C++/Geeks4Geeks/Bubble Sort/bubble-sort.cpp @@ -0,0 +1,70 @@ +// { Driver Code Starts +//Initial Template for C++ + +// C program for implementation of Bubble sort +#include + +// 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;iarr[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 +

Given an array A[] of N integers and an index Key. Your task is to print the element present at index key in the array.

+ +

 

+ +

Example 1:

+ +
Input:
+5 2
+10 20 30 40 50
+Output:
+30
+ +

 

+ +

Example 2:

+ +
Input:
+7 4
+10 20 30 40 50 60 70
+Output:
+50
+ +

 

+ +

Your Task:  
+You don't need to read input or print anything. Your task is to complete the function findElementAtIndex() which takes the array A[], its size N and an integer Key as inputs and returns the element present at index Key.

+ +


+Expected Time Complexity: O(1)
+Expected Auxiliary Space: O(1)

+ +

 

+ +

Constraints:
+1 ≤ N ≤ 100
+0 ≤ Key ≤ N - 1

+1 ≤ A[i] ≤ 100

+

+ \ No newline at end of file diff --git a/C++/Geeks4Geeks/C++ Array (print an element) _ Set 2/c-array-print-an-element-set-2.cpp b/C++/Geeks4Geeks/C++ Array (print an element) _ Set 2/c-array-print-an-element-set-2.cpp new file mode 100644 index 00000000..201e540a --- /dev/null +++ b/C++/Geeks4Geeks/C++ Array (print an element) _ Set 2/c-array-print-an-element-set-2.cpp @@ -0,0 +1,36 @@ +// { Driver Code Starts +#include +using namespace std; + + // } Driver Code Ends + + +class Solution{ + public: + int findElementAtIndex(int a[], int n, int key) + { + // Your code goes here + for(int i =0;i> t; + while (t--) { + int n, key; + cin >> n >> key; + int a[n]; + for (int i = 0; i < n; i++) cin >> a[i]; + Solution ob; + cout << ob.findElementAtIndex(a, n, key) << endl; + } + + return 0; +} // } Driver Code Ends \ No newline at end of file diff --git a/C++/Geeks4Geeks/C++ Arrays (Sum of array) _ Set 1/README.md b/C++/Geeks4Geeks/C++ Arrays (Sum of array) _ Set 1/README.md new file mode 100644 index 00000000..290461fb --- /dev/null +++ b/C++/Geeks4Geeks/C++ Arrays (Sum of array) _ Set 1/README.md @@ -0,0 +1,40 @@ +# C++ Arrays (Sum of array) | Set 1 +## School +
+

Given an array of N integers. Your task is to print the sum of all of the integers.

+ +

Example 1:

+ +
Input:
+4
+1 2 3 4
+Output:
+10
+ +

 

+ +

Example 2:

+ +
Input:
+6
+5 8 3 10 22 45
+Output:
+93
+ +

 

+ +

Your Task:  
+You don't need to read input or print anything. Your task is to complete the function getSum() which takes the array A[] and its size N as inputs and returns the sum of array in a new line.

+ +


+Expected Time Complexity: O(N)
+Expected Auxiliary Space: O(1)

+ +

 

+ +

Constraints:
+1 ≤ N ≤ 106

+0 ≤ Arr[i] ≤ 200

+

+
\ No newline at end of file diff --git a/C++/Geeks4Geeks/C++ Arrays (Sum of array) _ Set 1/c-arrays-sum-of-array-set-1.cpp b/C++/Geeks4Geeks/C++ Arrays (Sum of array) _ Set 1/c-arrays-sum-of-array-set-1.cpp new file mode 100644 index 00000000..679de6c3 --- /dev/null +++ b/C++/Geeks4Geeks/C++ Arrays (Sum of array) _ Set 1/c-arrays-sum-of-array-set-1.cpp @@ -0,0 +1,34 @@ +// { Driver Code Starts +#include +using namespace std; + + // } Driver Code Ends + + +class Solution{ + public: + int getSum(int a[], int n) { + // Your code goes here + int sum = 0; + for(int i=0;i> t; + while (t--) { + int n; + cin >> n; + int a[n]; + for (int i = 0; i < n; i++) cin >> a[i]; + Solution ob; + cout << ob.getSum(a, n) << endl; + } + + return 0; +} // } Driver Code Ends \ No newline at end of file diff --git a/C++/Geeks4Geeks/Check if Linked List is Palindrome/README.md b/C++/Geeks4Geeks/Check if Linked List is Palindrome/README.md new file mode 100644 index 00000000..f2edfb81 --- /dev/null +++ b/C++/Geeks4Geeks/Check if Linked List is Palindrome/README.md @@ -0,0 +1,36 @@ +# Check if Linked List is Palindrome +## Easy +
+

Given a singly linked list of size N of integers. The task is to check if the given linked list is palindrome or not.

+ +

Example 1:

+ +
Input:
+N = 3
+value[] = {1,2,1}
+Output: 1
+Explanation: The given linked list is
+1 2 1 , which is a palindrome and
+Hence, the output is 1.
+
+ +

Example 2:

+ +
Input:
+N = 4
+value[] = {1,2,3,4}
+Output: 0
+Explanation: The given linked list
+is 1 2 3 4 , which is not a palindrome
+and Hence, the output is 0.
+ +

Your Task:
+The task is to complete the function isPalindrome() which takes head as reference as the only parameter and returns true or false if linked list is palindrome or not respectively.

+ +

Expected Time Complexity: O(N)
+Expected Auxialliary Space Usage: O(1)  (ie, you should not use the recursive stack space as well)

+ +

Constraints:
+1 <= N <= 105

+

+
\ No newline at end of file diff --git a/C++/Geeks4Geeks/Check if Linked List is Palindrome/check-if-linked-list-is-palindrome.cpp b/C++/Geeks4Geeks/Check if Linked List is Palindrome/check-if-linked-list-is-palindrome.cpp new file mode 100644 index 00000000..aee06c1a --- /dev/null +++ b/C++/Geeks4Geeks/Check if Linked List is Palindrome/check-if-linked-list-is-palindrome.cpp @@ -0,0 +1,83 @@ +// { Driver Code Starts +#include +#include +#include +#include +using namespace std; +/* Link list Node */ +struct Node { + int data; + struct Node *next; + Node(int x) { + data = x; + next = NULL; + } +}; + + + + + // } Driver Code Ends +/* +struct Node { + int data; + struct Node *next; + Node(int x) { + data = x; + next = NULL; + } +}; +*/ + +class Solution{ + public: + bool isPalindrome(Node *head){ + Node* slow = head; + stack st; + while(slow!=NULL){ + st.push(slow->data); + slow = slow->next; + } + slow = head; + while(slow!=NULL){ + if(slow->data!=st.top()){ + return false; + } + st.pop(); + slow = slow->next; + } + return true; + } +}; + + + +// { Driver Code Starts. +/* Driver program to test above function*/ +int main() +{ + int T,i,n,l,firstdata; + cin>>T; + while(T--) + { + + struct Node *head = NULL, *tail = NULL; + cin>>n; + // taking first data of LL + cin>>firstdata; + head = new Node(firstdata); + tail = head; + // taking remaining data of LL + for(i=1;i>l; + tail->next = new Node(l); + tail = tail->next; + } + Solution obj; + cout< +

Given elements of a stack, clone the stack without using extra space.

+ +


+Example 1:

+ +
Input:
+N = 10
+st[] = {1, 1, 2, 2, 3, 4, 5, 5, 6, 7}
+Output:
+1 
+
+ +


+Your Task:  
+You don't need to read input or print anything. Your task is to complete the function clonestack() which takes the input stack st[], an empty stack cloned[], you have to clone the stack st into stack cloned.
+The driver code itself prints 1 in the output if the stack st is cloned properly and prints 0 otherwise.

+ +


+Expected Time Complexity: O(N*N)
+Expected Auxiliary Space: O(1)

+ +

Constraints:
+1 <= N <= 1000

+1<= st[i] <= 105

+

+ \ No newline at end of file diff --git a/C++/Geeks4Geeks/Clone a stack without using extra space/clone-a-stack-without-using-extra-space.cpp b/C++/Geeks4Geeks/Clone a stack without using extra space/clone-a-stack-without-using-extra-space.cpp new file mode 100644 index 00000000..12b99f36 --- /dev/null +++ b/C++/Geeks4Geeks/Clone a stack without using extra space/clone-a-stack-without-using-extra-space.cpp @@ -0,0 +1,64 @@ +// { Driver Code Starts +//Initial template for C++ + +#include +using namespace std; + + // } Driver Code Ends +//User function Template for C++ + +class Solution{ + public: + void clonestack(stack st, stack& cloned) + { + //code here + if(st.empty()){ + return; + } + int a = st.top(); + st.pop(); + clonestack(st, cloned); + cloned.push(a); + return; + } +}; + +// { Driver Code Starts. +int main() +{ + int t; + cin>>t; + while(t--) + { + int N; + cin >> N; + int arr[N]; + stack st; + vector copy; + for(int i = 0; i < N; i++){ + cin >> arr[i]; + st.push(arr[i]); + copy.push_back(arr[i]); + } + + reverse(copy.begin(), copy.end()); + + Solution ob; + stack cloned; + ob.clonestack(st,cloned); + vector check; + while(cloned.size()) + { + check.push_back(cloned.top()); + cloned.pop(); + } + + int flag = 0; + if(check!=copy) + flag = 1; + + cout<<1-flag< +

Given a singly linked list. The task is to find the length of the linked list, where length is defined as the number of nodes in the linked list.

+ +

Example 1:

+ +
Input:
+LinkedList: 1->2->3->4->5
+Output: 5
+Explanation: Count of nodes in the 
+linked list is 5, which is its length.
+
+ +

Example 2:

+ +
Input:
+LinkedList: 2->4->6->7->5->1->0
+Output: 7
+Explanation: Count of nodes in the
+linked list is 7. Hence, the output
+is 7.
+ +

Your Task:
+Your task is to complete the given function getCount(), which takes a head reference as an argument and should return the length of the linked list.

+ +

Expected Time Complexity : O(N)
+Expected Auxilliary Space : O(1)

+ +

Constraints:
+1 <= N <= 105
+1 <= value <= 103

+ +

 

+

+ \ No newline at end of file diff --git a/C++/Geeks4Geeks/Count the triplets/README.md b/C++/Geeks4Geeks/Count the triplets/README.md new file mode 100644 index 00000000..c1c43283 --- /dev/null +++ b/C++/Geeks4Geeks/Count the triplets/README.md @@ -0,0 +1,35 @@ +# Count the triplets +## Easy +
+

Given an array of distinct integers. The task is to count all the triplets such that sum of two elements equals the third element.
+ +
 
+ +

Example 1:

+ +
Input:
+N = 4
+arr[] = {1, 5, 3, 2}
+Output: 2
+Explanation: There are 2 triplets: 
+1 + 2 = 3 and 3 +2 = 5 
+ +

​Example 2:

+ +
Input: 
+N = 3
+arr[] = {2, 3, 4}
+Output: 0
+Explanation: No such triplet exits
+ +

Your Task:  
+You don't need to read input or print anything. Your task is to complete the function countTriplet() which takes the array arr[] and N as inputs and returns the triplet count

+
+Expected Time Complexity: O(N2)
+Expected Auxiliary Space: O(1)

+
+Constraints:
+1 ≤ N ≤ 103
+1 ≤ arr[i] ≤ 105

+

+
\ No newline at end of file diff --git a/C++/Geeks4Geeks/Count the triplets/count-the-triplets.cpp b/C++/Geeks4Geeks/Count the triplets/count-the-triplets.cpp new file mode 100644 index 00000000..3db1482d --- /dev/null +++ b/C++/Geeks4Geeks/Count the triplets/count-the-triplets.cpp @@ -0,0 +1,69 @@ +// { Driver Code Starts +#include +using namespace std; + + + + // } Driver Code Ends +//User function template for C++ +class Solution{ +public: + bool search(int arr[], int n, int key){ + int s = 0, e = n-1; + if(arr[s] == key or arr[e] == key) + return true; + while(s<=e){ + int mid = (s+e)/2; + if(arr[mid] == key) + return true; + else if(arr[mid]>key) + e = mid-1; + else if(arr[mid]> t; + while (t--) + { + int n; + cin >> n; + + int arr[n]; + for(int i = 0; i < n; i++) + cin >> arr[i]; + + Solution ob; + cout << ob.countTriplet(arr, n) << "\n"; + + } + + return 0; +} // } Driver Code Ends \ No newline at end of file diff --git a/C++/Geeks4Geeks/Cyclically rotate an array by one/README.md b/C++/Geeks4Geeks/Cyclically rotate an array by one/README.md new file mode 100644 index 00000000..e77b3050 --- /dev/null +++ b/C++/Geeks4Geeks/Cyclically rotate an array by one/README.md @@ -0,0 +1,41 @@ +# Cyclically rotate an array by one +## Basic +
+

Given an array, rotate the array by one position in clock-wise direction.

+ +

Example 1:

+ +
Input:
+N = 5
+A[] = {1, 2, 3, 4, 5}
+Output:
+5 1 2 3 4
+ +

 

+ +

Example 2:

+ +
Input:
+N = 8
+A[] = {9, 8, 7, 6, 4, 2, 1, 3}
+Output:
+3 9 8 7 6 4 2 1
+ +

 

+ +

Your Task:  
+You don't need to read input or print anything. Your task is to complete the function rotate() which takes the array A[] and its size N as inputs and modify the array.

+ +

 

+ +

Expected Time Complexity: O(N)
+Expected Auxiliary Space: O(1)

+ +

 

+ +

Constraints:
+1<=N<=105
+0<=a[i]<=105

+

+
\ No newline at end of file diff --git a/C++/Geeks4Geeks/Cyclically rotate an array by one/cyclically-rotate-an-array-by-one.c b/C++/Geeks4Geeks/Cyclically rotate an array by one/cyclically-rotate-an-array-by-one.c new file mode 100644 index 00000000..53b470a3 --- /dev/null +++ b/C++/Geeks4Geeks/Cyclically rotate an array by one/cyclically-rotate-an-array-by-one.c @@ -0,0 +1,40 @@ +// { Driver Code Starts +#include + +void rotate(int arr[], int n); + +int main() +{ + int t; + scanf("%d",&t); + while(t--) + { + int n; + scanf("%d",&n); + int a[n] , i; + for(i=0;i +

Given a stack with push(), pop(), empty() operations, delete the middle of the stack without using any additional data structure.
+Middle: ceil(size_of_stack/2.0)

+ +

Example 1:

+ +
Input: 
+Stack = {1, 2, 3, 4, 5}
+Output:
+ModifiedStack = {1, 2, 4, 5}
+Explanation:
+As the number of elements is 5 , 
+hence the middle element will be the 3rd
+element which is deleted
+
+ +

Example 2:

+ +
Input: 
+Stack = {1 2 3 4}
+Output:
+ModifiedStack = {1 3 4}
+Explanation:
+As the number of elements is 4 , 
+hence the middle element will be the 2nd
+element which is deleted
+ +

 

+ +

Your Task:
+You don't need to read input or print anything. Complete the function deleteMid() which takes the stack and its size as input parameters and modifies the stack in-place.
+Note: The output shows the stack from top to bottom.

+ +


+Expected Time Complexity: O(N)
+Expected Auxiliary Space: O(N)

+ +


+Constraints:
+2 ≤ size of stack ≤ 100

+

+ \ No newline at end of file diff --git a/C++/Geeks4Geeks/Delete middle element of a stack/delete-middle-element-of-a-stack.java b/C++/Geeks4Geeks/Delete middle element of a stack/delete-middle-element-of-a-stack.java new file mode 100644 index 00000000..e639298a --- /dev/null +++ b/C++/Geeks4Geeks/Delete middle element of a stack/delete-middle-element-of-a-stack.java @@ -0,0 +1,68 @@ +// { Driver Code Starts +//Initial template for Java + +import java.io.*; +import java.util.*; + +class GFG { + public static void main (String[] args) { + Scanner sc=new Scanner(System.in); + int t=sc.nextInt(); + + while(t-->0) + { + int sizeOfStack =sc.nextInt(); + Stack myStack=new Stack<>(); + + //adding elements to the stack + for(int i=0;ist,int sizeOfStack) + { + int middle=(int)Math.ceil(((sizeOfStack)/2.0)); + + if(sizeOfStack%2==0) + middle=middle+1; + Stackar=new Stack(); + + for(int i=0;i +

You are given a pointer/ reference to the node which is to be deleted from the linked list of nodes. The task is to delete the node. Pointer/ reference to head node is not given. 
+Note: No head reference is given to you. It is guaranteed that the node to be deleted is not a tail node in the linked list.

+ +

Example 1:

+ +
Input:
+N = 2
+value[] = {1,2}
+node = 1
+Output: 2
+Explanation: After deleting 1 from the
+linked list, we have remaining nodes
+as 2.
+
+ +

Example 2:

+ +
Input:
+N = 4
+value[] = {10,20,4,30}
+node = 20
+Output: 10 4 30
+Explanation: After deleting 20 from
+the linked list, we have remaining
+nodes as 10, 4 and 30.
+ +

Your Task:
+You only need to complete the function deleteNode that takes reference to the node that needs to be deleted. The printing is done automatically by the driver code.

+ +

Expected Time Complexity : O(1)
+Expected Auxilliary Space : O(1)

+ +

Constraints:
+1 <= N <= 103

+

+ \ No newline at end of file diff --git a/C++/Geeks4Geeks/Detect Loop in linked list/README.md b/C++/Geeks4Geeks/Detect Loop in linked list/README.md new file mode 100644 index 00000000..e107c4d9 --- /dev/null +++ b/C++/Geeks4Geeks/Detect Loop in linked list/README.md @@ -0,0 +1,42 @@ +# Detect Loop in linked list +## Easy +
+

Given a linked list of N nodes. The task is to check if the linked list has a loop. Linked list can contain self loop.

+ +

Example 1:

+ +
Input:
+N = 3
+value[] = {1,3,4}
+x = 2
+Output: True
+Explanation: In above test case N = 3.
+The linked list with nodes N = 3 is
+given. Then value of x=2 is given which
+means last node is connected with xth
+node of linked list. Therefore, there
+exists a loop.
+ +

Example 2:

+ +
Input:
+N = 4
+value[] = {1,8,3,4}
+x = 0
+Output: False
+Explanation: For N = 4 ,x = 0 means
+then lastNode->next = NULL, then
+the Linked list does not contains
+any loop.
+ +

Your Task:
+The task is to complete the function detectloop() which contains reference to the head as only argument. This function should return true if linked list contains loop, else return false.

+ +

Expected Time Complexity: O(N)
+Expected Auxiliary Space: O(1)

+ +

Constraints:
+1 ≤ N ≤ 104
+1 ≤ Data on Node ≤ 103

+

+
\ No newline at end of file diff --git a/C++/Geeks4Geeks/Detect Loop in linked list/detect-loop-in-linked-list.cpp b/C++/Geeks4Geeks/Detect Loop in linked list/detect-loop-in-linked-list.cpp new file mode 100644 index 00000000..47a08663 --- /dev/null +++ b/C++/Geeks4Geeks/Detect Loop in linked list/detect-loop-in-linked-list.cpp @@ -0,0 +1,99 @@ +// { Driver Code Starts +//Initial template code for C++ + +#include +using namespace std; + +struct Node +{ + int data; + Node* next; + + Node(int val) + { + data = val; + next = NULL; + } +}; + +void loopHere(Node* head, Node* tail, int position) +{ + if(position==0) return; + + Node* walk = head; + for(int i=1; inext; + tail->next = walk; +} + + + // } Driver Code Ends +//User function template for C++ + +/* + +struct Node +{ + int data; + struct Node *next; + Node(int x) { + data = x; + next = NULL; + } + +*/ +class Solution +{ + public: + //Function to check if the linked list has a loop. + bool detectLoop(Node* head) + { + // your code here + Node* fast = head, *slow = head; + while(fast!=NULL and fast->next!=NULL){ + fast = fast->next->next; + slow = slow->next; + if(fast == slow){ + return true; + } + } + return false; + } +}; + + +// { Driver Code Starts. + +int main() +{ + int t; + cin>>t; + while(t--) + { + int n, num; + cin>>n; + + Node *head, *tail; + cin>> num; + head = tail = new Node(num); + + for(int i=0 ; i> num; + tail->next = new Node(num); + tail = tail->next; + } + + int pos; + cin>> pos; + loopHere(head,tail,pos); + + Solution ob; + if(ob.detectLoop(head) ) + cout<< "True\n"; + else + cout<< "False\n"; + } + return 0; +} + // } Driver Code Ends \ No newline at end of file diff --git a/C++/Geeks4Geeks/Equilibrium Point/README.md b/C++/Geeks4Geeks/Equilibrium Point/README.md new file mode 100644 index 00000000..8d6e4783 --- /dev/null +++ b/C++/Geeks4Geeks/Equilibrium Point/README.md @@ -0,0 +1,43 @@ +# Equilibrium Point +## Easy +
+

Given an array A of n positive numbers. The task is to find the first Equilibium Point in the array. 
+Equilibrium Point in an array is a position such that the sum of elements before it is equal to the sum of elements after it.

+ +

Example 1:

+ +
Input: 
+n = 5 
+A[] = {1,3,5,2,2} 
+Output: 3 
+Explanation: For second test case 
+equilibrium point is at position 3 
+as elements before it (1+3) = 
+elements after it (2+2). 
+
+ +

 

+ +

Example 2:

+ +
Input:
+n = 1
+A[] = {1}
+Output: 1
+Explanation:
+Since its the only element hence
+its the only equilibrium point.
+ +

 

+ +

Your Task:
+The task is to complete the function equilibriumPoint() which takes the array and n as input parameters and returns the point of equilibrium. Return -1 if no such point exists.

+ +

Expected Time Complexity: O(n)
+Expected Auxiliary Space: O(1)

+ +

Constraints:
+1 <= n <= 106
+1 <= A[i] <= 108

+

+
\ No newline at end of file diff --git a/C++/Geeks4Geeks/Equilibrium Point/equilibrium-point.cpp b/C++/Geeks4Geeks/Equilibrium Point/equilibrium-point.cpp new file mode 100644 index 00000000..8a2a93fe --- /dev/null +++ b/C++/Geeks4Geeks/Equilibrium Point/equilibrium-point.cpp @@ -0,0 +1,83 @@ +// { Driver Code Starts +#include +using namespace std; + + + // } Driver Code Ends +class Solution{ + public: + // Function to find equilibrium point in the array. + // a: input array + // n: size of array + int equilibriumPoint(long long a[], int n) { + + // Your code here + // M! + // for(int i=0;i> t; + + while (t--) { + long long n; + + //taking input n + cin >> n; + long long a[n]; + + //adding elements to the array + for (long long i = 0; i < n; i++) { + cin >> a[i]; + } + + Solution ob; + + //calling equilibriumPoint() function + cout << ob.equilibriumPoint(a, n) << endl; + } + return 0; +} + // } Driver Code Ends \ No newline at end of file diff --git a/C++/Geeks4Geeks/Find Transition Point/README.md b/C++/Geeks4Geeks/Find Transition Point/README.md new file mode 100644 index 00000000..91e45a68 --- /dev/null +++ b/C++/Geeks4Geeks/Find Transition Point/README.md @@ -0,0 +1,39 @@ +# Find Transition Point +## Easy +
+

Given a sorted array containing only 0s and 1s, find the transition point. 

+ +


+Example 1:

+ +
Input:
+N = 5
+arr[] = {0,0,0,1,1}
+Output: 3
+Explanation: index 3 is the transition 
+point where 1 begins.
+ +


+Example 2:

+ +
Input:
+N = 4
+arr[] = {0,0,0,0}
+Output: -1
+Explanation: Since, there is no "1",
+the answer is -1.
+ +


+Your Task:
+You don't need to read input or print anything. The task is to complete the function transitionPoint() that takes array and N as input parameters and returns the 0 based index of the position where "0" ends and "1" begins. If array does not have any 1s, return -1. If array does not have any 0s, return 0.

+ +


+Expected Time Complexity: O(LogN)
+Expected Auxiliary Space: O(1)

+ +


+Constraints:
+1 ≤ N ≤ 500000
+0 ≤ arr[i] ≤ 1

+

+
\ No newline at end of file diff --git a/C++/Geeks4Geeks/Find duplicates in an array/README.md b/C++/Geeks4Geeks/Find duplicates in an array/README.md new file mode 100644 index 00000000..02c69005 --- /dev/null +++ b/C++/Geeks4Geeks/Find duplicates in an array/README.md @@ -0,0 +1,38 @@ +# Find duplicates in an array +## Easy +
+

Given an array a[] of size N which contains elements from 0 to N-1, you need to find all the elements occurring more than once in the given array.

+ +

Example 1:

+ +
Input:
+N = 4
+a[] = {0,3,1,2}
+Output: -1
+Explanation: N=4 and all elements from 0
+to (N-1 = 3) are present in the given
+array. Therefore output is -1.
+
+ +

Example 2:

+ +
Input:
+N = 5
+a[] = {2,3,1,2,3}
+Output: 2 3 
+Explanation: 2 and 3 occur more than once
+in the given array.
+ +

Your Task:
+Complete the function duplicates() which takes array a[] and n as input as parameters and returns a list of elements that occur more than once in the given array in sorted manner. If no such element is found, return list containing [-1]. 

+ +

Expected Time Complexity: O(n).
+Expected Auxiliary Space: O(n).
+Note : The extra space is only for the array to be returned.
+Try and perform all operation withing the provided array. 

+ +

Constraints:
+1 <= N <= 105
+0 <= A[i] <= N-1, for each valid i

+

+
\ No newline at end of file diff --git a/C++/Geeks4Geeks/Find duplicates in an array/find-duplicates-in-an-array.cpp b/C++/Geeks4Geeks/Find duplicates in an array/find-duplicates-in-an-array.cpp new file mode 100644 index 00000000..cc9f2e77 --- /dev/null +++ b/C++/Geeks4Geeks/Find duplicates in an array/find-duplicates-in-an-array.cpp @@ -0,0 +1,44 @@ +// { Driver Code Starts +#include +using namespace std; + + // } Driver Code Ends +class Solution{ + public: + vector duplicates(int arr[], int n) { + // code here + unordered_map mp; + vector b; + for(int i=0;i1){ + b.push_back(x.first); + } + } + sort(b.begin(),b.end()); + if(!b.size()){ + b.push_back(-1); + } + return b; + } +}; + + +// { Driver Code Starts. +int main() { + int t; + cin >> t; + while (t-- > 0) { + int n; + cin >> n; + int a[n]; + for (int i = 0; i < n; i++) cin >> a[i]; + Solution obj; + vector ans = obj.duplicates(a, n); + for (int i : ans) cout << i << ' '; + cout << endl; + } + return 0; +} + // } Driver Code Ends \ No newline at end of file diff --git a/C++/Geeks4Geeks/Find minimum and maximum element in an array/README.md b/C++/Geeks4Geeks/Find minimum and maximum element in an array/README.md new file mode 100644 index 00000000..e8cda976 --- /dev/null +++ b/C++/Geeks4Geeks/Find minimum and maximum element in an array/README.md @@ -0,0 +1,42 @@ +# Find minimum and maximum element in an array +## Basic +
+

Given an array A of size N of integers. Your task is to find the minimum and maximum elements in the array.

+ +

 

+ +

Example 1:

+ +
Input:
+N = 6
+A[] = {3, 2, 1, 56, 10000, 167}
+Output:
+min = 1, max =  10000
+ +

 

+ +

Example 2:

+ +
Input:
+N = 5
+A[]  = {1, 345, 234, 21, 56789}
+Output:
+min = 1, max = 56789
+ +

 

+ +

Your Task:  
+You don't need to read input or print anything. Your task is to complete the function getMinMax() which takes the array A[] and its size N as inputs and returns the minimum and maximum element of the array.

+ +

 

+ +

Expected Time Complexity: O(N)
+Expected Auxiliary Space: O(1)

+ +

 

+ +

Constraints:
+1 <= N <= 105
+1 <= Ai <=1012

+

+
\ No newline at end of file diff --git a/C++/Geeks4Geeks/Find minimum and maximum element in an array/find-minimum-and-maximum-element-in-an-array.cpp b/C++/Geeks4Geeks/Find minimum and maximum element in an array/find-minimum-and-maximum-element-in-an-array.cpp new file mode 100644 index 00000000..1d54052d --- /dev/null +++ b/C++/Geeks4Geeks/Find minimum and maximum element in an array/find-minimum-and-maximum-element-in-an-array.cpp @@ -0,0 +1,35 @@ +// { Driver Code Starts +#include +using namespace std; +#define ll long long + +pair getMinMax(long long a[], int n) ; + +int main() { + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + ll a[n]; + for (int i = 0; i < n; i++) cin >> a[i]; + + pair pp = getMinMax(a, n); + + cout << pp.first << " " << pp.second << endl; + } + return 0; +}// } Driver Code Ends + + +pair getMinMax(long long a[], int n) { + pair p; + long long maxi = a[0], mini = a[0]; + for(int i=1;i +

Given a singly linked list of N nodes.
+The task is to find the middle of the linked list. For example, if the linked list is
+1-> 2->3->4->5, then the middle node of the list is 3.
+If there are two middle nodes(in case, when N is even), print the second middle element.
+For example, if the linked list given is 1->2->3->4->5->6, then the middle node of the list is 4.

+ +

Example 1:

+ +
Input:
+LinkedList: 1->2->3->4->5
+Output: 3 
+Explanation: 
+Middle of linked list is 3.
+
+ +

Example 2: 

+ +
Input:
+LinkedList: 2->4->6->7->5->1
+Output: 7 
+Explanation: 
+Middle of linked list is 7.
+
+ +

Your Task:
+The task is to complete the function getMiddle() which takes a head reference as the only argument and should return the data at the middle node of the linked list.

+ +

Expected Time Complexity: O(N).
+Expected Auxiliary Space: O(1).

+ +

Constraints:
+1 <= N <= 5000

+

+ \ No newline at end of file diff --git a/C++/Geeks4Geeks/Finding middle element in a linked list/finding-middle-element-in-a-linked-list.cpp b/C++/Geeks4Geeks/Finding middle element in a linked list/finding-middle-element-in-a-linked-list.cpp new file mode 100644 index 00000000..c1b34438 --- /dev/null +++ b/C++/Geeks4Geeks/Finding middle element in a linked list/finding-middle-element-in-a-linked-list.cpp @@ -0,0 +1,103 @@ +// { Driver Code Starts +//Initial template for C++ + +#include +using namespace std; + +struct Node +{ + int data; + struct Node* next; + + Node(int x){ + data = x; + next = NULL; + } +}; +void printList(Node* node) +{ + while (node != NULL) { + cout << node->data <<" "; + node = node->next; + } + cout<<"\n"; +} + + + // } Driver Code Ends +/* Link list Node +struct Node { + int data; + Node* next; + + Node(int x){ + data = x; + next = NULL; + } + +}; */ +class Solution{ + public: + /* Should return data of middle node. If linked list is empty, then -1*/ + int count(Node *head){ + int cnt = 0; + Node* temp = head; + while(temp!=NULL){ + cnt++; + temp = temp->next; + } + return cnt; + } + int getMiddle(Node *head) + { + // Your code here + if(!head){ + return -1; + } + int len = count(head); + Node* temp = head; + if(len%2 == 0){ + len = (len/2); + ++len; + while(--len){ + temp = temp->next; + } + } + else{ + len = len/2; + while(len--){ + temp = temp->next; + } + } + + return temp->data; + } +}; + + +// { Driver Code Starts. + +int main() { + //code + int t; + cin>>t; + while(t--){ + int N; + cin>>N; + int data; + cin>>data; + struct Node *head = new Node(data); + struct Node *tail = head; + for (int i = 0; i < N-1; ++i) + { + cin>>data; + tail->next = new Node(data); + tail = tail->next; + } + + Solution ob; + cout << ob.getMiddle(head) << endl; + } + return 0; +} + // } Driver Code Ends \ No newline at end of file diff --git a/C++/Geeks4Geeks/Game with nos/README.md b/C++/Geeks4Geeks/Game with nos/README.md new file mode 100644 index 00000000..f27f0363 --- /dev/null +++ b/C++/Geeks4Geeks/Game with nos/README.md @@ -0,0 +1,45 @@ +# Game with nos +## Basic +
+

 You are given an array arr[], you have to re-construct an array arr[].
+The values in arr[] are obtained by doing Xor of consecutive elements in the array.

+ +

Example 1:

+ +
​Input : arr[ ] = {10, 11, 1, 2, 3}
+Output : 1 10 3 1 3
+Explanation:
+At index 0, arr[0] xor arr[1] = 1
+At index 1, arr[1] xor arr[2] = 10
+At index 2, arr[2] xor arr[3] = 3
+...
+At index 4, No element is left So, it will remain as
+it is.
+New Array will be {1, 10, 3, 1, 3}.
+
+ +


+​Example 2:

+ +
Input : arr[ ] = {5, 9, 7, 6} 
+Output :  12 14 1 6 
+ +

 

+ +

Your Task:
+This is a function problem. The input is already taken care of by the driver code. You only need to complete the function game_with_number() that takes an array (arr), sizeOfArray (n), and return the array re-constructed array arr. The driver code takes care of the printing.

+ +

Expected Time Complexity: O(N).
+Expected Auxiliary Space: O(1).

+ +

 

+ +

Constraints:

+ +

1 ≤ N ≤ 105

+ +

1 ≤ arr[i] ≤ 107

+ +

 

+

+
\ No newline at end of file diff --git a/C++/Geeks4Geeks/Geek-onacci Number/README.md b/C++/Geeks4Geeks/Geek-onacci Number/README.md new file mode 100644 index 00000000..d0cdb1dc --- /dev/null +++ b/C++/Geeks4Geeks/Geek-onacci Number/README.md @@ -0,0 +1,29 @@ +# Geek-onacci Number +## Easy +
+

Geek created a random series and given a name geek-onacci series. Given four integers A, B, C, N. A, B, C represents the first three numbers of geek-onacci series. Find the Nth number of the series. The nth number of geek-onacci series is a sum of the last three numbers (summation of N-1th, N-2th, and N-3th geek-onacci numbers)

+ +

Input:
+1. The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
+2. The first line of each test case contains four space-separated integers A, B, C, and N.

+
+Output: For each test case, print Nth geek-onacci number
+
+Constraints:
+1. 1 <= T <= 3

+2. 1 <= A, B, C <= 100
+3. 4 <= N <= 10

+
+Example:
+Input:

+3
+1 3 2 4
+1 3 2 5
+1 3 2 6

+ +

Output:
+6
+11
+19

+

+
\ No newline at end of file diff --git a/C++/Geeks4Geeks/Insertion Sort/README.md b/C++/Geeks4Geeks/Insertion Sort/README.md new file mode 100644 index 00000000..4b412921 --- /dev/null +++ b/C++/Geeks4Geeks/Insertion Sort/README.md @@ -0,0 +1,38 @@ +# Insertion Sort +## Easy +
+

The task is to complete the insert() function which is used to implement Insertion Sort.

+ +


+Example 1:

+ +
Input:
+N = 5
+arr[] = { 4, 1, 3, 9, 7}
+Output:
+1 3 4 7 9
+
+ +

Example 2:

+ +
Input:
+N = 10
+arr[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}
+Output:
+1 2 3 4 5 6 7 8 9 10
+ +

+Your Task: 
+ +
You don't have to read input or print anything. Your task is to complete the function insert() and insertionSort() where insert() takes the array, it's size and an index i and insertionSort() uses insert function to sort the array in ascending order using insertion sort algorithm. 
+ +


+Expected Time Complexity: O(N*N).
+Expected Auxiliary Space: O(1).

+ +


+Constraints:
+1 <= N <= 1000
+1 <= arr[i] <= 1000

+

+
\ No newline at end of file diff --git a/C++/Geeks4Geeks/Insertion Sort/insertion-sort.cpp b/C++/Geeks4Geeks/Insertion Sort/insertion-sort.cpp new file mode 100644 index 00000000..e13b4adf --- /dev/null +++ b/C++/Geeks4Geeks/Insertion Sort/insertion-sort.cpp @@ -0,0 +1,61 @@ +// { Driver Code Starts +// C program for insertion sort +#include +#include + +/* 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 Code Ends +class Solution +{ + public: + void insert(int arr[], int i) + { + //code here + } + public: + //Function to sort the array using insertion sort algorithm. + void insertionSort(int arr[], int n) + { + //code here + for(int i=0;iarr[j]){ + int temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + } + } + } + } +}; + +// { Driver Code Starts. +int main() +{ + int arr[1000],n,T,i; + + scanf("%d",&T); + + while(T--){ + + scanf("%d",&n); + + for(i=0;i +

Given two singly linked lists of size N and M, write a program to get the point where two linked lists intersect each other.

+ +

 

+ +

Example 1:

+ +
Input:
+LinkList1 = 3->6->9->common
+LinkList2 = 10->common
+common = 15->30->NULL
+Output: 15
+Explanation:
+Y ShapedLinked List
+
+ +

Example 2:

+ +
Input: 
+Linked List 1 = 4->1->common
+Linked List 2 = 5->6->1->common
+common = 8->4->5->NULL
+Output: 8
+Explanation: 
+
+4              5
+|              |
+1              6
+ \             /
+  8   -----  1 
+   |
+   4
+   |
+  5
+  |
+  NULL       
+ +

Your Task:
+You don't need to read input or print anything. The task is to complete the function intersetPoint() which takes the pointer to the head of linklist1(head1) and linklist2(head2) as input parameters and returns data value of a node where two linked lists intersect. If linked list do not merge at any point, then it should return -1.

+Challenge : Try to solve the problem without using any extra space.

+ +

 

+ +

Expected Time Complexity: O(N+M)
+Expected Auxiliary Space: O(1)

+ +

 

+ +

Constraints:
+1 ≤ N + M ≤ 2*105
+-1000 ≤ value ≤ 1000

+ +

 

+

+ \ No newline at end of file diff --git a/C++/Geeks4Geeks/Ishaan Loves Chocolates/README.md b/C++/Geeks4Geeks/Ishaan Loves Chocolates/README.md new file mode 100644 index 00000000..a1923b97 --- /dev/null +++ b/C++/Geeks4Geeks/Ishaan Loves Chocolates/README.md @@ -0,0 +1,41 @@ +# Ishaan Loves Chocolates +## Basic +
+

As we know, Ishaan has a love for chocolates. He has bought a huge chocolate bar that contains N chocolate squares. Each of the squares has a tastiness level which is denoted by an array A[].
+Ishaan can eat the first or the last square of the chocolate at once. Ishaan has a sister who loves chocolates too and she demands the last chocolate square. Now, Ishaan being greedy eats the more tasty square first. 
+Determine the tastiness level of the square which his sister gets.

+ +

Example 1:

+ +
​Input : arr[ ] = {5, 3, 1, 6, 9}
+Output : 1
+Explanation:
+Initially: 5 3 1 6 9
+In first step: 5 3 1 6
+In Second step: 5 3 1
+In Third step: 3 1
+​In Fourth step: 1
+​​Return 1
+ +


+
+​Example 2:

+ +
Input : arr[ ] = {5, 9, 2, 6} 
+Output :  2
+
+
+ +

Your Task:
+This is a function problem. The input is already taken care of by the driver code. You only need to complete the function chocolates() that takes an array (arr), sizeOfArray (n) and return the tastiness level of the square which his sister gets. The driver code takes care of the printing.

+ +

Expected Time Complexity: O(N).
+Expected Auxiliary Space: O(1).

+ +

 

+ +

Constraints : 
+1 ≤ N ≤ 105
+1 ≤ A[i] ≤ 109

+

+
\ No newline at end of file diff --git a/C++/Geeks4Geeks/Kadane's Algorithm/README.md b/C++/Geeks4Geeks/Kadane's Algorithm/README.md new file mode 100644 index 00000000..eb8b99e6 --- /dev/null +++ b/C++/Geeks4Geeks/Kadane's Algorithm/README.md @@ -0,0 +1,44 @@ +# Kadane's Algorithm +## Medium +
+

Given an array Arr[] of N integers. Find the contiguous sub-array(containing at least one number) which has the maximum sum and return its sum.

+ +


+Example 1:

+ +
Input:
+N = 5
+Arr[] = {1,2,3,-2,5}
+Output:
+9
+Explanation:
+Max subarray sum is 9
+of elements (1, 2, 3, -2, 5) which 
+is a contiguous subarray.
+
+ +

Example 2:

+ +
Input:
+N = 4
+Arr[] = {-1,-2,-3,-4}
+Output:
+-1
+Explanation:
+Max subarray sum is -1 
+of element (-1)
+ +


+Your Task:
+You don't need to read input or print anything. The task is to complete the function maxSubarraySum() which takes Arr[] and N as input parameters and returns the sum of subarray with maximum sum.

+ +


+Expected Time Complexity: O(N)
+Expected Auxiliary Space: O(1)

+ +


+Constraints:
+1 ≤ N ≤ 106
+-107 ≤ A[i] ≤ 107

+

+
\ No newline at end of file diff --git a/C++/Geeks4Geeks/Kadane's Algorithm/kadanes-algorithm.cpp b/C++/Geeks4Geeks/Kadane's Algorithm/kadanes-algorithm.cpp new file mode 100644 index 00000000..81cb8c4d --- /dev/null +++ b/C++/Geeks4Geeks/Kadane's Algorithm/kadanes-algorithm.cpp @@ -0,0 +1,49 @@ +// { Driver Code Starts +#include +using namespace std; + + + // } Driver Code Ends +class Solution{ + public: + // arr: input array + // n: size of array + //Function to find the sum of contiguous subarray with maximum sum. + long long maxSubarraySum(int arr[], int n){ + + // Your code here + long long ms = arr[0], cs = arr[0]; + for(int i=1;i>t; //input testcases + while(t--) //while testcases exist + { + + cin>>n; //input size of array + + int a[n]; + + for(int i=0;i>a[i]; //inputting elements of array + + Solution ob; + + cout << ob.maxSubarraySum(a, n) << endl; + } +} + // } Driver Code Ends \ No newline at end of file diff --git a/C++/Geeks4Geeks/Kth smallest element/README.md b/C++/Geeks4Geeks/Kth smallest element/README.md new file mode 100644 index 00000000..46161781 --- /dev/null +++ b/C++/Geeks4Geeks/Kth smallest element/README.md @@ -0,0 +1,46 @@ +# Kth smallest element +## Medium +
+

Given an array arr[] and an integer K where K is smaller than size of array, the task is to find the Kth smallest element in the given array. It is given that all array elements are distinct.

+ +

Example 1:

+ +
Input:
+N = 6
+arr[] = 7 10 4 3 20 15
+K = 3
+Output : 7
+Explanation :
+3rd smallest element in the given 
+array is 7.
+
+ +

Example 2:

+ +
Input:
+N = 5
+arr[] = 7 10 4 20 15
+K = 4
+Output : 15
+Explanation :
+4th smallest element in the given 
+array is 15.
+ +
Your Task:
+You don't have to read input or print anything. Your task is to complete the function kthSmallest() which takes the array arr[], integers l and r denoting the starting and ending index of the array and an integer K as input and returns the Kth smallest element.
+ +
 
+ +
 
+ +
Expected Time Complexity: O(n)
+ +
Expected Auxiliary Space: O(1)
+ +

Constraints:
+1 <= N <= 105
+1 <= arr[i] <= 105
+1 <= K <= N

+

+
\ No newline at end of file diff --git a/C++/Geeks4Geeks/Kth smallest element/kth-smallest-element.cpp b/C++/Geeks4Geeks/Kth smallest element/kth-smallest-element.cpp new file mode 100644 index 00000000..34acace2 --- /dev/null +++ b/C++/Geeks4Geeks/Kth smallest element/kth-smallest-element.cpp @@ -0,0 +1,46 @@ +// { Driver Code Starts +//Initial function template for C++ + +#include +using namespace std; + + // } Driver Code Ends +//User function template for C++ + +class Solution{ + public: + // arr : given array + // l : starting index of the array i.e 0 + // r : ending index of the array i.e size-1 + // k : find kth smallest element and return using this function + int kthSmallest(int arr[], int l, int r, int k) { + //code here + r = r+1; + int n = (r-l); + sort(arr, arr+n); + return arr[k-1]; + } +}; + +// { Driver Code Starts. + +int main() +{ + int test_case; + cin>>test_case; + while(test_case--) + { + int number_of_elements; + cin>>number_of_elements; + int a[number_of_elements]; + + for(int i=0;i>a[i]; + + int k; + cin>>k; + Solution ob; + cout< +

Given an array A[] of size n. The task is to find the largest element in it.

+ +

Example 1:

+ +
Input:
+n = 5
+A[] = {1, 8, 7, 56, 90}
+Output:
+90
+Explanation:
+The largest element of given array is 90.
+ +

 

+ +

Example 2:

+ +
Input:
+n = 7
+A[] = {1, 2, 0, 3, 2, 4, 5}
+Output:
+5
+Explanation:
+The largest element of given array is 5.
+ +

 

+ +

Your Task:  
+You don't need to read input or print anything. Your task is to complete the function largest() which takes the array A[] and its size as inputs and returns the maximum element in the array.

+ +

 

+ +

Expected Time Complexity: O(N)
+Expected Auxiliary Space: O(1)

+ +

 

+ +

Constraints:
+1 <= n<= 103
+0 <= A[i] <= 103
+Array may contain duplicate elements. 

+

+ \ No newline at end of file diff --git a/C++/Geeks4Geeks/Largest Element in Array/largest-element-in-array.cpp b/C++/Geeks4Geeks/Largest Element in Array/largest-element-in-array.cpp new file mode 100644 index 00000000..3f9ed10d --- /dev/null +++ b/C++/Geeks4Geeks/Largest Element in Array/largest-element-in-array.cpp @@ -0,0 +1,43 @@ +// { Driver Code Starts +//Initial Template for C++ + +#include +using namespace std; + + // } Driver Code Ends +//User function Template for C++ + +class Solution +{ +public: + int largest(vector &arr, int n) + { + int maxi = arr[0]; + for(int i=1;i> t; + while (t--) + { + int n; + cin >> n; + vectorarr(n); + for (int i = 0; i < n; i++) + { + cin >> arr[i]; + } + Solution ob; + cout << ob.largest(arr, n) << "\n"; + } + return 0; +} + // } Driver Code Ends \ No newline at end of file diff --git a/C++/Geeks4Geeks/Leaders in an array/README.md b/C++/Geeks4Geeks/Leaders in an array/README.md new file mode 100644 index 00000000..9a7cd713 --- /dev/null +++ b/C++/Geeks4Geeks/Leaders in an array/README.md @@ -0,0 +1,48 @@ +# Leaders in an array +## Easy +
+

Given an array A of positive integers. Your task is to find the leaders in the array. An element of array is leader if it is greater than or equal to all the elements to its right side. The rightmost element is always a leader. 

+ +

 

+ +

Example 1:

+ +
Input:
+n = 6
+A[] = {16,17,4,3,5,2}
+Output: 17 5 2
+Explanation: The first leader is 17 
+as it is greater than all the elements
+to its right.  Similarly, the next 
+leader is 5. The right most element 
+is always a leader so it is also 
+included.
+
+ +

 

+ +

Example 2:

+ +
Input:
+n = 5
+A[] = {1,2,3,4,0}
+Output: 4 0
+
+ +

 

+ +

Your Task:
+You don't need to read input or print anything. The task is to complete the function leader() which takes array A and n as input parameters and returns an array of leaders in order of their appearance.

+ +

 

+ +

Expected Time Complexity: O(n)
+Expected Auxiliary Space: O(n)

+ +

 

+ +

Constraints:
+1 <= n <= 107
+0 <= Ai <= 107

+

+
\ No newline at end of file diff --git a/C++/Geeks4Geeks/Leaders in an array/leaders-in-an-array.cpp b/C++/Geeks4Geeks/Leaders in an array/leaders-in-an-array.cpp new file mode 100644 index 00000000..1f051051 --- /dev/null +++ b/C++/Geeks4Geeks/Leaders in an array/leaders-in-an-array.cpp @@ -0,0 +1,60 @@ +// { Driver Code Starts +// C++ program to remove recurring digits from +// a given number +#include +using namespace std; + + + // } Driver Code Ends + + +class Solution{ + //Function to find the leaders in the array. + public: + vector leaders(int a[], int n){ + // Code here + vector res; + int maxi = a[n-1]; + res.push_back(maxi); + for(int i=n-2;i>=0;i--){ + if(a[i]>=maxi){ + maxi = a[i]; + res.push_back(maxi); + } + } + reverse(res.begin(), res.end()); + return res; + } +}; + +// { Driver Code Starts. + +int main() +{ + long long t; + cin >> t;//testcases + while (t--) + { + long long n; + cin >> n;//total size of array + + int a[n]; + + //inserting elements in the array + for(long long i =0;i> a[i]; + } + Solution obj; + //calling leaders() function + vector v = obj.leaders(a, n); + + //printing elements of the vector + for(auto it = v.begin();it!=v.end();it++){ + cout << *it << " "; + } + + cout << endl; + + } +} + // } Driver Code Ends \ No newline at end of file diff --git a/C++/Geeks4Geeks/Left View of Binary Tree/README.md b/C++/Geeks4Geeks/Left View of Binary Tree/README.md new file mode 100644 index 00000000..d4fb5ae6 --- /dev/null +++ b/C++/Geeks4Geeks/Left View of Binary Tree/README.md @@ -0,0 +1,42 @@ +# Left View of Binary Tree +## Easy +
+

Given a Binary Tree, print Left view of it. Left view of a Binary Tree is set of nodes visible when tree is visited from Left side. The task is to complete the function leftView(), which accepts root of the tree as argument.

+ +

Left view of following tree is 1 2 4 8.

+ +

          1
+       /     \
+     2        3
+   /     \    /    \
+  4     5   6    7
+   \
+     8   

+ +

Example 1:

+ +
Input:
+   1
+ /  \
+3    2
+Output: 1 3
+
+
+ +

Example 2:

+ +
Input:
+
+Output: 10 20 40
+
+ +

Your Task:
+You just have to complete the function leftView() that prints the left view. The newline is automatically appended by the driver code.

+Expected Time Complexity: O(N).
+Expected Auxiliary Space: O(Height of the Tree).

+ +

Constraints:
+0 <= Number of nodes <= 100
+1 <= Data of a node <= 1000

+

+
\ No newline at end of file diff --git a/C++/Geeks4Geeks/Linked List Insertion/README.md b/C++/Geeks4Geeks/Linked List Insertion/README.md new file mode 100644 index 00000000..2a867abb --- /dev/null +++ b/C++/Geeks4Geeks/Linked List Insertion/README.md @@ -0,0 +1,50 @@ +# Linked List Insertion +## Basic +
+

Create a link list of size N according to the given input literals. Each integer input is accompanied by an indicator which can either be 0 or 1. If it is 0, insert the integer in the beginning of the link list. If it is 1, insert the integer at the end of the link list. 
+Hint: When inserting at the end, make sure that you handle NULL explicitly.

+ +

Example 1:

+ +
Input:
+LinkedList: 9->0->5->1->6->1->2->0->5->0
+Output: 5 2 9 5 6
+Explanation:
+Length of Link List = N = 5
+9 0 indicated that 9 should be
+inserted in the beginning. Modified
+Link List = 9.
+5 1 indicated that 5 should be
+inserted in the end. Modified Link
+List = 9,5.
+6 1 indicated that 6 should be
+inserted in the end. Modified Link
+List = 9,5,6.
+2 0 indicated that 2 should be
+inserted in the beginning. Modified
+Link List = 2,9,5,6.
+5 0 indicated that 5 should be
+inserted in the beginning. Modified
+Link List = 5,2,9,5,6. 
+Final linked list = 5, 2, 9, 5, 6.
+
+
+ +

Example 2:

+ +
Input:
+LinkedList: 5->1->6->1->9->1
+Output: 5 6 9
+
+
+ +

Your Task:
+You only need to complete the functions insertAtBeginning() and
insertAtEnd() that takes the head of link list and integer value of the data to be inserted as inputs and returns the head of the modified link list. 

+ +

Expected Time Complexity: O(1) for insertAtBeginning() and O(N) for insertAtEnd().
+Expected Auxiliary Space: O(1) for both.

+ +

Constraints:
+1 <= N <= 104

+

+
\ No newline at end of file diff --git a/C++/Geeks4Geeks/Linked List Insertion/linked-list-insertion.cpp b/C++/Geeks4Geeks/Linked List Insertion/linked-list-insertion.cpp new file mode 100644 index 00000000..0790ae3c --- /dev/null +++ b/C++/Geeks4Geeks/Linked List Insertion/linked-list-insertion.cpp @@ -0,0 +1,98 @@ +// { Driver Code Starts +#include +using namespace std; + +struct Node +{ + int data; + struct Node* next; + + Node(int x){ + data = x; + next = NULL; + } +}; +void printList(Node* node) +{ + while (node != NULL) { + cout << node->data <<" "; + node = node->next; + } + cout<<"\n"; +} + + + + // } Driver Code Ends +/*Structure of the linked list node is as +struct Node { + int data; + struct Node * next; + Node(int x) { + data = x; + next = NULL; + } +}; */ + +class Solution{ + public: + //Function to insert a node at the beginning of the linked list. + Node *insertAtBegining(Node *head, int x) { + // Your code here + Node* n = new Node(x); + if(!head){ + head = n; + }else{ + n->next = head; + head = n; + } + return head; + } + + + //Function to insert a node at the end of the linked list. + Node *insertAtEnd(Node *head, int x) { + // Your code here + Node* n = new Node(x); + if(!head){ + head = n; + } + else{ + Node* temp = head; + while(temp->next!=NULL){ + temp = temp->next; + } + temp->next = n; + } + return head; + } +}; + + +// { Driver Code Starts. +int main() +{ + int t; + cin>>t; + while(t--) + { + int n; + cin>>n; + struct Node *head = NULL; + for (int i = 0; i < n; ++i) + { + int data, indicator; + cin>>data; + cin>>indicator; + Solution obj; + if(indicator) + head = obj.insertAtEnd(head, data); + else + head = obj.insertAtBegining(head, data); + } + printList(head); + } + return 0; +} + + // } Driver Code Ends \ No newline at end of file diff --git a/C++/Geeks4Geeks/Merge Sort/README.md b/C++/Geeks4Geeks/Merge Sort/README.md new file mode 100644 index 00000000..c5345898 --- /dev/null +++ b/C++/Geeks4Geeks/Merge Sort/README.md @@ -0,0 +1,35 @@ +# Merge Sort +## Medium +
+

Given an array arr[], its starting position l and its ending position r. Sort the array using merge sort algorithm.
+Example 1:

+ +
Input:
+N = 5
+arr[] = {4 1 3 9 7}
+Output:
+1 3 4 7 9
+
+ +

Example 2:

+ +
Input:
+N = 10
+arr[] = {10 9 8 7 6 5 4 3 2 1}
+Output:
+1 2 3 4 5 6 7 8 9 10
+ +

+Your Task:
+You don't need to take the input or print anything. Your task is to complete the function merge() which takes arr[], l, m, r as its input parameters and modifies arr[] in-place such that it is sorted from position l to position r, and function mergeSort() which uses merge() to sort the array in ascending order using merge sort algorithm.

+
+Expected Time Complexity: O(nlogn) 
+ +
Expected Auxiliary Space: O(n)
+ +


+Constraints:
+1 <= N <= 105
+1 <= arr[i] <= 103

+

+
\ No newline at end of file diff --git a/C++/Geeks4Geeks/Merge Sort/merge-sort.cpp b/C++/Geeks4Geeks/Merge Sort/merge-sort.cpp new file mode 100644 index 00000000..7d37582d --- /dev/null +++ b/C++/Geeks4Geeks/Merge Sort/merge-sort.cpp @@ -0,0 +1,87 @@ +// { Driver Code Starts +#include +#include +using namespace std; + + + +/* 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 Code Ends +class Solution +{ + public: + void merge(int arr[], int l, int m, int r) + { + // Your code here + vector a; + int i=l,j=m+1; + while(i<=m and j<=r){ + if(arr[i]=r){ + return; + } + int mid = (l+r)/2; + mergeSort(arr, l, mid); + mergeSort(arr, mid+1, r); + return merge(arr, l, mid, r); + } +}; + +// { Driver Code Starts. + + +int main() +{ + int n,T,i; + + scanf("%d",&T); + + while(T--){ + + scanf("%d",&n); + int arr[n+1]; + for(i=0;i +

Given two binary max heaps as arrays, merge the given heaps to form a new max heap.

+ +

 

+ +

Example 1:

+ +
Input  : 
+n = 4 m = 3
+a[] = {10, 5, 6, 2}, 
+b[] = {12, 7, 9}
+Output : 
+{12, 10, 9, 2, 5, 7, 6}
+Explanation :
+
+
+
+
+ +

 

+ +

 

+ +

Your Task:  
+You don't need to read input or print anything. Your task is to complete the function mergeHeaps() which takes the array a[], b[], its size n and m, as inputs and return the merged max heap. Since there can be multiple solutions, therefore, to check for the correctness of your solution, your answer will be checked by the driver code and will return 1 if it is correct, else it returns 0.

+ +

 

+ +

Expected Time Complexity: O(n.Logn)
+Expected Auxiliary Space: O(n + m)

+ +

 

+ +

Constraints:
+1 <= n, m <= 105
+1 <= a[i], b[i] <= 2*105

+ +


+ +

 

+

+ \ No newline at end of file diff --git a/C++/Geeks4Geeks/Merge two binary Max heaps/merge-two-binary-max-heaps.cpp b/C++/Geeks4Geeks/Merge two binary Max heaps/merge-two-binary-max-heaps.cpp new file mode 100644 index 00000000..d40d4556 --- /dev/null +++ b/C++/Geeks4Geeks/Merge two binary Max heaps/merge-two-binary-max-heaps.cpp @@ -0,0 +1,70 @@ +// { Driver Code Starts +// Initial Template for C++ + +// C++ program to merge two max heaps. + +#include +using namespace std; + + + // } Driver Code Ends +// User function Template for C++ + +class Solution{ + public: + vector mergeHeaps(vector &a, vector &b, int n, int m) { + // your code here + vector res; + for(int i=0;i()); + return res; + } +}; + +// { Driver Code Starts. + +bool isMerged(vector &arr1, vector &arr2, vector &merged){ + if (arr1.size() + arr2.size() != merged.size()){ + return false; + } + arr1.insert(arr1.end(),arr2.begin(),arr2.end()); + sort(arr1.begin(),arr1.end()); + vector mergedCopy =merged; + sort(mergedCopy.begin(),mergedCopy.end()); + if (arr1 != mergedCopy){ + return false; + } + for(int i = 1; i merged[(i-1)/2]) + return false; + } + return true; +} +int main() { + int t; + cin >> t; + while (t--) { + int n, m, i; + cin >> n >> m; + vector a(n,0),b(m,0); + for (i = 0; i < n; i++) { + cin >> a[i]; + } + for (i = 0; i < m; i++) { + cin >> b[i]; + } + vector merged, copyA = a, copyB = b; + Solution obj; + merged = obj.mergeHeaps(a, b, n, m); + bool flag = isMerged(copyA, copyB, merged); + if(flag == false) cout<<0< +

Given an array of size N-1 such that it only contains distinct integers in the range of 1 to N. Find the missing element.

+ +

Example 1:

+ +
Input:
+N = 5
+A[] = {1,2,3,5}
+Output: 4
+
+ +

Example 2:

+ +
Input:
+N = 10
+A[] = {6,1,2,8,3,4,7,10,5}
+Output: 9
+ +


+Your Task :
+You don't need to read input or print anything. Complete the function MissingNumber() that takes array and N as input  parameters and returns the value of the missing number.

+ +


+Expected Time Complexity: O(N)
+Expected Auxiliary Space: O(1)

+ +


+Constraints:
+1 ≤ N ≤ 106
+1 ≤ A[i] ≤ 106

+

+ \ No newline at end of file diff --git a/C++/Geeks4Geeks/Missing number in array/missing-number-in-array.cpp b/C++/Geeks4Geeks/Missing number in array/missing-number-in-array.cpp new file mode 100644 index 00000000..8099fa93 --- /dev/null +++ b/C++/Geeks4Geeks/Missing number in array/missing-number-in-array.cpp @@ -0,0 +1,42 @@ +// { Driver Code Starts +// Initial template for C++ + +#include +using namespace std; + + + + + // } Driver Code Ends +// User function template for C++ + +class Solution{ + public: + int MissingNumber(vector& array, int n) { + // Your code goes here + sort(array.begin(),array.end()); + int Osum = (n*(n+1))/2; + int pSum=0; + for(int i=0;i> t; + while (t--) { + int n; + cin >> n; + + vector array(n - 1); + for (int i = 0; i < n - 1; ++i) cin >> array[i]; + Solution obj; + cout << obj.MissingNumber(array, n) << "\n"; + } + return 0; +} // } Driver Code Ends \ No newline at end of file diff --git a/C++/Geeks4Geeks/Multiply left and right array sum/README.md b/C++/Geeks4Geeks/Multiply left and right array sum/README.md new file mode 100644 index 00000000..55b1ffd6 --- /dev/null +++ b/C++/Geeks4Geeks/Multiply left and right array sum/README.md @@ -0,0 +1,38 @@ +# Multiply left and right array sum. +## Basic +
+

Pitsy needs help with the given task by her teacher. The task is to divide an array into two sub-array (left and right) containing n/2 elements each and do the sum of the subarrays and then multiply both the subarrays.

+ +

Note: If the length of the array is odd then the right half will contain one element more than the left half.

+ +

Example 1:

+ +
Input : arr[ ] = {1, 2, 3, 4}
+Output : 21
+Explanation:
+Sum up an array from index 0 to 1 = 3
+Sum up an array from index 2 to 3 = 7
+Their multiplication is 21.
+
+ +


+Example 2:

+ +
Input : arr[ ] = {1, 2} 
+Output :  2 
+ +

 

+ +

Your Task:
+This is a function problem. The input is already taken care of by the driver code. You only need to complete the function multiply() that takes an array (arr), sizeOfArray (n), and return the sum of the subarrays and then multiply both the subarrays. The driver code takes care of the printing.

+ +

Expected Time Complexity: O(N).
+Expected Auxiliary Space: O(1).

+ +


+Constraints:
+1 ≤ T ≤ 100
+1 ≤ N ≤ 1000
+1 ≤ A[i] ≤ 100

+

+
\ No newline at end of file diff --git a/C++/Geeks4Geeks/Multiply left and right array sum/multiply-left-and-right-array-sum..cpp b/C++/Geeks4Geeks/Multiply left and right array sum/multiply-left-and-right-array-sum..cpp new file mode 100644 index 00000000..396d00cd --- /dev/null +++ b/C++/Geeks4Geeks/Multiply left and right array sum/multiply-left-and-right-array-sum..cpp @@ -0,0 +1,53 @@ +// { Driver Code Starts +//Initial Template for C++ + +#include +using namespace std; + +int multiply(int arr[], int n); + + +int main() { + //code + int t; + cin>>t; + while (t--) + { + int n,sum1=0,sum2=0; + cin>>n; + int a[n]; + for(int i=0;i>a[i]; + + cout << multiply(a, n) << endl; + + } + return 0; +}// } Driver Code Ends + + +//User function Template for C++ + +int leftadd(int arr[], int n){ + int sum=0; + for(int i=0;i +

An element is called a peak element if its value is not smaller than the value of its adjacent elements(if they exists).
+Given an array arr[] of size N, find the index of any one of its peak elements.
+Note: The generated output will always be 1 if the index that you return is correct. Otherwise output will be 0. 

+ +


+Example 1:

+ +
Input:
+N = 3
+arr[] = {1,2,3}
+Output: 2
+Explanation: index 2 is 3.
+It is the peak element as it is 
+greater than its neighbour 2.
+
+ +

Example 2:

+ +
Input:
+N = 2
+arr[] = {3,4}
+Output: 1
+Explanation: 4 (at index 1) is the 
+peak element as it is greater than 
+its only neighbour element 3.
+
+ +

 

+ +

Your Task:
+You don't have to read input or print anything. Complete the function peakElement() which takes the array arr[] and its size N as input parameters and return the index of any one of its peak elements.
+
+Can you solve the problem in expected time complexity?

+ +

 

+ +

Expected Time Complexity: O(log N)
+Expected Auxiliary Space: O(1)

+ +


+Constraints:
+1 ≤ N ≤ 105
+1 ≤ A[] ≤ 106

+

+ \ No newline at end of file diff --git a/C++/Geeks4Geeks/Peak element/peak-element.c b/C++/Geeks4Geeks/Peak element/peak-element.c new file mode 100644 index 00000000..931b79a3 --- /dev/null +++ b/C++/Geeks4Geeks/Peak element/peak-element.c @@ -0,0 +1,63 @@ +// { Driver Code Starts +//Initial Template for C + +#include +#include + + + // } Driver Code Ends +//User function Template for C + +int peakElement(int arr[], int n) +{ + // Your code here + int temp = 0; + for(int i=0;i arr[i-1]){ + temp = i; + } + } + return temp; +} + +// { Driver Code Starts. + +int main() { + int t; + scanf("%d", &t); + while(t--) + { + int n; + scanf("%d", &n); + int a[n], tmp[n]; + for(int i=0;i=n) + printf("0\n"); + else + { + if(n==1 && A==0) + f=1; + else + if(A==0 && a[0]>=a[1]) + f=1; + else if(A==n-1 && a[n-1]>=a[n-2]) + f=1; + else if(a[A]>=a[A+1] && a[A]>= a[A-1]) + f=1; + else + f=0; + printf("%d\n", f); + } + + } + + return 0; +} // } Driver Code Ends \ No newline at end of file diff --git a/C++/Geeks4Geeks/Play With OR/README.md b/C++/Geeks4Geeks/Play With OR/README.md new file mode 100644 index 00000000..302fe938 --- /dev/null +++ b/C++/Geeks4Geeks/Play With OR/README.md @@ -0,0 +1,42 @@ +# Play With OR +## Basic +
+

 You are given an array arr[], you have to re-construct an array arr[].
+The values in arr[] are obtained by doing OR(bitwise or) of consecutive elements in the array.

+ +

Example 1:

+ +
​Input : arr[ ] = {10, 11, 1, 2, 3}
+Output : 11 11 3 3 3
+Explanation:
+At index 0, arr[0] or arr[1] = 11
+At index 1, arr[1] or arr[2] = 11
+At index 2, arr[2] or arr[3] = 3
+...
+At index 4, No element is left So, it will 
+remain as it is.
+New Array will be {11, 11, 3, 3, 3}.
+
+ +


+​Example 2:

+ +
Input : arr[ ] = {5, 9, 2, 6} 
+Output :  13 11 6 6 
+ +

 

+ +

Your Task:
+This is a function problem. The input is already taken care of by the driver code. You only need to complete the function game_with_number() that takes an array (arr), sizeOfArray (n), and return the array re-constructed array arr. The driver code takes care of the printing.

+ +

Expected Time Complexity: O(N).
+Expected Auxiliary Space: O(1).

+ +

 

+ +

Constraints:

+ +

1 ≤ N ≤ 105
+1 ≤ arr[i] ≤ 107

+

+
\ No newline at end of file diff --git a/C++/Geeks4Geeks/Play With OR/play-with-or.cpp b/C++/Geeks4Geeks/Play With OR/play-with-or.cpp new file mode 100644 index 00000000..c238513f --- /dev/null +++ b/C++/Geeks4Geeks/Play With OR/play-with-or.cpp @@ -0,0 +1,43 @@ +// { Driver Code Starts +#include +using namespace std; + + +int* game_with_number(int arr[], int n); + +int main() +{ + + int t;cin>> t; + while(t--) + { + int n; + cin >> n; + int arr[n]; + + for(int i=0;i>arr[i]; + + int *arr2; + + arr2 = game_with_number(arr, n); + for(int i = 0;i < n; i++) + cout << arr2[i] << " "; + + cout << endl; + + } + +} +// } Driver Code Ends + + +int* game_with_number(int arr[], int n) +{ + + // Complete the function + for(int i=0;i +

Given a linked list of N nodes such that it may contain a loop.

+ +
+

A loop here means that the last node of the link list is connected to the node at position X. If the link list does not have any loop, X=0.

+
+ +

Remove the loop from the linked list, if it is present.  

+ +


+Example 1:

+ +
Input:
+N = 3
+value[] = {1,3,4}
+X = 2
+Output: 1
+Explanation: The link list looks like
+1 -> 3 -> 4
+     ^    |
+     |____|    
+A loop is present. If you remove it 
+successfully, the answer will be 1. 
+
+ +


+Example 2:

+ +
Input:
+N = 4
+value[] = {1,8,3,4}
+X = 0
+Output: 1
+Explanation: The Linked list does not 
+contains any loop. 
+ +


+Example 3:

+ +
Input:
+N = 4
+value[] = {1,2,3,4}
+X = 1
+Output: 1
+Explanation: The link list looks like 
+1 -> 2 -> 3 -> 4
+|______________|
+A loop is present. 
+If you remove it successfully, 
+the answer will be 1. 
+ +


+Your Task:
+You don't need to read input or print anything. Your task is to complete the function removeLoop() which takes the head of the linked list as the input parameter. Simply remove the loop in the list (if present) without disconnecting any nodes from the list.
+Note: The generated output will be if your submitted code is correct.

+ +


+Expected time complexity: O(N)
+Expected auxiliary space: O(1)

+ +


+Constraints:
+1 ≤ N ≤ 104

+

+ \ No newline at end of file diff --git a/C++/Geeks4Geeks/Reverse a linked list/README.md b/C++/Geeks4Geeks/Reverse a linked list/README.md new file mode 100644 index 00000000..cda4b622 --- /dev/null +++ b/C++/Geeks4Geeks/Reverse a linked list/README.md @@ -0,0 +1,34 @@ +# Reverse a linked list +## Easy +
+

Given a linked list of N nodes. The task is to reverse this list.

+ +

Example 1:

+ +
Input:
+LinkedList: 1->2->3->4->5->6
+Output: 6 5 4 3 2 1
+Explanation: After reversing the list, 
+elements are 6->5->4->3->2->1.
+
+ +

Example 2:

+ +
Input:
+LinkedList: 2->7->8->9->10
+Output: 10 9 8 7 2
+Explanation: After reversing the list,
+elements are 10->9->8->7->2.
+ +

Your Task:
+The task is to complete the function reverseList() with head reference as the only argument and should return new head after reversing the list.

+ +

Expected Time Complexity: O(N).
+Expected Auxiliary Space: O(1).

+ +

Constraints:
+1 <= N <= 104

+ +

 

+

+
\ No newline at end of file diff --git a/C++/Geeks4Geeks/Reverse a linked list/reverse-a-linked-list.cpp b/C++/Geeks4Geeks/Reverse a linked list/reverse-a-linked-list.cpp new file mode 100644 index 00000000..6f1a0a43 --- /dev/null +++ b/C++/Geeks4Geeks/Reverse a linked list/reverse-a-linked-list.cpp @@ -0,0 +1,100 @@ +// { Driver Code Starts +//Initial Template for C++// C program to find n'th Node in linked list +#include +#include +#include +using namespace std; + +/* Link list Node */ +struct Node { + int data; + struct Node *next; + Node(int x) + { + data = x; + next = NULL; + } +}; + + + + // } Driver Code Ends +/* Linked List Node structure: + +struct Node +{ + int data; + struct Node *next; +} + +*/ + +class Solution +{ + public: + //Function to reverse a linked list. + struct Node* reverseList(struct Node *head) + { + // code here + // return head of reversed list + Node* prev = NULL; + Node* n = head; + Node* curr = head; + while(curr!=NULL){ + n = curr; + curr = curr->next; + n->next = prev; + prev = n; + } + head = prev; + return head; + } +}; + + + +// { Driver Code Starts. + +void printList(struct Node *head) +{ + struct Node *temp = head; + while (temp != NULL) + { + printf("%d ", temp->data); + temp = temp->next; + } +} + +/* Driver program to test above function*/ +int main() +{ + int T,n,l,firstdata; + cin>>T; + + while(T--) + { + struct Node *head = NULL, *tail = NULL; + + cin>>n; + + cin>>firstdata; + head = new Node(firstdata); + tail = head; + + for (int i=1; i>l; + tail->next = new Node(l); + tail = tail->next; + } + + Solution ob; + head = ob. reverseList(head); + + printList(head); + cout << endl; + } + return 0; +} + + // } Driver Code Ends \ No newline at end of file diff --git a/C++/Geeks4Geeks/Reverse array in groups/README.md b/C++/Geeks4Geeks/Reverse array in groups/README.md new file mode 100644 index 00000000..c13d9796 --- /dev/null +++ b/C++/Geeks4Geeks/Reverse array in groups/README.md @@ -0,0 +1,43 @@ +# Reverse array in groups +## Basic +
+

Given an array arr[] of positive integers of size N. Reverse every sub-array group of size K.

+ +

 

+ +

Example 1:

+ +
Input:
+N = 5, K = 3
+arr[] = {1,2,3,4,5}
+Output: 3 2 1 5 4
+Explanation: First group consists of elements
+1, 2, 3. Second group consists of 4,5.
+ +

 

+ +

Example 2:

+ +
Input:
+N = 4, K = 3
+arr[] = {5,6,8,9}
+Output: 8 6 5 9
+
+ +

 

+ +

Your Task:
+You don't need to read input or print anything. The task is to complete the function reverseInGroups() which takes the array, N and K as input parameters and modifies the array in-place. 

+ +

 

+ +

Expected Time Complexity: O(N)
+Expected Auxiliary Space: O(N)

+ +

 

+ +

Constraints:
+1 ≤ N, K ≤ 107
+1 ≤ A[i] ≤ 1018

+

+
\ No newline at end of file diff --git a/C++/Geeks4Geeks/Reverse array in groups/reverse-array-in-groups.cpp b/C++/Geeks4Geeks/Reverse array in groups/reverse-array-in-groups.cpp new file mode 100644 index 00000000..28d0db3f --- /dev/null +++ b/C++/Geeks4Geeks/Reverse array in groups/reverse-array-in-groups.cpp @@ -0,0 +1,56 @@ +// { Driver Code Starts +//Initial template for C++ + +#include +using namespace std; + + + // } Driver Code Ends +//User function template for C++ + +class Solution{ +public: + //Function to reverse every sub-array group of size k. + void reverseInGroups(vector& arr, int n, int k){ + // code here + // vector res; + for(int i=0;i> t; + while(t--){ + int n; + cin >> n; + vector arr; + int k; + cin >> k; + + for(long long i = 0; i> x; + arr.push_back(x); + } + Solution ob; + ob.reverseInGroups(arr, n, k); + + for(long long i = 0; i +

Given a String S, reverse the string without reversing its individual words. Words are separated by dots.

+ +

Example 1:

+ +
Input:
+S = i.like.this.program.very.much
+Output: much.very.program.this.like.i
+Explanation: After reversing the whole
+string(not individual words), the input
+string becomes
+much.very.program.this.like.i
+
+ +

Example 2:

+ +
Input:
+S = pqr.mno
+Output: mno.pqr
+Explanation: After reversing the whole
+string , the input string becomes
+mno.pqr
+
+ +


+Your Task:
+You dont need to read input or print anything. Complete the function reverseWords() which takes string S as input parameter and returns a string containing the words in reversed order. Each word in the returning string should also be separated by '.' 

+ +


+Expected Time Complexity: O(|S|)
+Expected Auxiliary Space: O(|S|)

+ +


+Constraints:
+1 <= |S| <= 2000

+ +

 

+

+ \ No newline at end of file diff --git a/C++/Geeks4Geeks/Reverse words in a given string/reverse-words-in-a-given-string.cpp b/C++/Geeks4Geeks/Reverse words in a given string/reverse-words-in-a-given-string.cpp new file mode 100644 index 00000000..e4f5d6ae --- /dev/null +++ b/C++/Geeks4Geeks/Reverse words in a given string/reverse-words-in-a-given-string.cpp @@ -0,0 +1,48 @@ +// { Driver Code Starts +#include +using namespace std; + + // } Driver Code Ends + + +class Solution +{ + public: + //Function to reverse words in a given string. + string reverseWords(string S) + { + // code here + stack st; + string a=""; + for(int i=0;i> t; + while (t--) + { + string s; + cin >> s; + Solution obj; + cout< +

Given a singly linked list of size N. The task is to left-shift the linked list by k nodes, where k is a given positive integer smaller than or equal to length of the linked list.

+ +

Example 1:

+ +
Input:
+N = 5
+value[] = {2, 4, 7, 8, 9}
+k = 3
+Output: 8 9 2 4 7
+Explanation:
+Rotate 1: 4 -> 7 -> 8 -> 9 -> 2
+Rotate 2: 7 -> 8 -> 9 -> 2 -> 4
+Rotate 3: 8 -> 9 -> 2 -> 4 -> 7
+
+ +

Example 2:

+ +
Input:
+N = 8
+value[] = {1, 2, 3, 4, 5, 6, 7, 8}
+k = 4
+Output: 5 6 7 8 1 2 3 4
+
+ +


+Your Task:
+You don't need to read input or print anything. Your task is to complete the function rotate() which takes a head reference as the first argument and k as the second argument, and returns the head of the rotated linked list.

+ +

Expected Time Complexity: O(N).
+Expected Auxiliary Space: O(1).

+ +

Constraints:
+1 <= N <= 103
+1 <= k <= N

+

+ \ No newline at end of file diff --git a/C++/Geeks4Geeks/Rotate a Linked List/rotate-a-linked-list.cpp b/C++/Geeks4Geeks/Rotate a Linked List/rotate-a-linked-list.cpp new file mode 100644 index 00000000..20278c0c --- /dev/null +++ b/C++/Geeks4Geeks/Rotate a Linked List/rotate-a-linked-list.cpp @@ -0,0 +1,94 @@ +// { Driver Code Starts +#include +using namespace std; + +struct Node { + int data; + struct Node *next; + Node(int x) { + data = x; + next = NULL; + } +}; + + + // } Driver Code Ends +/* + +struct Node { + int data; + struct Node *next; + Node(int x) { + data = x; + next = NULL; + } +}; + +*/ + + +class Solution +{ + public: + //Function to rotate a linked list. + Node* rotate(Node* head, int k) + { + // Your code here + Node* temp1 = head; + Node* temp2 = head; + while(temp1->next!=NULL) + temp1 = temp1->next; + for(int i=1;i<=k;i++){ + head = temp2->next; + temp1->next = temp2; + temp2->next = NULL; + temp1 = temp2; + temp2 = head; + } + return head; + } +}; + + + +// { Driver Code Starts. + +void printList(Node *n) +{ + while (n != NULL) + { + cout<< n->data << " "; + n = n->next; + } + cout<< endl; +} + +int main() +{ + int t; + cin>>t; + while(t--) + { + int n, val, k; + cin>>n; + + cin>> val; + Node *head = new Node(val); + Node *tail = head; + + for(int i=0; i> val; + tail->next = new Node(val); + tail = tail->next; + } + + cin>> k; + + Solution ob; + head = ob.rotate(head,k); + printList(head); + } + return 1; +} + // } Driver Code Ends \ No newline at end of file diff --git a/C++/Geeks4Geeks/Searching an element in a sorted array/README.md b/C++/Geeks4Geeks/Searching an element in a sorted array/README.md new file mode 100644 index 00000000..f343e792 --- /dev/null +++ b/C++/Geeks4Geeks/Searching an element in a sorted array/README.md @@ -0,0 +1,47 @@ +# Searching an element in a sorted array +## Basic +
+

Given an array arr[] sorted in ascending order of size N and an integer K. Check if K is present in the array or not.

+ +


+Example 1:

+ +
Input:
+N = 5, K = 6
+arr[] = {1,2,3,4,6}
+Output: 1
+Exlpanation: Since, 6 is present in 
+the array at index 4 (0-based indexing),
+output is 1.
+ +

 

+ +

Example 2:

+ +
Input:
+N = 5, K = 2
+arr[] = {1,3,4,5,6}
+Output: -1
+Exlpanation: Since, 2 is not present 
+in the array, output is -1.
+
+ +

 

+ +

Your Task:
+You don't need to read input or print anything. Complete the function searchInSorted() which takes the sorted array arr[], its size N and the element K as input parameters and returns 1 if K is present in the array, else it returns -1. 

+ +


+Expected Time Complexity: O(Log N)
+Expected Auxiliary Space: O(1)

+ +

 

+ +

Constraints:
+1 <= N <= 106
+1 <= K <= 106
+1 <= arr[i] <= 106

+ +

 

+

+
\ No newline at end of file diff --git a/C++/Geeks4Geeks/Segregate 0s and 1s/README.md b/C++/Geeks4Geeks/Segregate 0s and 1s/README.md new file mode 100644 index 00000000..8aab2dbe --- /dev/null +++ b/C++/Geeks4Geeks/Segregate 0s and 1s/README.md @@ -0,0 +1,37 @@ +# Segregate 0s and 1s +## Easy +
+

Given an array of length N consisting of only 0s and 1s in random order. Modify the array to segregate 0s on left side and 1s on the right side of the array.

+ +

Example 1:

+ +
Input:
+N = 5
+arr[] = {0, 0, 1, 1, 0}
+Output: 0 0 0 1 1
+
+ +


+Example 2:

+ +
Input:
+N = 4
+Arr[] = {1, 1, 1, 1}
+Output: 1 1 1 1
+Explanation: There are no 0 in the given array, 
+so the modified array is 1 1 1 1.
+ +


+Your Task:
+You don't need to read input or print anything. Your task is to complete the function segregate0and1() which takes arr[] and as input parameters and modifies arr[] in-place without using any extra memory.

+ +


+Expected Time Complexity: O(N)
+Expected Auxiliary Space: O(1)

+ +


+Constraints:
+1 ≤ N ≤ 107
+0 ≤ arri ≤ 1

+

+
\ No newline at end of file diff --git a/C++/Geeks4Geeks/Segregate 0s and 1s/segregate-0s-and-1s.cpp b/C++/Geeks4Geeks/Segregate 0s and 1s/segregate-0s-and-1s.cpp new file mode 100644 index 00000000..664318df --- /dev/null +++ b/C++/Geeks4Geeks/Segregate 0s and 1s/segregate-0s-and-1s.cpp @@ -0,0 +1,38 @@ +// { Driver Code Starts +//Initial template for C++ + +#include +using namespace std; + + // } Driver Code Ends +//User function template for C++ + +class Solution{ +public: + void segregate0and1(int arr[], int n) { + // code here + sort(arr, arr+n); + } +}; + +// { Driver Code Starts. + +int main() { + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + int arr[n]; + for (int i = 0; i < n; i++) { + cin >> arr[i]; + } + Solution ob; + ob.segregate0and1(arr, n); + for (int i = 0; i < n; i++) { + cout << arr[i] << " "; + } + cout << "\n"; + } + return 0; +} // } Driver Code Ends \ No newline at end of file diff --git a/C++/Geeks4Geeks/Sort an array of 0s, 1s and 2s/README.md b/C++/Geeks4Geeks/Sort an array of 0s, 1s and 2s/README.md new file mode 100644 index 00000000..721ba5bf --- /dev/null +++ b/C++/Geeks4Geeks/Sort an array of 0s, 1s and 2s/README.md @@ -0,0 +1,42 @@ +# Sort an array of 0s, 1s and 2s +## Easy +
+

Given an array of size N containing only 0s, 1s, and 2s; sort the array in ascending order.

+ +


+Example 1:

+ +
Input: 
+N = 5
+arr[]= {0 2 1 2 0}
+Output:
+0 0 1 2 2
+Explanation:
+0s 1s and 2s are segregated 
+into ascending order.
+ +

Example 2:

+ +
Input: 
+N = 3
+arr[] = {0 1 0}
+Output:
+0 0 1
+Explanation:
+0s 1s and 2s are segregated 
+into ascending order.
+ +


+Your Task:
+You don't need to read input or print anything. Your task is to complete the function sort012() that takes an array arr and N as input parameters and sorts the array in-place.

+ +


+Expected Time Complexity: O(N)
+Expected Auxiliary Space: O(1)

+ +


+Constraints:
+1 <= N <= 10^6
+0 <= A[i] <= 2

+

+
\ No newline at end of file diff --git a/C++/Geeks4Geeks/Special Stack/README.md b/C++/Geeks4Geeks/Special Stack/README.md new file mode 100644 index 00000000..1df3d856 --- /dev/null +++ b/C++/Geeks4Geeks/Special Stack/README.md @@ -0,0 +1,32 @@ +# Special Stack +## Easy +
+

Design a data-structure SpecialStack that supports all the stack operations like push(), pop(), isEmpty(), isFull() and an additional operation getMin() which should return minimum element from the SpecialStack. Your task is to complete all the functions, using stack data-Structure.

+ +


+Example 1:

+ +
Input:
+Stack: 18 19 29 15 16
+Output: 15
+Explanation:
+The minimum element of the stack is 15.
+
+ +

 

+ +


+Your Task:
+Since this is a function problem, you don't need to take inputs. You just have to complete 5 functions, push() which takes the stack and an integer x as input and pushes it into the stack; pop() which takes the stack as input and pops out the topmost element from the stack; isEmpty() which takes the stack as input and returns true/false depending upon whether the stack is empty or not; isFull() which takes the stack and the size of the stack as input and returns true/false depending upon whether the stack is full or not (depending upon the
+given size); getMin() which takes the stack as input and returns the minimum element of the stack. 
+Note: The output of the code will be the value returned by getMin() function.

+ +


+Expected Time Complexity: O(N) for getMin, O(1) for remaining all 4 functions.
+Expected Auxiliary Space: O(1) for all the 5 functions.

+ +


+Constraints:
+1 ≤ N ≤ 104

+

+
\ No newline at end of file diff --git a/C++/Geeks4Geeks/Special Stack/special-stack.cpp b/C++/Geeks4Geeks/Special Stack/special-stack.cpp new file mode 100644 index 00000000..3991abce --- /dev/null +++ b/C++/Geeks4Geeks/Special Stack/special-stack.cpp @@ -0,0 +1,62 @@ +// { Driver Code Starts +#include +#include +using namespace std; +void push(stack& s,int a); +bool isFull(stack& s,int n); +bool isEmpty(stack& s); +int pop(stack& s); +int getMin(stack& s); +//This is the STL stack (http://quiz.geeksforgeeks.org/stack-container-adaptors-the-c-standard-template-library-stl/). +stack s; +int main(){ + int t; + cin>>t; + while(t--){ + int n,a; + cin>>n; + while(!isEmpty(s)){ + pop(s); + } + while(!isFull(s,n)){ + cin>>a; + push(s,a); + } + cout<& s, int a){ + // Your code goes here + s.push(a); +} + +bool isFull(stack& s,int n){ + // Your code goes here + return s.size() == n; +} + +bool isEmpty(stack& s){ + // Your code goes here + return s.empty(); +} + +int pop(stack& s){ + // Your code goes here + int a = s.top(); + s.pop(); + return a; +} + +int getMin(stack& s){ + // Your code goes here + int a = s.top(); + s.pop(); + while(!s.empty()){ + int b = s.top(); + s.pop(); + a = min(a, b); + } + return a; +} \ No newline at end of file diff --git a/C++/Geeks4Geeks/Square root of a number/README.md b/C++/Geeks4Geeks/Square root of a number/README.md new file mode 100644 index 00000000..2e0b4fac --- /dev/null +++ b/C++/Geeks4Geeks/Square root of a number/README.md @@ -0,0 +1,41 @@ +# Square root of a number +## Medium +
+

Given an integer x, find the square root of x. If x is not a perfect square, then return floor(√x).

+ +

 

+ +

Example 1:

+ +
Input:
+x = 5
+Output: 2
+Explanation: Since, 5 is not a perfect 
+square, floor of square_root of 5 is 2.
+
+ +

Example 2:

+ +
Input:
+x = 4
+Output: 2
+Explanation: Since, 4 is a perfect 
+square, so its square root is 2.
+ +

 

+ +

Your Task:
+You don't need to read input or print anything. The task is to complete the function floorSqrt() which takes x as the input parameter and return its square root.
+Note: Try Solving the question without using sqrt Function.

+ +

 

+ +

Expected Time Complexity: O(log N)
+Expected Auxiliary Space: O(1)

+ +

 

+ +

Constraints:
+1 ≤ x ≤ 107

+

+
\ No newline at end of file diff --git a/C++/Geeks4Geeks/Trapping Rain Water/README.md b/C++/Geeks4Geeks/Trapping Rain Water/README.md new file mode 100644 index 00000000..3270fbfd --- /dev/null +++ b/C++/Geeks4Geeks/Trapping Rain Water/README.md @@ -0,0 +1,55 @@ +# Trapping Rain Water +## Medium +
+

Given an array arr[] of N non-negative integers representing the height of blocks. If width of each block is 1, compute how much water can be trapped between the blocks during the rainy season. 

+ +

Example 1:

+ +
Input:
+N = 6
+arr[] = {3,0,0,2,0,4}
+Output:
+10
+Explanation: 
+
+
+ +

Example 2:

+ +
Input:
+N = 4
+arr[] = {7,4,0,9}
+Output:
+10
+Explanation:
+Water trapped by above 
+block of height 4 is 3 units and above 
+block of height 0 is 7 units. So, the 
+total unit of water trapped is 10 units.
+
+ +

Example 3:

+ +
Input:
+N = 3
+arr[] = {6,9,9}
+Output:
+0
+Explanation:
+No water will be trapped.
+ +


+Your Task:
+You don't need to read input or print anything. The task is to complete the function trappingWater() which takes arr[] and N as input parameters and returns the total amount of water that can be trapped.

+ +


+Expected Time Complexity: O(N)
+Expected Auxiliary Space: O(N)

+ +


+Constraints:
+3 < N < 106
+0 < Ai < 108

+

+
\ No newline at end of file diff --git a/C++/Geeks4Geeks/Trapping Rain Water/trapping-rain-water.cpp b/C++/Geeks4Geeks/Trapping Rain Water/trapping-rain-water.cpp new file mode 100644 index 00000000..4c56842b --- /dev/null +++ b/C++/Geeks4Geeks/Trapping Rain Water/trapping-rain-water.cpp @@ -0,0 +1,68 @@ +// { Driver Code Starts +#include + +using namespace std; + + + // } Driver Code Ends +class Solution{ + + // Function to find the trapped water between the blocks. + public: + long long trappingWater(int arr[], int n){ + // code here + long long rightmax, leftmax, low=0, high=n-1; + leftmax=rightmax=INT_MIN; + long long result=0; + while(low<=high){ + if(arr[low]leftmax){ + leftmax = arr[low]; + } + else{ + result+=leftmax-arr[low]; + } + low++; + } + else{ + if(arr[high]>rightmax){ + rightmax = arr[high]; + } + else{ + result+=rightmax-arr[high]; + } + high--; + } + } + return result; + } +}; + +// { Driver Code Starts. + +int main(){ + + int t; + //testcases + cin >> t; + + while(t--){ + int n; + + //size of array + cin >> n; + + int a[n]; + + //adding elements to the array + for(int i =0;i> a[i]; + } + Solution obj; + //calling trappingWater() function + cout << obj.trappingWater(a, n) << endl; + + } + + return 0; +} // } Driver Code Ends \ No newline at end of file diff --git a/C++/Geeks4Geeks/Wave Array/README.md b/C++/Geeks4Geeks/Wave Array/README.md new file mode 100644 index 00000000..74328e0c --- /dev/null +++ b/C++/Geeks4Geeks/Wave Array/README.md @@ -0,0 +1,39 @@ +# Wave Array +## Easy +
+

Given a sorted array arr[] of distinct integers. Sort the array into a wave-like array and return it
+In other words, arrange the elements into a sequence such that arr[1] >= arr[2] <= arr[3] >= arr[4] <= arr[5].....

+ +

If there are multiple solutions, find the lexicographically smallest one.

+ +

Example 1:

+ +
Input:
+n = 5
+arr[] = {1,2,3,4,5}
+Output: 2 1 4 3 5
+Explanation: Array elements after 
+sorting it in wave form are 
+2 1 4 3 5.
+ +

Example 2:

+ +
Input:
+n = 6
+arr[] = {2,4,7,8,9,10}
+Output: 4 2 8 7 10 9
+Explanation: Array elements after 
+sorting it in wave form are 
+4 2 8 7 10 9.
+ +

Your Task:
+The task is to complete the function convertToWave(), which converts the given array to a wave array.

+ +

Expected Time Complexity: O(n).
+Expected Auxiliary Space: O(1).

+ +

Constraints:
+1 ≤ n ≤ 106
+0 ≤ arr[i] ≤107

+

+
\ No newline at end of file diff --git a/C++/Geeks4Geeks/Wave Array/wave-array.cpp b/C++/Geeks4Geeks/Wave Array/wave-array.cpp new file mode 100644 index 00000000..617a0bb5 --- /dev/null +++ b/C++/Geeks4Geeks/Wave Array/wave-array.cpp @@ -0,0 +1,42 @@ +// { Driver Code Starts +#include +using namespace std; + + + // } Driver Code Ends +class Solution{ + public: + // arr: input array + // n: size of array + //Function to sort the array into a wave-like array. + void convertToWave(vector& arr, int n){ + + // Your code here + for(int i=0;i>t; //Input testcases + while(t--) //While testcases exist + { + cin>>n; //input size of array + vector a(n); //declare vector of size n + for(int i=0;i>a[i]; //input elements of array + Solution ob; + ob.convertToWave(a, n); + + for(int i=0;i1431. Kids With the Greatest Number of Candies

Easy


There are n kids with candies. You are given an integer array candies, where each candies[i] represents the number of candies the ith kid has, and an integer extraCandies, denoting the number of extra candies that you have.

+ +

Return a boolean array result of length n, where result[i] is true if, after giving the ith kid all the extraCandies, they will have the greatest number of candies among all the kids, or false otherwise.

+ +

Note that multiple kids can have the greatest number of candies.

+ +

 

+

Example 1:

+ +
Input: candies = [2,3,5,1,3], extraCandies = 3
+Output: [true,true,true,false,true] 
+Explanation: If you give all extraCandies to:
+- Kid 1, they will have 2 + 3 = 5 candies, which is the greatest among the kids.
+- Kid 2, they will have 3 + 3 = 6 candies, which is the greatest among the kids.
+- Kid 3, they will have 5 + 3 = 8 candies, which is the greatest among the kids.
+- Kid 4, they will have 1 + 3 = 4 candies, which is not the greatest among the kids.
+- Kid 5, they will have 3 + 3 = 6 candies, which is the greatest among the kids.
+
+ +

Example 2:

+ +
Input: candies = [4,2,1,1,2], extraCandies = 1
+Output: [true,false,false,false,false] 
+Explanation: There is only 1 extra candy.
+Kid 1 will always have the greatest number of candies, even if a different kid is given the extra candy.
+
+ +

Example 3:

+ +
Input: candies = [12,1,12], extraCandies = 10
+Output: [true,false,true]
+
+ +

 

+

Constraints:

+ +
    +
  • n == candies.length
  • +
  • 2 <= n <= 100
  • +
  • 1 <= candies[i] <= 100
  • +
  • 1 <= extraCandies <= 50
  • +
+
\ No newline at end of file diff --git a/C++/LeetCode/kids-with-the-greatest-number-of-candies/kids-with-the-greatest-number-of-candies.cpp b/C++/LeetCode/kids-with-the-greatest-number-of-candies/kids-with-the-greatest-number-of-candies.cpp new file mode 100644 index 00000000..aa2d4f30 --- /dev/null +++ b/C++/LeetCode/kids-with-the-greatest-number-of-candies/kids-with-the-greatest-number-of-candies.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + vector kidsWithCandies(vector& candies, int extraCandies) { + int maximum_candies = candies[0]; + for(auto i:candies){ + maximum_candies = max(maximum_candies, i); + } + vector v; + for(int i=0;i=maximum_candies){ + v.push_back(true); + } + else{ + v.push_back(false); + } + } + return v; + } +}; \ No newline at end of file diff --git a/C++/LeetCode/maximum-subarray/maximum-subarray.cpp b/C++/LeetCode/maximum-subarray/maximum-subarray.cpp new file mode 100644 index 00000000..6f830eaf --- /dev/null +++ b/C++/LeetCode/maximum-subarray/maximum-subarray.cpp @@ -0,0 +1,12 @@ +class Solution { +public: + int maxSubArray(vector& nums) { + int ms = INT_MIN, minSum = 0, cs = 0; + for(int i=0;i234. Palindrome Linked List

Easy


Given the head of a singly linked list, return true if it is a palindrome.

+ +

 

+

Example 1:

+ +
Input: head = [1,2,2,1]
+Output: true
+
+ +

Example 2:

+ +
Input: head = [1,2]
+Output: false
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the list is in the range [1, 105].
  • +
  • 0 <= Node.val <= 9
  • +
+ +

 

+Follow up: Could you do it in O(n) time and O(1) space?
\ No newline at end of file diff --git a/C++/LeetCode/palindrome-linked-list/palindrome-linked-list.cpp b/C++/LeetCode/palindrome-linked-list/palindrome-linked-list.cpp new file mode 100644 index 00000000..d29c20a8 --- /dev/null +++ b/C++/LeetCode/palindrome-linked-list/palindrome-linked-list.cpp @@ -0,0 +1,30 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + bool isPalindrome(ListNode* head) { + stackst; + ListNode* temp = head; + while(temp){ + st.push(temp->val); + temp = temp->next; + } + temp = head; + while(temp and !st.empty()){ + if(temp->val!=st.top()){ + return false; + } + temp = temp->next; + st.pop(); + } + return true; + } +}; \ No newline at end of file diff --git a/C++/LeetCode/palindrome-number/README.md b/C++/LeetCode/palindrome-number/README.md new file mode 100644 index 00000000..3592b73a --- /dev/null +++ b/C++/LeetCode/palindrome-number/README.md @@ -0,0 +1,39 @@ +

9. Palindrome Number

Easy


Given an integer x, return true if x is palindrome integer.

+ +

An integer is a palindrome when it reads the same backward as forward.

+ +
    +
  • For example, 121 is a palindrome while 123 is not.
  • +
+ +

 

+

Example 1:

+ +
Input: x = 121
+Output: true
+Explanation: 121 reads as 121 from left to right and from right to left.
+
+ +

Example 2:

+ +
Input: x = -121
+Output: false
+Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
+
+ +

Example 3:

+ +
Input: x = 10
+Output: false
+Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
+
+ +

 

+

Constraints:

+ +
    +
  • -231 <= x <= 231 - 1
  • +
+ +

 

+Follow up: Could you solve it without converting the integer to a string?
\ No newline at end of file diff --git a/C++/LeetCode/palindrome-number/palindrome-number.cpp b/C++/LeetCode/palindrome-number/palindrome-number.cpp new file mode 100644 index 00000000..c293ddd3 --- /dev/null +++ b/C++/LeetCode/palindrome-number/palindrome-number.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + bool isPalindrome(int x) { + string s = to_string(x); + stackst; + for(int i=0;i<=s.length()-1;i++){ + st.push(s[i]); + } + int i=0; + while(!st.empty()){ + if(s[i]!=st.top()){ + return false; + } + st.pop(); + i++; + } + return true; + } +}; \ No newline at end of file diff --git a/C++/LeetCode/reverse-linked-list/README.md b/C++/LeetCode/reverse-linked-list/README.md new file mode 100644 index 00000000..243642c7 --- /dev/null +++ b/C++/LeetCode/reverse-linked-list/README.md @@ -0,0 +1,32 @@ +

206. Reverse Linked List

Easy


Given the head of a singly linked list, reverse the list, and return the reversed list.

+ +

 

+

Example 1:

+ +
Input: head = [1,2,3,4,5]
+Output: [5,4,3,2,1]
+
+ +

Example 2:

+ +
Input: head = [1,2]
+Output: [2,1]
+
+ +

Example 3:

+ +
Input: head = []
+Output: []
+
+ +

 

+

Constraints:

+ +
    +
  • The number of nodes in the list is the range [0, 5000].
  • +
  • -5000 <= Node.val <= 5000
  • +
+ +

 

+

Follow up: A linked list can be reversed either iteratively or recursively. Could you implement both?

+
\ No newline at end of file diff --git a/C++/LeetCode/reverse-linked-list/reverse-linked-list.cpp b/C++/LeetCode/reverse-linked-list/reverse-linked-list.cpp new file mode 100644 index 00000000..a56e2823 --- /dev/null +++ b/C++/LeetCode/reverse-linked-list/reverse-linked-list.cpp @@ -0,0 +1,24 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* reverseList(ListNode* head) { + ListNode* temp = head, *n = temp, *prev = nullptr; + while(temp){ + temp = temp->next; + n->next = prev; + prev = n; + n = temp; + } + head = prev; + return head; + } +}; \ No newline at end of file diff --git a/C++/LeetCode/running-sum-of-1d-array/README.md b/C++/LeetCode/running-sum-of-1d-array/README.md new file mode 100644 index 00000000..ae57887f --- /dev/null +++ b/C++/LeetCode/running-sum-of-1d-array/README.md @@ -0,0 +1,30 @@ +

1480. Running Sum of 1d Array

Easy


Given an array nums. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]).

+ +

Return the running sum of nums.

+ +

 

+

Example 1:

+ +
Input: nums = [1,2,3,4]
+Output: [1,3,6,10]
+Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].
+ +

Example 2:

+ +
Input: nums = [1,1,1,1,1]
+Output: [1,2,3,4,5]
+Explanation: Running sum is obtained as follows: [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1].
+ +

Example 3:

+ +
Input: nums = [3,1,2,10,1]
+Output: [3,4,6,16,17]
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 1000
  • +
  • -10^6 <= nums[i] <= 10^6
  • +
\ No newline at end of file diff --git a/C++/LeetCode/running-sum-of-1d-array/running-sum-of-1d-array.cpp b/C++/LeetCode/running-sum-of-1d-array/running-sum-of-1d-array.cpp new file mode 100644 index 00000000..81a04a91 --- /dev/null +++ b/C++/LeetCode/running-sum-of-1d-array/running-sum-of-1d-array.cpp @@ -0,0 +1,13 @@ +class Solution { +public: + vector runningSum(vector& nums) { + vector v; + int cs = nums[0]; + v.push_back(cs); + for(int i=1;i1. Two Sum

Easy


Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

+ +

You may assume that each input would have exactly one solution, and you may not use the same element twice.

+ +

You can return the answer in any order.

+ +

 

+

Example 1:

+ +
Input: nums = [2,7,11,15], target = 9
+Output: [0,1]
+Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
+
+ +

Example 2:

+ +
Input: nums = [3,2,4], target = 6
+Output: [1,2]
+
+ +

Example 3:

+ +
Input: nums = [3,3], target = 6
+Output: [0,1]
+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= nums.length <= 104
  • +
  • -109 <= nums[i] <= 109
  • +
  • -109 <= target <= 109
  • +
  • Only one valid answer exists.
  • +
diff --git a/C++/LeetCode/two-sum/two-sum.cpp b/C++/LeetCode/two-sum/two-sum.cpp new file mode 100644 index 00000000..a3e6865c --- /dev/null +++ b/C++/LeetCode/two-sum/two-sum.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + vector twoSum(vector& nums, int target) { + vector v; + for(int i=0;i20. Valid Parentheses

Easy


Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

+ +

An input string is valid if:

+ +
    +
  1. Open brackets must be closed by the same type of brackets.
  2. +
  3. Open brackets must be closed in the correct order.
  4. +
+ +

 

+

Example 1:

+ +
Input: s = "()"
+Output: true
+
+ +

Example 2:

+ +
Input: s = "()[]{}"
+Output: true
+
+ +

Example 3:

+ +
Input: s = "(]"
+Output: false
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= s.length <= 104
  • +
  • s consists of parentheses only '()[]{}'.
  • +
+
\ No newline at end of file diff --git a/C++/LeetCode/valid-parentheses/valid-parentheses.cpp b/C++/LeetCode/valid-parentheses/valid-parentheses.cpp new file mode 100644 index 00000000..0e6f7838 --- /dev/null +++ b/C++/LeetCode/valid-parentheses/valid-parentheses.cpp @@ -0,0 +1,77 @@ +class Solution { +public: + bool isValid(string s) { + + stack st; + for(int i=0;ist; + // for(int i=0;i=0;i--){ + // if(s[i]==')' and s[i-1] == '('){ + // while(!st.empty() and st.top()!='('){ + // st.pop(); + // } + // st.pop(); + // } + // else if(s[i]=='}' and s[i-1] == '{'){ + // while(!st.empty() and st.top()!='{'){ + // st.pop(); + // } + // st.pop(); + // } + // else if(s[i]==']' and s[i-1] == '['){ + // while(!st.empty() and st.top()!='['){ + // st.pop(); + // } + // st.pop(); + // } + // } + // if(st.empty()){ + // return true; + // } + // else + // return false; + } +}; \ No newline at end of file