Given an undirected graph G(V,E), find and print all the connected components of the given graph G. Note: 1. V is the number of vertices present in graph G and vertices are numbered from 0 to V-1. 2. E is the number of edges present in graph G. 3. You need to take input in main and create a function which should return all the connected components. And then print them in the main, not inside function. Print different components in new line. And each component should be printed in increasing order (separated by space). Order of different components doesn't matter. Input Format : The first line of input contains two integers, that denote the value of V and E. Each of the following E lines contains two space separated integers, that denote that there exists an edge between vertex a and b. Output Format : Print different components in new line. And each component should be printed in increasing order of vertices (separated by space). Order of different components doesn't matter....
You are given an array ‘Arr’ consisting of ‘N’ distinct integers and a positive integer ‘K’. Find out Kth smallest and Kth largest element of the array. It is guaranteed that K is not greater than the size of the array. Example: Let ‘N’ = 4, ‘Arr’ be [1, 2, 5, 4] and ‘K’ = 3. then the elements of this array in ascending order is [1, 2, 4, 5]. Clearly, the 3rd smallest and largest element of this array is 4 and 2 respectively. Input format: The first line of input contains an integer ‘T’ denoting the number of test cases. The next 2*T lines represent the ‘T’ test cases. The first line of each test case contains two space-separated integers ‘N’ and ‘K’ respectively. The second line of the test case contains ‘N’ space-separated integers representing elements of the array ‘Arr’. Output format : For each test case, print a line consisting of two space-separated integers that represent the Kth smallest and Kth largest elements of the array. Note: You do not need to print anythin...
You are given a string of length N. You need to reverse the string word by word. There can be multiple spaces between two words and there can be leading or trailing spaces but in the output reversed string you need to put a single space between two words, and your reversed string should not contain leading or trailing spaces. For example : If the given input string is " Welcome to Coding Ninjas", then you should return "Ninjas Coding to Welcome" as the reversed string has only a single space between two words and there is no leading or trailing space. Input Format : The first line of input contains a single integer T, representing the number of test cases or queries to be run. Then the T test cases follow. The first and only one of each test case contains a string that you need to reverse word by word. Output Format : For every test case, print the reversed string such that there should be only one space between two strings and there should not be any traili...
Comments
Post a Comment