- 233
- 18 064
Mathematics
India
เข้าร่วมเมื่อ 5 เม.ย. 2020
Rotate by 90 degree | GeeksForGeeks | Problem of the Day
Given a square matrix[][]. The task is to rotate it by 90 degrees in clockwise direction without using any extra space.
Examples:
Input: mat[][] = [[1 2 3], [4 5 6], [7 8 9]]
Output:
7 4 1
8 5 2
9 6 3
Input: mat[][] = [1 2], [3 4]
Output:
3 1
4 2
Input: mat[][] = [[1]]
Output:
1
#geeksforgeeks
#problemoftheday
#education
#computerscience
#coding
#array
#sorting
#sorts
#sort
#datastructure
#algorithmicproblemsolving
#algorithms
#algorithm
#dynamicprogramming
#dp
#potd
#gfg
#binarytree
#binarysearchtree
#bst
#string
#dictionary
#python
#stack
#queue
#python
#c++
#interview
Table of Contents
0:00 Problem Statement
0:27 Solution
1:39 Pseudo Code
3:01 Code - Python
3:24 Code - C++
Examples:
Input: mat[][] = [[1 2 3], [4 5 6], [7 8 9]]
Output:
7 4 1
8 5 2
9 6 3
Input: mat[][] = [1 2], [3 4]
Output:
3 1
4 2
Input: mat[][] = [[1]]
Output:
1
#geeksforgeeks
#problemoftheday
#education
#computerscience
#coding
#array
#sorting
#sorts
#sort
#datastructure
#algorithmicproblemsolving
#algorithms
#algorithm
#dynamicprogramming
#dp
#potd
#gfg
#binarytree
#binarysearchtree
#bst
#string
#dictionary
#python
#stack
#queue
#python
#c++
#interview
Table of Contents
0:00 Problem Statement
0:27 Solution
1:39 Pseudo Code
3:01 Code - Python
3:24 Code - C++
มุมมอง: 26
วีดีโอ
Find All Triplets with Zero Sum | GeeksForGeeks | Problem of the Day
มุมมอง 972 ชั่วโมงที่ผ่านมา
Given an array arr[], find all possible indices [i, j, k] of triplets [arr[i], arr[j], arr[k]] in the array whose sum is equal to zero. Return indices of triplets in any order and all the returned triplets indices should also be internally sorted, i.e., for any triplet indices [i, j, k], the condition i less than j less than k should hold. Examples: Input: arr[] = [0, -1, 2, -3, 1] Output: [[0,...
Swap and Maximize | GeeksForGeeks | Problem of the Day
มุมมอง 659 ชั่วโมงที่ผ่านมา
Given an array arr[ ] of positive elements. Consider the array as a circular array, meaning the element after the last element is the first element of the array. The task is to find the maximum sum of the absolute differences between consecutive elements with shuffling of array elements allowed i.e. shuffle the array elements and make [a1..an] such order that |a1 - a2| |a2 - a3| …… |an-1 - an| ...
Insert in Sorted way in a Sorted DLL | GeeksForGeeks | Problem of the Day
มุมมอง 6312 ชั่วโมงที่ผ่านมา
Given a sorted doubly linked list and an element x, you need to insert the element x into the correct position in the sorted Doubly linked list(DLL). Note: The DLL is sorted in ascending order Example: Input: LinkedList: 3-5-8-10-12 , x = 9 Output: 3-5-8-9-10-12 Explanation: Here node 9 is inserted in the Doubly Linked-List. Input: LinkedList: 1-4-10-11 , x = 15 Output: 1-4-10-11-15 #geeksforge...
Pairs with difference k | GeeksForGeeks | Problem of the Day
มุมมอง 11714 ชั่วโมงที่ผ่านมา
Given an array arr[] of positive integers. Find the number of pairs of integers whose difference equals a given number k. Note: (a, b) and (b, a) are considered the same. Also, the same numbers at different indices are considered different. Examples: Input: arr[] = [1, 5, 3, 4, 2], k = 3 Output: 2 Explanation: There are 2 pairs with difference 3,the pairs are {1, 4} and {5, 2} Input: arr[] = [8...
Remove duplicates in array | GeeksForGeeks | Problem of the Day
มุมมอง 5619 ชั่วโมงที่ผ่านมา
Given an array arr consisting of positive integers numbers, remove all duplicates numbers. Example: Input: arr[] = [2, 2, 3, 3, 7, 5] Output: [2, 3, 7, 5] Explanation: After removing the duplicates 2 and 3 we get 2 3 7 5. Input: arr[] = [2, 2, 5, 5, 7, 7] Output: [2, 5, 7] Explanation: After removing the duplicates 2, 5 and 7 we get 2 5 7. Expected Time Complexity: O(n) Expected Auxiliary Space...
Triplet Family | GeeksForGeeks | Problem of the Day
มุมมอง 7721 ชั่วโมงที่ผ่านมา
Given an array arr of integers. Find whether three numbers are such that the sum of two elements equals the third element. Example: Input: arr[] = [1, 2, 3, 4, 5] Output: true Explanation: The pair (1, 2) sums to 3. Input: arr[] = [5, 3, 4] Output: false Explanation: No triplets satisfy the condition. Expected Time Complexity: O(n2) Expected Auxilary Space: O(1) #geeksforgeeks #problemoftheday ...
Occurence of an integer in a Linked List | GeeksForGeeks | Problem of the Day
มุมมอง 49วันที่ผ่านมา
Given a singly linked list and a key, count the number of occurrences of the given key in the linked list. Examples: Input: Linked List: 1-2-1-2-1-3-1, key = 1 Output: 4 Explanation: 1 appears 4 times. Input: Linked List: 1-2-1-2-1, key = 3 Output: 0 Explanation: 3 appears 0 times. Expected Time Complexity: O(n) Expected Auxiliary Space: O(1) #geeksforgeeks #problemoftheday #education #computer...
Alternative Sorting | GeeksForGeeks | Problem of the Day
มุมมอง 50วันที่ผ่านมา
Given an array arr of distinct integers. Rearrange the array in such a way that the first element is the largest and the second element is the smallest, the third element is the second largest and the fourth element is the second smallest, and so on. Examples: Input: arr[] = [7, 1, 2, 3, 4, 5, 6] Output: [7, 1, 6, 2, 5, 3, 4] Explanation: The first element is first maximum and second element is...
Modify the Array | GeeksForGeeks | Problem of the Day
มุมมอง 84วันที่ผ่านมา
Given an array arr. Return the modified array in such a way that if the current and next numbers are valid numbers and are equal then double the current number value and replace the next number with 0. After the modification, rearrange the array such that all 0's are shifted to the end. Note: Assume ‘0’ as the invalid number and all others as a valid number. The sequence of the valid numbers is...
Find the Sum of Last N nodes of the Linked List | GeeksForGeeks | Problem of the Day
มุมมอง 43วันที่ผ่านมา
Given a single linked list, calculate the sum of the last n nodes. Note: It is guaranteed that n less than or equal to number of nodes. Examples: Input: Linked List: 5-9-6-3-4-10, n = 3 Output: 17 Explanation: The sum of the last three nodes in the linked list is 3 4 10 = 17. Input: Linked List: 1-2, n = 2 Output: 3 Explanation: The sum of the last two nodes in the linked list is 2 1 = 3. #geek...
Split the Array | GFG | Problem of the Day | Correction - Replace n(n-1)/2 to (pow(2,n-1)-1)%MOD
มุมมอง 23314 วันที่ผ่านมา
Given an array arr[] of integers, the task is to count the number of ways to split given array elements into two disjoint groups such that the XOR of elements of each group is equal. Note: The answer could be very large so print it by doing modulo with 109 7. Examples: Input : arr[] = [1, 2, 3] Output : 3 Explanation: {(1),(2, 3)}, {(2),(1, 3)}, {(3),(1, 2)} are three ways with equal XOR value ...
Sort a k sorted doubly linked list | GeeksForGeeks | Problem of the Day
มุมมอง 10114 วันที่ผ่านมา
Given a doubly linked list, each node is at most k-indices away from its target position. The problem is to sort the given doubly linked list. The distance can be assumed in either of the directions (left and right). Examples : Input: Doubly Linked List : 3 - 2 - 1 - 5 - 6 - 4 , k = 2 Output: 1 - 2 - 3 - 4 - 5 - 6 Explanation: After sorting the given 2-sorted list is 1 - 2 - 3 - 4 - 5 - 6. Inpu...
Nearest multiple of 10 | GeeksForGeeks | Problem of the Day
มุมมอง 20514 วันที่ผ่านมา
A string str is given to represent a positive number. The task is to round str to the nearest multiple of 10. If you have two multiples equally apart from str, choose the smallest element among them. Examples: Input: str = 29 Output: 30 Explanation: Close multiples are 20 and 30, and 30 is the nearest to 29. Input: str = 15 Output: 10 Explanation: 10 and 20 are equally distant multiples from 20...
Single Number | GeeksForGeeks | Problem of the Day
มุมมอง 5214 วันที่ผ่านมา
Given an array arr[] of positive integers where every element appears even times except for one. Find that number occurring an odd number of times. Examples: Input: arr[] = [1, 1, 2, 2, 2] Output: 2 Explanation: In the given array all element appear two times except 2 which appears thrice. Input: arr[] = [8, 8, 7, 7, 6, 6, 1] Output: 1 Explanation: In the given array all element appear two time...
Split Linked List Alternatingly | GeeksForGeeks | Problem of the Day
มุมมอง 7514 วันที่ผ่านมา
Split Linked List Alternatingly | GeeksForGeeks | Problem of the Day
Two Swaps | GeeksForGeeks | Problem of the Day
มุมมอง 19114 วันที่ผ่านมา
Two Swaps | GeeksForGeeks | Problem of the Day
Subarray range with given sum | GeeksForGeeks | Problem of the Day
มุมมอง 18021 วันที่ผ่านมา
Subarray range with given sum | GeeksForGeeks | Problem of the Day
Count Linked List Nodes | GeeksForGeeks | Problem of the Day
มุมมอง 6921 วันที่ผ่านมา
Count Linked List Nodes | GeeksForGeeks | Problem of the Day
Delete Alternate Nodes | GeeksForGeeks | Problem of the Day
มุมมอง 10021 วันที่ผ่านมา
Delete Alternate Nodes | GeeksForGeeks | Problem of the Day
Two Smallests in Every Subarray | GeeksForGeeks | Problem of the Day
มุมมอง 18321 วันที่ผ่านมา
Two Smallests in Every Subarray | GeeksForGeeks | Problem of the Day
Reorganize The Array | GeeksForGeeks | Problem of the Day
มุมมอง 9321 วันที่ผ่านมา
Reorganize The Array | GeeksForGeeks | Problem of the Day
Max distance between same elements | GeeksForGeeks | Problem of the Day
มุมมอง 6721 วันที่ผ่านมา
Max distance between same elements | GeeksForGeeks | Problem of the Day
Linked List Matrix | GeeksForGeeks | Problem of the Day
มุมมอง 19021 วันที่ผ่านมา
Linked List Matrix | GeeksForGeeks | Problem of the Day
Largest Pair Sum | GeeksForGeeks | Problem of the Day
มุมมอง 6828 วันที่ผ่านมา
Largest Pair Sum | GeeksForGeeks | Problem of the Day
XOR Linked List | GeeksForGeeks | Problem of the Day
มุมมอง 25828 วันที่ผ่านมา
XOR Linked List | GeeksForGeeks | Problem of the Day
Find the number of islands | GeeksForGeeks | Problem of the Day
มุมมอง 17028 วันที่ผ่านมา
Find the number of islands | GeeksForGeeks | Problem of the Day
Not a subset sum | GeeksForGeeks | Problem of the Day
มุมมอง 158หลายเดือนก่อน
Not a subset sum | GeeksForGeeks | Problem of the Day
Deletion and Reverse in Circular Linked List | GeekForGeeks | Problem of the Day
มุมมอง 189หลายเดือนก่อน
Deletion and Reverse in Circular Linked List | GeekForGeeks | Problem of the Day
Majority Vote | GeeksForGeeks | Problem of the Day
มุมมอง 88หลายเดือนก่อน
Majority Vote | GeeksForGeeks | Problem of the Day
Nice explanation brother ❤
def rotate(matrix): n = len(matrix) for i in range(n): for j in range(i, n): temp = matrix[i][j] matrix[i][j] = matrix[j][i] matrix[j][i] = temp for i in range(n): matrix[i].reverse()
void rotate(vector<vector<int> >& matrix) { int n = matrix.size(); for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { int temp = matrix[i][j]; matrix[i][j] = matrix[j][i]; matrix[j][i] = temp; } } for (int i = 0; i < n; i++) { reverse(matrix[i].begin(), matrix[i].end()); } }
Table of Contents 0:00 Problem Statement 0:27 Solution 1:39 Pseudo Code 3:01 Code - Python 3:24 Code - C++
class Solution: def findTriplets(self, arr): mp = dict() n = len(arr) for i in range(n - 1): for j in range(i + 1, n): s = arr[i] + arr[j] if s not in mp: mp[s] = [] mp[s].append([i, j]) ans_set = set() for i in range(n): diff = 0 - arr[i] if diff in mp: tmp = mp[diff] for element in tmp: if element[0] != i and element[1] != i: tmp_arr = [i, element[0], element[1]] tmp_arr.sort() ans_set.add(tuple(tmp_arr)) return [list(triplet) for triplet in ans_set]
class Solution { public: void insert_triplet(set<vector<int>> &ans, int i, int j, int k) { vector<int> tmp = {i, j, k}; sort(tmp.begin(), tmp.end()); //cout << "inserting " << i << ", " << j << ", " << k << endl; ans.insert(tmp); } vector<vector<int>> findTriplets(vector<int> &arr) { set<vector<int>> ans; map<int, vector<vector<int>>> mp; int n = arr.size(); for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { int s = arr[i] + arr[j]; vector<int> tmp = {i, j}; mp[s].push_back(tmp); } } for (int i = 0; i < n; i++) { int diff = 0 - arr[i]; if (mp.find(diff) != mp.end()) { vector<vector<int>> tmp_v = mp[diff]; for (int j = 0; j < tmp_v.size(); j++) { if (tmp_v[j][0] != i and tmp_v[j][1] != i) insert_triplet(ans, i, tmp_v[j][0], tmp_v[j][1]); } } } vector<vector<int>> ANS(ans.begin(), ans.end()); return ANS; } };
Table of Contents 0:00 Problem Statement 0:49 Solution 7:13 Pseudo Code 10:39 Code - Python 11:35 Code - C++
note: the recman's function was written mistakenly in the example!
class Solution: def maxSum(self,arr): ans = 0 n = len(arr) arr.sort() for i in range(n//2): ans += 2*arr[n-1-i] ans -= 2*arr[i] return ans
class Solution { public: long long maxSum(vector<int>& arr) { sort(arr.begin(), arr.end()); long long ans = 0; int n = arr.size(); for (int i = 0; i < n/2; i++) { ans += 2*arr[n - 1 - i]; ans -= 2*arr[i]; } return ans; } };
Table of Contents 0:00 Problem Statement 0:58 Solution 5:10 Pseudo Code 6:25 Code - Python 6:57 Code - C++
class Solution: #Function to insert a node in a sorted doubly linked list. def sortedInsert(self, head, x): NEW = Node(x) if not head: return NEW ptr = head prvs = None while ptr and ptr.data < x: prvs = ptr; ptr = ptr.next if ptr: ptr = ptr.prev if not ptr and not prvs: NEW.next = head head.prev = NEW head = NEW elif not ptr: prvs.next = NEW NEW.prev = prvs else: NEW.next = ptr.next NEW.prev = ptr if ptr.next: ptr.next.prev = NEW ptr.next = NEW return head
class Solution { public: Node* sortedInsert(Node* head, int x) { Node *NEW = getNode(x); if (!head) return NEW; Node *ptr = head, *prvs = nullptr; while (ptr and ptr->data < x) { prvs = ptr; ptr = ptr->next; } if (ptr) ptr = ptr->prev; // first node if (!ptr and !prvs) { NEW->next = head; head->prev = NEW; head = NEW; } else { // last node if (!ptr) { prvs->next = NEW; NEW->prev = prvs; } //in middle else { NEW->next = ptr->next; NEW->prev = ptr; if (ptr->next) ptr->next->prev = NEW; ptr->next = NEW; } } return head; } };
Table of Contents 0:00 Problem Statement 0:35 Solution 6:30 Pseudo Code 9:32 Code - Python 10:57 Code - C++
class Solution: def countPairsWithDiffK(self,arr, k): ans = i = j = 0 arr.sort() while j < len(arr): if abs(arr[i] - arr[j]) < k: j += 1 elif abs(arr[i] - arr[j]) > k: i += 1 else: count1 = count2 = 0 ele1 = arr[i] ele2 = arr[j] while j < len(arr) and arr[j] == ele2: count1 += 1 j += 1 while i < len(arr) and arr[i] == ele1: i += 1 count2 += 1 if ele1 == ele2: #print(f"for {ele1} count1={count1} and count2={count2}") ans += (count1*(count1-1))//2 else: #print(f"for {ele1} and {ele2} count1={count1} and count2={count2}") ans += count1*count2 return ans
class Solution { public: /* Returns count of pairs with difference k */ int countPairsWithDiffK(vector<int>& arr, int k) { sort(arr.begin(), arr.end()); int i = 0, j = 0; int ans = 0; while (j < arr.size()) { if (abs(arr[i] - arr[j]) < k) j++; else if (abs(arr[i] - arr[j]) > k) i++; else { int ele1 = arr[i], ele2 = arr[j]; int count1 = 0, count2 = 0; while (j < arr.size() and arr[j] == ele2) { count1++; j++; } while (i < arr.size() and arr[i] == ele1) { i++; count2++; } if (ele1 == ele2) ans += (count1*(count1-1))/2; else ans += count1*count2; } } return ans; } };
Table of Contents 0:00 Problem Statement 0:31 Solution 8:30 Pseudo Code 11:14 Code - Python 12:05 Code - C++
Doesnt it lead to Sc being to O(n) which doesnt follows expected TC
It does. In case if you want to do it in O(1) space, use an array/dictionary of length 101, which stores the occurrence of element of the array. Since arr[i] <=100. But the general concept remains the same.
class Solution: def removeDuplicates(self, arr): mp = dict() ans = [] for element in arr: if element not in mp: ans.append(element) mp[element] = True return ans
class Solution { public: vector<int> removeDuplicate(vector<int>& arr) { vector<int> ans; map<int,bool> mp; for (int i = 0; i < arr.size(); i++) { if (mp.find(arr[i]) == mp.end()) ans.push_back(arr[i]); mp[arr[i]] = true; } return ans; } };
very clear explanation that's what i was looking for deep knowledge . Thanks.
class Solution: def findTriplet(self, arr): arr.sort() i = len(arr) - 1; while i >= 0: j = 0 k = i - 1; while j < k: if arr[j] + arr[k] == arr[i]: return True if arr[j] + arr[k] < arr[i]: j += 1 else: k -= 1 i -= 1 return False
class Solution { public: bool findTriplet(vector<int>& arr) { sort(arr.begin(), arr.end()); for (int i = arr.size() - 1; i >= 0; i--) { int j = 0, k = i - 1; while (j < k) { if (arr[j] + arr[k] == arr[i]) return true; if (arr[j] + arr[k] < arr[i]) j++; else k--; } } return false; } };
Table of Contents 0:00 Problem Statement 0:40 Solution 3:05 Pseudo Code 4:20 Code - Python 5:04 Code - C++
class Solution: def count(self, head, key): ans = 0 while head: if head.data == key: ans += 1 head = head.next return ans
class Solution { public: int count(struct Node* head, int key) { int ans = 0; while (head) { if (head->data == key) ans++; head = head->next; } return ans; } };
code
class Solution: def alternateSort(self,arr): ans = [0 for _ in range(len(arr))] arr.sort() i = 0 j = len(arr) - 1 count = 0 while count < len(arr): if count%2 == 0: ans[count] = arr[j] j -= 1 else: ans[count] = arr[i] i += 1 count += 1 return ans
class Solution { public: vector<int> alternateSort(vector<int>& arr) { vector<int> ans(arr.size(), 0); sort(arr.begin(), arr.end()); int i = 0, j = arr.size() - 1; int count = 0; while (count < arr.size()) { if (count%2 == 0) ans[count++] = arr[j--]; else ans[count++] = arr[i++]; } return ans; } };
class Solution: def modifyAndRearrangeArr (self, arr) : ans = [0 for _ in range(len(arr))] for i in range(len(arr) - 1): if arr[i] == arr[i+1]: arr[i] = 2*arr[i] arr[i+1] = 0 j = 0 for i in range(len(arr)): if arr[i] != 0: ans[j] = arr[i] j += 1 return ans
class Solution { public: vector<int> modifyAndRearrangeArray(vector<int> &arr) { vector<int> ans (arr.size(), 0); for (int i = 0; i < arr.size() - 1; i++) { if (arr[i] != 0 and arr[i] == arr[i+1]) { arr[i] = 2*arr[i]; arr[i+1] = 0; } } int j = 0; for (int i = 0; i < arr.size(); i++) { if (arr[i] != 0) ans[j++] = arr[i]; } return ans; } };
I'm new to programming Where should I start? Can you help me out in problem solving? Where should I start? Want to Learn from you I need a mentor ; like you sir Hope you'll guide me
For programming, take any programming language course or watch any video series. For problem solving, you can start with "Data Structures And Algorithms Made Easy : Data Structures And Algorithmic Puzzles"
Excellent content
class Solution: def sumOfLastN_Nodes(self, head, n): length = 0 ptr = head while ptr: length += 1 ptr = ptr.next i = 0 ptr = head ans = 0 while ptr: if length - i <= n: ans += ptr.data ptr = ptr.next i += 1 return ans
class Solution { public: int sumOfLastN_Nodes(struct Node* head, int n) { int length = 0; Node *ptr = head; while (ptr) { length++; ptr = ptr->next; } ptr = head; int i = 0; int ans = 0; while (ptr) { if (length - i <= n) { // cout << ptr->data << " "; ans += ptr->data; } ptr = ptr->next; i++; } //cout << endl; return ans; } };
Sorry for the confusion guys! n(n-1)/2 is not the correct solution. The correct solution is- (pow(2, n-1) - 1)%MOD. Please change the code accordingly. I have updated the code. Alas! I can not update the video.
this logic is not working for java please check once
Thanks for pointing it out.
thank youuu, this was really nice explanation, glad i found ur channel
Thanks. Glad, you found the video helpful!
class Solution: def countgroup(self,arr): xor = 0 MOD = 1000000007 for element in arr: xor = xor ^ element if xor == 0: n = len(arr) ans = pow(2, n-1, MOD) - 1 return ans return 0
class Solution { public: int countgroup(vector<int>& arr) { int XOR = 0; int MOD = 1000000007; for (const auto i: arr) XOR = XOR ^ i; if (XOR == 0) { int n = arr.size(); return ((int)pow(2, n-1))%MOD - 1; } return 0; } };
Table of Contents 0:00 Problem Statement 0:56 Solution 4:44 Pseudo Code 5:37 Code - Python 6:07 Code - C++
Great work.Have subscribed.Do continue this useful stuff.
Thanks and Welcome to the channel!
import heapq class Solution: def sortAKSortedDLL(self, head, k): if not head: return head pq = [] ptr = None new_head = head i = 0 while i < k + 1 and head: heapq.heappush(pq, head.data) head = head.next i += 1 #print(pq) ptr = new_head while len(pq): data = heapq.heappop(pq) ptr.data = data if head: heapq.heappush(pq, head.data) head = head.next ptr = ptr.next return new_head
class Solution { public: class Compare { public: bool operator()(DLLNode* a, DLLNode* b) { return a->data > b->data; } }; // function to sort a k sorted doubly linked list DLLNode *sortAKSortedDLL(DLLNode *head, int k) { if (!head) return head; priority_queue<DLLNode*, vector<DLLNode*>, Compare> pq; for (int i = 0; i < k + 1 and head != NULL; i++) { pq.push(head); head = head->next; } DLLNode *new_head, *last; new_head = last = NULL; while (!pq.empty()) { if (!new_head) { last = pq.top(); last->prev = NULL; last->next = NULL; new_head = last; } else { last->next = pq.top(); pq.top()->prev = last; last = pq.top(); } pq.pop(); if (head) { pq.push(head); head = head->next; } } last->next = NULL; return new_head; } };
Table of Contents 0:00 Problem Statement 0:40 Solution 7:20 Pseudo Code 9:37 Code - Python 10:25 Code - C++
class Solution: def roundToNearest (self, string) : ans = "" n = len(string) if string[n-1] <= '5': ans = string[:-1] + '0' else: i = n - 1; ans = '0' carry = 1 i -= 1 while i >= 0: if string[i] == '9' and carry: ans = '0' + ans carry = 1 else: ans = str(int(string[i]) + carry) + ans carry = 0 i -= 1 if carry: ans = '1' + ans return ans
class Solution { public: string roundToNearest(string str) { string ans = ""; int n = str.length(); if (str[n-1] <= '5') { ans = str; ans[n-1] = '0'; } else { int carry = 1; int i = n - 1; i--; ans = '0'; while (i >= 0) { if (str[i] == '9' and carry) { ans = '0' + ans; carry = 1; } else { char next_num = str[i]; if (carry) next_num++; ans = next_num + ans; carry = 0; } i--; } if (carry) ans = '1' + ans; } return ans; } };
Table of Contents 0:00 Problem Statement 0:44 Solution 4:20 Pseudo Code 7:21 Pseudo Code Explanation 10:44 Code - Python 12:17 Code - C++
class Solution: def getSingle(self,arr): ans = 0 for element in arr: ans = ans ^ element return ans
class Solution { public: int getSingle(vector<int>& arr) { int ans = 0; for (const auto i: arr) ans = ans ^ i; return ans; } };