Skip to main content

Posts

Showing posts with the label Java

Rotate Array

  The input specifies there is 1 test case. For the first test case, the size of the array is 6. The array elements are 5 6 1 2 3 4. We use the arrayRotateCheck function to find the rotation index. Iteration 1: 5 > 6 (not true) Iteration 2: 6 > 1 (true) The current index (i) is 1, and i + 1 is 2. So, the rotation index is 2. The function returns 2. The output for the test case is 2. public   class   Solution   {      public   static   int   arrayRotateCheck ( int []   arr ){          for ( int   i   =   0   ;   i   <   arr . length - 1 ; i ++){              if ( arr [ i ]   >   arr [ i + 1 ]){                  return   i + 1 ;           ...

Quick Sort In JAVA Recursion

  Quick sort ek efficient sorting algorithm hai, jo divide-and-conquer approach ka use karta hai. Quick sort kaam karta hai by partitioning the array into smaller sub-arrays, sorting each sub-array, and then combining them to get the final sorted array. Isme Pivot Element select karega , Pivot element array me se koi bhi element karega aur pivot element lene ke bad usse compare karenge partition karke left side from pivot element is small element and right side from pivot will be bigger element Pivot element hamesa bigger select krenge taaki efficiency behtr aaye

2d Array

  Here is first square bracket will alocate for rows [] second square bracket will alocate for columns;

Reverse String Wordwise

Question:  Reverse the given string word wise. That is, the last word in given string should come at 1st place, last second word at 2nd place and so on. Individual words should remain as it is. Input format : String in a single line Output format : Word wise reversed string in a single line Constraints : 0 <= |S| <= 10^7 where |S| represents the length of string, S. Sample Input 1: Welcome to Coding Ninjas Sample Output 1: Ninjas Coding to Welcome Ans:  package learningJava ; import java . util .*; /** * This program demonstrates a function to reverse words in a given string. * It takes a string as input and reverses the order of words in the string. */ public class regPract { /** * Reverses the order of words in a given string. * * @param end The ending index of the input string. * @param start The starting index for extracting a word. * @param j An index variable used in loops. * @param input The input string to rever...

Binary Search In Java || Searching and Sorting Java

  What is Binary Search ? **Binary Search (द्विआधारी खोज)**: Binary Search ek prashikshan (searching) technique hai jo data ko tezi se khojne me madadgar hoti hai. Isme data ko do bhago me batakar dhunda jata hai, aur har bar ek adha data set ko chhodkar dusre adhe ko chhota banaya jata hai, jisse tezi se manchit samuday (sorted array) me kuch vishesh data ko khoja ja sake. Yadi aapko kisi data set me kisi vishesh item ko dhoondhna hai, to binary search us data set ko do hisson me vibhajit karega aur dekhega ki dhoondhne wala item kis bhag me ho sakta hai. Phir is prakriya ko pratek bar lagataar dubara karta hai aur data set ko adhikansh roop se ghata deta hai, jab tak aapko aapka vishesh item mil jaye ya fir yah spasht ho jaye ki item data set me maujood nahi hai. Binary Search ki khasiyat yah hai ki yah data set me tezi se khojne ki kshamata rakhta hai kyunki har bar dui sankhyaon ke bich ka adha data set ko chhod deta hai, jisse dhoondhne ke liye keval ek chota hissa bachta hai....

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 ]) ; } } }

Sum of Digits on Java

 Sum of Digits ka mtlb hota hai  integer ke digits ko add krna  Example 123 1+2+3 =6 package learning ; import java . util . Scanner ; public class learnNew { public static void main ( String [] args ){ Scanner scan = new Scanner ( System . in ) ; int n = scan . nextInt () ; int sum = 0 ; while ( n > 0 ) { int r = n % 10 ; sum = sum + r ; n = n / 10 ; } System . out . println ( sum ) ; } } Step:

Swap two variables in Java

 Question: write a java program to SWAP two variables . Ans: First of all let me clear swap concept what exactly swap is 👇👇👇👇 Swap ek programming concept hai jo do variables ke values ko exchange karta hai. Matlab, agar aapke paas a aur b do variables hain, to swap operation se a aur b ke values interchange ho jate hain. Java me, aap do variables ke values ko swap karne ke liye various tarike istemal kar sakte hain. Yahan ek example diya gaya hai jo aapko samjhane me madadgar ho sakta hai: package learning ; import java . util . Scanner ; public class learnNew { public static void main ( String [] args ){ Scanner scan = new Scanner ( System . in ) ; int a = 20 ; int b = 90 ; // swap concept int temp = a ; a = b ; b = temp ; System . out . println ( a ) ; System . out . println ( b ) ; } } 90 20

Escape Sequence In java

Question: Create a letter format using eScape sequence. Example-- Dear khare , Thanks Java couse is nice. Thanks Ans:  package learning ; import java . util . Scanner ; public class learnNew { public static void main ( String [] args ) { // Scanner scan = new Scanner(System.in); // String myString = "This space contains double and triple spaces"; // System.out.println(myString.indexOf(" ")); // System.out.println(myString.indexOf(" ")); // // // // // problem 5 String letter2 = "Dear khare ,\n\tThanks to buy our product. Thanks" ; System . out . println ( letter2 ) ; } } Here I'm using back slash \ n and back slash \t  What is this ?🤔🤔 Let me clear this                \n = this is a escape sequence character to create a new line .               \t  = this is a escape sequence character and its represent to TAB

Keywords in Java

Question: What is keyword ? Ans: keyword are reserved word whose value are already defined in java , We can not use use keyword variable (Identifier) in our  personal use . Example -   char char = 5; Yaha yellow background wala char ek keyword hai, ek data type hai lekin hamne variable name bhi char dala hai jo ki galt hai ham ise use  nhi kr skte . Keywords are case sensitive . Example:  char CHAR = 5 ;

Variable In Java

Question:  What is Variable  in Java  ? Ans: Variable is is the name of memory location where we stored different type of value . Example------      int a = 10; Yaha 10 ek value hai jise ham store kr rhe a name ki memory location pe aur ise store krne ke liye memory space de rha int jo ki ek data type hai mtlb jis name se memory me data value save hogi use variable kehte hai    Types of variables --------- 1) Local variable  Jo variable kisi method ya function ke andar hota hai use local variable kehte hai jaise { }  kisi if else ke andar hai to wo  local variable hoti hai. 2) static Variable Jisme static likha ho wo static variable hota hai 3 Instance Variable  4) final variable  

Loops in Java

There are 3 types of Loops in Java 1) while loop 2) for loop 3) do while loop 4) for each loop u can use any loop as per your convenience  while >>>>>>>>>>>>>>> int i = 1;    while (i<= 10){ i++; } System.out.print("finish my loop"); Yaha int i = 1 ka mtlb hai initialize mtlb ham start kaha se kr rhe yaha main 1 se start kr rha matlb maine kaha i nam ka integer bna lo jiski value store kr do 1 se aur tb tk run kro jab tk i 10 se chota na ho aur 10 ke barabar na ho  maine 1 bar chalaya lekin normal program ki trh increment nhi hoga , aage nhi bdhega to hamne yaha i++ kiya mtlb 1 aage bdhaya, increment kiya aur phir se ye check karega i hamara 2 tha to kya i 10 se chota hai to yeh 3 bna dega   mtlb jab tk loop khtm nhi hota tb tk loop ke bahar ka print nhi hoga , infinite loop kaise lgate hai .  . . . . . . .  while(true) System.out.print(""); jab tk condition false nhi ho jata tb tk while  loop r...