Find smallest and largest element from 2d array

Find the smallest and largest element and its position from the 2D array.

Example 1:

Input : {{6, 4, 2, 4}, {5, 6, 9, 2}, {5, 7, 2, 1}};
Output: Largest element 9 at (1,2)
        Smalast element 1 at (2,3)
Approach: 

Java

public class FIndSmallestAndLargestArray {
    static int smallElm = 0;
    static int smallI = 0;
    static int smallJ = 0;
    static int largElm = 0;
    static int largElmI = 0;    
    static int largElmJ = 0;

    public static void main(String[] args) {
        int a[][] = { { 6424 }, { 5692 },
                 { 5721 } };
        searchingElement(a);
        System.out.println("Largest element " + largElm 
            + " at (" + largElmI + "," + largElmJ + ")");
        System.out.println("Smalast element " + smallElm 
             + " at (" + smallI + "," + smallJ + ")");
    }

    // method two find smallest and largest element
    public static void searchingElement(int[][] a) {
        int m = 0;
        // iterate till row end
        for (int i = 0; i < a.length; i++) {
            // iterate till col end
            for (int j = 0; j < a[0].length; j++) {
                if (i == 0) {
                    smallElm = a[i][j];
                    smallI = i;
                    smallJ = j;
                    largElm = a[i][j];
                    largElmI = i;
                    largElmJ = j;
                }
                m = a[i][j];
                // for find smallest element
                if (smallElm > m) {
                    smallElm = a[i][j];
                    smallI = i;
                    smallJ = j;
                }
                // for find greatest element
                if (largElm < m) {
                    largElm = a[i][j];
                    largElmI = i;
                    largElmJ = j;
                }

            }
        }
    }
}

C++

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

int largElm=INT_MIN,largElmI=0,largElmJ=0;
int smallElm=INT_MAX,smallI=0,smallJ=0;
void searchingElement(vector<vector<int>> &arr)
{
    int m=0;
    int n=arr.size();
    if(n!=0)
      m=arr[0].size();
    for(int i=0;i<n;i++)
      {
          for(int j=0;j<m;j++)
           {
               if(arr[i][j]>largElm)
                 {
                     largElm=arr[i][j];
                     largElmI=i;
                     largElmJ=j;
                 }
                if(arr[i][j]<smallElm)
                 {
                     smallElm=arr[i][j];
                     smallI=i;
                     smallJ=j;
                 }
           }

      }
    
}
int main()
{
    vector<vector<int>> arr= { { 6424 }, { 5692 },
                 { 5721 } };
    searchingElement(arr);
    cout<<"Largest element "<<largElm;
    cout<< " at ("<<largElmI<<","<<largElmJ<<")"<<"\n";
    cout<<"Smalast element "<<smallElm; 
    cout<<" at ("<<smallI<<","<<smallJ<<")";
}


Find the First non repeated character in the String

Find the first nonrepeating character from the string. 

Example 1:

Input : str="Hi Hello World"
Output: i
Approach 1:

Java

public class NonRepeatableFirstChar {
    public static void main(String[] args) {
         //input string must contain one 
         //character which occur ones
        String str = "Hi Hellow World";
        char c = nonRepeatedFirst(str);
        System.out.println("Char is " + c);
    }

    public static char nonRepeatedFirst(String str) {
        char[] c = str.toCharArray();
        char ch = '\0';
        int length = str.length();
        int index = -1;
        while (length >= 0) {
            index++;
            ch = str.charAt(index);
            if (Character.isLetter(ch)) {
                int check = 0;
                for (int i = 0; i < c.length; i++) {
                    if (Character.isLetter(c[i]) && ch == c[i] && index != i) {
                        check = 1;
                        break;
                    }
                }
                if (check == 0)
                    return ch;
            }
            length--;
        }
        return ch;
    }
}

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

C++

#include <bits/stdc++.h>
using namespace std;
char firstNonRepeating(string str)
{
    int n=str.size();
    char ans='#';
    for(int i=0;i<n;i++)
      {
          ans=str[i];
          int j=i+1;
          int flag=0;
          for(j=i+1;j<n;j++)
            {
               if(str[j]==ans)
                  {
                      flag=1;
                      break;
                  }
            }
         if(flag==0)
            return ans;

      }
      return ans;
}
int main()
{
    string str="Hi Hello World";
    char first=firstNonRepeating(str);
    cout<<"First Non repeating is ";
    cout<<first<<"\n";
    return 0;
}
// Time Complexity : O(n^2)
// Space Complexity : O(1)


Approach:2

Java


import java.util.HashSet;

public class NonRepeatableFirstChar1 {
    public static void main(String[] args) {
        // input string must contain one
        // character which occur ones
        String str = "Hi Hellow World";
        char c = nonRepeatedFirst(str);
        System.out.println("Char is " + c);
    }

    // function to find the first
    // non repeating character
    static char nonRepeatedFirst(String str) {
        HashSet<Characterst = new HashSet<>();
        char ans = '\0';
        for (int i = str.length() - 1; i >= 0; i--) {
            if (Character.isLetter(str.charAt(i))) {
                if (!st.contains(str.charAt(i))) {
                    ans = str.charAt(i);
                    st.add(ans);
                }
            }
        }
        return ans;
    }
}
// Time Complexity : O(n)
// Space Complexity : O(n)

C++

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

//function to find the first 
//non repeating character
char firstNonRepeating(string str)
{
    set<char> st;
    char ans='#';
    for(int i=str.size()-1;i>=0;i--)
      {
          if(str[i]!=' ')
            {
                if(st.find(str[i])==st.end())
                  {
                      ans=str[i];
                      st.insert(str[i]);
                  }
            }
      }
    return ans;
}
int main()
{

    //input string must contain one 
    //character which occur ones
    string str="Hi Hello World";
    char first=firstNonRepeating(str);
    cout<<"First Non Repeating is ";
    cout<<first<<"\n";
    return 0;
}

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


String reverse without using String function

Reverse a given string without using the library.

Example 1:

Input: str="Hello Word" 
Output: droW olleH

Approach:

Java

public class ReverseString {
    public static void main(String[] args) {
        String str = "Hello Word";
        String strRev = reverseString(str);
        System.out.println("Reverse string is: " + strRev);
        System.out.println("Reverse string is: "
            + reverseStringChar(str));
    }

    public static String reverseString(String str) {
        // create new string for hold the result
        StringBuffer strBuff = new StringBuffer();
        for (int i = str.length() - 1; i >= 0; i--) {
            strBuff.append(str.charAt(i));
        }
        return strBuff.toString();
    }

    public static String reverseStringChar(String str) {
        // create new string for hold the result
        StringBuffer strBuff = new StringBuffer();
        char array[] = str.toCharArray();
        for (int i = array.length - 1; i >= 0; i--) {
            strBuff.append(Character.toString(array[i])); 
        }
        return strBuff.toString();

    }
}

C++

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

//function to reverse the string
string stringReverse(string str)
{

    //vaiable to hold the final
    //answer
    string rev="";

    //iterate till the length
    //of string
    for(int i=0;i<str.size();i++)
      {
          //rev=str[i]+rev
          //append in reverse order
          rev=str[i]+rev;

      }
    return rev;
}
int main()
{
    string str="Hello Word";
    string rev=stringReverse(str);
    cout<<"Reverse string is\n";
    cout<<rev<<"\n";
    return 0;
}