mirror of https://github.com/evanferrao/dsa
day2: array manipulation 1
This commit is contained in:
parent
75ac8e30cd
commit
154e6a0e61
|
|
@ -0,0 +1,29 @@
|
||||||
|
// write a code in java to find the total number of pairs whose sum is equal to a given number using function
|
||||||
|
import java.util.Scanner;
|
||||||
|
class TotalNumberOfParirsWhoseSumIsEqualToX{
|
||||||
|
public static void main(String[] args){
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
System.out.println("Enter the size of the array");
|
||||||
|
int n = sc.nextInt();
|
||||||
|
int[] arr = new int[n];
|
||||||
|
System.out.println("Enter the elements of the array");
|
||||||
|
for(int i=0;i<n;i++){
|
||||||
|
arr[i] = sc.nextInt();
|
||||||
|
}
|
||||||
|
System.out.println("Enter the value of x");
|
||||||
|
int x = sc.nextInt();
|
||||||
|
System.out.println("The total number of pairs whose sum is equal to "+x+" is "+totalPairs(arr,n,x));
|
||||||
|
sc.close();
|
||||||
|
}
|
||||||
|
public static int totalPairs(int[] arr,int n,int x){
|
||||||
|
int count = 0;
|
||||||
|
for(int i=0;i<n;i++){
|
||||||
|
for(int j=i+1;j<n;j++){
|
||||||
|
if(arr[i]+arr[j]==x){
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,32 @@
|
||||||
|
// write a code in java to find the total number of triplets whose sum is equal to a given number using function
|
||||||
|
import java.util.Scanner;
|
||||||
|
class TotalNumberOfTripletsWhoseSumIsEqualToX{
|
||||||
|
public static void main(String[] args){
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
System.out.println("Enter the size of the array");
|
||||||
|
int n = sc.nextInt();
|
||||||
|
int arr[] = new int[n];
|
||||||
|
System.out.println("Enter the elements of the array");
|
||||||
|
for(int i=0;i<n;i++){
|
||||||
|
arr[i] = sc.nextInt();
|
||||||
|
}
|
||||||
|
System.out.println("Enter the value of x");
|
||||||
|
int x = sc.nextInt();
|
||||||
|
System.out.println("The total number of triplets whose sum is equal to "+x+" is "+countTriplets(arr,n,x));
|
||||||
|
sc.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
static int countTriplets(int arr[],int n,int x){
|
||||||
|
int count = 0;
|
||||||
|
for(int i=0;i<n-2;i++){
|
||||||
|
for(int j=i+1;j<n-1;j++){
|
||||||
|
for(int k=j+1;k<n;k++){
|
||||||
|
if(arr[i]+arr[j]+arr[k]==x){
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,36 @@
|
||||||
|
// find the unique number in a givern array wherer all the elements are being twive with one value being unique using function
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
class UniqueArrayElement{
|
||||||
|
public static void main(String[] args){
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
System.out.println("Enter the number of elements in the array");
|
||||||
|
int n = sc.nextInt();
|
||||||
|
int[] arr = new int[n];
|
||||||
|
System.out.println("Enter the elements of the array");
|
||||||
|
for(int i=0;i<n;i++){
|
||||||
|
arr[i] = sc.nextInt();
|
||||||
|
}
|
||||||
|
System.out.println("The unique element in the array is "+findUnique(arr));
|
||||||
|
}
|
||||||
|
public static int findUnique(int[] arr){
|
||||||
|
int res = 0;
|
||||||
|
for(int i=0;i<arr.length;i++){
|
||||||
|
res = res^arr[i];
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
// dry run:
|
||||||
|
// arr = [1,2,3,4,1,2,3]
|
||||||
|
// res = 0
|
||||||
|
// res = 0^1 = 1
|
||||||
|
// res = 1^2 = 3
|
||||||
|
// res = 3^3 = 0
|
||||||
|
// res = 0^4 = 4
|
||||||
|
// res = 4^1 = 5
|
||||||
|
// res = 5^2 = 7
|
||||||
|
// res = 7^3 = 4
|
||||||
|
// return 4
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue