Find the sum of all the integers in the row number N



Given a pyramid of consecutive integers:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
......
Find the sum of all the integers in the row number N.
For example:
The row #3: 4 + 5 + 6 = 15
The row #5: 11 + 12 + 13 + 14 + 15 = 65
Solution:

public class ConsecutiveNumberPattern {
public static void main(String[] args) {
System.out.println(countrownums(6));
}
public static int countrownums(int rowno)
{
if(rowno==1)
return 1;
int sum=0;
int val=((rowno*(rowno-1))/2)+1;
//Here val gives the starting number of the row number passed as an argument
for(int i=1;i<=rowno;i++)
{
sum+=val++;
}
return sum;
}
}

Create function to merge two Arrays of Integer having



Given two arrays of ints that are sets, create a function to merge them to create a new set.A set must pass on these three conditions:- All values are positive- Sorted- Non-duplicates


Solution:
import java.util.LinkedHashSet;
import java.util.Set;
public class MergeSortedArrays {
public static void main(String[] args) {
int[] a= {1,2,6,9,10,27};
int[] b= {2,8,10,89};
int[] result=merge(a,b);
Set<Integer> set = new LinkedHashSet<Integer>();
for(int i : result)
{
set.add(i);
}
System.out.println(set);
}
public static int[] merge(int[] a,int[] b)
{
int m=0;int n=0;
int k=a.length+b.length;
int[] c=new int[k];
int j=0;
while(m<a.length && n<b.length)
{
c[j++] = a[m]<=b[n] ? a[m++] :b[n++];
}
while(m<a.length)
{
c[j++]=a[m++];
}
while(n<b.length)
{
c[j++]=b[n++];
}
return c;
}
}
//Result is sorted with no duplicates
[1, 2, 6, 8, 9, 10, 27, 89]

Sort integer array with duplicate values which are randomly placed over the entire array



Question :
Write code to sort an integer array of size N which has only three unique values 0,1,2 duplicated & randomly placed over the entire array.
- Memory used should be O(1)
- Run time should be O(N)

Solution
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class SortArray2 {
public static void main(String[] args) {
int[] a= { 1, 2, 1, 2, 0, 1, 2, 0, 0, 2, 1, 0 };
int[] r=sort(a);
System.out.println(Arrays.toString(r));
}
public static int[] sort(int[] a)
{
int index=0;
Map<Integer,Integer> map = new HashMap<Integer,Integer>();
for(int i=0;i<a.length;i++)
{
map.put(a[i], (map.getOrDefault(a[i], 0))+1);
}
System.out.println(map);
int i=0;
while(i<=2)
{
for(int j=1;j<=map.get(i);j++)
{
a[index++]=i;
}
i++;
}
return a;
}
}

Sum two numbers without using "+" operator


Write code to sum 2 integer but u cant use a+b method, you have to use either ++ or --. How you will handle negative numbers.


Solution:

public class AddingNumbersWithoutPlusOperator {
public static void main(String[] args) {
int n=addNumbers(11,19);
System.out.println(n);
}
public static int addNumbers(int n1,int n2)
{
//Handling when two Integers are Positive
while(n1>0)
{
n2++;
n1--;
}
//Handling negative numbers Challenge Lies in handling the negative Integers
while(n1<0)
{
n1++;
n2--;
}
return n2;
}
}

Xpath In Selenium


Xpath in Selenium


XPath (XML Path Language) is a query language for selecting elements from an XML/HTML document.

It helps you find an element from the whole document precisely and quickly.

Web pages are generally written in a language called HTML. If you load a web page on a browser (Chrome, Firefox, etc), you can easily access the corresponding HTML doc by hitting the F12 key.

Before going to create a Xpath, we need to understand few basic terms.

What is meant by nodes in Xpath ?


  • In XPath, there are many kinds of nodes for example element node, attribute node, text node, namespace, Text, processing-instruction, comment, and document nodes .
  • XML documents are treated as trees of nodes. The topmost element of the tree is called the root element.
  • Few example of nodes except root node.
    • Element node: represents the HTML element, i.e an HTML tag.
    • Attribute node: it represents an attribute in an element node, e.g. “href” attribute in <a href=”http://www.selenium.com”>example</a>
    • Comment node: represents comments in the HTML document which is denoted by (<!– … –>)
    • Text node: represents the text enclosed in an element node (example in <p>example</p>)


What is meant by Atomic values in Xpath ?


  • Atomic values are nodes with no children or parent.


What is meant by Items in Xpath ?


  • Items are atomic values or nodes.


Apart form this you should also understand the relationship between nodes

Xpath Axis OR Relationship of Nodes (with Examples)

To be added... by 03 June 2020