**Time Conversion**

### Problem Statement :

Given a time in 12 -hour AM/PM format, convert it to military (24-hour) time. Note: - 12:00:00AM on a 12-hour clock is 00:00:00 on a 24-hour clock. - 12:00:00PM on a 12-hour clock is 12:00:00 on a 24-hour clock. Example s = '12 : 01: 00PM' Return '12:01:00'. s = '12 : 01: 00AM' Return '00:01:00'. Function Description Complete the timeConversion function in the editor below. It should return a new string representing the input time in 24 hour format. timeConversion has the following parameter(s): string s: a time in 12 hour format Input Format A single string s that represents a time in 12 -hour clock format (i.e.: hh:mm:ss AM or hh:mm:ss PM) Constraints All input times are valid

### Solution :

` ````
Solution in C :
In C :
char* timeConversion(char* s) {
char test[2];
static char final[9];
final[0] = s[0];
final[1] = s[1];
final[2] = ':';
final[5] = ':';
final[3] = s[3];
final[4] = s[4];
final[6] = s[6];
final[7] = s[7];
if(s[8] == 'A')
{
if(s[0]=='1' && s[1]=='2')
{
final[0] = '0';
final[1] = '0';
}
else{
final[0] = s[0];
final[1] = s[1];
}
}
else if(s[8]== 'P')
{
if(s[0] == '0' && s[1] < '8')
{
final[0] = '1';
char hr = (int)s[1];
hr = hr+2;
final[1] = (char)hr;
}
else if(s[0] == '0' && (s[1] == '8' || s[1] == '9'))
{
final[0] = '2';
if(s[2] == '8') final[1] = '0';
else final[1] = '1';
}
else if(s[0] != '0' && (s[1] == '0' || s[1] == '1'))
{ final[0] = '2';
if(s[1] == '0') final[1] = '2';
else if(s[1] == '1') final[1] = '3';
}
}//end of PM if
return final;
}
In Java :
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine(); //07:05:45PM
DateFormat inFormat = new SimpleDateFormat( "hh:mm:ssaa");
DateFormat outFormat = new SimpleDateFormat( "HH:mm:ss");
Date date = null;
try {
date = inFormat.parse(s);
}catch (ParseException e ){
e.printStackTrace();
}
if( date != null ){
String myDate = outFormat.format(date);
System.out.println(myDate);
}
}
}
In Python3 :
ins = input().strip()
is_pm = ins[-2:].lower() == 'pm'
time_list = list(map(int, ins[:-2].split(':')))
if is_pm and time_list[0] < 12:
time_list[0] += 12
if not is_pm and time_list[0] == 12:
time_list[0] = 0
print(':'.join(map(lambda x: str(x).rjust(2, '0'), time_list)))
In C ++ :
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
string s;
string h;
int hr;
cin>>s;
hr = ((s[0]-'0')*10)+(s[1]-'0');
if(s[8]=='P'&&s[9]=='M'&& hr ==12) cout<<to_string(hr);
else if(s[8]=='P'&&s[9]=='M') cout<<to_string(hr+12);
else if(s[8]=='A'&&s[9]=='M'&&hr==12) cout<<"00";
else cout<< s[0]<<s[1];
for(int i =2;i<8;i++)
cout<<s[i];
cout<<endl;
return 0;
}
```

## View More Similar Problems

## Binary Search Tree : Lowest Common Ancestor

You are given pointer to the root of the binary search tree and two values v1 and v2. You need to return the lowest common ancestor (LCA) of v1 and v2 in the binary search tree. In the diagram above, the lowest common ancestor of the nodes 4 and 6 is the node 3. Node 3 is the lowest node which has nodes and as descendants. Function Description Complete the function lca in the editor b

View Solution →## Swap Nodes [Algo]

A binary tree is a tree which is characterized by one of the following properties: It can be empty (null). It contains a root node only. It contains a root node with a left subtree, a right subtree, or both. These subtrees are also binary trees. In-order traversal is performed as Traverse the left subtree. Visit root. Traverse the right subtree. For this in-order traversal, start from

View Solution →## Kitty's Calculations on a Tree

Kitty has a tree, T , consisting of n nodes where each node is uniquely labeled from 1 to n . Her friend Alex gave her q sets, where each set contains k distinct nodes. Kitty needs to calculate the following expression on each set: where: { u ,v } denotes an unordered pair of nodes belonging to the set. dist(u , v) denotes the number of edges on the unique (shortest) path between nodes a

View Solution →## Is This a Binary Search Tree?

For the purposes of this challenge, we define a binary tree to be a binary search tree with the following ordering requirements: The data value of every node in a node's left subtree is less than the data value of that node. The data value of every node in a node's right subtree is greater than the data value of that node. Given the root node of a binary tree, can you determine if it's also a

View Solution →## Square-Ten Tree

The square-ten tree decomposition of an array is defined as follows: The lowest () level of the square-ten tree consists of single array elements in their natural order. The level (starting from ) of the square-ten tree consists of subsequent array subsegments of length in their natural order. Thus, the level contains subsegments of length , the level contains subsegments of length , the

View Solution →## Balanced Forest

Greg has a tree of nodes containing integer data. He wants to insert a node with some non-zero integer value somewhere into the tree. His goal is to be able to cut two edges and have the values of each of the three new trees sum to the same amount. This is called a balanced forest. Being frugal, the data value he inserts should be minimal. Determine the minimal amount that a new node can have to a

View Solution →