Find integer from string and sum of them

  • Example 1:

Input:  jh2gv1b78bm9
Output: 2+1+7+8+9= 27
  • Approach 1:

        Java


public class SumInt
{
 public int sumInt(String str) {
        int aa = 0;
        for (int i = 0; i < str.length(); i++) {
            int xx = (intstr.charAt(i);
            String zz = "" + str.charAt(i) + "";
            if (xx > 47 && xx < 58) {
                aa += Integer.parseInt(zz);
            }
        }
        return aa;
    }

    public static void main(String args[]) {
        String str"jh2gv1b78 bm9";
        SumInt obj = new SumInt();
       int a = obj.sumInt(str);
        System.out.println(a);
    }
}

Find indices of the two numbers such that they add up to the target in the given array

Write a program to Find indices of the two numbers such that they add up to the target in the given array

Example

Input:  [1,0,5,3,6,8], targer=9
Output: [0,4] or [1,5]
Approach

Java

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import javafx.util.Pair;

public class TwoSum {
    public static void main(String aa[]) {
        TwoSum s = new TwoSum();
        int[] nums = { 105389 };
        int target = 9;
        int result[] = s.twoSum(nums, target);
        System.out.println(Arrays.toString(result));
        // [1,5]
    }

    public int[] twoSum(int[] numsint target) {
        ArrayList<Pair<IntegerInteger>> l = 
                new ArrayList<Pair<IntegerInteger>>();
        for (int i = 0; i < nums.length; i++) {
            Pair p = new Pair(nums[i], i);
            l.add(p);
        }
        // sort Pair of array list based on key
        Collections.sort(l, new Comparator<Pair<IntegerInteger>>() {
            @Override
            public int compare(final Pair<IntegerIntegero1
                    final Pair<IntegerIntegero2) {
                return o1.getKey() - o2.getKey();
            }
        });

        int sP = 0, lP = nums.length - 1;
      while (sP < lP) {
        if ((l.get(sP).getKey() + l.get(lP).getKey()) == target) {
         return new int[] { l.get(sP).getValue(), l.get(lP).getValue() };
      } else if ((l.get(sP).getKey() + l.get(lP).getKey()) > target) {
         lP--;
       } else {
         sP++;
        }
    }
   return null;
    }

}

                 

Check given number is prime number

Write a program to check if the given number is prime or not.

Prime Number: A number that is divisible by 1 and itself is a prime number.

Note: 1 is not a prime number.

Example:

Input: 13
Output13 is prime number
ExplanationIf number only divisible by 1 and itself then number is prime.

Approach: Iterate till n-1 if the number is divisible by any number then the number is not prime, else the number is prime.

C

#include <stdio.h>
int main()
{
    int n = 13;
    if (n <= 1)
        printf("Number is not prime ");
    else
    {
        int flag = 0;
        for (int i = 2i < ni++)
        {
            if (n % i == 0)
            {
                flag = 1;
                break;
            }
        }
        if (flag == 0)
        {
            printf("Number is prime ");
        }
        else
        {
            printf("Number is not prime ");
        }
    }
    return 0;
}

Java

public class CheckIsPrime{
     
  public static void main(String[] args) {
    int number=13;
    if(number>1 && checkIsPrime(number))
    System.out.println(number+" is prime number");
    else
    System.out.println(number+" is not prime number");
  }
//Method to check prime number
  private static boolean checkIsPrime(int number) {
    
    for(int i=2;i<number;i++)
       {
 // If a number is divisible by any number from 2 to n-1
 // then it is not a prime
        if(number%i==0)
           return false;
       }
     return true;
  }
}
//Time Complexity:O(n)
//Space Complexity:O(1)

C++

#include <bits/stdc++.h>
using namespace std;
//Function to check for prime numbers
bool checkPrime(int n)
{
  if(n==1)
    return false;
  for(int i=2;i<n;i++)
    {
  //If a number is divisible by
// any number from 2 to n-1
  // then it is not a prime
      if(n%i==0)
        return false;
     }
   return true;
}
int main()
{
   int n=13;
   if(checkPrime(n))
      cout<<n<<" is a prime\n";
   else
      cout<<n<<" is not a prime\n";
}

//Time Complexity :O(n)
//Space Complexity:O(1)


print the triangle left to right

Print the triangle increase and then decreasing left to right order

Example:

*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*

Approach 

Java

public class Triangle {
    public static void main(String[] args) {
        int n = 10;
        printTriangle(n);
    }

    public static void printTriangle(int r) {
        int k = 1;
        for (int i = 1; i <= r; i++) {
            if (i > r / 2) {
                --k;
            }
            for (int j = 0; j < k; j++) {
                System.out.print("* ");
            }
            System.out.println("");
            if (i < r / 2) {
                ++k;
            }
        }
    }
}

C++

#include <bits/stdc++.h>
using namespace std;

void printTriangle(int n) 
{

    //print triangle in increasing
    //no of columns
   for(int i=1;i<=n/2;i++)
     {
         for(int j=1;j<=i;j++)
            cout<<"* ";
        cout<<"\n";
     }
     //print the middle one
    for(int i=1;i<=(n+1)/2;i++)
       cout<<"* ";
    cout<<"\n";
//print triangle in decreasing
  for(int i=n/2;i>=1;i--)
    {
        for(int j=1;j<=i;j++)
           cout<<"* ";
      cout<<"\n";
    }
     
}
int main()
{
    //n should be odd 
    int n = 9;
    printTriangle(n);
}