Strivers Problem Solving Java

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 11

STRIVERS PROBLEM SOLVING :

1. User Input / Output :

import java.util.*;
public class Solution {

public static void main(String[] args) {


// Write your code here
Scanner sc = new Scanner(System.in);

char ch = sc.next().charAt(0);

if ((ch >= 'A' && ch <= 'Z')) {


System.out.println(1);
} else if ((ch >= 'a' && ch <= 'z')) {
System.out.println(0);
} else {
System.out.println(-1);
}

}
}
----------------------------
2.Data Types:

public class Solution {


public static int dataTypes(String type) {
// Write your code here
if(type.equals("Integer")){
return 4;
}else if(type.equals("Long")){
return 8;
}else if(type.equals("Float")){
return 4;
}else if(type.equals("Double")){
return 8;
}else if (type.equals("Character")){
return 1;
}
return 0;
}
}
-------------
3.If Else statements:
public class Solution {
public static String compareIfElse(int a, int b) {
// Write your code here
if(a > b){
return "greater";
}else if(a < b){
return "smaller";
}else if(a == b){
return "equal";
}
return "invalid";
}
}
-------------
4. Switch Statement:
public class Solution {
public static double areaSwitchCase(int ch, double []a) {
// Write your code here

switch(ch) {
case 1 :
double r = a[0];
return Math.PI*r*r;
// break;

case 2 :
double l = a[0];
double b = a[1];
return l*b;
// break;

default:
return 0;
}

// return 0.0;
}
}
----------------
5. for loop ( Nth Fibonacci Number) :

import java.util.Scanner;

