Greater than Neighbor

CONCEPTS USED:

Arrays

DIFFICULTY LEVEL:

Easy

PROBLEM STATEMENT(SIMPLIFIED):

Given an array A of N elements, your task is to print all those indexes that have values greater than its left and right neighbors. In case of extreme indexes like 0 and N-1, check with their single neighbor.

See original problem statement here

For Example:

Input : A = [7 2 8 4 3 9 1]

Output : 0 2 5 

Explanation : 

Element at index 0 i.e. 7 is greater than its only, right neighbor 2.

Element at index 2 i.e. 8 is greater than both of its neighbor 2 and 4.

Element at index 5 i.e. 9 is greater than both of its neighbor 3 and 1.

SOLVING APPROACH:

The idea is to learn the best online programming courses and traverse the array from left and for each index, check if the value at it is greater than its neighbor. If Yes print its index. If no such index is found, simply print -1.

SOLUTIONS:

#include <stdio.h>

int main()
{
  int t; scanf("%d", &t);

  while(t--){
    int n; scanf("%d", &n);
    int arr[n];

    for(int i=0; i<n; i++){
      scanf("%d", &arr[i]);
    }

    int flag = 0;
    for(int i=0; i<n; i++){
        if(i == 0){
            if(arr[i] > arr[i+1]){
                printf("%d ", i);
                flag = 1;
            }
        }

        else if(i == n-1){
            if(arr[i] > arr[i-1]){
                printf("%d ", i);
                flag = 1;
            }
        }
        else{
            if(arr[i] > arr[i-1] && arr[i] > arr[i+1]){
                printf("%d ", i);
                flag = 1;
            }
        }  
    }
    if(flag == 0)
      printf("-1");
    printf("\n");
  }

  return 0;
}

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

  int main()
  {
    int t; cin >> t;

    while(t--){
      int n; cin >> n;
      int arr[n];

      for(int i=0; i<n; i++){
        cin >> arr[i];
      }

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

            if(arr[i] > arr[i+1]){
              cout << i << " ";
              flag = 1;
            }
        }

        else if(i == n-1){
            if(arr[i] > arr[i-1]){
              cout << i << " ";
              flag = 1;
            }
        }

        else{
            if(arr[i] > arr[i-1] && arr[i] > arr[i+1]){
                cout << i << " ";
                flag = 1;
            }
        } 
      }

      if(flag == 0)
        cout << "-1";
      cout << endl;
    }

    return 0;
}



import java.util.*;
  import java.io.*;

  public class Main {
    public static void main(String args[]) throws IOException {

      Scanner sc = new Scanner(System.in);
      int t = sc.nextInt();

      while(t != 0){
        int n = sc.nextInt();
        int arr[] = new int[n];

        for(int i=0; i<n; i++){
          arr[i] = sc.nextInt();
        }

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

            if(arr[i] > arr[i+1]){
              System.out.print(i + " ");
              flag = 1;
            }
          }

          else if(i == n-1){
            if(arr[i] > arr[i-1]){
              System.out.print(i + " ");
              flag = 1;
            }
          }
          else{
            if(arr[i] > arr[i-1] && arr[i] > arr[i+1]){
                System.out.print(i + " ");
                flag = 1;
            }
          }
        }
        if(flag == 0)
          System.out.print("-1");
        System.out.println();

        t--;
      }

    }
  }




Space Complexity : O(1)
Previous post Greater And Least
Next post Interesting Array

Leave a Reply

Your email address will not be published. Required fields are marked *