## Count the number of co-prime pairs in an array

Program: Count the number of co-prime pairs in an array. (co-prime numbers means Any two numbers whose GCD is 1) Input: The first line contains an integer T, total number of elements. Then follow T elements. Output: Count the number of co-prime pairs in an array. Sample Input and Output: Input 1: 3 1 2 3 Output 1: 3 Here, Co-prime pairs are (1, 2), (2, 3), (1,...

## HOW TO GET DISTINCT ELEMENTS FROM AN ARRAY BY AVOIDING DUPLICATE ELEMENTS?

Here we explain how to find distinct elements from an array by avoiding duplicate elements. In the below program we use array of integers int num[], in this array we found distinct elements. For finding the distinct elements we use the following two ways. Program: Find distinct elements from an array public class MyDisticntElements { public static void printDistinctElements(int[] arr) { for(int i=0;i<arr.length;i++) { boolean isDistinct = false;...

## PROGRAM TO ADD TWO NUMBERS WITHOUT USING PLUS (‘+’) SIGN

In our daily life we add some numbers and for this addition we use "+" sign. I have a one question, Can it be possible without using plus sign we add numbers? the answer is Yes! we can add two numbers without using the plus ("+") sign. In the below program we use two different methods for addition of two numbers. First method add() in which we add...

## Java String Methods with Examples

Java string methods with examples: There are different string methods in java we will use this methods for perform various operations on the string in java.Following are some Java String Methods with Examples. Program: Get character from a string In this program we use charAt() method to get or access the single character from a specified position. class StringExample { public static void main(String[] args) { String str...

## Basic operations on String in Java- Strings Concatenation, Compare

Following programs are for performing the strings basic operations in java like string concatenation, string compare. To perform the string concatenation, string compare in java uses the inbuilt methods in java. String Length: Program: Find the length of string Here we first create the string named str and use the length() method to get the size of the String. class StringExample { public static void main(String[] args) {...

## Reverse a String

How to reverse the string? We will use the following different solutions for reversing the string. First we get a input from user using command line. In the first solution of this problem we buffer the input string using the StringBuffer(String string) method. Then reverse this buffer and after reversing convert the buffer into string. Program: public class StringReverse { public static void main(String[] args) { String str...

## Linear Search

Linear Search is a used for finding the key element in the multiple of elements in a given array. Now a days, Linear search is not that much used because Linear search algorithm is slow as compare to binary search or hashing. Algorithm: First step is traverse the arrayNext is match the search element with the array elements.If the search element is found then return the index position...

## Selection Sort

Selection sort is a simple sorting algorithm. This sorting algorithm is an in-place comparison-based algorithm in which the list is divided into two parts, the sorted part at the left end and the unsorted part at the right end. Initially, the sorted part is empty and the unsorted part is the entire list. The smallest element is selected from the unsorted array and swapped with theleftmost element, and...

## Quick Sort

Sort algorithms order the elements of an array according to a predefined order.Quick sort is a divide and conquer algorithm. In a divide and conquer sortingalgorithm the original data is separated into two parts "divide" which areindividually sorted and "conquered" and then combined. Algorithm: If the array contains only one element or zero elements than the array is sorted.If the array contains more than one element than: Select...

## Merge Sort

There are different sorting algorithms are available like selection sort, insertion sort etc. In this article we learn about one of them sorting algorithm, working of this sorting algorithm and example of this sorting algorithm. Merge sort algorithm is the simplest sorting algorithm as compare to other sorting algorithms. The Merge sorting algorithm can be used to sorting a collection of objects. Merge sort is a so called...