public class Solution {

public static int fib(int n){


int a =0;
int b = 1;
int c;

for(int i =2;i<=n;i++){
c=a+b;
a=b;
b=c;
}
return b;
}

public static void main(String[] args) {

/* Your class should be named Solution.


* Read input as specified in the question.
* Print output as specified in the question.
*/
Scanner sc = new Scanner(System.in);

int n = sc.nextInt();

int result = fib(n);

System.out.println(result);
}

------------------

6. while (sum of even and odd ):

import java.util.Scanner;

public class Main {

public static void main(String[] args) {


// Write your code here
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();

int sumEven = 0;
int sumOdd =0;

while(n>0){
int digit = n%10;

if(digit%2 == 0){
sumEven+=digit;
}else {
sumOdd+=digit;
}
n=n/10;
}

System.out.println(sumEven+" "+sumOdd);

}
}

=--------------------
7.Count Digits :
public class Solution {
public static int countDigits(int n){
// Write your code here.
int count=0;
int original =n;

while(n>0){
int digit = n%10;

if(digit!=0 && original%digit ==0){


count++;

}
n/=10;
}

return count;
}
}

Example:
Input: ‘n’ = 336

Output: 3

Explanation:
336 is divisible by both ‘3’ and ‘6’. Since ‘3’ occurs twice it is counted two
times.
-----------------
8. Reverse a Number:
Example 1:

Input: x = 123
Output: 321

class Solution {
public int reverse(int x) {
int revN = 0;
while (x != 0) {
int digit = x % 10;
x /= 10;
if (revN > Integer.MAX_VALUE / 10 || (revN == Integer.MAX_VALUE / 10 &&
digit > 7)) return 0;
if (revN < Integer.MIN_VALUE / 10 || (revN == Integer.MIN_VALUE / 10 &&
digit < -8)) return 0;
revN = revN * 10 + digit;
}
return revN;
}
}

OR

Sample Input 1 :
2
0
12
Sample Output 1:
0
805306368
Explanation For Sample Input 1 :
For the first test case :
Since the given number N = 0 is represented as 00000000000000000000000000000000 in
its binary representation. So after reversing the bits, it will become
00000000000000000000000000000000 which is equal to 0 only. So the output is 0.

-----------------

9.Check Palindrome:
class Solution {
public boolean isPalindrome(int x) {

int ori = x;
int rev =0;

while(x > 0){


int digit = x%10;
rev = rev*10 + digit;
x/=10;

}
if(ori == rev ){
return true;
}
return false;

}
}

----------
10. GCD OR HCF :
public class Solution {
public static int calcGCD(int n, int m){
// Write your code here.
while(m >0 && n>0){
if(n > m){
n=n%m;
}else{
m = m%n;
}

if(n == 0){
return m;
}
else{
return n;
}
// return 0;
}
}
--------------------
11. Armstrong number :
import java.util.*;
public class Main {

public static boolean armstrongNumber(int n){


int cnt = count(n);
int dup = n;
int num =0;
while(n > 0){
int digit1 = n%10;
num = num+ (int)Math.pow(digit1,cnt);
n/=10;
}
if(num == dup){
return true;
}
return false;
}

public static int count(int n){


int cnt =0;
while(n>0){
int digit = n%10;
cnt++;
n/=10;

}
return cnt;

public static void main(String[] args) {


// Write your code here
Scanner sc = new Scanner(System.in);
int n = sc .nextInt();

boolean result = armstrongNumber(n);

System.out.println(result);

}
}

----------------
12. Print all divisors (Sum of all divisors):
public class Solution {
public static int sumOfAllDivisors(int n){
int totalSum = 0;
for (int i = 1; i <= n; i++) {
totalSum += sumOfDivisors(i);
}
return totalSum;
}

private static int sumOfDivisors(int num) {


int sum = 0;
int sqrtNum = (int) Math.sqrt(num);
for(int i=1;i<=sqrtNum;i++){
if(num%i == 0){
sum+=i;
if(i != num/i){
sum+=num/i;
}

}
return sum;
}
}
------------------
13. check prime :
public class Solution {
public static String isPrime(int num) {
//Your code goes here
if(num<= 1){
return "NO";
}
for(int i=2;i<=(int) Math.sqrt(num);i++){

if(num%i == 0){
return "NO";

}
return "YES";
}
}

----------
14. Largest Element in the Array:
import java.util.* ;
import java.io.*;

public class Solution {

static int largestElement(int[] arr, int n) {


// Write your code here.
int max = arr[0];
for(int i=1;i<n;i++){
if(arr[i] >max){
max = arr[i];

}
}
return max;

}
}
------------
15. Second Largest Number:
Sample Input 1 :
4
3 4 5 2
Sample Output 1 :
4 3
Explanation For Sample Input 1 :
The second largest element after 5 is 4 only, and the second smallest element after
2 is 3.

public class Solution {

public static int[] getSecondOrderElements(int n, int[] a) {

int largest = Integer.MIN_VALUE;


int secLargest = Integer.MIN_VALUE;

for (int i = 0; i < n; i++) {


if (a[i] > largest) {
secLargest = largest;
largest = a[i];
} else if (a[i] < largest && a[i] > secLargest) {
secLargest = a[i];
}
}
int smaller = Integer.MAX_VALUE;
int secSmaller = Integer.MAX_VALUE;
for (int i = 0; i < n; i++) {
if (a[i] < smaller) {
secSmaller = smaller;
smaller = a[i];
} else if (a[i] > smaller && a[i] < secSmaller) {
secSmaller = a[i];
}
}

return new int[]{secLargest, secSmaller};


}

}
--------------
16. Check if the array is sorted:
public class Solution {
public static int isSorted(int n, int []a) {
// Write your code here.
int res =0;

for(int i=0;i<n;i++){
if(a[i]< res){
return 0;

}
res = a[i];
}
return 1;
}
}
------------
17. Remove duplicates from Sorted array:
public class Solution {
public static int removeDuplicates(int[] arr,int n) {
// Write your code here.
int i=0;
for(int j=1;j<n;j++){
if(arr[i] != arr[j]){
arr[i+1] = arr[j];
i++;
}
}
return i+1;
}
}
---------
18. Left Rotate an Array by One :
import java.util.* ;
import java.io.*;

public class Solution {

static int[] rotateArray(int[] arr, int n) {


// Write your code here.
int temp = arr[0];
for(int i=1;i<n;i++){
arr[i-1] =arr[i];

}
arr[n-1] = temp;
return arr;

}
}
-------------
19. Left rotate an array by D places:
import java.util.ArrayList;

public class Solution {


public static ArrayList<Integer> rotateArray(ArrayList<Integer> arr, int k) {
// Write your code here.
int n = arr.size();
k = k % n;

reverse(arr, 0, k - 1);
reverse(arr, k, n - 1);
reverse(arr, 0, n - 1);

return arr;

private static void reverse(ArrayList<Integer> arr, int start, int end) {


// while (start <= end) {
// int temp = arr[start];
// arr[start] = arr[end];
// arr[end] = temp;
// start++;
// end--;
// }

while (start <= end) {


int temp = arr.get(start);
arr.set(start, arr.get(end));
arr.set(end, temp);
start++;
end--;
}
}
}
----------
20. Move zero to end :
public class Solution {
public static int[] moveZeros(int n, int []a) {
// Write your code here.
int j =-1;

for(int i=0;i<n;i++){
if(a[i] == 0){
j=i;
break;
}

if(j == -1){
return a;
}

for(int i=j+1;i<n;i++){
if(a[i] !=0){
int temp = a[i];
a[i] = a[j];
a[j] = temp;
j++;
}

return a;
}
}
-------------------
21. Linear Search :
import java.util.*;
public class Solution {
public static int linearSearch(int n, int num, int []arr){
// Write your code here.

int search = -1;

for(int i=0;i<n;i++){
if(arr[i] == num){

search = i;

break;

}
return search;
}
}
------------------
22. find the missing number :
class Solution {
public int missingNumber(int[] nums) {

int N = nums.length;

int sum = N * (N +1) /2;


int sum2 = 0;

for(int i=0;i<N;i++){
sum2 += nums[i];

}
return sum - sum2;
}
}

----------
23. Maximum consecutive ones :
class Solution {
public int findMaxConsecutiveOnes(int[] nums) {

int n = nums.length;

int maxi = 0;
int cnt =0;

for(int i =0;i<n;i++){
if(nums[i] == 1){
cnt++;
maxi = Math.max(maxi,cnt);
}
else{
cnt = 0;
}
}

return maxi;

}
}
-----------

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy