Convert Varchar Data to MySQL Date Format

AmitDiwan
Updated on 02-Jan-2020 05:17:18

741 Views

To convert VARCHAR data to date format, you can use STR_TO_DATE() −mysql> create table DemoTable1989    (    DueDate varchar(20)    ); Query OK, 0 rows affected (0.91 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1989 values('31/01/2015'); Query OK, 1 row affected (0.52 sec) mysql> insert into DemoTable1989 values('01/12/2018'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable1989 values('25/10/2019'); Query OK, 1 row affected (0.11 sec)Display all records from the table using select statement −mysql> select * from DemoTable1989;This will produce the following output −+------------+ | DueDate    | +------------+ | 31/01/2015 | ... Read More

Alternative of WHERE Clause for Each SELECT Field in MySQL

AmitDiwan
Updated on 02-Jan-2020 05:15:50

518 Views

You can use CASE statement −mysql> create table DemoTable1988    (    Value1 int,    Value2 int,    Price int    ); Query OK, 0 rows affected (0.64 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1988 values(10, 7, 500); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable1988 values(7, 9, 400); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1988 values(8, 7, 200); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1988 values(7, 4, 300); Query OK, 1 row affected (0.16 sec)Display all records from the table using select ... Read More

Convert Date Timestamp to Return Month Number

AmitDiwan
Updated on 02-Jan-2020 05:13:03

228 Views

To return only the month number, you can use DATE_FORMAT() -mysql> create table DemoTable1999    (    ArrivalDate timestamp    ); Query OK, 0 rows affected (0.66 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1999 values('2019-01-01 12:34:00'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1999 values('2019-12-31 10:04:00'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1999 values('2018-10-11 04:04:30'); Query OK, 1 row affected (0.14 sec)Display all records from the table using select statement −mysql> select * from DemoTable1999;This will produce the following output −+---------------------+ | ArrivalDate         ... Read More

Maximum Element Between Two Nodes of BST in C++

Narendra Kumar
Updated on 31-Dec-2019 13:39:16

264 Views

Problem statementGiven an array of N elements and two integers A, B which belongs to the given array. Create a Binary Search Tree by inserting element from arr[0] to arr[n-1]. The task is to find the maximum element in the path from A to B.ExampleIf array is {24, 23, 15, 36, 19, 41, 25, 35} the we can form BST as follows −If we consider A = 19 and B = 41 then maximum element between these two nodes is 41AlgorithmFind Lowest Common Ancestor(LCA) of node A and B.Find maximum node between LCA and A. Let us call it as ... Read More

Maximum Edge Removal from Tree to Make Even Forest in C++

Narendra Kumar
Updated on 31-Dec-2019 12:28:10

430 Views

Problem statementGiven an undirected tree which has even number of vertices, we need to remove the maximum number of edges from this tree such that each connected component of the resultant forest has an even number of vertices.ExampleIn above shown tree, we can remove at max 2 edges 0-2 and 0-4 shown in red such that each connected component will have even number of vertices.AlgorithmDo DFS from any starting node as tree is connectedInitialize count of nodes in subtree rooted under current node as 0Do following recursively for every subtree of current node −If size of current subtree is even, ... Read More

Maximum Determinant of a Matrix with Values 0 or N in C++

Narendra Kumar
Updated on 31-Dec-2019 12:22:55

138 Views

Problem statementWe have given a positive number n, and we have to find a 3*3 matrix which can be formed with combination of 0 or n and has maximum determinants.ExampleIf n = 15 then we can create matrix as follows −{{15, 15, 0}{0, 15, 15}{15, 0, 0}}For any 3*3 matrix having elements either 0 or n, the maximum possible determinant is 2 *(n)3. Hence answer is −2 * (15)3 = 6750AlgorithmFor any 3*3 matrix having elements either 0 or n, the maximum possible determinant is 2 *(n)3ExampleLet us now see an example − Live Demo#include using namespace std; int getMaxDeterminant(int ... Read More

Maximum Bitwise AND Pair from Given Range in C++

Narendra Kumar
Updated on 31-Dec-2019 12:19:58

137 Views

Problem statementGiven a range [L, R], the task is to find a pair (X, Y) such that L ≤ X < Y ≤ R and X & Y is maximum among all the possible pairs then print the bitwise AND of the found pair.ExampleIf L = 1 and R = 10 then maximum bitwise AND value is 8 which can be formed as follows −1000 # Binary representation of 8 Bitwise AND 1001 # Binary representation of 9 ---- 1000 # Final resultAlgorithmIterate from L to R and check the bitwise AND for every possible pair and print the maximum ... Read More

Maximum Average Sum Partition of an Array in C++

Narendra Kumar
Updated on 31-Dec-2019 12:17:51

226 Views

Problem statementGiven an array, we partition a row of numbers A into at most K adjacent (non-empty) groups, then the score is the sum of the average of each group. What is the maximum score that can be scored?ExampleIf input array is {9, 2, 5, 3, 10} then we can partition array as follows −{9} {2, 5, 3} and {10} then average sum of this is −9 + (2 + 5 + 3)/ 3 + 10 = 22.33AlgorithmWe can use memorization technique to solve this problem −Let memo[i][k] be the best score portioning A[i to n-1] into at most K ... Read More

Maximum Average of a Subarray of Size At Least X and At Most Y in C++

Narendra Kumar
Updated on 31-Dec-2019 12:13:14

279 Views

Problem statementGiven an array arr[] and two integers X and Y. The task is to find a sub-array of size of at least X and at most Y with the maximum averageExampleIf input array is {2, 10, 15, 7, 8, 4} and x = 3 and Y = 3 then we can obtain maximum average 12.5 as follows −(10 + 15) / 2 = 12.5AlgorithmIterate over every sub-array of size starting from X to size Y and find the maximum average among all such sub-arrays.To reduce the time complexity, we can use prefix sum array to get the sum of ... Read More

Maximum Array from Two Given Arrays in C++

Narendra Kumar
Updated on 31-Dec-2019 12:10:02

205 Views

Problem statementGiven two same sized arrays A[] and B[]. Task is to form a third array of same size. The result array should have maximum n elements from both array. It should have chosen elements of A[] first, then chosen elements of B[] in same order as they appear in original arrays. If there are common elements, then only one element should be present in res[] and priority should be given to A[]ExampleIf input arrays are −arr1[] = {9, 17, 2, 25, 6} arr2[] = {17, 4, 8, 10, 1} then final array is: {9, 17, 25, 8, 10}Please note ... Read More

Advertisements