Skip to main content

Posts

Showing posts from September, 2023

Triplet Sum In Array Java

 What is Triplet Sum ? Ans: "Triplet sum" ka matlab hota hai ki aap ek array mein se teeno elements ko select karke unka sum calculate karte hain. Triplet sum ek common programming problem hai, jahan aapko ek array of integers diya hota hai aur aapko teeno aise elements ko dhundna hota hai jo ki ek specific sum ke barabar hain. Yadi hum ek example dekhein, to maan lijiye aapke paas ek array hai: [1, 2, 3, 4, 5, 6] aur aapko triplet sum nikalna hai jiska sum 9 ho. To aapke pasandida triplet sum [2, 3, 4] hoga kyunki 2 + 3 + 4 = 9. Triplet sum problems programming interviews mein bhi aksar puche jate hain aur inhe solve karne ke liye alag-alag algorithms ka istemal hota hai. Aap brute-force approach ka istemal karke, yani teeno nested loops ka istemal karke, ya efficient tarike se bhi triplet sum problems ko solve kar sakte hain. package aRray ; import java . util .*; public class tripletSum { public static void main ( String [] args ) { // TODO Auto-generated m

Pair Sum In Array Java

  Question: What is pair Sum ? Ans:  "Pair sum" ka matlab hota hai ki aapko ek array mein diye gaye kisi bhi do numbers ka sum nikalna hai. Java mein pair sum nikalne ke liye aap ek array mein se do numbers ko select karke unka sum calculate kar sakte hain. Yeh kaam aksar loops (for loop ya while loop) ka istemal karke kiya jata hai. Ek example ke roop mein, agar aapke paas ek array hai [2, 4, 6, 8, 10] aur aapko 10 ke liye pair sum nikalna hai, to aap is tarah se kar sakte hain: package aRray ; import java . util .*; public class pairSum { public static void main ( String [] args ) { // TODO Auto-generated method stub Scanner scan = new Scanner ( System . in ) ; int size = scan . nextInt () ; int count = 0 ; int targetSum = 10 ; // yaha jo add krke chahiye wo dalenge int [] arr = new int [ size ] ; // array of size for ( int i = 0 ; i < size ; i ++ ) { arr [ i ] = scan . nextInt () ; }

Intersection of Two Arrays Java

package aRray ; import java . util .*; public class intersectionTwoArray { public static void intersectionTwoArray ( int [] arr1 , int [] arr2 , int size ) { for ( int i = 0 ; i < size ; i ++ ) { for ( int j = 0 ; j < size ; j ++ ) { if ( arr1 [ i ] == arr2 [ j ]) { System . out . println ( arr2 [ j ]) ; } } } } public static void main ( String [] args ) { // TODO Auto-generated method stub Scanner scan = new Scanner ( System . in ) ; int size = scan . nextInt () ; int [] arr1 = new int [ size ] ; for ( int i = 0 ; i < size ; i ++ ) { arr1 [ i ] = scan . nextInt () ; } int [] arr2 = new int [ size ] ; for ( int j = 0 ; j < size ; j ++ ) { arr2 [ j ] = scan . nextInt () ; } ///if u want to call in void intersectionTwoArray ( arr1

Find Duplicate In Array Java

  package aRray ; import java . util .*; public class practicalArray { public static int findDuplicate ( int arr [] , int size ) { for ( int i = 0 ; i < size ; i ++ ) { for ( int j = i + 1 ; j < size ; j ++ ) { if ( arr [ i ] == arr [ j ]) { return arr [ i ] ; } } } return - 1 ; } public static void main ( String [] args ) { Scanner scan = new Scanner ( System . in ) ; int size = scan . nextInt () ; int [] arr = new int [ size ] ; for ( int i = 0 ; i < size ; i ++ ) { arr [ i ] = scan . nextInt () ; } int result = findDuplicate ( arr , size ) ; System . out . println ( result ) ; //print process } }

Find Unique Number in Array Java , XOR

 Question:  WAP to find unique number in Array Java package aRray ; import java . util .*; public class practicalArray { public static void main ( String [] args ) { Scanner scan = new Scanner ( System . in ) ; // Step 1: Input size of the array from the user int size = scan . nextInt () ; // Step 2: Create an integer array of the specified size int [] arr = new int [ size ] ; // Step 3: Initialize 'res' with the first element of the array int res = arr [ 0 ] ; // Step 4: Loop to read array elements and find the unique number for ( int i = 1 ; i < size ; i ++ ) { // Read an integer from the user and store it in the array arr [ i ] = scan . nextInt () ; // Step 5: Use XOR (^) operation to find the unique number res = res ^ arr [ i ] ; } // Step 6: Print the unique number System . out . printl

Check greatest number in Array Java

package aRray ; import java . util .*; public class practicalArray { // check gratest number public static int greatestNumber ( int size , int [] arr , int max ) { for ( int i = 0 ; i < size ; i ++ ) { if ( arr [ i ] > max ) { max = arr [ i ] ; } } return max ; } public static void main ( String [] args ) { Scanner scan = new Scanner ( System . in ) ; int size = scan . nextInt () ; int arr [] = new int [ size ] ; int max = 0 ; for ( int i = 0 ; i < size ; i ++ ) { arr [ i ] = scan . nextInt () ; } int result = greatestNumber ( size , arr , max ) ; System . out . println ( result ) ; } }  

Arrange Numbers in Array Java

package aRray ; import java . util .*; public class newArrangeNumber { public static void main ( String [] args ) { Scanner scan = new Scanner ( System . in ) ; int size = scan . nextInt () ;                     int start = 0 ;                     int end = size - 1 ; int arr [ ] = new int [ size ] ;                     //this is basically index for ( int value = 1 ; start <= end ; value ++ ) { if ( value % 2 == 1 ) { arr [ start ] = value ; start ++; } else { arr [ end ] = value ; end --; } } for ( int i = 0 ; i < size ; i ++ ) { System . out . println ( arr [ i ]) ; } } }

Search Integer in Array Java

Question:  You have been given a random integer array/list(ARR) of size N, and an integer X. You need to search for the integer X in the given array/list using 'Linear Search'.   package aRray ; import java . util .*; public class myArray { public static int checkN ( int [] arr , int size , int found ) { // int [] arr , int size, int found - --- -- these are input types for ( int i = 0 ; i < size ; i ++ ) { if ( found == arr [ i ]) { return i ; } } return - 1 ; } public static void main ( String [] args ) { Scanner scan = new Scanner ( System . in ) ; int size = scan . nextInt () ; //this is for input int arr [] = new int [ size ] ; //for loop to store data in ith position for ( int i = 0 ; i < size ; i ++ ) { // 0 is starting index of array so i m starting from 0 arr [ i ] = scan . nextInt () ; //this is for store value in index f